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
.tracecompass
.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
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
30 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.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
40 public final class LttngRelaydConnectionManager
{
41 private static LttngRelaydConnectionManager fConnectionManager
;
42 private Map
<LttngRelaydConnectionInfo
, LttngRelaydConsumer
> fConnections
= new HashMap
<>();
45 * Get an instance of the trace manager.
47 * @return The trace manager
49 public static synchronized LttngRelaydConnectionManager
getInstance() {
50 if (fConnectionManager
== null) {
51 fConnectionManager
= new LttngRelaydConnectionManager();
52 TmfSignalManager
.register(fConnectionManager
);
54 return fConnectionManager
;
58 * Get the cosumer for the given relayd connection information.
60 * @param connectionInfo
61 * the connection information
63 * @return the consumer
65 public LttngRelaydConsumer
getConsumer(final LttngRelaydConnectionInfo connectionInfo
) {
66 if (!fConnections
.containsKey(connectionInfo
)) {
67 LttngRelaydConsumer lttngRelaydConsumer
= new LttngRelaydConsumer(connectionInfo
);
68 fConnections
.put(connectionInfo
, lttngRelaydConsumer
);
69 return lttngRelaydConsumer
;
72 return fConnections
.get(connectionInfo
);
75 private static LttngRelaydConnectionInfo
getEntry(final ITmfTrace trace
) throws CoreException
{
76 if (trace
instanceof CtfTmfTrace
) {
77 CtfTmfTrace ctfTmfTrace
= (CtfTmfTrace
) trace
;
78 if (!ctfTmfTrace
.isComplete()) {
79 IResource resource
= ctfTmfTrace
.getResource();
80 String host
= resource
.getPersistentProperty(CtfConstants
.LIVE_HOST
);
81 String port
= resource
.getPersistentProperty(CtfConstants
.LIVE_PORT
);
82 String sessionName
= resource
.getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
83 if (host
!= null && port
!= null && sessionName
!= null && !sessionName
.isEmpty()) {
84 LttngRelaydConnectionInfo entry
= new LttngRelaydConnectionInfo(host
, Integer
.parseInt(port
), sessionName
);
94 * Listen to trace opened so that we can start the relayd job if necessary.
97 * the signal to be processed
100 public void traceOpened(final TmfTraceOpenedSignal signal
) {
103 LttngRelaydConnectionInfo entry
= getEntry(signal
.getTrace());
105 LttngRelaydConsumer consumer
= getConsumer(entry
);
107 consumer
.run((CtfTmfTrace
) signal
.getTrace());
109 } catch (CoreException e
) {
110 Activator
.getDefault().logError(Messages
.LttngRelaydConnectionManager_ConnectionError
, e
);
111 ErrorDialog
.openError(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), null, Messages
.LttngRelaydConnectionManager_ConnectionError
, new Status(IStatus
.WARNING
,
112 Activator
.PLUGIN_ID
, e
.getLocalizedMessage(), e
));
117 * Listen to trace closed so that we can stop the relayd job.
120 * the signal to be processed
123 public void traceClosed(final TmfTraceClosedSignal signal
) {
124 LttngRelaydConnectionInfo entry
;
126 entry
= getEntry(signal
.getTrace());
128 LttngRelaydConsumer comsumer
= getConsumer(entry
);
129 if (comsumer
!= null) {
132 fConnections
.remove(entry
);
134 } catch (CoreException e
) {
135 // Something went wrong with the resource. That's OK, the trace is
136 // getting closed anyway.
141 * Dispose of all the manager's resources (i.e. its connections).
143 public void dispose() {
144 for (LttngRelaydConsumer consumer
: fConnections
.values()) {