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
.core
.component
;
15 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Tracer
;
16 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.request
.TmfCoalescedEventRequest
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
25 * An extension of TmfDataProvider timestamped events providers.
28 * @author Francois Chouinard
30 public abstract class TmfEventProvider
<T
extends ITmfEvent
> extends TmfDataProvider
<T
> {
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
39 public TmfEventProvider() {
44 public void init(String name
, Class
<T
> type
) {
45 super.init(name
, type
);
49 * Standard constructor
52 * The name of the provider
54 * The type of handled events
56 public TmfEventProvider(String name
, Class
<T
> type
) {
61 * Standard constructor which also sets the queue size
64 * The name of the provider
66 * The type of handled events
68 * The size of the queue
70 public TmfEventProvider(String name
, Class
<T
> type
, int queueSize
) {
71 super(name
, type
, queueSize
);
78 * The other TmfEventProvider to copy
80 public TmfEventProvider(TmfEventProvider
<T
> other
) {
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
90 boolean requestCompleted
= super.isCompleted(request
, data
, nbRead
);
91 if (!requestCompleted
&& request
instanceof ITmfEventRequest
<?
>) {
92 ITmfTimestamp endTime
= ((ITmfEventRequest
<?
>) request
).getRange().getEndTime();
93 return data
.getTimestamp().compareTo(endTime
, false) > 0;
95 return requestCompleted
;
99 protected synchronized void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
100 if (request
instanceof ITmfEventRequest
<?
>) {
101 ITmfEventRequest
<T
> eventRequest
= (ITmfEventRequest
<T
>) request
;
102 TmfCoalescedEventRequest
<T
> coalescedRequest
= new TmfCoalescedEventRequest
<T
>(eventRequest
.getDataType(), eventRequest
.getRange(),
103 eventRequest
.getIndex(), eventRequest
.getNbRequested(), eventRequest
.getBlockSize(), eventRequest
.getExecType());
104 coalescedRequest
.addRequest(eventRequest
);
105 if (Tracer
.isRequestTraced()) {
106 Tracer
.traceRequest(request
, "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
107 Tracer
.traceRequest(coalescedRequest
, "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
109 fPendingCoalescedRequests
.add(coalescedRequest
);
111 super.newCoalescedDataRequest(request
);
116 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
118 if (! (request
instanceof ITmfEventRequest
)) {
119 super.queueBackgroundRequest(request
, blockSize
, indexing
);
123 final TmfDataProvider
<T
> provider
= this;
125 Thread thread
= new Thread() {
129 if (Tracer
.isRequestTraced()) {
130 Tracer
.traceRequest(request
, "is being serviced by " + provider
.getName()); //$NON-NLS-1$
135 final Integer
[] CHUNK_SIZE
= new Integer
[1];
136 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
138 final Integer
[] nbRead
= new Integer
[1];
141 final Boolean
[] isFinished
= new Boolean
[1];
142 isFinished
[0] = Boolean
.FALSE
;
144 long startIndex
= request
.getIndex();
146 while (!isFinished
[0]) {
148 TmfEventRequest
<T
> subRequest
= new TmfEventRequest
<T
>(request
.getDataType(), ((ITmfEventRequest
<?
>) request
).getRange(), startIndex
+ nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
) {
151 public synchronized boolean isCompleted() {
152 return super.isCompleted() || request
.isCompleted();
156 public void handleData(T data
) {
157 super.handleData(data
);
158 if (request
.getDataType().isInstance(data
)) {
159 request
.handleData(data
);
161 if (this.getNbRead() > CHUNK_SIZE
[0]) {
162 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
167 public void handleCompleted() {
168 nbRead
[0] += this.getNbRead();
169 if (nbRead
[0] >= request
.getNbRequested() || (this.getNbRead() < CHUNK_SIZE
[0])) {
170 if (this.isCancelled()) {
172 } else if (this.isFailed()) {
177 isFinished
[0] = Boolean
.TRUE
;
179 super.handleCompleted();
183 if (!isFinished
[0]) {
184 queueRequest(subRequest
);
187 subRequest
.waitForCompletion();
188 if (request
.isCompleted()) {
189 isFinished
[0] = Boolean
.TRUE
;
191 } catch (InterruptedException e
) {
195 if (startIndex
== 0 && nbRead
[0].equals(CHUNK_SIZE
[0])) { // do this only once if the event request index is unknown
196 startIndex
= subRequest
.getIndex(); // update the start index with the index of the first subrequest's
197 } // start time event which was set during the arm request
198 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);