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