Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
d18dd09b ASL |
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 | ||
4918b8f2 | 13 | package org.eclipse.linuxtools.tmf.tests.stubs.trace; |
d18dd09b ASL |
14 | |
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
73005152 | 18 | import java.util.concurrent.locks.ReentrantLock; |
d18dd09b | 19 | |
20658947 | 20 | import org.eclipse.core.resources.IResource; |
72f1e62a | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
dfee01ae | 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
23 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
6c13869b | 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
20658947 | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer; |
6c13869b FC |
29 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
30 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; | |
31 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; | |
d18dd09b ASL |
32 | |
33 | /** | |
34 | * <b><u>TmfTraceStub</u></b> | |
35 | * <p> | |
8d2e2848 | 36 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 37 | */ |
3b38ea61 | 38 | @SuppressWarnings("nls") |
7e6347b0 | 39 | public class TmfTraceStub extends TmfTrace<TmfEvent> implements ITmfEventParser<TmfEvent> { |
d18dd09b | 40 | |
e31e01e8 | 41 | // ------------------------------------------------------------------------ |
d18dd09b | 42 | // Attributes |
e31e01e8 | 43 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
44 | |
45 | // The actual stream | |
ff4ed569 | 46 | private RandomAccessFile fTrace; |
d18dd09b | 47 | |
7e6347b0 FC |
48 | // // The associated event parser |
49 | // private ITmfEventParser<TmfEvent> fParser; | |
d18dd09b | 50 | |
73005152 | 51 | // The synchronization lock |
085d898f FC |
52 | private final ReentrantLock fLock = new ReentrantLock(); |
53 | ||
e31e01e8 | 54 | // ------------------------------------------------------------------------ |
d18dd09b | 55 | // Constructors |
e31e01e8 | 56 | // ------------------------------------------------------------------------ |
d18dd09b | 57 | |
20658947 FC |
58 | /** |
59 | * @param path | |
60 | * @throws FileNotFoundException | |
61 | */ | |
62 | public TmfTraceStub() { | |
63 | super(); | |
7e6347b0 | 64 | fParser = new TmfEventParserStub(this); |
20658947 FC |
65 | } |
66 | ||
d18dd09b | 67 | /** |
3ef62bac | 68 | * @param path |
d18dd09b ASL |
69 | * @throws FileNotFoundException |
70 | */ | |
085d898f | 71 | public TmfTraceStub(final String path) throws FileNotFoundException { |
20658947 | 72 | this(path, DEFAULT_TRACE_CACHE_SIZE, false); |
8d2e2848 FC |
73 | } |
74 | ||
75 | /** | |
3ef62bac | 76 | * @param path |
e31e01e8 | 77 | * @param cacheSize |
8d2e2848 FC |
78 | * @throws FileNotFoundException |
79 | */ | |
085d898f | 80 | public TmfTraceStub(final String path, final int cacheSize) throws FileNotFoundException { |
3ef62bac | 81 | this(path, cacheSize, false); |
d18dd09b ASL |
82 | } |
83 | ||
1703b536 FC |
84 | /** |
85 | * @param path | |
86 | * @param cacheSize | |
87 | * @throws FileNotFoundException | |
88 | */ | |
89 | public TmfTraceStub(final String path, final int cacheSize, final long interval) throws FileNotFoundException { | |
90 | super(null, TmfEvent.class, path, cacheSize, interval); | |
91 | fTrace = new RandomAccessFile(path, "r"); | |
7e6347b0 | 92 | fParser = new TmfEventParserStub(this); |
1703b536 FC |
93 | } |
94 | ||
20658947 FC |
95 | /** |
96 | * @param path | |
97 | * @param cacheSize | |
98 | * @throws FileNotFoundException | |
99 | */ | |
100 | public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException { | |
101 | this(path, cacheSize, false, null, indexer); | |
102 | } | |
103 | ||
d18dd09b | 104 | /** |
3ef62bac | 105 | * @param path |
ff4ed569 | 106 | * @param waitForCompletion |
d18dd09b ASL |
107 | * @throws FileNotFoundException |
108 | */ | |
085d898f | 109 | public TmfTraceStub(final String path, final boolean waitForCompletion) throws FileNotFoundException { |
20658947 | 110 | this(path, DEFAULT_TRACE_CACHE_SIZE, waitForCompletion); |
8d2e2848 | 111 | } |
085d898f | 112 | |
8d2e2848 | 113 | /** |
3ef62bac | 114 | * @param path |
8d2e2848 | 115 | * @param cacheSize |
ff4ed569 | 116 | * @param waitForCompletion |
8d2e2848 FC |
117 | * @throws FileNotFoundException |
118 | */ | |
085d898f | 119 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException { |
09e86496 | 120 | super(null, TmfEvent.class, path, cacheSize); |
3ef62bac | 121 | fTrace = new RandomAccessFile(path, "r"); |
7e6347b0 | 122 | fParser = new TmfEventParserStub(this); |
d18dd09b ASL |
123 | } |
124 | ||
20658947 FC |
125 | /** |
126 | * @param path | |
127 | * @param cacheSize | |
128 | * @param waitForCompletion | |
129 | * @throws FileNotFoundException | |
130 | */ | |
131 | public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException { | |
20658947 FC |
132 | super(resource, TmfEvent.class, path, cacheSize); |
133 | fTrace = new RandomAccessFile(path, "r"); | |
7e6347b0 | 134 | fParser = new TmfEventParserStub(this); |
20658947 | 135 | } |
085d898f | 136 | |
73005152 | 137 | /** |
3ef62bac | 138 | * @param path |
73005152 BH |
139 | * @param cacheSize |
140 | * @param waitForCompletion | |
141 | * @param parser | |
142 | * @throws FileNotFoundException | |
143 | */ | |
7e6347b0 | 144 | @SuppressWarnings("unchecked") |
20658947 FC |
145 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, |
146 | final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException { | |
147 | super(null, TmfEvent.class, path, cacheSize, 0, indexer); | |
3ef62bac | 148 | fTrace = new RandomAccessFile(path, "r"); |
7e6347b0 | 149 | fParser = (ITmfEventParser<ITmfEvent>) ((parser != null) ? parser : new TmfEventParserStub(this)); |
73005152 | 150 | } |
085d898f | 151 | |
ff4ed569 | 152 | /** |
f17b2f70 | 153 | * Copy constructor |
ff4ed569 | 154 | */ |
f17b2f70 | 155 | public TmfTraceStub(final TmfTraceStub trace) throws FileNotFoundException { |
20658947 | 156 | super(trace); |
25e48683 | 157 | fTrace = new RandomAccessFile(getPath(), "r"); |
7e6347b0 | 158 | fParser = new TmfEventParserStub(this); |
ff4ed569 | 159 | } |
085d898f | 160 | |
20658947 FC |
161 | public void indexTrace() { |
162 | fIndexer.buildIndex(true); | |
163 | } | |
164 | ||
165 | @Override | |
166 | public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws FileNotFoundException { | |
167 | fTrace = new RandomAccessFile(path, "r"); | |
7e6347b0 | 168 | fParser = new TmfEventParserStub(this); |
20658947 FC |
169 | super.initTrace(resource, path, type); |
170 | } | |
f17b2f70 | 171 | |
1703b536 FC |
172 | @Override |
173 | public void initialize(final IResource resource, final String path, final Class<TmfEvent> type) throws FileNotFoundException { | |
174 | super.initialize(resource, path, type); | |
175 | } | |
176 | ||
e31e01e8 | 177 | // ------------------------------------------------------------------------ |
d18dd09b | 178 | // Accessors |
e31e01e8 | 179 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
180 | |
181 | public RandomAccessFile getStream() { | |
182 | return fTrace; | |
183 | } | |
184 | ||
e31e01e8 | 185 | // ------------------------------------------------------------------------ |
d18dd09b | 186 | // Operators |
e31e01e8 | 187 | // ------------------------------------------------------------------------ |
d18dd09b | 188 | |
085d898f FC |
189 | @Override |
190 | @SuppressWarnings("unchecked") | |
7e6347b0 | 191 | public TmfContext seekEvent(final ITmfLocation<?> location) { |
d18dd09b | 192 | try { |
09e86496 FC |
193 | fLock.lock(); |
194 | try { | |
195 | if (fTrace != null) { | |
196 | // Position the trace at the requested location and | |
197 | // returns the corresponding context | |
198 | long loc = 0; | |
199 | long rank = 0; | |
200 | if (location != null) { | |
201 | loc = ((TmfLocation<Long>) location).getLocation(); | |
202 | rank = ITmfContext.UNKNOWN_RANK; | |
203 | } | |
20658947 | 204 | if (loc != fTrace.getFilePointer()) { |
09e86496 | 205 | fTrace.seek(loc); |
20658947 | 206 | } |
09e86496 FC |
207 | final TmfContext context = new TmfContext(getCurrentLocation(), rank); |
208 | return context; | |
73005152 | 209 | } |
09e86496 FC |
210 | } catch (final IOException e) { |
211 | e.printStackTrace(); | |
212 | } catch (final NullPointerException e) { | |
213 | e.printStackTrace(); | |
73005152 | 214 | } |
09e86496 | 215 | } catch (final NullPointerException e) { |
085d898f FC |
216 | e.printStackTrace(); |
217 | } | |
73005152 BH |
218 | finally{ |
219 | fLock.unlock(); | |
220 | } | |
085d898f | 221 | return null; |
d18dd09b ASL |
222 | } |
223 | ||
c76c54bb | 224 | |
085d898f | 225 | @Override |
7e6347b0 | 226 | public TmfContext seekEvent(final double ratio) { |
085d898f | 227 | fLock.lock(); |
c76c54bb | 228 | try { |
73005152 | 229 | if (fTrace != null) { |
085d898f | 230 | final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length()))); |
7e6347b0 | 231 | final TmfContext context = seekEvent(location); |
73005152 BH |
232 | context.setRank(ITmfContext.UNKNOWN_RANK); |
233 | return context; | |
234 | } | |
085d898f | 235 | } catch (final IOException e) { |
c76c54bb | 236 | e.printStackTrace(); |
73005152 BH |
237 | } finally { |
238 | fLock.unlock(); | |
c76c54bb | 239 | } |
085d898f | 240 | |
c76c54bb FC |
241 | return null; |
242 | } | |
243 | ||
244 | @Override | |
12c155f5 | 245 | @SuppressWarnings("rawtypes") |
085d898f | 246 | public double getLocationRatio(final ITmfLocation location) { |
73005152 | 247 | fLock.lock(); |
c76c54bb | 248 | try { |
085d898f FC |
249 | if (fTrace != null) |
250 | if (location.getLocation() instanceof Long) | |
73005152 | 251 | return (double) ((Long) location.getLocation()) / fTrace.length(); |
085d898f | 252 | } catch (final IOException e) { |
c76c54bb | 253 | e.printStackTrace(); |
73005152 BH |
254 | } finally { |
255 | fLock.unlock(); | |
c76c54bb FC |
256 | } |
257 | return 0; | |
258 | } | |
259 | ||
4e3aa37d | 260 | @Override |
085d898f | 261 | public TmfLocation<Long> getCurrentLocation() { |
73005152 | 262 | fLock.lock(); |
d18dd09b | 263 | try { |
085d898f | 264 | if (fTrace != null) |
73005152 | 265 | return new TmfLocation<Long>(fTrace.getFilePointer()); |
085d898f FC |
266 | } catch (final IOException e) { |
267 | e.printStackTrace(); | |
268 | } finally { | |
269 | fLock.unlock(); | |
270 | } | |
271 | return null; | |
272 | } | |
273 | ||
274 | @Override | |
275 | public ITmfEvent parseEvent(final ITmfContext context) { | |
276 | fLock.lock(); | |
277 | try { | |
278 | // parseNextEvent will update the context | |
279 | if (fTrace != null) { | |
7e6347b0 | 280 | final ITmfEvent event = fParser.parseEvent(context.clone()); |
085d898f | 281 | return event; |
73005152 | 282 | } |
7e6347b0 FC |
283 | // } |
284 | // catch (final IOException e) { | |
285 | // e.printStackTrace(); | |
73005152 BH |
286 | } finally { |
287 | fLock.unlock(); | |
d18dd09b ASL |
288 | } |
289 | return null; | |
290 | } | |
291 | ||
085d898f FC |
292 | @Override |
293 | public void setTimeRange(final TmfTimeRange range) { | |
294 | super.setTimeRange(range); | |
295 | } | |
d18dd09b | 296 | |
085d898f FC |
297 | @Override |
298 | public void setStartTime(final ITmfTimestamp startTime) { | |
299 | super.setStartTime(startTime); | |
ff4ed569 FC |
300 | } |
301 | ||
085d898f FC |
302 | @Override |
303 | public void setEndTime(final ITmfTimestamp endTime) { | |
304 | super.setEndTime(endTime); | |
ff4ed569 FC |
305 | } |
306 | ||
1703b536 FC |
307 | @Override |
308 | public void setStreamingInterval(final long interval) { | |
309 | super.setStreamingInterval(interval); | |
310 | } | |
311 | ||
085d898f FC |
312 | @Override |
313 | public void dispose() { | |
314 | fLock.lock(); | |
315 | try { | |
316 | if (fTrace != null) { | |
317 | fTrace.close(); | |
318 | fTrace = null; | |
319 | } | |
320 | } catch (final IOException e) { | |
321 | // Ignore | |
322 | } finally { | |
323 | fLock.unlock(); | |
324 | } | |
325 | super.dispose(); | |
ff4ed569 FC |
326 | } |
327 | ||
e31e01e8 | 328 | } |