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