Improved error handling for connection errors in LTTng control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / model / impl / TargetNodeComponent.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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 **********************************************************************/
13 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl;
14
15 import java.util.List;
16
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.linuxtools.internal.lttng2.core.control.model.TargetNodeState;
24 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
25 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
26 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
27 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.property.TargetNodePropertySource;
28 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell;
29 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy;
30 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.RemoteSystemProxy;
31 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService;
32 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.LTTngControlServiceFactory;
33 import org.eclipse.rse.core.RSECorePlugin;
34 import org.eclipse.rse.core.model.IHost;
35 import org.eclipse.rse.core.model.IRSECallback;
36 import org.eclipse.rse.core.model.ISystemRegistry;
37 import org.eclipse.rse.core.subsystems.CommunicationsEvent;
38 import org.eclipse.rse.core.subsystems.ICommunicationsListener;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.views.properties.IPropertySource;
43
44 /**
45 * <p>
46 * Implementation of the trace node component.
47 * </p>
48 *
49 * @author Bernd Hufmann
50 */
51 public class TargetNodeComponent extends TraceControlComponent implements ICommunicationsListener {
52
53 // ------------------------------------------------------------------------
54 // Constants
55 // ------------------------------------------------------------------------
56 /**
57 * Path to icon file for this component (state connected).
58 */
59 public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$
60 /**
61 * Path to icon file for this component (state disconnected).
62 */
63 public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
64
65 // ------------------------------------------------------------------------
66 // Attributes
67 // ------------------------------------------------------------------------
68 /**
69 * The node connection state.
70 */
71 private TargetNodeState fState = TargetNodeState.DISCONNECTED;
72 /**
73 * The image to be displayed in state disconnected.
74 */
75 private Image fDisconnectedImage = null;
76 /**
77 * The connection implementation.
78 */
79 private IHost fHost = null;
80 /**
81 * The remote proxy implementation.
82 */
83 private IRemoteSystemProxy fRemoteProxy = null;
84 /**
85 * The control service for LTTng specific commands.
86 */
87 private ILttngControlService fService = null;
88 /**
89 * The command shell for issuing commands.
90 */
91 private ICommandShell fShell = null;
92
93 // ------------------------------------------------------------------------
94 // Constructors
95 // ------------------------------------------------------------------------
96 /**
97 * Constructor
98 * @param name - the name of the component
99 * @param parent - the parent of the component
100 * @param host - the host connection implementation
101 * @param proxy - the remote proxy implementation
102 */
103 public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host, IRemoteSystemProxy proxy) {
104 super(name, parent);
105 setImage(TARGET_NODE_CONNECTED_ICON_FILE);
106 fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
107 fHost = host;
108 fRemoteProxy = proxy;
109 setToolTip(fHost.getHostName());
110 }
111
112 /**
113 * Constructor (using default proxy)
114 * @param name - the name of the component
115 * @param parent - the parent of the component
116 * @param host - the host connection implementation
117 */
118 public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host) {
119 this(name, parent, host, new RemoteSystemProxy(host));
120 }
121
122 // ------------------------------------------------------------------------
123 // Accessors
124 // ------------------------------------------------------------------------
125 /*
126 * (non-Javadoc)
127 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getImage()
128 */
129 @Override
130 public Image getImage() {
131 if (fState == TargetNodeState.CONNECTED) {
132 return super.getImage();
133 }
134 return fDisconnectedImage;
135 }
136
137 /*
138 * (non-Javadoc)
139 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getTargetNodeState()
140 */
141 @Override
142 public TargetNodeState getTargetNodeState() {
143 return fState;
144 }
145
146 /*
147 * (non-Javadoc)
148 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#setTargetNodeState(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent.TargetNodeState)
149 */
150 @Override
151 public void setTargetNodeState(TargetNodeState state) {
152 fState = state;
153 fireComponentChanged(TargetNodeComponent.this);
154 }
155
156 /*
157 * (non-Javadoc)
158 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getControlService()
159 */
160 @Override
161 public ILttngControlService getControlService() {
162 return fService;
163 }
164
165 /*
166 * (non-Javadoc)
167 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#setControlService(org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService)
168 */
169 @Override
170 public void setControlService(ILttngControlService service) {
171 fService = service;
172 }
173
174 /*
175 * (non-Javadoc)
176 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getAdapter(java.lang.Class)
177 */
178 @Override
179 public Object getAdapter(Class adapter) {
180 if (adapter == IPropertySource.class) {
181 return new TargetNodePropertySource(this);
182 }
183 return null;
184 }
185
186 /**
187 * @return remote host name
188 */
189 public String getHostName() {
190 return fHost.getHostName();
191 }
192
193 /**
194 * @return remote system proxy implementation
195 */
196 public IRemoteSystemProxy getRemoteSystemProxy() {
197 return fRemoteProxy;
198 }
199
200 /**
201 * @return all available sessions.
202 */
203 public TraceSessionComponent[] getSessions() {
204 List<ITraceControlComponent> compenents = getChildren(TraceSessionGroup.class);
205 if (compenents.size() > 0) {
206 TraceSessionGroup group = (TraceSessionGroup)compenents.get(0);
207 List<ITraceControlComponent> sessions = group.getChildren(TraceSessionComponent.class);
208 return sessions.toArray(new TraceSessionComponent[sessions.size()]);
209 }
210 return new TraceSessionComponent[0];
211 }
212
213 /**
214 * @return node version
215 */
216 public String getNodeVersion() {
217 // Control service is null during connection to node
218 if (getControlService() != null) {
219 return getControlService().getVersion();
220 }
221 return ""; //$NON-NLS-1$
222 }
223
224 /**
225 * Returns if node supports filtering of events
226 * @return <code>true</code> if node supports filtering else <code>false</code>
227 */
228 public boolean isEventFilteringSupported() {
229 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
230 }
231
232 /**
233 * Returns if node supports networks streaming or not
234 * @return <code>true</code> if node supports filtering else <code>false</code>
235 *
236 */
237 public boolean isNetworkStreamingSupported() {
238 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
239 }
240
241 // ------------------------------------------------------------------------
242 // Operations
243 // ------------------------------------------------------------------------
244
245 /*
246 * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#communicationsStateChange(org.eclipse.rse.core.subsystems.CommunicationsEvent)
247 */
248 @Override
249 public void communicationsStateChange(CommunicationsEvent e) {
250 if (e.getState() == CommunicationsEvent.AFTER_DISCONNECT ||
251 e.getState() == CommunicationsEvent.CONNECTION_ERROR) {
252 handleDisconnected();
253 } if ((e.getState() == CommunicationsEvent.AFTER_CONNECT) && (fState != TargetNodeState.CONNECTING)) {
254 handleConnected();
255 }
256 }
257
258 /* (non-Javadoc)
259 * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#isPassiveCommunicationsListener()
260 */
261 @Override
262 public boolean isPassiveCommunicationsListener() {
263 return true;
264 }
265
266 /*
267 * (non-Javadoc)
268 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceControlComponent#dispose()
269 */
270 @Override
271 public void dispose() {
272 fRemoteProxy.removeCommunicationListener(this);
273 }
274
275 /**
276 * Method to connect this node component to the remote target node.
277 */
278 public void connect() {
279 if (fState == TargetNodeState.DISCONNECTED) {
280 try {
281 setTargetNodeState(TargetNodeState.CONNECTING);
282 fRemoteProxy.connect(new IRSECallback() {
283 @Override
284 public void done(IStatus status, Object result) {
285 // Note: result might be null!
286 if(status.isOK()) {
287 handleConnected();
288 } else {
289 handleDisconnected();
290 }
291 }
292 });
293 } catch (Exception e) {
294 setTargetNodeState(TargetNodeState.DISCONNECTED);
295 Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
296 }
297 }
298 }
299
300 /**
301 * Method to disconnect this node component to the remote target node.
302 */
303 public void disconnect() {
304 if (fState == TargetNodeState.CONNECTED) {
305 try {
306 setTargetNodeState(TargetNodeState.DISCONNECTING);
307 fRemoteProxy.disconnect();
308 } catch (Exception e) {
309 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
310 } finally {
311 handleDisconnected();
312 }
313 }
314 }
315
316 /**
317 * Retrieves the trace configuration from the target node and populates the
318 * information in the tree model. The execution is done in a own job.
319 */
320 public void getConfigurationFromNode() {
321 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
322 @Override
323 protected IStatus run(IProgressMonitor monitor) {
324
325 try {
326 // Get provider information from node
327 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
328 addChild(providerGroup);
329 providerGroup.getProviderFromNode(monitor);
330
331 // Get session information from node
332 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
333 addChild(sessionGroup);
334 sessionGroup.getSessionsFromNode(monitor);
335 } catch (ExecutionException e) {
336 removeAllChildren();
337 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
338 }
339
340 return Status.OK_STATUS;
341 }
342 };
343 job.setUser(true);
344 job.schedule();
345 }
346
347 /**
348 * Refresh the node configuration
349 */
350 public void refresh() {
351 removeAllChildren();
352 getConfigurationFromNode();
353 }
354
355 /**
356 * Deregisters host from registry.
357 */
358 public void deregister() {
359 ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
360 registry.deleteHost(fHost);
361 }
362
363 // ------------------------------------------------------------------------
364 // Helper function
365 // ------------------------------------------------------------------------
366 /**
367 * @return returns the control service for LTTng specific commands.
368 * @throws ExecutionException
369 */
370 private ILttngControlService createControlService() throws ExecutionException {
371 if (fShell == null) {
372 fShell = fRemoteProxy.createCommandShell();
373 fRemoteProxy.addCommunicationListener(this);
374 }
375 fService = LTTngControlServiceFactory.getInstance().getLttngControlService(fShell);
376 return fService;
377 }
378
379 /**
380 * Handles the connected event.
381 */
382 private void handleConnected() {
383 setTargetNodeState(TargetNodeState.CONNECTED);
384 try {
385 createControlService();
386 getConfigurationFromNode();
387 } catch (final ExecutionException e) {
388 // Disconnect only if no control service, otherwise stay connected.
389 if (getControlService() == null) {
390 disconnect();
391 }
392
393 // Notify user
394 Display.getDefault().asyncExec(new Runnable() {
395 @Override
396 public void run() {
397 ErrorDialog er = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
398 Messages.TraceControl_ErrorTitle, Messages.TraceControl_RetrieveNodeConfigurationFailure,
399 new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e),
400 IStatus.ERROR);
401 er.open();
402 }
403 });
404 Activator.getDefault().logError(Messages.TraceControl_RetrieveNodeConfigurationFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
405 }
406 }
407
408 /**
409 * Handles the disconnected event.
410 */
411 private void handleDisconnected() {
412 removeAllChildren();
413 setTargetNodeState(TargetNodeState.DISCONNECTED);
414 fShell = null;
415 fService = null;
416 }
417 }
This page took 0.043848 seconds and 6 git commands to generate.