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