8a09dd96157b45f3bb76d9bf7e97f3d7a8a6b789
[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 getControlService().isVersionSupported("2.4.0"); //$NON-NLS-1$;
282 }
283 /**
284 * Returns if node supports adding contexts on event
285 * @return <code>true</code> if it supports adding contexts on events else <code>false</code>
286 *
287 */
288 public boolean isContextOnEventSupported() {
289 return !getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
290 }
291
292 /**
293 * Checks if given version is supported by this ILTTngControlService implementation.
294 *
295 * @param version The version to check
296 * @return <code>true</code> if version is supported else <code>false</code>
297 */
298 public boolean isVersionSupported(String version) {
299 return getControlService().isVersionSupported(version);
300 }
301
302 // ------------------------------------------------------------------------
303 // Operations
304 // ------------------------------------------------------------------------
305
306 @Override
307 public void connectionChanged(RemoteConnectionChangeEvent e) {
308 if (fState == TargetNodeState.CONNECTING) {
309 return;
310 }
311
312 switch (e.getType()) {
313 case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
314 case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
315 handleDisconnected();
316 break;
317 case RemoteConnectionChangeEvent.CONNECTION_OPENED:
318 handleConnected();
319 break;
320 default:
321 break;
322 }
323 }
324
325 /**
326 * Method to connect this node component to the remote target node.
327 */
328 public void connect() {
329 if (fState == TargetNodeState.DISCONNECTED) {
330 try {
331 setTargetNodeState(TargetNodeState.CONNECTING);
332 Job job = new Job(format(Messages.TraceControl_OpenConnectionTo, getName())) {
333 @Override
334 protected IStatus run(IProgressMonitor monitor) {
335 try {
336 fRemoteProxy.connect(checkNotNull(monitor));
337 return Status.OK_STATUS;
338 } catch (Exception e) {
339 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure, e);
340 }
341 }
342 };
343 job.addJobChangeListener(new JobChangeAdapter() {
344 @Override
345 public void done(IJobChangeEvent event) {
346 IStatus status = event.getResult();
347 if (status.isOK()) {
348 handleConnected();
349 } else {
350 handleDisconnected();
351 if (status.getSeverity() != IStatus.CANCEL) {
352 Activator.getDefault().getLog().log(status);
353 }
354 }
355 }
356 });
357 job.schedule();
358 } catch (Exception e) {
359 setTargetNodeState(TargetNodeState.DISCONNECTED);
360 Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
361 }
362 }
363 }
364
365 /**
366 * Method to disconnect this node component to the remote target node.
367 */
368 public void disconnect() {
369 if (fState == TargetNodeState.CONNECTED) {
370 try {
371 setTargetNodeState(TargetNodeState.DISCONNECTING);
372 fRemoteProxy.disconnect();
373 } catch (Exception e) {
374 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
375 } finally {
376 handleDisconnected();
377 }
378 }
379 }
380
381 /**
382 * Retrieves the trace configuration from the target node and populates the
383 * information in the tree model. The execution is done in a own job.
384 */
385 public void getConfigurationFromNode() {
386 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
387 @Override
388 protected IStatus run(IProgressMonitor monitor) {
389
390 try {
391 // Get provider information from node
392 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
393 addChild(providerGroup);
394
395 // Get session information from node
396 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
397 addChild(sessionGroup);
398
399 providerGroup.getProviderFromNode(monitor);
400 sessionGroup.getSessionsFromNode(monitor);
401 } catch (ExecutionException e) {
402 removeAllChildren();
403 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
404 }
405
406 return Status.OK_STATUS;
407 }
408 };
409 job.setUser(true);
410 job.schedule();
411 }
412
413 /**
414 * Refresh the node configuration
415 */
416 public void refresh() {
417 removeAllChildren();
418 getConfigurationFromNode();
419 }
420
421 // ------------------------------------------------------------------------
422 // Helper function
423 // ------------------------------------------------------------------------
424
425 /**
426 * @return returns the control service for LTTng specific commands.
427 * @throws ExecutionException
428 */
429 private ILttngControlService createControlService() throws ExecutionException {
430 if (fService == null) {
431 try {
432 ICommandShell shell = fRemoteProxy.createCommandShell();
433 fShell = shell;
434 fService = LTTngControlServiceFactory.getLttngControlService(shell);
435 } catch (ExecutionException e) {
436 disposeControlService();
437 throw e;
438 }
439 }
440 return fService;
441 }
442
443 /**
444 * Handles the connected event.
445 */
446 private void handleConnected() {
447 try {
448 createControlService();
449 getConfigurationFromNode();
450 // Set connected only after the control service has been created and the jobs for creating the
451 // sub-nodes are scheduled.
452 setTargetNodeState(TargetNodeState.CONNECTED);
453 } catch (final ExecutionException e) {
454 // Disconnect only if no control service, otherwise stay connected.
455 if (getControlService() == NULL_CONTROL_SERVICE) {
456 fState = TargetNodeState.CONNECTED;
457 disconnect();
458 }
459
460 // Notify user
461 Display.getDefault().asyncExec(new Runnable() {
462 @Override
463 public void run() {
464 ErrorDialog er = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
465 Messages.TraceControl_ErrorTitle, Messages.TraceControl_RetrieveNodeConfigurationFailure,
466 new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e),
467 IStatus.ERROR);
468 er.open();
469 }
470 });
471 Activator.getDefault().logError(Messages.TraceControl_RetrieveNodeConfigurationFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
472 }
473 }
474
475 /**
476 * Handles the disconnected event.
477 */
478 private void handleDisconnected() {
479 disposeControlService();
480 setTargetNodeState(TargetNodeState.DISCONNECTED);
481 removeAllChildren();
482 }
483
484 @Override
485 public void addChild(ITraceControlComponent component) {
486 if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
487 return;
488 }
489 super.addChild(component);
490 }
491 }
This page took 0.043186 seconds and 5 git commands to generate.