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