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