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 / CreateNewTrace.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.io.File;
17import java.util.ArrayList;
18import java.util.Iterator;
19import java.util.List;
20import java.util.concurrent.TimeUnit;
21
22import org.eclipse.jface.action.IAction;
23import org.eclipse.jface.viewers.ISelection;
24import org.eclipse.jface.viewers.IStructuredSelection;
6c13869b
FC
25import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TargetResource;
26import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource;
27import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource.TraceState;
28import org.eclipse.linuxtools.lttng.core.tracecontrol.model.config.TraceConfig;
29import org.eclipse.linuxtools.lttng.core.tracecontrol.service.ILttControllerService;
e8d771d5 30import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
e8d771d5 31import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
74237cc3 32import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
e8d771d5
BH
33import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.NewTraceDialog;
34import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
35import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
36import org.eclipse.rse.core.model.ISystemRegistry;
37import org.eclipse.rse.core.model.SystemStartHere;
38import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
39import org.eclipse.rse.ui.SystemBasePlugin;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.tm.tcf.protocol.IToken;
42import org.eclipse.tm.tcf.util.TCFTask;
43import org.eclipse.ui.IObjectActionDelegate;
44import org.eclipse.ui.IViewActionDelegate;
45import org.eclipse.ui.IViewPart;
46import org.eclipse.ui.IWorkbenchPart;
47import org.eclipse.ui.IWorkbenchWindow;
48import org.eclipse.ui.IWorkbenchWindowActionDelegate;
49
50/**
51 * <b><u>CreateNewTrace</u></b>
52 * <p>
53 * Action implementation to create a new trace.
54 * </p>
55 */
56public class CreateNewTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
74237cc3
FC
61
62 private final List<TargetResource> fSelectedFiles;
e8d771d5
BH
63
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
68 /**
69 * Constructor for CreateNewTrace.
70 */
71 public CreateNewTrace() {
74237cc3 72 fSelectedFiles = new ArrayList<TargetResource>();
e8d771d5
BH
73 }
74
75 // ------------------------------------------------------------------------
76 // Operations
77 // ------------------------------------------------------------------------
78
79 /*
80 * (non-Javadoc)
74237cc3
FC
81 *
82 * @see
83 * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
84 * action.IAction, org.eclipse.ui.IWorkbenchPart)
e8d771d5
BH
85 */
86 @Override
87 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
88 }
89
90 /**
91 * Returns the first selected target resource.
92 *
93 * @return first selected target resource
94 */
95 protected TargetResource getFirstSelectedTarget() {
96 if (fSelectedFiles.size() > 0) {
74237cc3 97 return fSelectedFiles.get(0);
e8d771d5
BH
98 }
99 return null;
100 }
101
102 /*
103 * (non-Javadoc)
74237cc3 104 *
e8d771d5
BH
105 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
106 */
107 @Override
108 public void run(IAction action) {
109 Shell shell = getShell();
a79913eb 110 final TargetResource targetResource = getFirstSelectedTarget();
74237cc3 111 TraceSubSystem subSystem = (TraceSubSystem) targetResource.getSubSystem();
a79913eb 112 NewTraceDialog dialog = new NewTraceDialog(shell, subSystem, targetResource);
74237cc3 113
e8d771d5
BH
114 final TraceConfig traceConfig = dialog.open();
115
a79913eb
FC
116 if (traceConfig == null) {
117 return;
118 }
e8d771d5 119
a79913eb
FC
120 try {
121 final ILttControllerService service = subSystem.getControllerService();
e8d771d5 122
a79913eb
FC
123 TraceResource trace = new TraceResource(targetResource.getSubSystem(), service);
124 trace.setName(traceConfig.getTraceName());
125 trace.setParent(targetResource);
126 trace.setTraceConfig(traceConfig);
74237cc3 127
a79913eb
FC
128 if (targetResource.isUst()) {
129 boolean ok = setupUstLocation(service, targetResource, traceConfig);
130 if (!ok) {
131 return;
e8d771d5 132 }
a79913eb 133 }
74237cc3 134
a79913eb
FC
135 trace.setupTrace();
136
137 if (!targetResource.isUst()) {
138
139 // Enable all channels by default
140 trace.setChannelEnable(TraceControlConstants.Lttng_Control_AllChannels, true);
e8d771d5 141
74237cc3
FC
142 // Set overwrite mode for all channels according to user
143 // selection (true for flight recorder, false for normal)
a79913eb
FC
144 trace.setChannelOverwrite(TraceControlConstants.Lttng_Control_AllChannels, traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE);
145
146 // Set channel timer for all channels
74237cc3 147 final long period = 1000;
a79913eb
FC
148 trace.setChannelTimer(TraceControlConstants.Lttng_Control_AllChannels, period);
149
150 // Set subbuffer size for all channels
74237cc3 151 final long subbufSize = 16384;
a79913eb
FC
152 trace.setChannelSubbufSize(TraceControlConstants.Lttng_Control_AllChannels, subbufSize);
153
154 // Set number of subbuffers for all channels
155 final long subbufNum = 2;
156 trace.setChannelSubbufNum(TraceControlConstants.Lttng_Control_AllChannels, subbufNum);
e8d771d5
BH
157 }
158
a79913eb
FC
159 if (traceConfig.isNetworkTrace()) {
160
161 File newDir = new File(traceConfig.getTracePath());
162 if (!newDir.exists()) {
163 boolean created = newDir.mkdirs();
164 if (!created) {
165 throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
166 }
167 }
168 }
74237cc3 169
e8d771d5
BH
170 if (trace.isUst()) {
171 // in UST the tracing is started after setupTrace!!
172 trace.setTraceState(TraceState.STARTED);
74237cc3 173 } else {
e8d771d5
BH
174 trace.setTraceState(TraceState.CONFIGURED);
175 }
74237cc3 176
a79913eb 177 targetResource.addTrace(trace);
e8d771d5
BH
178
179 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
a79913eb 180 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CREATED, trace, targetResource, subSystem, null);
74237cc3 181
a79913eb
FC
182 } catch (Exception e) {
183 SystemMessageException sysExp;
184 if (e instanceof SystemMessageException) {
74237cc3 185 sysExp = (SystemMessageException) e;
a79913eb 186 } else {
74237cc3 187 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
a79913eb 188 }
74237cc3
FC
189 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorNewTrace + " (" + //$NON-NLS-1$
190 Messages.Lttng_Resource_Trace + ": " + traceConfig.getTraceName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
a79913eb
FC
191
192 return;
e8d771d5 193 }
a79913eb 194
e8d771d5
BH
195 }
196
197 /*
198 * (non-Javadoc)
74237cc3
FC
199 *
200 * @see
201 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
202 * .IAction, org.eclipse.jface.viewers.ISelection)
e8d771d5
BH
203 */
204 @Override
205 @SuppressWarnings("unchecked")
206 public void selectionChanged(IAction action, ISelection selection) {
207 if (selection instanceof IStructuredSelection) {
208 fSelectedFiles.clear();
209 // store the selected targets to be used when running
210 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
211 while (theSet.hasNext()) {
212 Object obj = theSet.next();
213 if (obj instanceof TargetResource) {
74237cc3 214 fSelectedFiles.add((TargetResource) obj);
e8d771d5
BH
215 }
216 }
217 }
218 }
219
220 /**
221 * Returns the active workbench shell of this plug-in.
222 *
223 * @return active workbench shell.
224 */
225 protected Shell getShell() {
226 return SystemBasePlugin.getActiveWorkbenchShell();
227 }
74237cc3 228
e8d771d5
BH
229 /*
230 * (non-Javadoc)
74237cc3
FC
231 *
232 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.
233 * IWorkbenchWindow)
e8d771d5
BH
234 */
235 @Override
236 public void init(IWorkbenchWindow window) {
237
238 }
239
240 /*
241 * (non-Javadoc)
74237cc3 242 *
e8d771d5
BH
243 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
244 */
245 @Override
246 public void dispose() {
247 }
248
249 /*
250 * (non-Javadoc)
74237cc3 251 *
e8d771d5
BH
252 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
253 */
254 @Override
255 public void init(IViewPart view) {
256 }
74237cc3 257
e8d771d5
BH
258 /*
259 * Setup the trace location for UST.
74237cc3
FC
260 */
261 private boolean setupUstLocation(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig)
262 throws Exception {
e8d771d5 263 if (traceConfig.isNetworkTrace()) {
a79913eb
FC
264 File localDir = new File(traceConfig.getTracePath());
265 if (!localDir.exists()) {
266 boolean success = localDir.mkdirs();
267 if (!success) {
268 return false;
269 }
270 }
e8d771d5
BH
271
272 // Create future task
273 boolean ok = new TCFTask<Boolean>() {
274 @Override
275 public void run() {
276
277 // Setup trace location using Lttng controller service proxy
74237cc3
FC
278 service.writeTraceNetwork(targetResource.getParent().getName(), targetResource.getName(), traceConfig.getTraceName(),
279 traceConfig.getNumChannel(), traceConfig.getIsAppend(), traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
280 traceConfig.getMode() == TraceConfig.NORMAL_MODE, new ILttControllerService.DoneWriteTraceNetwork() {
281
282 @Override
283 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
284 if (error != null) {
285 // Notify with error
286 error(error);
287 return;
288 }
289
290 // Notify about success
291 done(true);
292 }
293 });
294 }
295 }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
e8d771d5
BH
296 return ok;
297 } else {
298 // Create future task
299 boolean ok = new TCFTask<Boolean>() {
300 @Override
301 public void run() {
302
303 // Setup trace location using Lttng controller service proxy
74237cc3
FC
304 service.writeTraceLocal(targetResource.getParent().getName(), targetResource.getName(), traceConfig.getTraceName(),
305 traceConfig.getTracePath(), traceConfig.getNumChannel(), traceConfig.getIsAppend(),
306 traceConfig.getMode() == TraceConfig.NORMAL_MODE, traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
e8d771d5
BH
307 new ILttControllerService.DoneWriteTraceLocal() {
308
74237cc3
FC
309 @Override
310 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
311 if (error != null) {
312 // Notify with error
313 error(error);
314 return;
315 }
316
317 // Notify about success
318 done(true);
319 }
320 });
321 }
322 }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
e8d771d5
BH
323 return ok;
324 }
325 }
326
4dc47e28 327}
This page took 0.038883 seconds and 5 git commands to generate.