Commit | Line | Data |
---|---|---|
6fd3c6e9 MAL |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Marc-Andre Laperle - Initial implementation | |
11 | **********************************************************************/ | |
12 | ||
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.control.ui.relayd; |
6fd3c6e9 MAL |
14 | |
15 | import java.util.HashMap; | |
16 | import java.util.Map; | |
17 | ||
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; | |
9bc60be7 | 23 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
2bdf0193 AM |
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; | |
9722e5d7 | 30 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
6fd3c6e9 MAL |
31 | import org.eclipse.ui.PlatformUI; |
32 | ||
33 | /** | |
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 | |
36 | * the connection. | |
37 | * | |
38 | * @author Marc-Andre Laperle | |
6fd3c6e9 MAL |
39 | */ |
40 | public final class LttngRelaydConnectionManager { | |
41 | private static LttngRelaydConnectionManager fConnectionManager; | |
42 | private Map<LttngRelaydConnectionInfo, LttngRelaydConsumer> fConnections = new HashMap<>(); | |
43 | ||
44 | /** | |
45 | * Get an instance of the trace manager. | |
46 | * | |
47 | * @return The trace manager | |
48 | */ | |
49 | public static synchronized LttngRelaydConnectionManager getInstance() { | |
50 | if (fConnectionManager == null) { | |
51 | fConnectionManager = new LttngRelaydConnectionManager(); | |
52 | TmfSignalManager.register(fConnectionManager); | |
53 | } | |
54 | return fConnectionManager; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Get the cosumer for the given relayd connection information. | |
59 | * | |
60 | * @param connectionInfo | |
61 | * the connection information | |
62 | * | |
63 | * @return the consumer | |
64 | */ | |
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; | |
70 | } | |
71 | ||
72 | return fConnections.get(connectionInfo); | |
73 | } | |
74 | ||
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); | |
85 | return entry; | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
90 | return null; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Listen to trace opened so that we can start the relayd job if necessary. | |
95 | * | |
96 | * @param signal | |
97 | * the signal to be processed | |
98 | */ | |
99 | @TmfSignalHandler | |
100 | public void traceOpened(final TmfTraceOpenedSignal signal) { | |
101 | ||
102 | try { | |
103 | LttngRelaydConnectionInfo entry = getEntry(signal.getTrace()); | |
104 | if (entry != null) { | |
105 | LttngRelaydConsumer consumer = getConsumer(entry); | |
106 | consumer.connect(); | |
107 | consumer.run((CtfTmfTrace) signal.getTrace()); | |
108 | } | |
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)); | |
113 | } | |
114 | } | |
115 | ||
116 | /** | |
117 | * Listen to trace closed so that we can stop the relayd job. | |
118 | * | |
119 | * @param signal | |
120 | * the signal to be processed | |
121 | */ | |
122 | @TmfSignalHandler | |
123 | public void traceClosed(final TmfTraceClosedSignal signal) { | |
124 | LttngRelaydConnectionInfo entry; | |
125 | try { | |
126 | entry = getEntry(signal.getTrace()); | |
127 | if (entry != null) { | |
128 | LttngRelaydConsumer comsumer = getConsumer(entry); | |
129 | if (comsumer != null) { | |
130 | comsumer.dispose(); | |
131 | } | |
132 | fConnections.remove(entry); | |
133 | } | |
134 | } catch (CoreException e) { | |
135 | // Something went wrong with the resource. That's OK, the trace is | |
136 | // getting closed anyway. | |
137 | } | |
138 | } | |
139 | ||
140 | /** | |
141 | * Dispose of all the manager's resources (i.e. its connections). | |
142 | */ | |
143 | public void dispose() { | |
144 | for (LttngRelaydConsumer consumer : fConnections.values()) { | |
145 | consumer.dispose(); | |
146 | } | |
147 | } | |
148 | } |