Commit | Line | Data |
---|---|---|
8584dc20 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 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 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.request; | |
14 | ||
15 | import org.eclipse.core.runtime.IStatus; | |
16 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
17 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
18 | import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; | |
19 | ||
20 | /** | |
21 | * The TMF request API. | |
22 | * <p> | |
23 | * ITmfRequest:s are used to obtain blocks of events from an event provider. Open | |
24 | * ranges can be used, typically for continuous streaming. | |
25 | * <p> | |
26 | * The request is processed asynchronously by a TmfRequestHandler which invokes | |
27 | * the request's handleEvent() synchronously for each event that matches the | |
28 | * request filter(s). | |
29 | * <p> | |
30 | * The TmfProvider indicates that the request is completed by calling done(). | |
31 | * The request can be canceled at any time with cancel(). In case of unexpected | |
32 | * exception or error, fail() will be called. | |
33 | * <p> | |
34 | * Typical usage: | |
35 | * <pre> | |
36 | * <code> | |
37 | * ITmfRequest request = new TmfRequest(someParams) { | |
38 | * @Override | |
39 | * public synchronized void handleEvent(ITmfEvent event) { | |
40 | * super.handleEvent(event); | |
41 | * // do something | |
42 | * } | |
43 | * @Override | |
44 | * public void handleCompleted() { | |
45 | * // do something | |
46 | * } | |
47 | * @Override | |
48 | * public void handleCancel() { | |
49 | * // do something | |
50 | * } | |
51 | * @Override | |
52 | * public void handleFailure() { | |
53 | * // do something | |
54 | * } | |
55 | * }; | |
56 | * request.addEventFilter(myFilter); | |
57 | * fProcessor.process(request); | |
58 | * </code> | |
59 | * </pre> | |
60 | * | |
61 | * @author Francois Chouinard | |
62 | * @version 1.0 | |
63 | * @since 2.0 | |
64 | */ | |
65 | public interface ITmfRequest { | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Constants | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | /** The request count for all the events */ | |
72 | public static final long ALL_EVENTS = Long.MAX_VALUE; | |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Enumerated Types | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
78 | /** | |
79 | * The request execution type/priority | |
80 | * @author francois | |
81 | */ | |
82 | public enum TmfRequestPriority { | |
83 | /** | |
84 | * Normal priority request (preemptible) | |
85 | */ | |
86 | NORMAL, | |
87 | /** | |
88 | * High priority request (non-preemptible) | |
89 | */ | |
90 | HIGH | |
91 | } | |
92 | ||
93 | /** | |
94 | * The request execution state | |
95 | * @author francois | |
96 | */ | |
97 | public enum TmfRequestState { | |
98 | /** | |
99 | * The request is created but has not started being processed yet | |
100 | */ | |
101 | PENDING, | |
102 | /** | |
103 | * The request is being handled but has not completed yet | |
104 | */ | |
105 | RUNNING, | |
106 | /** | |
107 | * The request has completed | |
108 | */ | |
109 | COMPLETED | |
110 | } | |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Getters | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
116 | /** | |
117 | * @return the request ID | |
118 | */ | |
119 | public int getRequestId(); | |
120 | ||
121 | /** | |
122 | * @return the request type | |
123 | */ | |
124 | public TmfRequestPriority getRequestPriority(); | |
125 | ||
126 | /** | |
127 | * @return the time range of interest | |
128 | */ | |
129 | public TmfTimeRange getTimeRange(); | |
130 | ||
131 | /** | |
132 | * @return the number of events requested | |
133 | */ | |
134 | public long getNbRequested(); | |
135 | ||
136 | /** | |
137 | * @return the index of the first event requested | |
138 | */ | |
139 | public long getStartIndex(); | |
140 | ||
141 | /** | |
142 | * @return the number of events read so far | |
143 | */ | |
144 | public long getNbEventsRead(); | |
145 | ||
146 | /** | |
147 | * @param filterType the filter type to retrieve | |
148 | * | |
149 | * @return the corresponding filter (if any) | |
150 | */ | |
151 | public ITmfFilter getEventFilter(Class<?> filterType); | |
152 | ||
153 | /** | |
154 | * Add/replace a filter | |
155 | * | |
156 | * @param filter the new filter | |
157 | */ | |
158 | public void addEventFilter(ITmfFilter filter); | |
159 | ||
160 | /** | |
161 | * Check if the event matches the request | |
162 | * | |
163 | * @param event the event to test | |
164 | * | |
165 | * @return true if the event matches the request, false otherwise | |
166 | */ | |
167 | public boolean matches(ITmfEvent event); | |
168 | ||
169 | /** | |
170 | * Sets the request's parent in the hierarchy | |
171 | * | |
172 | * @param parent the parent request | |
173 | */ | |
174 | public void setParent(ITmfRequest parent); | |
175 | ||
176 | /** | |
177 | * Gets the request's parent in the hierarchy | |
178 | * | |
179 | * @return the parent request | |
180 | */ | |
181 | public ITmfRequest getParent(); | |
182 | ||
183 | /** | |
184 | * Completion notification for teh parent request | |
185 | * | |
186 | * @param child the child request | |
187 | */ | |
188 | public void notifyParent(ITmfRequest child); | |
189 | ||
190 | // ------------------------------------------------------------------------ | |
191 | // Request execution state | |
192 | // ------------------------------------------------------------------------ | |
193 | ||
194 | /** | |
195 | * @return the request execution state | |
196 | */ | |
197 | public TmfRequestState getState(); | |
198 | ||
199 | /** | |
200 | * @return true if the request is still active | |
201 | */ | |
202 | public boolean isRunning(); | |
203 | ||
204 | /** | |
205 | * @return true if the request is completed | |
206 | */ | |
207 | public boolean isCompleted(); | |
208 | ||
209 | // ------------------------------------------------------------------------ | |
210 | // Request completion status | |
211 | // ------------------------------------------------------------------------ | |
212 | ||
213 | /** | |
214 | * @return the request completion status | |
215 | */ | |
216 | public IStatus getStatus(); | |
217 | ||
218 | /** | |
219 | * @return true if the request completed successfully | |
220 | */ | |
221 | public boolean isOK(); | |
222 | ||
223 | /** | |
224 | ||
225 | * @return true if the request has failed */ | |
226 | public boolean isFailed(); | |
227 | ||
228 | /** | |
229 | * @return true if the request was cancelled | |
230 | */ | |
231 | public boolean isCancelled(); | |
232 | ||
233 | // ------------------------------------------------------------------------ | |
234 | // Request operations | |
235 | // ------------------------------------------------------------------------ | |
236 | ||
237 | /** | |
238 | * Put the request in the running state | |
239 | */ | |
240 | public void start(); | |
241 | ||
242 | /** | |
243 | * Put the request in the terminated state | |
244 | */ | |
245 | public void done(); | |
246 | ||
247 | /** | |
248 | * Put the request in the failed state | |
249 | */ | |
250 | public void fail(); | |
251 | ||
252 | /** | |
253 | * Put the request in the cancelled state | |
254 | */ | |
255 | public void cancel(); | |
256 | ||
257 | /** | |
258 | * To suspend the client thread until the request starts | |
259 | * (or is canceled). | |
260 | * | |
261 | * @throws InterruptedException thrown if the request was cancelled | |
262 | */ | |
263 | public void waitForStart() throws InterruptedException; | |
264 | ||
265 | /** | |
266 | * To suspend the client thread until the request completes | |
267 | * (or is canceled). | |
268 | * | |
269 | * @throws InterruptedException thrown if the request was cancelled | |
270 | */ | |
271 | public void waitForCompletion() throws InterruptedException; | |
272 | ||
273 | // ------------------------------------------------------------------------ | |
274 | // Request processing hooks | |
275 | // ------------------------------------------------------------------------ | |
276 | ||
277 | /** | |
278 | * Request processing start notification | |
279 | */ | |
280 | public void handleStarted(); | |
281 | ||
282 | /** | |
283 | * Process the piece of data | |
284 | * | |
285 | * @param event the data to process | |
286 | */ | |
287 | public void handleEvent(ITmfEvent event); | |
288 | ||
289 | /** | |
290 | * Request processing completion notification | |
291 | */ | |
292 | public void handleCompleted(); | |
293 | ||
294 | /** | |
295 | * Request successful completion notification | |
296 | */ | |
297 | public void handleSuccess(); | |
298 | ||
299 | /** | |
300 | * Request failure notification | |
301 | */ | |
302 | public void handleFailure(); | |
303 | ||
304 | /** | |
305 | * Request cancellation notification | |
306 | */ | |
307 | public void handleCancel(); | |
308 | ||
309 | } |