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