1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Alexandre Montplaisir - Consolidate constructors, merge with TmfDataRequest
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.request
;
16 import java
.util
.concurrent
.CountDownLatch
;
18 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.TmfCoreTracer
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
24 * TmfEventRequest's are used to obtain series of events from an event provider.
25 * Open ranges can be used, especially for continuous streaming.
27 * The request is processed asynchronously by a TmfEventProvider and, as events
28 * become available, handleData() is invoked synchronously for each one.
30 * The TmfEventProvider indicates that the request is completed by calling
31 * done(). The request can be cancelled at any time with cancel().
36 * TmfEventRequest request = new TmfEventRequest(DataType.class, range, startIndex, nbEvents, priority) {
38 * public void handleData(ITmfEvent event) {
39 * // do something with the event
42 * public void handleSuccess() {
43 * // callback for when the request completes successfully
46 * public void handleFailure() {
47 * // callback for when the request fails due to an error
50 * public void handleCancel() {
51 * // callback for when the request is cancelled via .cancel()
56 * eventProvider.sendRequest(request);
60 * TODO: Implement request failures (codes, etc...)
62 * @author Francois Chouinard
64 public abstract class TmfEventRequest
implements ITmfEventRequest
{
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 private static int fRequestNumber
= 0;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
76 private final Class
<?
extends ITmfEvent
> fDataType
;
77 private final ExecutionType fExecType
;
79 /** A unique request ID */
80 private final int fRequestId
;
82 /** The requested events time range */
83 private final TmfTimeRange fRange
;
85 /** The index (rank) of the requested event */
86 protected long fIndex
;
88 /** The number of requested events (ALL_DATA for all) */
89 protected int fNbRequested
;
91 /** The number of reads so far */
94 private final CountDownLatch startedLatch
= new CountDownLatch(1);
95 private final CountDownLatch completedLatch
= new CountDownLatch(1);
97 private boolean fRequestRunning
;
98 private boolean fRequestCompleted
;
99 private boolean fRequestFailed
;
100 private boolean fRequestCanceled
;
102 private ITmfFilter fEventFilter
;
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * Request 'n' events of a given type, for the *whole* trace, at the given
113 * The requested data type.
115 * The index of the first event to retrieve. You can use '0' to
116 * start at the beginning of the trace.
118 * The number of events requested. You can use
119 * {@link TmfEventRequest#ALL_DATA} to indicate you want all
120 * events in the trace.
122 * The requested execution priority.
124 public TmfEventRequest(Class
<?
extends ITmfEvent
> dataType
,
127 ExecutionType priority
) {
128 this(dataType
, TmfTimeRange
.ETERNITY
, index
, nbRequested
, priority
);
132 * Request 'n' events of a given type, for the given time range, at the
136 * The requested data type.
138 * The time range of the requested events. You can use
139 * {@link TmfTimeRange#ETERNITY} to indicate you want to cover
142 * The index of the first event to retrieve. You can use '0' to
143 * start at the beginning of the trace.
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.
149 * The requested execution priority.
151 public TmfEventRequest(Class
<?
extends ITmfEvent
> dataType
,
155 ExecutionType priority
) {
157 synchronized (TmfEventRequest
.class) {
158 fRequestId
= fRequestNumber
++;
160 fDataType
= dataType
;
162 fNbRequested
= nbRequested
;
163 fExecType
= priority
;
167 fRequestRunning
= false;
168 fRequestCompleted
= false;
169 fRequestFailed
= false;
170 fRequestCanceled
= false;
172 /* Setup the request tracing if it's enabled */
173 if (TmfCoreTracer
.isRequestTraced()) {
174 String type
= getClass().getName();
175 type
= type
.substring(type
.lastIndexOf('.') + 1);
176 @SuppressWarnings("nls")
177 String message
= "CREATED "
178 + (getExecType() == ExecutionType
.BACKGROUND ?
"(BG)" : "(FG)")
179 + " Type=" + type
+ " Index=" + getIndex() + " NbReq=" + getNbRequested()
180 + " Range=" + getRange()
181 + " DataType=" + getDataType().getSimpleName();
182 TmfCoreTracer
.traceRequest(fRequestId
, message
);
186 // ------------------------------------------------------------------------
188 // ------------------------------------------------------------------------
191 public int getRequestId() {
196 public long getIndex() {
201 public ExecutionType
getExecType() {
206 public int getNbRequested() {
211 public synchronized int getNbRead() {
216 public synchronized boolean isRunning() {
217 return fRequestRunning
;
221 public synchronized boolean isCompleted() {
222 return fRequestCompleted
;
226 public synchronized boolean isFailed() {
227 return fRequestFailed
;
231 public synchronized boolean isCancelled() {
232 return fRequestCanceled
;
236 public Class
<?
extends ITmfEvent
> getDataType() {
241 public TmfTimeRange
getRange() {
246 public ITmfFilter
getProviderFilter() {
251 public void setProviderFilter(ITmfFilter provider
) {
252 fEventFilter
= provider
;
255 // ------------------------------------------------------------------------
257 // ------------------------------------------------------------------------
260 * This method is called by the event provider to set the index
261 * corresponding to the time range start time
264 * The start time index
266 protected void setIndex(int index
) {
271 public void setStartIndex(int index
) {
275 // ------------------------------------------------------------------------
277 // ------------------------------------------------------------------------
280 public void handleData(ITmfEvent event
) {
285 public void handleStarted() {
286 if (TmfCoreTracer
.isRequestTraced()) {
287 TmfCoreTracer
.traceRequest(getRequestId(), "STARTED"); //$NON-NLS-1$
292 public void handleCompleted() {
293 boolean requestFailed
= false;
294 boolean requestCanceled
= false;
295 synchronized (this) {
296 requestFailed
= fRequestFailed
;
297 requestCanceled
= fRequestCanceled
;
302 } else if (requestCanceled
) {
307 if (TmfCoreTracer
.isRequestTraced()) {
308 TmfCoreTracer
.traceRequest(getRequestId(), "COMPLETED (" + fNbRead
+ " events read)"); //$NON-NLS-1$ //$NON-NLS-2$
313 public void handleSuccess() {
314 if (TmfCoreTracer
.isRequestTraced()) {
315 TmfCoreTracer
.traceRequest(getRequestId(), "SUCCEEDED"); //$NON-NLS-1$
320 public void handleFailure() {
321 if (TmfCoreTracer
.isRequestTraced()) {
322 TmfCoreTracer
.traceRequest(getRequestId(), "FAILED"); //$NON-NLS-1$
327 public void handleCancel() {
328 if (TmfCoreTracer
.isRequestTraced()) {
329 TmfCoreTracer
.traceRequest(getRequestId(), "CANCELLED"); //$NON-NLS-1$
334 * To suspend the client thread until the request starts (or is canceled).
336 * @throws InterruptedException
337 * If the thread was interrupted while waiting
339 public void waitForStart() throws InterruptedException
{
340 while (!fRequestRunning
) {
341 startedLatch
.await();
346 public void waitForCompletion() throws InterruptedException
{
347 while (!fRequestCompleted
) {
348 completedLatch
.await();
353 public void start() {
354 synchronized (this) {
355 fRequestRunning
= true;
358 startedLatch
.countDown();
363 synchronized (this) {
364 if (!fRequestCompleted
) {
365 fRequestRunning
= false;
366 fRequestCompleted
= true;
374 completedLatch
.countDown();
380 synchronized (this) {
381 fRequestFailed
= true;
387 public void cancel() {
388 synchronized (this) {
389 fRequestCanceled
= true;
394 // ------------------------------------------------------------------------
396 // ------------------------------------------------------------------------
399 public String
toString() {
400 String name
= getClass().getName();
401 int dot
= name
.lastIndexOf('.');
403 name
= name
.substring(dot
+ 1);
405 return '[' + name
+ '(' + getRequestId() + ',' + getDataType().getSimpleName() +
406 ',' + getExecType() + ',' + getRange() + ',' + getIndex() +
407 ',' + getNbRequested() + ")]"; //$NON-NLS-1$