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