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