First part of LTTng 2.0 support
[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;
144 fireCompenentChanged(TargetNodeComponent.this);
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();
229 }
230 }
231 });
232 } catch (Exception e) {
233 setTargetNodeState(TargetNodeState.DISCONNECTED);
234 LTTngUiPlugin.logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + ").", e); //$NON-NLS-1$ //$NON-NLS-2$
235 }
236 }
237 }
238
239 /**
240 * Method to disconnect this node component to the remote target node.
241 */
242 public void disconnect() {
243 if (fState == TargetNodeState.CONNECTED) {
244 try {
245 setTargetNodeState(TargetNodeState.DISCONNECTING);
246 fRemoteProxy.disconnect();
247 } catch (Exception e) {
248 LTTngUiPlugin.logError(Messages.TraceControl_DisconnectionFailure + getName(), e);
249 } finally {
250 handleDisconnected();
251 }
252 }
253 }
254
255 /**
256 * Retrieves the trace configuration from the target node and populates the information
257 * in the tree model. The execution is done in a own job.
258 *
259 * @throws ExecutionException
260 */
261 public void getConfigurationFromNode() throws ExecutionException {
262 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
263 @Override
264 protected IStatus run(IProgressMonitor monitor) {
265
266 try {
267 // Get provider information from node
268 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
269 addChild(providerGroup);
270 providerGroup.getProviderFromNode(monitor);
271
272 // Get session information from node
273 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
274 addChild(sessionGroup);
275 sessionGroup.getSessionsFromNode(monitor);
276 } catch (ExecutionException e) {
277 removeAllChildren();
278 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString());
279 }
280
281 return Status.OK_STATUS;
282 }
283 };
284 job.setUser(true);
285 job.schedule();
286
287 }
288
289 // ------------------------------------------------------------------------
290 // Helper function
291 // ------------------------------------------------------------------------
292 /**
293 * @return returns the control service for LTTng specific commands.
294 * @throws ExecutionException
295 */
296 private ILttngControlService createControlService() throws ExecutionException {
297 if (fShell == null) {
298 fShell = fRemoteProxy.createCommandShell();
299 fRemoteProxy.addCommunicationListener(this);
300 }
301 fService = new LTTngControlService(fShell);
302 return fService;
303 }
304
305 /**
306 * Handles the connected event.
307 */
308 private void handleConnected() {
309 setTargetNodeState(TargetNodeState.CONNECTED);
310 try {
311 createControlService();
312 getConfigurationFromNode();
313 } catch (ExecutionException e) {
314 LTTngUiPlugin.logError(Messages.TraceControl_ListSessionFailure, e);
315 }
316 }
317
318 /**
319 * Handles the disconnected event.
320 */
321 private void handleDisconnected() {
322 removeAllChildren();
323 setTargetNodeState(TargetNodeState.DISCONNECTED);
324 fShell = null;
325 fService = null;
326 }
327}
This page took 0.049251 seconds and 5 git commands to generate.