Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
0283f7ff | 3 | * |
d18dd09b ASL |
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 | |
0283f7ff | 8 | * |
d18dd09b ASL |
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 | |
2352aed9 | 20 | import org.eclipse.core.resources.IProject; |
20658947 | 21 | import org.eclipse.core.resources.IResource; |
6256d8ad | 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
dfee01ae | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b | 24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
b4f71e4a | 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
0316808c | 29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
20658947 | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer; |
6c13869b | 31 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
cb8c854e | 32 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; |
6c13869b | 33 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
d18dd09b ASL |
34 | |
35 | /** | |
36 | * <b><u>TmfTraceStub</u></b> | |
37 | * <p> | |
8d2e2848 | 38 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 39 | */ |
54a7a54c | 40 | @SuppressWarnings({"nls","javadoc"}) |
6256d8ad | 41 | public class TmfTraceStub extends TmfTrace implements ITmfEventParser { |
d18dd09b | 42 | |
e31e01e8 | 43 | // ------------------------------------------------------------------------ |
d18dd09b | 44 | // Attributes |
e31e01e8 | 45 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
46 | |
47 | // The actual stream | |
ff4ed569 | 48 | private RandomAccessFile fTrace; |
d18dd09b | 49 | |
7e6347b0 FC |
50 | // // The associated event parser |
51 | // private ITmfEventParser<TmfEvent> fParser; | |
d18dd09b | 52 | |
73005152 | 53 | // The synchronization lock |
085d898f FC |
54 | private final ReentrantLock fLock = new ReentrantLock(); |
55 | ||
66262ad8 BH |
56 | private ITmfTimestamp fInitialRangeOffset = null; |
57 | ||
e31e01e8 | 58 | // ------------------------------------------------------------------------ |
d18dd09b | 59 | // Constructors |
e31e01e8 | 60 | // ------------------------------------------------------------------------ |
d18dd09b | 61 | |
20658947 FC |
62 | public TmfTraceStub() { |
63 | super(); | |
0316808c | 64 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
65 | } |
66 | ||
d18dd09b | 67 | /** |
3ef62bac | 68 | * @param path |
d18dd09b ASL |
69 | * @throws FileNotFoundException |
70 | */ | |
b4f71e4a | 71 | public TmfTraceStub(final String path) throws TmfTraceException { |
0316808c | 72 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false); |
8d2e2848 FC |
73 | } |
74 | ||
75 | /** | |
3ef62bac | 76 | * @param path |
e31e01e8 | 77 | * @param cacheSize |
8d2e2848 FC |
78 | * @throws FileNotFoundException |
79 | */ | |
b4f71e4a | 80 | public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException { |
3ef62bac | 81 | this(path, cacheSize, false); |
d18dd09b ASL |
82 | } |
83 | ||
1703b536 FC |
84 | /** |
85 | * @param path | |
86 | * @param cacheSize | |
87 | * @throws FileNotFoundException | |
88 | */ | |
b4f71e4a | 89 | public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException { |
2771b032 | 90 | super(null, ITmfEvent.class, path, cacheSize, interval); |
b4f71e4a FC |
91 | try { |
92 | fTrace = new RandomAccessFile(path, "r"); | |
93 | } catch (FileNotFoundException e) { | |
94 | throw new TmfTraceException(e.getMessage()); | |
95 | } | |
0316808c | 96 | setParser(new TmfEventParserStub(this)); |
1703b536 FC |
97 | } |
98 | ||
20658947 FC |
99 | /** |
100 | * @param path | |
101 | * @param cacheSize | |
102 | * @throws FileNotFoundException | |
103 | */ | |
6256d8ad | 104 | public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException { |
20658947 FC |
105 | this(path, cacheSize, false, null, indexer); |
106 | } | |
107 | ||
d18dd09b | 108 | /** |
3ef62bac | 109 | * @param path |
ff4ed569 | 110 | * @param waitForCompletion |
d18dd09b ASL |
111 | * @throws FileNotFoundException |
112 | */ | |
b4f71e4a | 113 | public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException { |
0316808c | 114 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion); |
8d2e2848 | 115 | } |
085d898f | 116 | |
8d2e2848 | 117 | /** |
3ef62bac | 118 | * @param path |
8d2e2848 | 119 | * @param cacheSize |
ff4ed569 | 120 | * @param waitForCompletion |
8d2e2848 FC |
121 | * @throws FileNotFoundException |
122 | */ | |
b4f71e4a | 123 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
2771b032 | 124 | super(null, ITmfEvent.class, path, cacheSize); |
b4f71e4a FC |
125 | try { |
126 | fTrace = new RandomAccessFile(path, "r"); | |
127 | } catch (FileNotFoundException e) { | |
128 | throw new TmfTraceException(e.getMessage()); | |
129 | } | |
0316808c | 130 | setParser(new TmfEventParserStub(this)); |
07671572 FC |
131 | if (waitForCompletion) { |
132 | indexTrace(); | |
133 | } | |
d18dd09b ASL |
134 | } |
135 | ||
20658947 FC |
136 | /** |
137 | * @param path | |
138 | * @param cacheSize | |
139 | * @param waitForCompletion | |
140 | * @throws FileNotFoundException | |
141 | */ | |
b4f71e4a | 142 | public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
2771b032 | 143 | super(resource, ITmfEvent.class, path, cacheSize); |
b4f71e4a FC |
144 | try { |
145 | fTrace = new RandomAccessFile(path, "r"); | |
146 | } catch (FileNotFoundException e) { | |
147 | throw new TmfTraceException(e.getMessage()); | |
148 | } | |
0316808c | 149 | setParser(new TmfEventParserStub(this)); |
20658947 | 150 | } |
085d898f | 151 | |
73005152 | 152 | /** |
3ef62bac | 153 | * @param path |
73005152 BH |
154 | * @param cacheSize |
155 | * @param waitForCompletion | |
156 | * @param parser | |
157 | * @throws FileNotFoundException | |
158 | */ | |
20658947 | 159 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, |
6256d8ad | 160 | final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException { |
2771b032 | 161 | super(null, ITmfEvent.class, path, cacheSize, 0, indexer); |
b4f71e4a FC |
162 | try { |
163 | fTrace = new RandomAccessFile(path, "r"); | |
164 | } catch (FileNotFoundException e) { | |
165 | throw new TmfTraceException(e.getMessage()); | |
166 | } | |
0316808c | 167 | setParser((parser != null) ? parser : new TmfEventParserStub(this)); |
73005152 | 168 | } |
085d898f | 169 | |
ff4ed569 | 170 | /** |
f17b2f70 | 171 | * Copy constructor |
ff4ed569 | 172 | */ |
b4f71e4a | 173 | public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException { |
20658947 | 174 | super(trace); |
b4f71e4a FC |
175 | try { |
176 | fTrace = new RandomAccessFile(getPath(), "r"); | |
177 | } catch (FileNotFoundException e) { | |
178 | throw new TmfTraceException(e.getMessage()); | |
179 | } | |
0316808c | 180 | setParser(new TmfEventParserStub(this)); |
ff4ed569 | 181 | } |
085d898f | 182 | |
20658947 | 183 | public void indexTrace() { |
c7e1020d | 184 | indexTrace(true); |
20658947 FC |
185 | } |
186 | ||
187 | @Override | |
6256d8ad | 188 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
b4f71e4a FC |
189 | try { |
190 | fTrace = new RandomAccessFile(path, "r"); | |
191 | } catch (FileNotFoundException e) { | |
192 | throw new TmfTraceException(e.getMessage()); | |
193 | } | |
0316808c | 194 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
195 | super.initTrace(resource, path, type); |
196 | } | |
f17b2f70 | 197 | |
1703b536 | 198 | @Override |
6256d8ad | 199 | public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
1703b536 FC |
200 | super.initialize(resource, path, type); |
201 | } | |
202 | ||
e31e01e8 | 203 | // ------------------------------------------------------------------------ |
d18dd09b | 204 | // Accessors |
e31e01e8 | 205 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
206 | |
207 | public RandomAccessFile getStream() { | |
208 | return fTrace; | |
209 | } | |
210 | ||
66262ad8 BH |
211 | public void setInitialRangeOffset(ITmfTimestamp initOffset) { |
212 | fInitialRangeOffset = initOffset; | |
213 | } | |
214 | ||
215 | @Override | |
216 | public ITmfTimestamp getInitialRangeOffset() { | |
217 | if (fInitialRangeOffset != null) { | |
218 | return fInitialRangeOffset; | |
219 | } | |
220 | return super.getInitialRangeOffset(); | |
221 | } | |
222 | ||
e31e01e8 | 223 | // ------------------------------------------------------------------------ |
d18dd09b | 224 | // Operators |
e31e01e8 | 225 | // ------------------------------------------------------------------------ |
d18dd09b | 226 | |
085d898f | 227 | @Override |
1e1bef82 | 228 | public TmfContext seekEvent(final ITmfLocation location) { |
d18dd09b | 229 | try { |
09e86496 FC |
230 | fLock.lock(); |
231 | try { | |
232 | if (fTrace != null) { | |
233 | // Position the trace at the requested location and | |
234 | // returns the corresponding context | |
235 | long loc = 0; | |
236 | long rank = 0; | |
237 | if (location != null) { | |
5976d44a | 238 | loc = (Long) location.getLocationInfo(); |
09e86496 FC |
239 | rank = ITmfContext.UNKNOWN_RANK; |
240 | } | |
20658947 | 241 | if (loc != fTrace.getFilePointer()) { |
09e86496 | 242 | fTrace.seek(loc); |
20658947 | 243 | } |
09e86496 FC |
244 | final TmfContext context = new TmfContext(getCurrentLocation(), rank); |
245 | return context; | |
73005152 | 246 | } |
09e86496 FC |
247 | } catch (final IOException e) { |
248 | e.printStackTrace(); | |
249 | } catch (final NullPointerException e) { | |
250 | e.printStackTrace(); | |
73005152 | 251 | } |
3427112b FC |
252 | finally{ |
253 | fLock.unlock(); | |
254 | } | |
09e86496 | 255 | } catch (final NullPointerException e) { |
085d898f FC |
256 | e.printStackTrace(); |
257 | } | |
085d898f | 258 | return null; |
d18dd09b ASL |
259 | } |
260 | ||
c76c54bb | 261 | |
085d898f | 262 | @Override |
7e6347b0 | 263 | public TmfContext seekEvent(final double ratio) { |
085d898f | 264 | fLock.lock(); |
c76c54bb | 265 | try { |
73005152 | 266 | if (fTrace != null) { |
15e89960 | 267 | final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length()))); |
7e6347b0 | 268 | final TmfContext context = seekEvent(location); |
73005152 BH |
269 | context.setRank(ITmfContext.UNKNOWN_RANK); |
270 | return context; | |
271 | } | |
085d898f | 272 | } catch (final IOException e) { |
c76c54bb | 273 | e.printStackTrace(); |
73005152 BH |
274 | } finally { |
275 | fLock.unlock(); | |
c76c54bb | 276 | } |
085d898f | 277 | |
c76c54bb FC |
278 | return null; |
279 | } | |
280 | ||
281 | @Override | |
1e1bef82 | 282 | public double getLocationRatio(ITmfLocation location) { |
73005152 | 283 | fLock.lock(); |
c76c54bb | 284 | try { |
0283f7ff | 285 | if (fTrace != null) { |
5976d44a FC |
286 | if (location.getLocationInfo() instanceof Long) { |
287 | return (double) ((Long) location.getLocationInfo()) / fTrace.length(); | |
0283f7ff FC |
288 | } |
289 | } | |
085d898f | 290 | } catch (final IOException e) { |
c76c54bb | 291 | e.printStackTrace(); |
73005152 BH |
292 | } finally { |
293 | fLock.unlock(); | |
c76c54bb FC |
294 | } |
295 | return 0; | |
296 | } | |
297 | ||
4e3aa37d | 298 | @Override |
1e1bef82 | 299 | public ITmfLocation getCurrentLocation() { |
73005152 | 300 | fLock.lock(); |
d18dd09b | 301 | try { |
0283f7ff | 302 | if (fTrace != null) { |
cb8c854e | 303 | return new TmfLongLocation(fTrace.getFilePointer()); |
0283f7ff | 304 | } |
085d898f FC |
305 | } catch (final IOException e) { |
306 | e.printStackTrace(); | |
307 | } finally { | |
308 | fLock.unlock(); | |
309 | } | |
310 | return null; | |
311 | } | |
312 | ||
313 | @Override | |
6256d8ad | 314 | public ITmfEvent parseEvent(final ITmfContext context) { |
085d898f FC |
315 | fLock.lock(); |
316 | try { | |
317 | // parseNextEvent will update the context | |
0316808c | 318 | if (fTrace != null && getParser() != null && context != null) { |
6256d8ad | 319 | final ITmfEvent event = getParser().parseEvent(context.clone()); |
085d898f | 320 | return event; |
73005152 | 321 | } |
73005152 BH |
322 | } finally { |
323 | fLock.unlock(); | |
d18dd09b ASL |
324 | } |
325 | return null; | |
326 | } | |
327 | ||
b75d6b65 | 328 | @Override |
9b749023 | 329 | public synchronized void setNbEvents(final long nbEvents) { |
b75d6b65 FC |
330 | super.setNbEvents(nbEvents); |
331 | } | |
332 | ||
085d898f FC |
333 | @Override |
334 | public void setTimeRange(final TmfTimeRange range) { | |
335 | super.setTimeRange(range); | |
336 | } | |
d18dd09b | 337 | |
085d898f FC |
338 | @Override |
339 | public void setStartTime(final ITmfTimestamp startTime) { | |
340 | super.setStartTime(startTime); | |
ff4ed569 FC |
341 | } |
342 | ||
085d898f FC |
343 | @Override |
344 | public void setEndTime(final ITmfTimestamp endTime) { | |
345 | super.setEndTime(endTime); | |
ff4ed569 FC |
346 | } |
347 | ||
1703b536 FC |
348 | @Override |
349 | public void setStreamingInterval(final long interval) { | |
350 | super.setStreamingInterval(interval); | |
351 | } | |
352 | ||
085d898f | 353 | @Override |
9b749023 | 354 | public synchronized void dispose() { |
085d898f FC |
355 | fLock.lock(); |
356 | try { | |
357 | if (fTrace != null) { | |
358 | fTrace.close(); | |
359 | fTrace = null; | |
360 | } | |
361 | } catch (final IOException e) { | |
362 | // Ignore | |
363 | } finally { | |
364 | fLock.unlock(); | |
365 | } | |
366 | super.dispose(); | |
ff4ed569 FC |
367 | } |
368 | ||
2352aed9 FC |
369 | /* (non-Javadoc) |
370 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String) | |
371 | */ | |
372 | @Override | |
373 | public boolean validate(IProject project, String path) { | |
374 | return fileExists(path); | |
375 | } | |
376 | ||
9b749023 | 377 | } |