1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
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;
17 import static java.text.MessageFormat.format;
18 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
20 import java.util.List;
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;
51 * Implementation of the trace node component.
54 * @author Bernd Hufmann
56 public class TargetNodeComponent extends TraceControlComponent implements IRemoteConnectionChangeListener {
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Path to icon file for this component (state connected).
65 public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$
67 * Path to icon file for this component (state disconnected).
69 public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
71 private static final ILttngControlService NULL_CONTROL_SERVICE = new NullControlService();
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * The node connection state.
80 private TargetNodeState fState = TargetNodeState.DISCONNECTED;
82 * The image to be displayed in state disconnected.
84 private Image fDisconnectedImage = null;
86 * The remote proxy implementation.
88 private @NonNull RemoteSystemProxy fRemoteProxy;
90 * The control service for LTTng specific commands.
92 private ILttngControlService fService = null;
94 * The command shell for issuing commands.
96 private ICommandShell fShell = null;
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
106 * the name of the component
108 * the parent of the component
110 * the remote proxy implementation
112 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull RemoteSystemProxy proxy) {
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());
122 * Constructor (using default proxy)
125 * the name of the component
127 * the parent of the component
129 * the host connection implementation
131 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull IRemoteConnection host) {
132 this(name, parent, new RemoteSystemProxy(host));
136 public void dispose() {
137 fRemoteProxy.getRemoteConnection().removeConnectionChangeListener(this);
138 fRemoteProxy.dispose();
139 disposeControlService();
142 private void disposeControlService() {
144 final ICommandShell shell = fShell;
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
156 public Image getImage() {
157 if (fState == TargetNodeState.CONNECTED) {
158 return super.getImage();
160 return fDisconnectedImage;
164 public TargetNodeState getTargetNodeState() {
169 public void setTargetNodeState(TargetNodeState state) {
171 fireComponentChanged(TargetNodeComponent.this);
175 public ILttngControlService getControlService() {
176 return fService == null ? NULL_CONTROL_SERVICE : fService;
180 public void setControlService(ILttngControlService service) {
185 public <T> @Nullable T getAdapter(Class<T> adapter) {
186 if (adapter == IPropertySource.class) {
187 return adapter.cast(new TargetNodePropertySource(this));
193 * @return remote system proxy implementation
195 public @NonNull RemoteSystemProxy getRemoteSystemProxy() {
200 * @return all available sessions.
202 public 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 TraceSessionComponent[sessions.size()]);
209 return new TraceSessionComponent[0];
213 * @return node version
215 public String getNodeVersion() {
216 // Control service is null during connection to node
217 if (getControlService() != NULL_CONTROL_SERVICE) {
218 return getControlService().getVersionString();
220 return ""; //$NON-NLS-1$
224 * Returns if node supports filtering of events
225 * @return <code>true</code> if node supports filtering else <code>false</code>
227 public boolean isEventFilteringSupported() {
228 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
232 * Returns if node supports networks streaming or not
233 * @return <code>true</code> if node supports filtering else <code>false</code>
236 public boolean isNetworkStreamingSupported() {
237 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
241 * Returns if node supports configuring buffer type or not
242 * @return <code>true</code> if node supports buffer type configuration else <code>false</code>
244 public boolean isBufferTypeConfigSupported() {
245 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
249 * Returns if node supports trace file rotation or not
250 * @return <code>true</code> if node supports trace file rotation else <code>false</code>
252 public boolean isTraceFileRotationSupported() {
253 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
257 * Returns if node supports periodical flush for metadata or not
258 * @return <code>true</code> if node supports periodical flush for metadata else <code>false</code>
260 public boolean isPeriodicalMetadataFlushSupported() {
261 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
264 * Returns if node supports snapshots or not
265 * @return <code>true</code> if it supports snapshots else <code>false</code>
268 public boolean isSnapshotSupported() {
269 return getControlService().isVersionSupported("2.3.0"); //$NON-NLS-1$
272 * Returns if node supports live or not
273 * @return <code>true</code> if it supports live else <code>false</code>
276 public boolean isLiveSupported() {
277 return getControlService().isVersionSupported("2.4.0"); //$NON-NLS-1$;
280 * Returns if node supports adding contexts on event
281 * @return <code>true</code> if it supports adding contexts on events else <code>false</code>
284 public boolean isContextOnEventSupported() {
285 return !getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
289 * Checks if given version is supported by this ILTTngControlService implementation.
291 * @param version The version to check
292 * @return <code>true</code> if version is supported else <code>false</code>
294 public boolean isVersionSupported(String version) {
295 return getControlService().isVersionSupported(version);
298 // ------------------------------------------------------------------------
300 // ------------------------------------------------------------------------
303 public void connectionChanged(RemoteConnectionChangeEvent e) {
304 if (fState == TargetNodeState.CONNECTING) {
308 switch (e.getType()) {
309 case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
310 case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
311 handleDisconnected();
313 case RemoteConnectionChangeEvent.CONNECTION_OPENED:
322 * Method to connect this node component to the remote target node.
324 public void connect() {
325 if (fState == TargetNodeState.DISCONNECTED) {
327 setTargetNodeState(TargetNodeState.CONNECTING);
328 Job job = new Job(format(Messages.TraceControl_OpenConnectionTo, getName())) {
330 protected IStatus run(IProgressMonitor monitor) {
332 fRemoteProxy.connect(checkNotNull(monitor));
333 return Status.OK_STATUS;
334 } catch (Exception e) {
335 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure, e);
339 job.addJobChangeListener(new JobChangeAdapter() {
341 public void done(IJobChangeEvent event) {
342 IStatus status = event.getResult();
346 handleDisconnected();
347 if (status.getSeverity() != IStatus.CANCEL) {
348 Activator.getDefault().getLog().log(status);
354 } catch (Exception e) {
355 setTargetNodeState(TargetNodeState.DISCONNECTED);
356 Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
362 * Method to disconnect this node component to the remote target node.
364 public void disconnect() {
365 if (fState == TargetNodeState.CONNECTED) {
367 setTargetNodeState(TargetNodeState.DISCONNECTING);
368 fRemoteProxy.disconnect();
369 } catch (Exception e) {
370 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
372 handleDisconnected();
378 * Retrieves the trace configuration from the target node and populates the
379 * information in the tree model. The execution is done in a own job.
381 public void getConfigurationFromNode() {
382 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
384 protected IStatus run(IProgressMonitor monitor) {
387 // Get provider information from node
388 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
389 addChild(providerGroup);
391 // Get session information from node
392 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
393 addChild(sessionGroup);
395 providerGroup.getProviderFromNode(monitor);
396 sessionGroup.getSessionsFromNode(monitor);
397 } catch (ExecutionException e) {
399 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
402 return Status.OK_STATUS;
410 * Refresh the node configuration
412 public void refresh() {
414 getConfigurationFromNode();
417 // ------------------------------------------------------------------------
419 // ------------------------------------------------------------------------
422 * @return returns the control service for LTTng specific commands.
423 * @throws ExecutionException
425 private ILttngControlService createControlService() throws ExecutionException {
426 if (fService == null) {
428 ICommandShell shell = fRemoteProxy.createCommandShell();
430 fService = LTTngControlServiceFactory.getLttngControlService(shell);
431 } catch (ExecutionException e) {
432 disposeControlService();
440 * Handles the connected event.
442 private void handleConnected() {
444 createControlService();
445 getConfigurationFromNode();
446 // Set connected only after the control service has been created and the jobs for creating the
447 // sub-nodes are scheduled.
448 setTargetNodeState(TargetNodeState.CONNECTED);
449 } catch (final ExecutionException e) {
450 // Disconnect only if no control service, otherwise stay connected.
451 if (getControlService() == NULL_CONTROL_SERVICE) {
452 fState = TargetNodeState.CONNECTED;
457 Display.getDefault().asyncExec(new Runnable() {
460 ErrorDialog er = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
461 Messages.TraceControl_ErrorTitle, Messages.TraceControl_RetrieveNodeConfigurationFailure,
462 new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e),
467 Activator.getDefault().logError(Messages.TraceControl_RetrieveNodeConfigurationFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
472 * Handles the disconnected event.
474 private void handleDisconnected() {
475 disposeControlService();
476 setTargetNodeState(TargetNodeState.DISCONNECTED);
481 public void addChild(ITraceControlComponent component) {
482 if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
485 super.addChild(component);