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