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