Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 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.component; | |
14 | ||
9b635e61 | 15 | import org.eclipse.linuxtools.tmf.Tracer; |
8c8bf09f ASL |
16 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
17 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 | 18 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
a79913eb | 19 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType; |
2fb2eb37 | 20 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; |
951d134a | 21 | import org.eclipse.linuxtools.tmf.request.TmfCoalescedEventRequest; |
a79913eb | 22 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; |
8c8bf09f ASL |
23 | |
24 | /** | |
25 | * <b><u>TmfEventProvider</u></b> | |
26 | * <p> | |
8c8bf09f ASL |
27 | */ |
28 | public abstract class TmfEventProvider<T extends TmfEvent> extends TmfDataProvider<T> { | |
29 | ||
00641a97 FC |
30 | // ------------------------------------------------------------------------ |
31 | // Constructors | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
12c155f5 | 34 | public TmfEventProvider() { |
00641a97 | 35 | super(); |
12c155f5 | 36 | } |
8c8bf09f | 37 | |
12c155f5 FC |
38 | public void init(String name, Class<T> eventType) { |
39 | super.init(name, eventType); | |
40 | } | |
8c8bf09f | 41 | |
12c155f5 FC |
42 | public TmfEventProvider(String name, Class<T> type) { |
43 | super(name, type); | |
44 | } | |
45 | ||
46 | public TmfEventProvider(String name, Class<T> type, int queueSize) { | |
47 | super(name, type, queueSize); | |
48 | } | |
49 | ||
00641a97 FC |
50 | public TmfEventProvider(TmfEventProvider<T> other) { |
51 | super(other); | |
12c155f5 FC |
52 | } |
53 | ||
00641a97 FC |
54 | // ------------------------------------------------------------------------ |
55 | // TmfDataProvider | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
12c155f5 FC |
58 | @Override |
59 | public boolean isCompleted(ITmfDataRequest<T> request, T data, int nbRead) { | |
60 | boolean requestCompleted = super.isCompleted(request, data, nbRead); | |
61 | if (!requestCompleted && request instanceof ITmfEventRequest<?> && !data.isNullRef()) { | |
62 | TmfTimestamp endTime = ((ITmfEventRequest<?>) request).getRange().getEndTime(); | |
63 | return data.getTimestamp().compareTo(endTime, false) > 0; | |
64 | } | |
65 | return requestCompleted; | |
66 | } | |
67 | ||
68 | @Override | |
69 | protected synchronized void newCoalescedDataRequest(ITmfDataRequest<T> request) { | |
70 | if (request instanceof ITmfEventRequest<?>) { | |
71 | ITmfEventRequest<T> eventRequest = (ITmfEventRequest<T>) request; | |
72 | TmfCoalescedEventRequest<T> coalescedRequest = new TmfCoalescedEventRequest<T>(fType, eventRequest.getRange(), | |
8edafa7f | 73 | eventRequest.getIndex(), eventRequest.getNbRequested(), eventRequest.getBlockSize(), eventRequest.getExecType()); |
12c155f5 FC |
74 | coalescedRequest.addRequest(eventRequest); |
75 | if (Tracer.isRequestTraced()) { | |
76 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); //$NON-NLS-1$ | |
77 | } | |
78 | fPendingCoalescedRequests.add(coalescedRequest); | |
79 | } else { | |
80 | super.newCoalescedDataRequest(request); | |
81 | } | |
82 | } | |
951d134a | 83 | |
a79913eb FC |
84 | @Override |
85 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { | |
86 | ||
87 | if (! (request instanceof ITmfEventRequest)) { | |
88 | super.queueBackgroundRequest(request, blockSize, indexing); | |
89 | return; | |
90 | } | |
91 | ||
92 | Thread thread = new Thread() { | |
93 | @Override | |
94 | public void run() { | |
95 | request.start(); | |
96 | ||
97 | final Integer[] CHUNK_SIZE = new Integer[1]; | |
98 | CHUNK_SIZE[0] = Math.min(request.getNbRequested(), blockSize + ((indexing) ? 1 : 0)); | |
99 | ||
100 | final Integer[] nbRead = new Integer[1]; | |
101 | nbRead[0] = 0; | |
102 | ||
103 | final Boolean[] isFinished = new Boolean[1]; | |
104 | isFinished[0] = Boolean.FALSE; | |
105 | ||
106 | int startIndex = request.getIndex(); | |
107 | ||
108 | while (!isFinished[0]) { | |
109 | ||
110 | TmfEventRequest<T> subRequest= new TmfEventRequest<T>(request.getDataType(), ((ITmfEventRequest<?>) request).getRange(), startIndex + nbRead[0], CHUNK_SIZE[0], blockSize, ExecutionType.BACKGROUND) | |
111 | { | |
112 | @Override | |
113 | public void handleData(T data) { | |
114 | super.handleData(data); | |
115 | request.handleData(data); | |
116 | if (this.getNbRead() > CHUNK_SIZE[0]) { | |
117 | System.out.println("ERROR - Read too many events"); //$NON-NLS-1$ | |
118 | } | |
119 | } | |
120 | ||
121 | @Override | |
122 | public void handleCompleted() { | |
123 | nbRead[0] += this.getNbRead(); | |
124 | if (nbRead[0] >= request.getNbRequested() || (this.getNbRead() < CHUNK_SIZE[0])) { | |
90de83da | 125 | if (this.isCancelled()) { |
a79913eb | 126 | request.cancel(); |
90de83da BH |
127 | } else if (this.isFailed()) { |
128 | request.fail(); | |
129 | } else { | |
a79913eb FC |
130 | request.done(); |
131 | } | |
132 | isFinished[0] = Boolean.TRUE; | |
133 | } | |
134 | super.handleCompleted(); | |
135 | } | |
136 | }; | |
137 | ||
138 | if (!isFinished[0]) { | |
139 | queueRequest(subRequest); | |
140 | ||
141 | try { | |
142 | subRequest.waitForCompletion(); | |
143 | } catch (InterruptedException e) { | |
144 | e.printStackTrace(); | |
145 | } | |
146 | ||
147 | if (startIndex == 0 && nbRead[0] == CHUNK_SIZE[0]) { // do this only once if the event request index is unknown | |
148 | startIndex = subRequest.getIndex(); // update the start index with the index of the first subrequest's | |
149 | } // start time event which was set during the arm request | |
150 | CHUNK_SIZE[0] = Math.min(request.getNbRequested() - nbRead[0], blockSize); | |
151 | } | |
152 | } | |
153 | } | |
154 | }; | |
155 | ||
156 | thread.start(); | |
157 | } | |
8c8bf09f | 158 | } |