Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / actions / StopTrace.java
CommitLineData
e8d771d5
BH
1/*******************************************************************************
2 * Copyright (c) 2011 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 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
12 *
13 *******************************************************************************/
14package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions;
15
16import java.util.ArrayList;
17import java.util.Iterator;
18import java.util.List;
19import java.util.concurrent.TimeUnit;
20
21import org.eclipse.jface.action.IAction;
22import org.eclipse.jface.viewers.ISelection;
23import org.eclipse.jface.viewers.IStructuredSelection;
6c13869b
FC
24import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource;
25import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource.TraceState;
26import org.eclipse.linuxtools.lttng.core.tracecontrol.model.config.TraceConfig;
27import org.eclipse.linuxtools.lttng.core.tracecontrol.service.ILttControllerService;
e8d771d5 28import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
e8d771d5 29import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
a79913eb 30import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
e8d771d5
BH
31import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
32import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
33import org.eclipse.rse.core.model.ISystemRegistry;
34import org.eclipse.rse.core.model.SystemStartHere;
35import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
36import org.eclipse.rse.ui.SystemBasePlugin;
37import org.eclipse.swt.widgets.Shell;
38import org.eclipse.tm.tcf.protocol.IToken;
39import org.eclipse.tm.tcf.util.TCFTask;
40import org.eclipse.ui.IObjectActionDelegate;
41import org.eclipse.ui.IViewActionDelegate;
42import org.eclipse.ui.IViewPart;
43import org.eclipse.ui.IWorkbenchPart;
44import org.eclipse.ui.IWorkbenchWindow;
45import org.eclipse.ui.IWorkbenchWindowActionDelegate;
46
47/**
48 * <b><u>StopTrace</u></b>
49 * <p>
50 * Action implementation to stop a trace which deallocates all resources on the remote system.
51 * </p>
52 */
53public class StopTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
59 private List<TraceResource> fSelectedTraces;
60
61 // ------------------------------------------------------------------------
62 // Constructors
63 // ------------------------------------------------------------------------
64
65 public StopTrace() {
66 fSelectedTraces = new ArrayList<TraceResource>();
67 }
68
69 // ------------------------------------------------------------------------
70 // Operations
71 // ------------------------------------------------------------------------
72
73 /*
74 * (non-Javadoc)
75 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
76 */
77 @Override
78 public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
79 }
80
81 /*
82 * (non-Javadoc)
83 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
84 */
85 @Override
86 public void run(IAction arg0) {
87 int size = fSelectedTraces.size();
88 for (int i = 0; i < size; i++) {
89
90 final TraceResource trace = (TraceResource) fSelectedTraces.get(i);
91 TraceSubSystem subSystem = (TraceSubSystem)trace.getSubSystem();
92
93 try {
94 final ILttControllerService service = subSystem.getControllerService();
a79913eb
FC
95
96 TraceConfig traceConfig = trace.getTraceConfig();
97 if (traceConfig != null && traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE) {
98 setupLocation(service, trace, traceConfig);
99 }
100
e8d771d5
BH
101 // Create future task
102 @SuppressWarnings("unused")
103 Boolean success = new TCFTask<Boolean>() {
104 @Override
105 public void run() {
106
107 // Setup trace using Lttng controller service proxy
108 service.destroyTrace(trace.getParent().getParent().getName(), trace.getParent().getName(), trace.getName(), new ILttControllerService.DoneDestroyTrace() {
109
110 @Override
111 public void doneDestroyTrace(IToken token, Exception error, Object str) {
112 if (error != null) {
113 // Notify with error
114 error(error);
115 return;
116 }
117
118 // Notify about success
119 done(Boolean.valueOf(true));
120 }
121 });
122 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
123
124 trace.setTraceState(TraceState.STOPPED);
125
126 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
127 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CHANGED, trace, trace.getParent(), subSystem, null);
128
129 } catch (Exception e) {
130 SystemMessageException sysExp;
131 if (e instanceof SystemMessageException) {
132 sysExp = (SystemMessageException)e;
133 } else {
134 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
135 }
136 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorStop + " (" + //$NON-NLS-1$
137 Messages.Lttng_Resource_Trace + ": " + trace.getName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
138 }
139 }
140 }
141
142 /*
143 * (non-Javadoc)
144 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
145 */
146 @SuppressWarnings("unchecked")
147 @Override
148 public void selectionChanged(IAction action, ISelection selection) {
149
150 if (selection instanceof IStructuredSelection) {
151 fSelectedTraces.clear();
152
153 // store the selected targets to be used when running
154 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
155 while (theSet.hasNext()) {
156 Object obj = theSet.next();
157 if (obj instanceof TraceResource) {
158 fSelectedTraces.add((TraceResource)obj);
159 }
160 }
161 }
162
163 }
164
165 /**
166 * Returns the active workbench shell of this plug-in.
167 *
168 * @return active workbench shell.
169 */
170 protected Shell getShell() {
171 return SystemBasePlugin.getActiveWorkbenchShell();
172 }
173
174 /*
175 * (non-Javadoc)
176 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
177 */
178 @Override
179 public void init(IWorkbenchWindow window) {
180 }
181
182 /*
183 * (non-Javadoc)
184 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
185 */
186 @Override
187 public void dispose() {
188 }
189
190 /*
191 * (non-Javadoc)
192 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
193 */
194 @Override
195 public void init(IViewPart view) {
196 }
a79913eb
FC
197
198 /*
199 * Setup the trace location. Only flight recorder channels are written when trace is stopped.
200 */
201 private void setupLocation(final ILttControllerService service, final TraceResource trace, final TraceConfig traceConfig) throws Exception {
202 boolean success = false;
203
204 if (traceConfig.isNetworkTrace()) {
205
206 // Create future task
207 success = new TCFTask<Boolean>() {
208 @Override
209 public void run() {
210
211 // Setup trace transport using Lttng controller service proxy
212 service.writeTraceNetwork(trace.getParent().getParent().getName(),
213 trace.getParent().getName(),
214 traceConfig.getTraceName(),
215 traceConfig.getNumChannel(),
216 traceConfig.getIsAppend(),
217 true, // write only flight recorder channels
218 false,
219 new ILttControllerService.DoneWriteTraceNetwork() {
220
221 @Override
222 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
223 if (error != null) {
224 // Notify with error
225 error(error);
226 return;
227 }
228
229 // Notify about success
230 done(Boolean.valueOf(true));
231 }
232 });
233 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
234
235 } else {
236
237 // Create future task
238 success = new TCFTask<Boolean>() {
239 @Override
240 public void run() {
241
242 // Setup trace transport using Lttng controller service proxy
243 service.writeTraceLocal(trace.getParent().getParent().getName(),
244 trace.getParent().getName(),
245 traceConfig.getTraceName(),
246 traceConfig.getTracePath(),
247 traceConfig.getNumChannel(),
248 traceConfig.getIsAppend(),
249 true, // write only flight recorder channels
250 false,
251 new ILttControllerService.DoneWriteTraceLocal() {
252
253 @Override
254 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
255 if (error != null) {
256 // Notify with error
257 error(error);
258 return;
259 }
260
261 // Notify about success
262 done(Boolean.valueOf(true));
263 }
264 });
265 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
266 }
267
268 if (success) {
269 //FIXME: wait 2 seconds to allow time for channels to be written before destroying the trace
270 try {
271 Thread.sleep(2000);
272 } catch (InterruptedException e) {
273 e.printStackTrace();
274 }
275 }
276 }
277
278
e8d771d5 279}
This page took 0.035476 seconds and 5 git commands to generate.