tmf: add JUnits for coalescing across parent/children providers
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / 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
2bdf0193 14package org.eclipse.tracecompass.tmf.core.request;
8c8bf09f 15
fd3f1eff
AM
16import java.util.concurrent.CountDownLatch;
17
2bdf0193
AM
18import org.eclipse.tracecompass.internal.tmf.core.TmfCoreTracer;
19import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
20import org.eclipse.tracecompass.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
c4767854
AM
85 /** The index (rank) of the requested event
86 * @since 3.0*/
fd3f1eff
AM
87 protected long fIndex;
88
c4767854
AM
89 /** The number of requested events (ALL_DATA for all)
90 * @since 3.0*/
fd3f1eff
AM
91 protected int fNbRequested;
92
93 /** The number of reads so far */
94 private int fNbRead;
95
96 private final CountDownLatch startedLatch = new CountDownLatch(1);
97 private final CountDownLatch completedLatch = new CountDownLatch(1);
98
99 private boolean fRequestRunning;
100 private boolean fRequestCompleted;
101 private boolean fRequestFailed;
102 private boolean fRequestCanceled;
8c8bf09f
ASL
103
104 // ------------------------------------------------------------------------
fd3f1eff 105 // Constructors
8c8bf09f
ASL
106 // ------------------------------------------------------------------------
107
108 /**
fd3f1eff
AM
109 * Request 'n' events of a given type, for the *whole* trace, at the given
110 * priority.
111 *
112 * @param dataType
113 * The requested data type.
114 * @param index
115 * The index of the first event to retrieve. You can use '0' to
116 * start at the beginning of the trace.
117 * @param nbRequested
118 * The number of events requested. You can use
119 * {@link TmfEventRequest#ALL_DATA} to indicate you want all
120 * events in the trace.
121 * @param priority
122 * The requested execution priority.
123 */
124 public TmfEventRequest(Class<? extends ITmfEvent> dataType,
125 long index,
126 int nbRequested,
127 ExecutionType priority) {
128 this(dataType, TmfTimeRange.ETERNITY, index, nbRequested, priority);
129 }
130
131 /**
132 * Request 'n' events of a given type, for the given time range, at the
133 * given priority.
0283f7ff 134 *
7184fc40
AM
135 * @param dataType
136 * The requested data type.
137 * @param range
138 * The time range of the requested events. You can use
139 * {@link TmfTimeRange#ETERNITY} to indicate you want to cover
140 * the whole trace.
141 * @param index
142 * The index of the first event to retrieve. You can use '0' to
143 * start at the beginning of the trace.
144 * @param nbRequested
145 * The number of events requested. You can use
146 * {@link TmfEventRequest#ALL_DATA} to indicate you want all
147 * events in the time range.
7184fc40
AM
148 * @param priority
149 * The requested execution priority.
0d9a6d76 150 */
7184fc40
AM
151 public TmfEventRequest(Class<? extends ITmfEvent> dataType,
152 TmfTimeRange range,
153 long index,
154 int nbRequested,
7184fc40 155 ExecutionType priority) {
fd3f1eff 156
bc4f881c
AM
157 synchronized (this.getClass()) {
158 fRequestId = fRequestNumber++;
159 }
fd3f1eff
AM
160 fDataType = dataType;
161 fIndex = index;
162 fNbRequested = nbRequested;
163 fExecType = priority;
7184fc40 164 fRange = range;
fd3f1eff
AM
165 fNbRead = 0;
166
167 fRequestRunning = false;
168 fRequestCompleted = false;
169 fRequestFailed = false;
170 fRequestCanceled = false;
90891c08 171
fd3f1eff 172 /* Setup the request tracing if it's enabled */
5500a7f0 173 if (TmfCoreTracer.isRequestTraced()) {
90891c08
FC
174 String type = getClass().getName();
175 type = type.substring(type.lastIndexOf('.') + 1);
176 @SuppressWarnings("nls")
0283f7ff 177 String message = "CREATED "
fd3f1eff 178 + (getExecType() == ExecutionType.BACKGROUND ? "(BG)" : "(FG)")
0283f7ff 179 + " Type=" + type + " Index=" + getIndex() + " NbReq=" + getNbRequested()
4cf201de 180 + " Range=" + getRange()
90891c08 181 + " DataType=" + getDataType().getSimpleName();
8b56808c 182 TmfCoreTracer.traceRequest(fRequestId, message);
90891c08 183 }
8c8bf09f
ASL
184 }
185
fd3f1eff
AM
186 /**
187 * Resets the request counter (used for testing)
188 */
bc4f881c 189 public static synchronized void reset() {
fd3f1eff
AM
190 fRequestNumber = 0;
191 }
192
8c8bf09f
ASL
193 // ------------------------------------------------------------------------
194 // Accessors
195 // ------------------------------------------------------------------------
196
fd3f1eff
AM
197 @Override
198 public int getRequestId() {
199 return fRequestId;
200 }
201
202 @Override
203 public long getIndex() {
204 return fIndex;
205 }
206
c4767854
AM
207 /**
208 * @since 3.0
209 */
fd3f1eff
AM
210 @Override
211 public ExecutionType getExecType() {
212 return fExecType;
213 }
214
215 @Override
216 public int getNbRequested() {
217 return fNbRequested;
218 }
219
220 @Override
221 public synchronized int getNbRead() {
222 return fNbRead;
223 }
224
225 @Override
226 public synchronized boolean isRunning() {
227 return fRequestRunning;
228 }
229
230 @Override
231 public synchronized boolean isCompleted() {
232 return fRequestCompleted;
233 }
234
235 @Override
236 public synchronized boolean isFailed() {
237 return fRequestFailed;
238 }
239
240 @Override
241 public synchronized boolean isCancelled() {
242 return fRequestCanceled;
243 }
244
245 @Override
246 public Class<? extends ITmfEvent> getDataType() {
247 return fDataType;
248 }
249
d4011df2 250 @Override
7184fc40 251 public TmfTimeRange getRange() {
5419a136 252 return fRange;
8c8bf09f
ASL
253 }
254
a79913eb
FC
255 // ------------------------------------------------------------------------
256 // Setters
257 // ------------------------------------------------------------------------
258
259 /**
fd3f1eff
AM
260 * This method is called by the event provider to set the index
261 * corresponding to the time range start time
0283f7ff 262 *
7184fc40 263 * @param index
fd3f1eff 264 * The start time index
a79913eb 265 */
fd3f1eff
AM
266 protected void setIndex(int index) {
267 fIndex = index;
268 }
269
a79913eb 270 @Override
7184fc40
AM
271 public void setStartIndex(int index) {
272 setIndex(index);
a79913eb
FC
273 }
274
fd3f1eff
AM
275 // ------------------------------------------------------------------------
276 // Operators
277 // ------------------------------------------------------------------------
278
279 @Override
280 public void handleData(ITmfEvent event) {
41f3b36b 281 fNbRead++;
fd3f1eff
AM
282 }
283
284 @Override
285 public void handleStarted() {
286 if (TmfCoreTracer.isRequestTraced()) {
8b56808c 287 TmfCoreTracer.traceRequest(getRequestId(), "STARTED"); //$NON-NLS-1$
fd3f1eff
AM
288 }
289 }
290
291 @Override
292 public void handleCompleted() {
293 boolean requestFailed = false;
294 boolean requestCanceled = false;
295 synchronized (this) {
296 requestFailed = fRequestFailed;
297 requestCanceled = fRequestCanceled;
298 }
299
300 if (requestFailed) {
301 handleFailure();
302 } else if (requestCanceled) {
303 handleCancel();
304 } else {
305 handleSuccess();
306 }
307 if (TmfCoreTracer.isRequestTraced()) {
8b56808c 308 TmfCoreTracer.traceRequest(getRequestId(), "COMPLETED (" + fNbRead + " events read)"); //$NON-NLS-1$ //$NON-NLS-2$
fd3f1eff
AM
309 }
310 }
311
312 @Override
313 public void handleSuccess() {
314 if (TmfCoreTracer.isRequestTraced()) {
8b56808c 315 TmfCoreTracer.traceRequest(getRequestId(), "SUCCEEDED"); //$NON-NLS-1$
fd3f1eff
AM
316 }
317 }
318
319 @Override
320 public void handleFailure() {
321 if (TmfCoreTracer.isRequestTraced()) {
8b56808c 322 TmfCoreTracer.traceRequest(getRequestId(), "FAILED"); //$NON-NLS-1$
fd3f1eff
AM
323 }
324 }
325
326 @Override
327 public void handleCancel() {
328 if (TmfCoreTracer.isRequestTraced()) {
8b56808c 329 TmfCoreTracer.traceRequest(getRequestId(), "CANCELLED"); //$NON-NLS-1$
fd3f1eff
AM
330 }
331 }
332
333 /**
334 * To suspend the client thread until the request starts (or is canceled).
335 *
336 * @throws InterruptedException
337 * If the thread was interrupted while waiting
338 */
339 public void waitForStart() throws InterruptedException {
340 while (!fRequestRunning) {
341 startedLatch.await();
342 }
343 }
344
345 @Override
346 public void waitForCompletion() throws InterruptedException {
347 while (!fRequestCompleted) {
348 completedLatch.await();
349 }
350 }
351
352 @Override
353 public void start() {
354 synchronized (this) {
355 fRequestRunning = true;
356 }
357 handleStarted();
358 startedLatch.countDown();
359 }
360
361 @Override
362 public void done() {
363 synchronized (this) {
364 if (!fRequestCompleted) {
365 fRequestRunning = false;
366 fRequestCompleted = true;
367 } else {
368 return;
369 }
370 }
371 try {
372 handleCompleted();
373 } finally {
374 completedLatch.countDown();
375 }
376 }
377
378 @Override
379 public void fail() {
380 synchronized (this) {
381 fRequestFailed = true;
382 }
383 done();
384 }
385
386 @Override
387 public void cancel() {
388 synchronized (this) {
389 fRequestCanceled = true;
390 }
391 done();
392 }
393
2fb2eb37
FC
394 // ------------------------------------------------------------------------
395 // Object
396 // ------------------------------------------------------------------------
397
398 @Override
399 // All requests have a unique id
400 public int hashCode() {
7184fc40 401 return getRequestId();
2fb2eb37
FC
402 }
403
404 @Override
405 public boolean equals(Object other) {
7184fc40
AM
406 if (other instanceof TmfEventRequest) {
407 TmfEventRequest request = (TmfEventRequest) other;
fd3f1eff
AM
408 return request.fDataType == fDataType
409 && request.fIndex == fIndex
410 && request.fNbRequested == fNbRequested
411 && request.fRange.equals(fRange);
7184fc40
AM
412 }
413 return false;
2fb2eb37
FC
414 }
415
416 @Override
417 public String toString() {
b1b156f3
PT
418 String name = getClass().getName();
419 int dot = name.lastIndexOf('.');
420 if (dot >= 0) {
421 name = name.substring(dot + 1);
422 }
fd3f1eff
AM
423 return '[' + name + '(' + getRequestId() + ',' + getDataType().getSimpleName() +
424 ',' + getExecType() + ',' + getRange() + ',' + getIndex() +
425 ',' + getNbRequested() + ")]"; //$NON-NLS-1$
2fb2eb37
FC
426 }
427
8c8bf09f 428}
This page took 0.10199 seconds and 5 git commands to generate.