Disable live tracing feature
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / model / impl / TargetNodeComponent.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 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 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
13 * Bernd Hufmann - Update to org.eclipse.remote API 2.0
14 **********************************************************************/
15 package org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl;
16
17 import static java.text.MessageFormat.format;
18 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
20 import java.util.List;
21
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
27 import org.eclipse.core.runtime.jobs.Job;
28 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.eclipse.jface.dialogs.ErrorDialog;
32 import org.eclipse.remote.core.IRemoteConnection;
33 import org.eclipse.remote.core.IRemoteConnectionChangeListener;
34 import org.eclipse.remote.core.RemoteConnectionChangeEvent;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
38 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
39 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
40 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
41 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.property.TargetNodePropertySource;
42 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.ILttngControlService;
43 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.LTTngControlServiceFactory;
44 import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
45 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.views.properties.IPropertySource;
48
49 /**
50 * <p>
51 * Implementation of the trace node component.
52 * </p>
53 *
54 * @author Bernd Hufmann
55 */
56 public class TargetNodeComponent extends TraceControlComponent implements IRemoteConnectionChangeListener {
57
58 // ------------------------------------------------------------------------
59 // Constants
60 // ------------------------------------------------------------------------
61
62 /**
63 * Path to icon file for this component (state connected).
64 */
65 public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$
66 /**
67 * Path to icon file for this component (state disconnected).
68 */
69 public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
70
71 private static final ILttngControlService NULL_CONTROL_SERVICE = new NullControlService();
72
73 // ------------------------------------------------------------------------
74 // Attributes
75 // ------------------------------------------------------------------------
76
77 /**
78 * The node connection state.
79 */
80 private TargetNodeState fState = TargetNodeState.DISCONNECTED;
81 /**
82 * The image to be displayed in state disconnected.
83 */
84 private Image fDisconnectedImage = null;
85 /**
86 * The remote proxy implementation.
87 */
88 private @NonNull RemoteSystemProxy fRemoteProxy;
89 /**
90 * The control service for LTTng specific commands.
91 */
92 private ILttngControlService fService = null;
93 /**
94 * The command shell for issuing commands.
95 */
96 private ICommandShell fShell = null;
97
98 // ------------------------------------------------------------------------
99 // Constructors
100 // ------------------------------------------------------------------------
101
102 /**
103 * Constructor
104 *
105 * @param name
106 * the name of the component
107 * @param parent
108 * the parent of the component
109 * @param proxy
110 * the remote proxy implementation
111 */
112 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull RemoteSystemProxy proxy) {
113 super(name, parent);
114 setImage(TARGET_NODE_CONNECTED_ICON_FILE);
115 fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
116 fRemoteProxy = proxy;
117 fRemoteProxy.getRemoteConnection().addConnectionChangeListener(this);
118 setToolTip(fRemoteProxy.getRemoteConnection().getName());
119 }
120
121 /**
122 * Constructor (using default proxy)
123 *
124 * @param name
125 * the name of the component
126 * @param parent
127 * the parent of the component
128 * @param host
129 * the host connection implementation
130 */
131 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull IRemoteConnection host) {
132 this(name, parent, new RemoteSystemProxy(host));
133 }
134
135 @Override
136 public void dispose() {
137 fRemoteProxy.getRemoteConnection().removeConnectionChangeListener(this);
138 fRemoteProxy.dispose();
139 disposeControlService();
140 }
141
142 private void disposeControlService() {
143 fService = null;
144 final ICommandShell shell = fShell;
145 if (shell != null) {
146 shell.dispose();
147 fShell = null;
148 }
149 }
150
151 // ------------------------------------------------------------------------
152 // Accessors
153 // ------------------------------------------------------------------------
154
155 @Override
156 public Image getImage() {
157 if (fState == TargetNodeState.CONNECTED) {
158 return super.getImage();
159 }
160 return fDisconnectedImage;
161 }
162
163 @Override
164 public TargetNodeState getTargetNodeState() {
165 return fState;
166 }
167
168 @Override
169 public void setTargetNodeState(TargetNodeState state) {
170 fState = state;
171 fireComponentChanged(TargetNodeComponent.this);
172 }
173
174 @Override
175 public ILttngControlService getControlService() {
176 return fService == null ? NULL_CONTROL_SERVICE : fService;
177 }
178
179 @Override
180 public void setControlService(ILttngControlService service) {
181 fService = service;
182 }
183
184 @Override
185 public <T> @Nullable T getAdapter(Class<T> adapter) {
186 if (adapter == IPropertySource.class) {
187 return adapter.cast(new TargetNodePropertySource(this));
188 }
189 return null;
190 }
191
192 /**
193 * @return remote system proxy implementation
194 */
195 public @NonNull RemoteSystemProxy getRemoteSystemProxy() {
196 return fRemoteProxy;
197 }
198
199 /**
200 * @return all available sessions.
201 */
202 public @NonNull TraceSessionComponent[] getSessions() {
203 List<ITraceControlComponent> compenents = getChildren(TraceSessionGroup.class);
204 if (compenents.size() > 0) {
205 TraceSessionGroup group = (TraceSessionGroup)compenents.get(0);
206 List<ITraceControlComponent> sessions = group.getChildren(TraceSessionComponent.class);
207 return sessions.toArray(new @NonNull TraceSessionComponent[sessions.size()]);
208 }
209 return new TraceSessionComponent[0];
210 }
211
212 /**
213 * @return node version
214 */
215 public String getNodeVersion() {
216 // Control service is null during connection to node
217 if (getControlService() != NULL_CONTROL_SERVICE) {
218 return getControlService().getVersionString();
219 }
220 return ""; //$NON-NLS-1$
221 }
222
223 /**
224 * Returns if node supports filtering of events
225 * @param isKernel - <code>true</code> for kernel provider else <code>false</code>
226 * @return <code>true</code> if node supports filtering else <code>false</code>
227 */
228 public boolean isEventFilteringSupported(boolean isKernel) {
229 if (isKernel) {
230 return getControlService().isVersionSupported("2.7.0"); //$NON-NLS-1$
231 }
232 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
233 }
234
235 /**
236 * Returns if node supports networks streaming or not
237 * @return <code>true</code> if node supports filtering else <code>false</code>
238 *
239 */
240 public boolean isNetworkStreamingSupported() {
241 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
242 }
243
244 /**
245 * Returns if node supports configuring buffer type or not
246 * @return <code>true</code> if node supports buffer type configuration else <code>false</code>
247 */
248 public boolean isBufferTypeConfigSupported() {
249 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
250 }
251
252 /**
253 * Returns if node supports trace file rotation or not
254 * @return <code>true</code> if node supports trace file rotation else <code>false</code>
255 */
256 public boolean isTraceFileRotationSupported() {
257 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
258 }
259
260 /**
261 * Returns if node supports periodical flush for metadata or not
262 * @return <code>true</code> if node supports periodical flush for metadata else <code>false</code>
263 */
264 public boolean isPeriodicalMetadataFlushSupported() {
265 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
266 }
267 /**
268 * Returns if node supports snapshots or not
269 * @return <code>true</code> if it supports snapshots else <code>false</code>
270 *
271 */
272 public boolean isSnapshotSupported() {
273 return getControlService().isVersionSupported("2.3.0"); //$NON-NLS-1$
274 }
275 /**
276 * Returns if node supports live or not
277 * @return <code>true</code> if it supports live else <code>false</code>
278 *
279 */
280 public boolean isLiveSupported() {
281 return false;
282 // FIXME: Disable Live support until we have a better implementation
283 //return getControlService().isVersionSupported("2.4.0"); //$NON-NLS-1$;
284 }
285 /**
286 * Returns if node supports adding contexts on event
287 * @return <code>true</code> if it supports adding contexts on events else <code>false</code>
288 *
289 */
290 public boolean isContextOnEventSupported() {
291 return !getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
292 }
293
294 /**
295 * Checks if given version is supported by this ILTTngControlService implementation.
296 *
297 * @param version The version to check
298 * @return <code>true</code> if version is supported else <code>false</code>
299 */
300 public boolean isVersionSupported(String version) {
301 return getControlService().isVersionSupported(version);
302 }
303
304 // ------------------------------------------------------------------------
305 // Operations
306 // ------------------------------------------------------------------------
307
308 @Override
309 public void connectionChanged(RemoteConnectionChangeEvent e) {
310 if (fState == TargetNodeState.CONNECTING) {
311 return;
312 }
313
314 switch (e.getType()) {
315 case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
316 case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
317 handleDisconnected();
318 break;
319 case RemoteConnectionChangeEvent.CONNECTION_OPENED:
320 handleConnected();
321 break;
322 default:
323 break;
324 }
325 }
326
327 /**
328 * Method to connect this node component to the remote target node.
329 */
330 public void connect() {
331 if (fState == TargetNodeState.DISCONNECTED) {
332 try {
333 setTargetNodeState(TargetNodeState.CONNECTING);
334 Job job = new Job(format(Messages.TraceControl_OpenConnectionTo, getName())) {
335 @Override
336 protected IStatus run(IProgressMonitor monitor) {
337 try {
338 fRemoteProxy.connect(checkNotNull(monitor));
339 return Status.OK_STATUS;
340 } catch (Exception e) {
341 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure, e);
342 }
343 }
344 };
345 job.addJobChangeListener(new JobChangeAdapter() {
346 @Override
347 public void done(IJobChangeEvent event) {
348 IStatus status = event.getResult();
349 if (status.isOK()) {
350 handleConnected();
351 } else {
352 handleDisconnected();
353 if (status.getSeverity() != IStatus.CANCEL) {
354 Activator.getDefault().getLog().log(status);
355 }
356 }
357 }
358 });
359 job.schedule();
360 } catch (Exception e) {
361 setTargetNodeState(TargetNodeState.DISCONNECTED);
362 Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
363 }
364 }
365 }
366
367 /**
368 * Method to disconnect this node component to the remote target node.
369 */
370 public void disconnect() {
371 if (fState == TargetNodeState.CONNECTED) {
372 try {
373 setTargetNodeState(TargetNodeState.DISCONNECTING);
374 fRemoteProxy.disconnect();
375 } catch (Exception e) {
376 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
377 } finally {
378 handleDisconnected();
379 }
380 }
381 }
382
383 /**
384 * Retrieves the trace configuration from the target node and populates the
385 * information in the tree model. The execution is done in a own job.
386 */
387 public void getConfigurationFromNode() {
388 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
389 @Override
390 protected IStatus run(IProgressMonitor monitor) {
391
392 try {
393 // Get provider information from node
394 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
395 addChild(providerGroup);
396
397 // Get session information from node
398 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
399 addChild(sessionGroup);
400
401 providerGroup.getProviderFromNode(monitor);
402 sessionGroup.getSessionsFromNode(monitor);
403 } catch (ExecutionException e) {
404 removeAllChildren();
405 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
406 }
407
408 return Status.OK_STATUS;
409 }
410 };
411 job.setUser(true);
412 job.schedule();
413 }
414
415 /**
416 * Refresh the node configuration
417 */
418 public void refresh() {
419 removeAllChildren();
420 getConfigurationFromNode();
421 }
422
423 // ------------------------------------------------------------------------
424 // Helper function
425 // ------------------------------------------------------------------------
426
427 /**
428 * @return returns the control service for LTTng specific commands.
429 * @throws ExecutionException
430 */
431 private ILttngControlService createControlService() throws ExecutionException {
432 if (fService == null) {
433 try {
434 ICommandShell shell = fRemoteProxy.createCommandShell();
435 fShell = shell;
436 fService = LTTngControlServiceFactory.getLttngControlService(shell);
437 } catch (ExecutionException e) {
438 disposeControlService();
439 throw e;
440 }
441 }
442 return fService;
443 }
444
445 /**
446 * Handles the connected event.
447 */
448 private void handleConnected() {
449 try {
450 createControlService();
451 getConfigurationFromNode();
452 // Set connected only after the control service has been created and the jobs for creating the
453 // sub-nodes are scheduled.
454 setTargetNodeState(TargetNodeState.CONNECTED);
455 } catch (final ExecutionException e) {
456 // Disconnect only if no control service, otherwise stay connected.
457 if (getControlService() == NULL_CONTROL_SERVICE) {
458 fState = TargetNodeState.CONNECTED;
459 disconnect();
460 }
461
462 // Notify user
463 Display.getDefault().asyncExec(new Runnable() {
464 @Override
465 public void run() {
466 ErrorDialog er = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
467 Messages.TraceControl_ErrorTitle, Messages.TraceControl_RetrieveNodeConfigurationFailure,
468 new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e),
469 IStatus.ERROR);
470 er.open();
471 }
472 });
473 Activator.getDefault().logError(Messages.TraceControl_RetrieveNodeConfigurationFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
474 }
475 }
476
477 /**
478 * Handles the disconnected event.
479 */
480 private void handleDisconnected() {
481 disposeControlService();
482 setTargetNodeState(TargetNodeState.DISCONNECTED);
483 removeAllChildren();
484 }
485
486 @Override
487 public void addChild(ITraceControlComponent component) {
488 if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
489 return;
490 }
491 super.addChild(component);
492 }
493 }
This page took 0.057759 seconds and 5 git commands to generate.