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