| 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.lttng.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.lttng.ui.LTTngUiPlugin; |
| 22 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; |
| 23 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent; |
| 24 | import org.eclipse.linuxtools.lttng.ui.views.control.model.TargetNodeState; |
| 25 | import org.eclipse.linuxtools.lttng.ui.views.control.property.TargetNodePropertySource; |
| 26 | import org.eclipse.linuxtools.lttng.ui.views.control.remote.IRemoteSystemProxy; |
| 27 | import org.eclipse.linuxtools.lttng.ui.views.control.remote.RemoteSystemProxy; |
| 28 | import org.eclipse.linuxtools.lttng.ui.views.control.service.ICommandShell; |
| 29 | import org.eclipse.linuxtools.lttng.ui.views.control.service.ILttngControlService; |
| 30 | import org.eclipse.linuxtools.lttng.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 = LTTngUiPlugin.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.lttng.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.lttng.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.lttng.ui.views.control.model.impl.TraceControlComponent#setTargetNodeState(org.eclipse.linuxtools.lttng.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.lttng.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.lttng.ui.views.control.model.impl.TraceControlComponent#setControlService(org.eclipse.linuxtools.lttng.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.lttng.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.lttng.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 | LTTngUiPlugin.logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + ").", e); //$NON-NLS-1$ //$NON-NLS-2$ |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Method to disconnect this node component to the remote target node. |
| 268 | */ |
| 269 | public void disconnect() { |
| 270 | if (fState == TargetNodeState.CONNECTED) { |
| 271 | try { |
| 272 | setTargetNodeState(TargetNodeState.DISCONNECTING); |
| 273 | fRemoteProxy.disconnect(); |
| 274 | } catch (Exception e) { |
| 275 | LTTngUiPlugin.logError(Messages.TraceControl_DisconnectionFailure + getName(), e); |
| 276 | } finally { |
| 277 | handleDisconnected(); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Retrieves the trace configuration from the target node and populates the information |
| 284 | * in the tree model. The execution is done in a own job. |
| 285 | * |
| 286 | * @throws ExecutionException |
| 287 | */ |
| 288 | public void getConfigurationFromNode() { |
| 289 | Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) { |
| 290 | @Override |
| 291 | protected IStatus run(IProgressMonitor monitor) { |
| 292 | |
| 293 | try { |
| 294 | // Get provider information from node |
| 295 | TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this); |
| 296 | addChild(providerGroup); |
| 297 | providerGroup.getProviderFromNode(monitor); |
| 298 | |
| 299 | // Get session information from node |
| 300 | TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this); |
| 301 | addChild(sessionGroup); |
| 302 | sessionGroup.getSessionsFromNode(monitor); |
| 303 | } catch (ExecutionException e) { |
| 304 | removeAllChildren(); |
| 305 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString()); |
| 306 | } |
| 307 | |
| 308 | return Status.OK_STATUS; |
| 309 | } |
| 310 | }; |
| 311 | job.setUser(true); |
| 312 | job.schedule(); |
| 313 | } |
| 314 | |
| 315 | public void refresh() { |
| 316 | removeAllChildren(); |
| 317 | getConfigurationFromNode(); |
| 318 | } |
| 319 | |
| 320 | // ------------------------------------------------------------------------ |
| 321 | // Helper function |
| 322 | // ------------------------------------------------------------------------ |
| 323 | /** |
| 324 | * @return returns the control service for LTTng specific commands. |
| 325 | * @throws ExecutionException |
| 326 | */ |
| 327 | private ILttngControlService createControlService() throws ExecutionException { |
| 328 | if (fShell == null) { |
| 329 | fShell = fRemoteProxy.createCommandShell(); |
| 330 | fRemoteProxy.addCommunicationListener(this); |
| 331 | } |
| 332 | fService = new LTTngControlService(fShell); |
| 333 | return fService; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Handles the connected event. |
| 338 | */ |
| 339 | private void handleConnected() { |
| 340 | setTargetNodeState(TargetNodeState.CONNECTED); |
| 341 | try { |
| 342 | createControlService(); |
| 343 | getConfigurationFromNode(); |
| 344 | } catch (ExecutionException e) { |
| 345 | LTTngUiPlugin.logError(Messages.TraceControl_ListSessionFailure, e); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Handles the disconnected event. |
| 351 | */ |
| 352 | private void handleDisconnected() { |
| 353 | removeAllChildren(); |
| 354 | setTargetNodeState(TargetNodeState.DISCONNECTED); |
| 355 | fShell = null; |
| 356 | fService = null; |
| 357 | } |
| 358 | } |