Commit | Line | Data |
---|---|---|
eb1bab5b BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 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 | * Bernd Hufmann - Initial API and implementation | |
11 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.lttng.ui.views.control.model.impl; | |
13 | ||
14 | import org.eclipse.core.commands.ExecutionException; | |
15 | import org.eclipse.core.runtime.IProgressMonitor; | |
16 | import org.eclipse.core.runtime.IStatus; | |
17 | import org.eclipse.core.runtime.Status; | |
18 | import org.eclipse.core.runtime.jobs.Job; | |
19 | import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin; | |
20 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; | |
21 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent; | |
22 | import org.eclipse.linuxtools.lttng.ui.views.control.model.TargetNodeState; | |
23 | import org.eclipse.linuxtools.lttng.ui.views.control.property.TargetNodePropertySource; | |
24 | import org.eclipse.linuxtools.lttng.ui.views.control.remote.IRemoteSystemProxy; | |
25 | import org.eclipse.linuxtools.lttng.ui.views.control.remote.RemoteSystemProxy; | |
26 | import org.eclipse.linuxtools.lttng.ui.views.control.service.ICommandShell; | |
27 | import org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService; | |
28 | import org.eclipse.linuxtools.lttng.ui.views.control.service.LTTngControlService; | |
29 | import org.eclipse.rse.core.model.IHost; | |
30 | import org.eclipse.rse.core.model.IRSECallback; | |
31 | import org.eclipse.rse.core.subsystems.CommunicationsEvent; | |
32 | import org.eclipse.rse.core.subsystems.ICommunicationsListener; | |
33 | import org.eclipse.swt.graphics.Image; | |
34 | import org.eclipse.ui.views.properties.IPropertySource; | |
35 | ||
36 | /** | |
37 | * <b><u>TargetNodeComponent</u></b> | |
38 | * <p> | |
39 | * Implementation of the trace node component. | |
40 | * </p> | |
41 | */ | |
42 | public class TargetNodeComponent extends TraceControlComponent implements ICommunicationsListener { | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Constants | |
46 | // ------------------------------------------------------------------------ | |
47 | /** | |
48 | * Path to icon file for this component (state connected). | |
49 | */ | |
50 | public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$ | |
51 | /** | |
52 | * Path to icon file for this component (state disconnected). | |
53 | */ | |
54 | public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$ | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Attributes | |
58 | // ------------------------------------------------------------------------ | |
59 | /** | |
60 | * The node connection state. | |
61 | */ | |
62 | private TargetNodeState fState = TargetNodeState.DISCONNECTED; | |
63 | /** | |
64 | * The image to be displayed in state disconnected. | |
65 | */ | |
66 | private Image fDisconnectedImage = null; | |
67 | /** | |
68 | * The connection implementation. | |
69 | */ | |
70 | private IHost fHost = null; | |
71 | /** | |
72 | * The remote proxy implementation. | |
73 | */ | |
74 | private IRemoteSystemProxy fRemoteProxy = null; | |
75 | /** | |
76 | * The control service for LTTng specific commands. | |
77 | */ | |
78 | private ILttngControlService fService = null; | |
79 | /** | |
80 | * The command shell for issuing commands. | |
81 | */ | |
82 | private ICommandShell fShell = null; | |
83 | ||
84 | // ------------------------------------------------------------------------ | |
85 | // Constructors | |
86 | // ------------------------------------------------------------------------ | |
87 | /** | |
88 | * Constructor | |
89 | * @param name - the name of the component | |
90 | * @param parent - the parent of the component | |
91 | * @param host - the host connection implementation | |
92 | * @param proxy - the remote proxy implementation | |
93 | */ | |
94 | public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host, IRemoteSystemProxy proxy) { | |
95 | super(name, parent); | |
96 | setImage(TARGET_NODE_CONNECTED_ICON_FILE); | |
97 | fDisconnectedImage = LTTngUiPlugin.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE); | |
98 | fHost = host; | |
99 | fRemoteProxy = proxy; | |
100 | setToolTip(fHost.getHostName()); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Constructor (using default proxy) | |
105 | * @param name - the name of the component | |
106 | * @param parent - the parent of the component | |
107 | * @param host - the host connection implementation | |
108 | */ | |
109 | public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host) { | |
110 | this(name, parent, host, new RemoteSystemProxy(host)); | |
111 | } | |
112 | ||
113 | // ------------------------------------------------------------------------ | |
114 | // Accessors | |
115 | // ------------------------------------------------------------------------ | |
116 | /* | |
117 | * (non-Javadoc) | |
118 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#getImage() | |
119 | */ | |
120 | @Override | |
121 | public Image getImage() { | |
122 | if (fState == TargetNodeState.CONNECTED) { | |
123 | return super.getImage(); | |
124 | } | |
125 | return fDisconnectedImage; | |
126 | } | |
127 | ||
128 | /* | |
129 | * (non-Javadoc) | |
130 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#getTargetNodeState() | |
131 | */ | |
132 | @Override | |
133 | public TargetNodeState getTargetNodeState() { | |
134 | return fState; | |
135 | } | |
136 | ||
137 | /* | |
138 | * (non-Javadoc) | |
139 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#setTargetNodeState(org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent.TargetNodeState) | |
140 | */ | |
141 | @Override | |
142 | public void setTargetNodeState(TargetNodeState state) { | |
143 | fState = state; | |
4775bcbf | 144 | fireComponentChanged(TargetNodeComponent.this); |
eb1bab5b BH |
145 | } |
146 | ||
147 | /* | |
148 | * (non-Javadoc) | |
149 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#getControlService() | |
150 | */ | |
151 | @Override | |
152 | public ILttngControlService getControlService() { | |
153 | return fService; | |
154 | } | |
155 | ||
156 | /* | |
157 | * (non-Javadoc) | |
158 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#setControlService(org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService) | |
159 | */ | |
160 | @Override | |
161 | public void setControlService(ILttngControlService service) { | |
162 | fService = (ILttngControlService)service; | |
163 | } | |
164 | ||
165 | /* | |
166 | * (non-Javadoc) | |
167 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceControlComponent#getAdapter(java.lang.Class) | |
168 | */ | |
169 | @SuppressWarnings("rawtypes") | |
170 | @Override | |
171 | public Object getAdapter(Class adapter) { | |
172 | if (adapter == IPropertySource.class) { | |
173 | return new TargetNodePropertySource(this); | |
174 | } | |
175 | return null; | |
176 | } | |
177 | ||
bbb3538a BH |
178 | /** |
179 | * @return remote host name | |
180 | */ | |
eb1bab5b BH |
181 | public String getHostName() { |
182 | return fHost.getHostName(); | |
183 | } | |
184 | ||
bbb3538a BH |
185 | /** |
186 | * @return remote system proxy implementation | |
187 | */ | |
188 | public IRemoteSystemProxy getRemoteSystemProxy() { | |
189 | return fRemoteProxy; | |
190 | } | |
191 | ||
eb1bab5b BH |
192 | // ------------------------------------------------------------------------ |
193 | // Operations | |
194 | // ------------------------------------------------------------------------ | |
195 | ||
196 | /* | |
197 | * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#communicationsStateChange(org.eclipse.rse.core.subsystems.CommunicationsEvent) | |
198 | */ | |
199 | @Override | |
200 | public void communicationsStateChange(CommunicationsEvent e) { | |
201 | if (e.getState() == CommunicationsEvent.AFTER_DISCONNECT || | |
202 | e.getState() == CommunicationsEvent.CONNECTION_ERROR) { | |
203 | handleDisconnected(); | |
204 | } if ((e.getState() == CommunicationsEvent.AFTER_CONNECT) && (fState != TargetNodeState.CONNECTING)) { | |
205 | handleConnected(); | |
206 | } | |
207 | } | |
208 | ||
209 | /* (non-Javadoc) | |
210 | * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#isPassiveCommunicationsListener() | |
211 | */ | |
212 | @Override | |
213 | public boolean isPassiveCommunicationsListener() { | |
214 | return true; | |
215 | } | |
216 | ||
217 | /* | |
218 | * (non-Javadoc) | |
219 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.TraceControlComponent#dispose() | |
220 | */ | |
221 | @Override | |
222 | public void dispose() { | |
223 | fRemoteProxy.removeCommunicationListener(this); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Method to connect this node component to the remote target node. | |
228 | */ | |
229 | public void connect() { | |
230 | if (fState == TargetNodeState.DISCONNECTED) { | |
231 | try { | |
232 | setTargetNodeState(TargetNodeState.CONNECTING); | |
233 | fRemoteProxy.connect(new IRSECallback() { | |
234 | @Override | |
235 | public void done(IStatus status, Object result) { | |
236 | // Note: result might be null! | |
237 | if(status.isOK()) { | |
238 | handleConnected(); | |
06b9339e BH |
239 | } else { |
240 | handleDisconnected(); | |
eb1bab5b BH |
241 | } |
242 | } | |
243 | }); | |
244 | } catch (Exception e) { | |
245 | setTargetNodeState(TargetNodeState.DISCONNECTED); | |
246 | LTTngUiPlugin.logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + ").", e); //$NON-NLS-1$ //$NON-NLS-2$ | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
252 | * Method to disconnect this node component to the remote target node. | |
253 | */ | |
254 | public void disconnect() { | |
255 | if (fState == TargetNodeState.CONNECTED) { | |
256 | try { | |
257 | setTargetNodeState(TargetNodeState.DISCONNECTING); | |
258 | fRemoteProxy.disconnect(); | |
259 | } catch (Exception e) { | |
260 | LTTngUiPlugin.logError(Messages.TraceControl_DisconnectionFailure + getName(), e); | |
261 | } finally { | |
262 | handleDisconnected(); | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | /** | |
268 | * Retrieves the trace configuration from the target node and populates the information | |
269 | * in the tree model. The execution is done in a own job. | |
270 | * | |
271 | * @throws ExecutionException | |
272 | */ | |
273 | public void getConfigurationFromNode() throws ExecutionException { | |
274 | Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) { | |
275 | @Override | |
276 | protected IStatus run(IProgressMonitor monitor) { | |
277 | ||
278 | try { | |
279 | // Get provider information from node | |
280 | TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this); | |
281 | addChild(providerGroup); | |
282 | providerGroup.getProviderFromNode(monitor); | |
283 | ||
284 | // Get session information from node | |
285 | TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this); | |
286 | addChild(sessionGroup); | |
287 | sessionGroup.getSessionsFromNode(monitor); | |
288 | } catch (ExecutionException e) { | |
289 | removeAllChildren(); | |
290 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString()); | |
291 | } | |
292 | ||
293 | return Status.OK_STATUS; | |
294 | } | |
295 | }; | |
296 | job.setUser(true); | |
297 | job.schedule(); | |
298 | ||
299 | } | |
300 | ||
301 | // ------------------------------------------------------------------------ | |
302 | // Helper function | |
303 | // ------------------------------------------------------------------------ | |
304 | /** | |
305 | * @return returns the control service for LTTng specific commands. | |
306 | * @throws ExecutionException | |
307 | */ | |
308 | private ILttngControlService createControlService() throws ExecutionException { | |
309 | if (fShell == null) { | |
310 | fShell = fRemoteProxy.createCommandShell(); | |
311 | fRemoteProxy.addCommunicationListener(this); | |
312 | } | |
313 | fService = new LTTngControlService(fShell); | |
314 | return fService; | |
315 | } | |
316 | ||
317 | /** | |
318 | * Handles the connected event. | |
319 | */ | |
320 | private void handleConnected() { | |
321 | setTargetNodeState(TargetNodeState.CONNECTED); | |
322 | try { | |
323 | createControlService(); | |
324 | getConfigurationFromNode(); | |
325 | } catch (ExecutionException e) { | |
326 | LTTngUiPlugin.logError(Messages.TraceControl_ListSessionFailure, e); | |
327 | } | |
328 | } | |
329 | ||
330 | /** | |
331 | * Handles the disconnected event. | |
332 | */ | |
333 | private void handleDisconnected() { | |
334 | removeAllChildren(); | |
335 | setTargetNodeState(TargetNodeState.DISCONNECTED); | |
336 | fShell = null; | |
337 | fService = null; | |
338 | } | |
339 | } |