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 | |
39 | * @since 3.1 | |
40 | */ | |
41 | public final class LttngRelaydConnectionManager { | |
42 | private static LttngRelaydConnectionManager fConnectionManager; | |
43 | private Map<LttngRelaydConnectionInfo, LttngRelaydConsumer> fConnections = new HashMap<>(); | |
44 | ||
45 | /** | |
46 | * Get an instance of the trace manager. | |
47 | * | |
48 | * @return The trace manager | |
49 | */ | |
50 | public static synchronized LttngRelaydConnectionManager getInstance() { | |
51 | if (fConnectionManager == null) { | |
52 | fConnectionManager = new LttngRelaydConnectionManager(); | |
53 | TmfSignalManager.register(fConnectionManager); | |
54 | } | |
55 | return fConnectionManager; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Get the cosumer for the given relayd connection information. | |
60 | * | |
61 | * @param connectionInfo | |
62 | * the connection information | |
63 | * | |
64 | * @return the consumer | |
65 | */ | |
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; | |
71 | } | |
72 | ||
73 | return fConnections.get(connectionInfo); | |
74 | } | |
75 | ||
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); | |
86 | return entry; | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | return null; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Listen to trace opened so that we can start the relayd job if necessary. | |
96 | * | |
97 | * @param signal | |
98 | * the signal to be processed | |
99 | */ | |
100 | @TmfSignalHandler | |
101 | public void traceOpened(final TmfTraceOpenedSignal signal) { | |
102 | ||
103 | try { | |
104 | LttngRelaydConnectionInfo entry = getEntry(signal.getTrace()); | |
105 | if (entry != null) { | |
106 | LttngRelaydConsumer consumer = getConsumer(entry); | |
107 | consumer.connect(); | |
108 | consumer.run((CtfTmfTrace) signal.getTrace()); | |
109 | } | |
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)); | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Listen to trace closed so that we can stop the relayd job. | |
119 | * | |
120 | * @param signal | |
121 | * the signal to be processed | |
122 | */ | |
123 | @TmfSignalHandler | |
124 | public void traceClosed(final TmfTraceClosedSignal signal) { | |
125 | LttngRelaydConnectionInfo entry; | |
126 | try { | |
127 | entry = getEntry(signal.getTrace()); | |
128 | if (entry != null) { | |
129 | LttngRelaydConsumer comsumer = getConsumer(entry); | |
130 | if (comsumer != null) { | |
131 | comsumer.dispose(); | |
132 | } | |
133 | fConnections.remove(entry); | |
134 | } | |
135 | } catch (CoreException e) { | |
136 | // Something went wrong with the resource. That's OK, the trace is | |
137 | // getting closed anyway. | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
142 | * Dispose of all the manager's resources (i.e. its connections). | |
143 | */ | |
144 | public void dispose() { | |
145 | for (LttngRelaydConsumer consumer : fConnections.values()) { | |
146 | consumer.dispose(); | |
147 | } | |
148 | } | |
149 | } |