1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.component
;
15 import java
.lang
.reflect
.Array
;
16 import java
.util
.Vector
;
17 import java
.util
.concurrent
.BlockingQueue
;
18 import java
.util
.concurrent
.LinkedBlockingQueue
;
19 import java
.util
.concurrent
.SynchronousQueue
;
21 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
22 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
;
23 import org
.eclipse
.linuxtools
.tmf
.request
.TmfCoalescedDataRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.request
.TmfRequestExecutor
;
26 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfEndSynchSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
28 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfStartSynchSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfContext
;
32 * <b><u>TmfProvider</u></b>
34 * The TmfProvider<T> is a provider for a data of type <T>.
36 * This abstract class implements the housekeeking methods to register/
37 * deregister the event provider and to handle generically the event requests.
39 * The concrete class can either re-implement processRequest() entirely or
40 * just implement the hooks (initializeContext() and getNext()).
42 * TODO: Add support for providing multiple data types.
44 public abstract class TmfDataProvider
<T
extends TmfData
> extends TmfComponent
implements ITmfDataProvider
<T
> {
46 final protected Class
<T
> fType
;
48 public static final int DEFAULT_QUEUE_SIZE
= 1000;
49 protected final int fQueueSize
;
50 protected final BlockingQueue
<T
> fDataQueue
;
51 protected final TmfRequestExecutor fExecutor
;
53 private Integer fSynchDepth
;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 public TmfDataProvider(String name
, Class
<T
> type
) {
60 this(name
, type
, DEFAULT_QUEUE_SIZE
);
63 protected TmfDataProvider(String name
, Class
<T
> type
, int queueSize
) {
65 fQueueSize
= queueSize
;
67 fDataQueue
= (queueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
69 fExecutor
= new TmfRequestExecutor();
72 TmfProviderManager
.register(fType
, this);
76 public void register() {
78 TmfProviderManager
.register(fType
, this);
82 public void deregister() {
83 TmfProviderManager
.deregister(fType
, this);
88 public int getQueueSize() {
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
96 public void sendRequest(final TmfDataRequest
<T
> request
) {
98 if (fSynchDepth
> 0) {
99 // We are in coalescing mode: client should NEVER wait
100 // (otherwise we will have deadlock...)
101 coalesceDataRequest(request
);
103 // Process the request immediately
104 queueRequest(request
);
109 * This method queues the coalesced requests.
113 private synchronized void fireRequests() {
114 for (TmfDataRequest
<T
> request
: fPendingCoalescedRequests
) {
115 queueRequest(request
);
117 fPendingCoalescedRequests
.clear();
120 // ------------------------------------------------------------------------
121 // Coalescing (primitive test...)
122 // ------------------------------------------------------------------------
124 protected Vector
<TmfCoalescedDataRequest
<T
>> fPendingCoalescedRequests
= new Vector
<TmfCoalescedDataRequest
<T
>>();
126 protected synchronized void newCoalescedDataRequest(TmfDataRequest
<T
> request
) {
127 TmfCoalescedDataRequest
<T
> coalescedRequest
=
128 new TmfCoalescedDataRequest
<T
>(fType
, request
.getIndex(), request
.getNbRequested(), request
.getBlockize());
129 coalescedRequest
.addRequest(request
);
130 fPendingCoalescedRequests
.add(coalescedRequest
);
133 protected synchronized void coalesceDataRequest(TmfDataRequest
<T
> request
) {
134 for (TmfCoalescedDataRequest
<T
> req
: fPendingCoalescedRequests
) {
135 if (req
.isCompatible(request
)) {
136 req
.addRequest(request
);
140 newCoalescedDataRequest(request
);
143 // ------------------------------------------------------------------------
144 // Request processing
145 // ------------------------------------------------------------------------
147 protected void queueRequest(final TmfDataRequest
<T
> request
) {
149 // Process the request
150 Thread thread
= new Thread() {
155 // Extract the generic information
156 int blockSize
= request
.getBlockize();
157 int nbRequested
= request
.getNbRequested();
159 // Create the result buffer
160 Vector
<T
> result
= new Vector
<T
>();
163 // Initialize the execution
164 ITmfContext context
= armRequest(request
);
165 if (context
== null) {
170 // Get the ordered events
171 T data
= getNext(context
);
172 while (data
!= null && !isCompleted(request
, data
, nbRead
))
175 if (++nbRead
% blockSize
== 0) {
176 pushData(request
, result
);
178 // To avoid an unnecessary read passed the last data requested
179 if (nbRead
< nbRequested
) {
180 data
= getNext(context
);
183 pushData(request
, result
);
187 fExecutor
.queueRequest(thread
);
191 * Format the result data and forwards it to the requester.
192 * Note: after handling, the data is *removed*.
197 @SuppressWarnings("unchecked")
198 protected void pushData(ITmfDataRequest
<T
> request
, Vector
<T
> data
) {
199 synchronized(request
) {
200 if (!request
.isCompleted()) {
201 T
[] result
= (T
[]) Array
.newInstance(fType
, data
.size());
202 data
.toArray(result
);
203 request
.setData(result
);
204 request
.handleData();
205 data
.removeAllElements();
211 * Initialize the provider based on the request. The context is
212 * provider specific and will be updated by getNext().
215 * @return an application specific context; null if request can't be serviced
217 public abstract ITmfContext
armRequest(TmfDataRequest
<T
> request
);
220 * Return the next piece of data based on the context supplied. The context
221 * would typically be updated for the subsequent read.
226 public T
getNext(ITmfContext context
) {
228 T event
= fDataQueue
.take();
230 } catch (InterruptedException e
) {
237 * Makes the generated result data available for getNext()
241 public void queueResult(T data
) {
243 fDataQueue
.put(data
);
244 } catch (InterruptedException e1
) {
245 e1
.printStackTrace();
250 * Checks if the data meets the request completion criteria.
256 public boolean isCompleted(TmfDataRequest
<T
> request
, T data
, int nbRead
) {
257 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
260 // ------------------------------------------------------------------------
262 // ------------------------------------------------------------------------
265 public void startSynch(TmfStartSynchSignal signal
) {
272 public void endSynch(TmfEndSynchSignal signal
) {
275 if (fSynchDepth
== 0) {