Internalize lttng.core APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / actions / StartTrace.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.io.File;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TraceResource;
27 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TraceResource.TraceState;
28 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.config.TraceConfig;
29 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.service.ILttControllerService;
30 import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.utility.LiveTraceManager;
31 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
32 import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
33 import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
34 import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.SelectTracePathDialog;
35 import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
36 import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
37 import org.eclipse.rse.core.model.ISystemRegistry;
38 import org.eclipse.rse.core.model.SystemStartHere;
39 import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
40 import org.eclipse.rse.ui.SystemBasePlugin;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.tm.tcf.protocol.IToken;
43 import org.eclipse.tm.tcf.util.TCFTask;
44 import org.eclipse.ui.IObjectActionDelegate;
45 import org.eclipse.ui.IViewActionDelegate;
46 import org.eclipse.ui.IViewPart;
47 import org.eclipse.ui.IWorkbenchPart;
48 import org.eclipse.ui.IWorkbenchWindow;
49 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
50
51 /**
52 * <b><u>StartTrace</u></b>
53 * <p>
54 * Action implementation to start and resume a trace. Starting a trace the first time will allocate all
55 * necessary resources and configure all necessary parameters on the remote system.
56 * </p>
57 */
58 public class StartTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
59
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 private List<TraceResource> fSelectedTraces;
65
66 // ------------------------------------------------------------------------
67 // Constructors
68 // ------------------------------------------------------------------------
69
70 public StartTrace() {
71 fSelectedTraces = new ArrayList<TraceResource>();
72 }
73
74 // ------------------------------------------------------------------------
75 // Operations
76 // ------------------------------------------------------------------------
77
78 /*
79 * (non-Javadoc)
80 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
81 */
82 @Override
83 public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
84 }
85
86 /*
87 * (non-Javadoc)
88 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
89 */
90 @Override
91 public void run(IAction arg0) {
92 for (int i = 0; i < fSelectedTraces.size(); i++) {
93
94 TraceResource trace = (TraceResource) fSelectedTraces.get(i);
95 TraceSubSystem subSystem = (TraceSubSystem)trace.getSubSystem();
96
97 TraceConfig traceConfig = trace.getTraceConfig();
98 if (traceConfig != null) {
99 try {
100 ILttControllerService service = subSystem.getControllerService();
101 if (trace.getTraceState() == TraceState.CONFIGURED) {
102 setTraceTransport(service, trace, traceConfig);
103 allocTrace(service, trace, traceConfig);
104 setupLocation(service, trace, traceConfig);
105 }
106 // for network traces and if trace path is not available, open a dialog box for the user to specify the trace path
107 else if (traceConfig.isNetworkTrace() && (TraceConfig.InvalidTracePath.equals(traceConfig.getTracePath()))) {
108
109 SelectTracePathDialog selectDialog = new SelectTracePathDialog(SystemBasePlugin.getActiveWorkbenchShell());
110
111 if (selectDialog.open() == Window.OK) {
112 traceConfig.setTracePath(selectDialog.getTracePath());
113 }
114 else {
115 // we don't have place to store the trace files ... go to the next trace
116 continue;
117 }
118 }
119
120 startTrace(service, trace, traceConfig);
121
122 trace.setTraceState(TraceState.STARTED);
123
124 if (trace.isNetworkTraceAndStarted()) {
125 LiveTraceManager.setLiveTrace(trace.getTraceConfig().getTracePath(), true);
126 }
127
128 // Refresh display
129 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
130 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CHANGED, trace, trace.getParent(), subSystem, null);
131
132 } catch (Exception e) {
133 SystemMessageException sysExp;
134 if (e instanceof SystemMessageException) {
135 sysExp = (SystemMessageException)e;
136 } else {
137 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
138 }
139 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorStart + " (" + //$NON-NLS-1$
140 Messages.Lttng_Resource_Trace + ": " + trace.getName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
141 }
142 }
143 }
144 }
145
146 /*
147 * (non-Javadoc)
148 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
149 */
150 @SuppressWarnings("unchecked")
151 @Override
152 public void selectionChanged(IAction action, ISelection selection) {
153 if (selection instanceof IStructuredSelection) {
154 fSelectedTraces.clear();
155 // store the selected targets to be used when running
156 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
157 while (theSet.hasNext()) {
158 Object obj = theSet.next();
159 if (obj instanceof TraceResource) {
160 fSelectedTraces.add((TraceResource)obj);
161 }
162 }
163 }
164 }
165
166 /**
167 * Returns the active workbench shell of this plug-in.
168 *
169 * @return active workbench shell.
170 */
171 protected Shell getShell() {
172 return SystemBasePlugin.getActiveWorkbenchShell();
173 }
174
175 /*
176 * (non-Javadoc)
177 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
178 */
179 @Override
180 public void init(IWorkbenchWindow arg0) {
181 }
182
183 /*
184 * (non-Javadoc)
185 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
186 */
187 @Override
188 public void dispose() {
189 }
190
191 /*
192 * (non-Javadoc)
193 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
194 */
195 @Override
196 public void init(IViewPart view) {
197 }
198
199 /*
200 * Setup trace transport on the remote system
201 */
202 private void setTraceTransport(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
203 // Create future task
204 new TCFTask<Boolean>() {
205 @Override
206 public void run() {
207
208 // Setup trace transport using Lttng controller service proxy
209 service.setTraceTransport(trace.getParent().getParent().getName(),
210 trace.getParent().getName(),
211 oldConfig.getTraceName(),
212 oldConfig.getTraceTransport(),
213 new ILttControllerService.DoneSetTraceTransport() {
214
215 @Override
216 public void doneSetTraceTransport(IToken token, Exception error, Object str) {
217 if (error != null) {
218 // Notify with error
219 error(error);
220 return;
221 }
222
223 // Notify about success
224 done(Boolean.valueOf(true));
225 }
226 });
227 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
228 }
229
230 /*
231 * Allocate trace resources on the remote system.
232 */
233 private void allocTrace(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
234 new TCFTask<Boolean>() {
235 @Override
236 public void run() {
237
238 // Setup trace transport using Lttng controller service proxy
239 service.allocTrace(trace.getParent().getParent().getName(),
240 trace.getParent().getName(),
241 trace.getName(),
242 new ILttControllerService.DoneAllocTrace() {
243
244 @Override
245 public void doneAllocTrace(IToken token, Exception error, Object str) {
246 if (error != null) {
247 // Notify with error
248 error(error);
249 return;
250 }
251
252 // Notify about success
253 done(Boolean.valueOf(true));
254 }
255 });
256 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
257 }
258
259 /*
260 * Setup the trace location. Only normal channels are written while trace is started.
261 */
262 private void setupLocation(final ILttControllerService service, final TraceResource trace, final TraceConfig traceConfig) throws Exception {
263 if (traceConfig.isNetworkTrace()) {
264
265 File newDir = new File(traceConfig.getTracePath());
266 if (!newDir.exists()) {
267 boolean created = newDir.mkdirs();
268 if (!created) {
269 throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
270 }
271 }
272
273 if (traceConfig.getProject() != null) {
274 ImportToProject.linkTrace(getShell(), trace, traceConfig.getProject(), traceConfig.getTraceName());
275 }
276
277 // Create future task
278 new TCFTask<Boolean>() {
279 @Override
280 public void run() {
281
282 // Setup trace transport using Lttng controller service proxy
283 service.writeTraceNetwork(trace.getParent().getParent().getName(),
284 trace.getParent().getName(),
285 traceConfig.getTraceName(),
286 traceConfig.getTracePath(),
287 traceConfig.getNumChannel(),
288 traceConfig.getIsAppend(),
289 false,
290 true, // write only normal channels
291 new ILttControllerService.DoneWriteTraceNetwork() {
292
293 @Override
294 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
295 if (error != null) {
296 // Notify with error
297 error(error);
298 return;
299 }
300
301 // Notify about success
302 done(Boolean.valueOf(true));
303 }
304 });
305 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
306
307 } else {
308
309 // Create future task
310 new TCFTask<Boolean>() {
311 @Override
312 public void run() {
313
314 // Setup trace transport using Lttng controller service proxy
315 service.writeTraceLocal(trace.getParent().getParent().getName(),
316 trace.getParent().getName(),
317 traceConfig.getTraceName(),
318 traceConfig.getTracePath(),
319 traceConfig.getNumChannel(),
320 traceConfig.getIsAppend(),
321 false,
322 true, // write only normal channels
323 new ILttControllerService.DoneWriteTraceLocal() {
324
325 @Override
326 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
327 if (error != null) {
328 // Notify with error
329 error(error);
330 return;
331 }
332
333 // Notify about success
334 done(Boolean.valueOf(true));
335 }
336 });
337 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
338 }
339 }
340
341 /*
342 * Starts the trace on the remote system.
343 */
344 private void startTrace(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
345 new TCFTask<Boolean>() {
346 @Override
347 public void run() {
348
349 // Setup trace transport using Lttng controller service proxy
350 service.startTrace(trace.getParent().getParent().getName(), trace.getParent().getName(), oldConfig.getTraceName(), new ILttControllerService.DoneStartTrace() {
351
352 @Override
353 public void doneStartTrace(IToken token, Exception error, Object str) {
354 if (error != null) {
355
356 // Notify with error
357 error(error);
358 return;
359 }
360
361 // Notify about success
362 done(Boolean.valueOf(true));
363 }
364 });
365 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS );
366 }
367 }
This page took 0.039588 seconds and 5 git commands to generate.