tmf: Move TmfEventRequest.ALL_DATA to the interface
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / request / TmfEventRequest.java
CommitLineData
8c8bf09f 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 Ericsson
0283f7ff 3 *
8c8bf09f
ASL
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
0283f7ff 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
fd3f1eff 11 * Alexandre Montplaisir - Consolidate constructors, merge with TmfDataRequest
8c8bf09f
ASL
12 *******************************************************************************/
13
6c13869b 14package org.eclipse.linuxtools.tmf.core.request;
8c8bf09f 15
fd3f1eff
AM
16import java.util.concurrent.CountDownLatch;
17
5500a7f0 18import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer;
72f1e62a 19import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
3bd46eef 20import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
8c8bf09f
ASL
21
22/**
fd3f1eff
AM
23 * TmfEventRequest's are used to obtain series of events from an event provider.
24 * Open ranges can be used, especially for continuous streaming.
25 * <p>
26 * The request is processed asynchronously by a TmfEventProvider and, as events
27 * become available, handleData() is invoked synchronously for each one.
28 * <p>
29 * The TmfEventProvider indicates that the request is completed by calling
30 * done(). The request can be cancelled at any time with cancel().
31 * <p>
32 * Typical usage:
33 *
34 * <pre><code>
35 * TmfEventRequest request = new TmfEventRequest(DataType.class, range, startIndex, nbEvents, priority) {
36 *
37 * public void handleData(ITmfEvent event) {
38 * // do something with the event
39 * }
40 *
41 * public void handleSuccess() {
42 * // callback for when the request completes successfully
43 * }
44 *
45 * public void handleFailure() {
46 * // callback for when the request fails due to an error
47 * }
48 *
49 * public void handleCancel() {
50 * // callback for when the request is cancelled via .cancel()
51 * }
52 *
53 * };
54 *
55 * eventProvider.sendRequest(request);
56 * </code></pre>
57 *
58 *
59 * TODO: Implement request failures (codes, etc...)
0283f7ff 60 *
8fd82db5 61 * @author Francois Chouinard
fd3f1eff 62 * @since 3.0
8c8bf09f 63 */
fd3f1eff
AM
64public abstract class TmfEventRequest implements ITmfEventRequest {
65
66 // ------------------------------------------------------------------------
67 // Constants
68 // ------------------------------------------------------------------------
69
fd3f1eff 70 private static int fRequestNumber = 0;
8c8bf09f
ASL
71
72 // ------------------------------------------------------------------------
73 // Attributes
74 // ------------------------------------------------------------------------
75
fd3f1eff
AM
76 private final Class<? extends ITmfEvent> fDataType;
77 private final ExecutionType fExecType;
78
79 /** A unique request ID */
80 private final int fRequestId;
81
82 /** The requested events time range */
83 private final TmfTimeRange fRange;
84
85 /** The index (rank) of the requested event */
86 protected long fIndex;
87
88 /** The number of requested events (ALL_DATA for all) */
89 protected int fNbRequested;
90
91 /** The number of reads so far */
92 private int fNbRead;
93
94 private final CountDownLatch startedLatch = new CountDownLatch(1);
95 private final CountDownLatch completedLatch = new CountDownLatch(1);
96
97 private boolean fRequestRunning;
98 private boolean fRequestCompleted;
99 private boolean fRequestFailed;
100 private boolean fRequestCanceled;
8c8bf09f
ASL
101
102 // ------------------------------------------------------------------------
fd3f1eff 103 // Constructors
8c8bf09f
ASL
104 // ------------------------------------------------------------------------
105
106 /**
fd3f1eff
AM
107 * Request 'n' events of a given type, for the *whole* trace, at the given
108 * priority.
109 *
110 * @param dataType
111 * The requested data type.
112 * @param index
113 * The index of the first event to retrieve. You can use '0' to
114 * start at the beginning of the trace.
115 * @param nbRequested
116 * The number of events requested. You can use
117 * {@link TmfEventRequest#ALL_DATA} to indicate you want all
118 * events in the trace.
119 * @param priority
120 * The requested execution priority.
121 */
122 public TmfEventRequest(Class<? extends ITmfEvent> dataType,
123 long index,
124 int nbRequested,
125 ExecutionType priority) {
126 this(dataType, TmfTimeRange.ETERNITY, index, nbRequested, priority);
127 }
128
129 /**
130 * Request 'n' events of a given type, for the given time range, at the
131 * given priority.
0283f7ff 132 *
7184fc40
AM
133 * @param dataType
134 * The requested data type.
135 * @param range
136 * The time range of the requested events. You can use
137 * {@link TmfTimeRange#ETERNITY} to indicate you want to cover
138 * the whole trace.
139 * @param index
140 * The index of the first event to retrieve. You can use '0' to
141 * start at the beginning of the trace.
142 * @param nbRequested
143 * The number of events requested. You can use
144 * {@link TmfEventRequest#ALL_DATA} to indicate you want all
145 * events in the time range.
7184fc40
AM
146 * @param priority
147 * The requested execution priority.
0d9a6d76 148 */
7184fc40
AM
149 public TmfEventRequest(Class<? extends ITmfEvent> dataType,
150 TmfTimeRange range,
151 long index,
152 int nbRequested,
7184fc40 153 ExecutionType priority) {
fd3f1eff
AM
154
155 fRequestId = fRequestNumber++;
156 fDataType = dataType;
157 fIndex = index;
158 fNbRequested = nbRequested;
159 fExecType = priority;
7184fc40 160 fRange = range;
fd3f1eff
AM
161 fNbRead = 0;
162
163 fRequestRunning = false;
164 fRequestCompleted = false;
165 fRequestFailed = false;
166 fRequestCanceled = false;
90891c08 167
fd3f1eff 168 /* Setup the request tracing if it's enabled */
5500a7f0 169 if (TmfCoreTracer.isRequestTraced()) {
90891c08
FC
170 String type = getClass().getName();
171 type = type.substring(type.lastIndexOf('.') + 1);
172 @SuppressWarnings("nls")
0283f7ff 173 String message = "CREATED "
fd3f1eff 174 + (getExecType() == ExecutionType.BACKGROUND ? "(BG)" : "(FG)")
0283f7ff 175 + " Type=" + type + " Index=" + getIndex() + " NbReq=" + getNbRequested()
4cf201de 176 + " Range=" + getRange()
90891c08 177 + " DataType=" + getDataType().getSimpleName();
5500a7f0 178 TmfCoreTracer.traceRequest(this, message);
90891c08 179 }
8c8bf09f
ASL
180 }
181
fd3f1eff
AM
182 /**
183 * Resets the request counter (used for testing)
184 */
185 public static void reset() {
186 fRequestNumber = 0;
187 }
188
8c8bf09f
ASL
189 // ------------------------------------------------------------------------
190 // Accessors
191 // ------------------------------------------------------------------------
192
fd3f1eff
AM
193 @Override
194 public int getRequestId() {
195 return fRequestId;
196 }
197
198 @Override
199 public long getIndex() {
200 return fIndex;
201 }
202
203 @Override
204 public ExecutionType getExecType() {
205 return fExecType;
206 }
207
208 @Override
209 public int getNbRequested() {
210 return fNbRequested;
211 }
212
213 @Override
214 public synchronized int getNbRead() {
215 return fNbRead;
216 }
217
218 @Override
219 public synchronized boolean isRunning() {
220 return fRequestRunning;
221 }
222
223 @Override
224 public synchronized boolean isCompleted() {
225 return fRequestCompleted;
226 }
227
228 @Override
229 public synchronized boolean isFailed() {
230 return fRequestFailed;
231 }
232
233 @Override
234 public synchronized boolean isCancelled() {
235 return fRequestCanceled;
236 }
237
238 @Override
239 public Class<? extends ITmfEvent> getDataType() {
240 return fDataType;
241 }
242
d4011df2 243 @Override
7184fc40 244 public TmfTimeRange getRange() {
5419a136 245 return fRange;
8c8bf09f
ASL
246 }
247
a79913eb
FC
248 // ------------------------------------------------------------------------
249 // Setters
250 // ------------------------------------------------------------------------
251
252 /**
fd3f1eff
AM
253 * This method is called by the event provider to set the index
254 * corresponding to the time range start time
0283f7ff 255 *
7184fc40 256 * @param index
fd3f1eff 257 * The start time index
a79913eb 258 */
fd3f1eff
AM
259 protected void setIndex(int index) {
260 fIndex = index;
261 }
262
a79913eb 263 @Override
7184fc40
AM
264 public void setStartIndex(int index) {
265 setIndex(index);
a79913eb
FC
266 }
267
fd3f1eff
AM
268 // ------------------------------------------------------------------------
269 // Operators
270 // ------------------------------------------------------------------------
271
272 @Override
273 public void handleData(ITmfEvent event) {
274 if (event != null) {
275 fNbRead++;
276 }
277 }
278
279 @Override
280 public void handleStarted() {
281 if (TmfCoreTracer.isRequestTraced()) {
282 TmfCoreTracer.traceRequest(this, "STARTED"); //$NON-NLS-1$
283 }
284 }
285
286 @Override
287 public void handleCompleted() {
288 boolean requestFailed = false;
289 boolean requestCanceled = false;
290 synchronized (this) {
291 requestFailed = fRequestFailed;
292 requestCanceled = fRequestCanceled;
293 }
294
295 if (requestFailed) {
296 handleFailure();
297 } else if (requestCanceled) {
298 handleCancel();
299 } else {
300 handleSuccess();
301 }
302 if (TmfCoreTracer.isRequestTraced()) {
303 TmfCoreTracer.traceRequest(this, "COMPLETED (" + fNbRead + " events read)"); //$NON-NLS-1$ //$NON-NLS-2$
304 }
305 }
306
307 @Override
308 public void handleSuccess() {
309 if (TmfCoreTracer.isRequestTraced()) {
310 TmfCoreTracer.traceRequest(this, "SUCCEEDED"); //$NON-NLS-1$
311 }
312 }
313
314 @Override
315 public void handleFailure() {
316 if (TmfCoreTracer.isRequestTraced()) {
317 TmfCoreTracer.traceRequest(this, "FAILED"); //$NON-NLS-1$
318 }
319 }
320
321 @Override
322 public void handleCancel() {
323 if (TmfCoreTracer.isRequestTraced()) {
324 TmfCoreTracer.traceRequest(this, "CANCELLED"); //$NON-NLS-1$
325 }
326 }
327
328 /**
329 * To suspend the client thread until the request starts (or is canceled).
330 *
331 * @throws InterruptedException
332 * If the thread was interrupted while waiting
333 */
334 public void waitForStart() throws InterruptedException {
335 while (!fRequestRunning) {
336 startedLatch.await();
337 }
338 }
339
340 @Override
341 public void waitForCompletion() throws InterruptedException {
342 while (!fRequestCompleted) {
343 completedLatch.await();
344 }
345 }
346
347 @Override
348 public void start() {
349 synchronized (this) {
350 fRequestRunning = true;
351 }
352 handleStarted();
353 startedLatch.countDown();
354 }
355
356 @Override
357 public void done() {
358 synchronized (this) {
359 if (!fRequestCompleted) {
360 fRequestRunning = false;
361 fRequestCompleted = true;
362 } else {
363 return;
364 }
365 }
366 try {
367 handleCompleted();
368 } finally {
369 completedLatch.countDown();
370 }
371 }
372
373 @Override
374 public void fail() {
375 synchronized (this) {
376 fRequestFailed = true;
377 }
378 done();
379 }
380
381 @Override
382 public void cancel() {
383 synchronized (this) {
384 fRequestCanceled = true;
385 }
386 done();
387 }
388
2fb2eb37
FC
389 // ------------------------------------------------------------------------
390 // Object
391 // ------------------------------------------------------------------------
392
393 @Override
394 // All requests have a unique id
395 public int hashCode() {
7184fc40 396 return getRequestId();
2fb2eb37
FC
397 }
398
399 @Override
400 public boolean equals(Object other) {
7184fc40
AM
401 if (other instanceof TmfEventRequest) {
402 TmfEventRequest request = (TmfEventRequest) other;
fd3f1eff
AM
403 return request.fDataType == fDataType
404 && request.fIndex == fIndex
405 && request.fNbRequested == fNbRequested
406 && request.fRange.equals(fRange);
7184fc40
AM
407 }
408 return false;
2fb2eb37
FC
409 }
410
411 @Override
412 public String toString() {
b1b156f3
PT
413 String name = getClass().getName();
414 int dot = name.lastIndexOf('.');
415 if (dot >= 0) {
416 name = name.substring(dot + 1);
417 }
fd3f1eff
AM
418 return '[' + name + '(' + getRequestId() + ',' + getDataType().getSimpleName() +
419 ',' + getExecType() + ',' + getRange() + ',' + getIndex() +
420 ',' + getNbRequested() + ")]"; //$NON-NLS-1$
2fb2eb37
FC
421 }
422
8c8bf09f 423}
This page took 0.064275 seconds and 5 git commands to generate.