1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial implementation
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.relayd
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.IStatus
;
21 import org
.eclipse
.core
.runtime
.Status
;
22 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.Activator
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfConstants
;
30 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfTrace
;
31 import org
.eclipse
.ui
.PlatformUI
;
34 * Manages relayd connections. When a trace is opened, it creates a connection
35 * if the trace was started with live support. When a trace is closed, is closes
38 * @author Marc-Andre Laperle
41 public final class LttngRelaydConnectionManager
{
42 private static LttngRelaydConnectionManager fConnectionManager
;
43 private Map
<LttngRelaydConnectionInfo
, LttngRelaydConsumer
> fConnections
= new HashMap
<>();
46 * Get an instance of the trace manager.
48 * @return The trace manager
50 public static synchronized LttngRelaydConnectionManager
getInstance() {
51 if (fConnectionManager
== null) {
52 fConnectionManager
= new LttngRelaydConnectionManager();
53 TmfSignalManager
.register(fConnectionManager
);
55 return fConnectionManager
;
59 * Get the cosumer for the given relayd connection information.
61 * @param connectionInfo
62 * the connection information
64 * @return the consumer
66 public LttngRelaydConsumer
getConsumer(final LttngRelaydConnectionInfo connectionInfo
) {
67 if (!fConnections
.containsKey(connectionInfo
)) {
68 LttngRelaydConsumer lttngRelaydConsumer
= new LttngRelaydConsumer(connectionInfo
);
69 fConnections
.put(connectionInfo
, lttngRelaydConsumer
);
70 return lttngRelaydConsumer
;
73 return fConnections
.get(connectionInfo
);
76 private static LttngRelaydConnectionInfo
getEntry(final ITmfTrace trace
) throws CoreException
{
77 if (trace
instanceof CtfTmfTrace
) {
78 CtfTmfTrace ctfTmfTrace
= (CtfTmfTrace
) trace
;
79 if (!ctfTmfTrace
.isComplete()) {
80 IResource resource
= ctfTmfTrace
.getResource();
81 String host
= resource
.getPersistentProperty(CtfConstants
.LIVE_HOST
);
82 String port
= resource
.getPersistentProperty(CtfConstants
.LIVE_PORT
);
83 String sessionName
= resource
.getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
84 if (host
!= null && port
!= null && sessionName
!= null && !sessionName
.isEmpty()) {
85 LttngRelaydConnectionInfo entry
= new LttngRelaydConnectionInfo(host
, Integer
.parseInt(port
), sessionName
);
95 * Listen to trace opened so that we can start the relayd job if necessary.
98 * the signal to be processed
101 public void traceOpened(final TmfTraceOpenedSignal signal
) {
104 LttngRelaydConnectionInfo entry
= getEntry(signal
.getTrace());
106 LttngRelaydConsumer consumer
= getConsumer(entry
);
108 consumer
.run((CtfTmfTrace
) signal
.getTrace());
110 } catch (CoreException e
) {
111 Activator
.getDefault().logError(Messages
.LttngRelaydConnectionManager_ConnectionError
, e
);
112 ErrorDialog
.openError(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), null, Messages
.LttngRelaydConnectionManager_ConnectionError
, new Status(IStatus
.WARNING
,
113 Activator
.PLUGIN_ID
, e
.getLocalizedMessage(), e
));
118 * Listen to trace closed so that we can stop the relayd job.
121 * the signal to be processed
124 public void traceClosed(final TmfTraceClosedSignal signal
) {
125 LttngRelaydConnectionInfo entry
;
127 entry
= getEntry(signal
.getTrace());
129 LttngRelaydConsumer comsumer
= getConsumer(entry
);
130 if (comsumer
!= null) {
133 fConnections
.remove(entry
);
135 } catch (CoreException e
) {
136 // Something went wrong with the resource. That's OK, the trace is
137 // getting closed anyway.
142 * Dispose of all the manager's resources (i.e. its connections).
144 public void dispose() {
145 for (LttngRelaydConsumer consumer
: fConnections
.values()) {