tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
CommitLineData
d18dd09b 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2009, 2014 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
ea271da6 11 * Patrick Tasse - Updated for removal of context clone
d18dd09b
ASL
12 *******************************************************************************/
13
4918b8f2 14package org.eclipse.linuxtools.tmf.tests.stubs.trace;
d18dd09b
ASL
15
16import java.io.FileNotFoundException;
17import java.io.IOException;
18import java.io.RandomAccessFile;
032ecd45 19import java.nio.ByteBuffer;
73005152 20import java.util.concurrent.locks.ReentrantLock;
d18dd09b 21
2352aed9 22import org.eclipse.core.resources.IProject;
20658947 23import org.eclipse.core.resources.IResource;
a94410d9
MK
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.core.runtime.Status;
26import org.eclipse.linuxtools.internal.tmf.core.Activator;
6256d8ad 27import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
b4f71e4a 28import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
b5ddd705
GB
29import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
30import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
31import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
3bd46eef
AM
32import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
33import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
032ecd45 34import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
6c13869b 35import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
7e6347b0 36import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
6c13869b 37import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
6c13869b 38import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
032ecd45 39import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
032ecd45
MAL
40import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
41import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
a3db8436
AM
42import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
43import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
d18dd09b
ASL
44
45/**
46 * <b><u>TmfTraceStub</u></b>
47 * <p>
8d2e2848 48 * Dummy test trace. Use in conjunction with TmfEventParserStub.
d18dd09b 49 */
032ecd45 50public class TmfTraceStub extends TmfTrace implements ITmfEventParser, ITmfPersistentlyIndexable {
d18dd09b 51
e31e01e8 52 // ------------------------------------------------------------------------
d18dd09b 53 // Attributes
e31e01e8 54 // ------------------------------------------------------------------------
d18dd09b
ASL
55
56 // The actual stream
ff4ed569 57 private RandomAccessFile fTrace;
d18dd09b 58
7e6347b0
FC
59// // The associated event parser
60// private ITmfEventParser<TmfEvent> fParser;
d18dd09b 61
73005152 62 // The synchronization lock
085d898f
FC
63 private final ReentrantLock fLock = new ReentrantLock();
64
66262ad8
BH
65 private ITmfTimestamp fInitialRangeOffset = null;
66
e31e01e8 67 // ------------------------------------------------------------------------
d18dd09b 68 // Constructors
e31e01e8 69 // ------------------------------------------------------------------------
d18dd09b 70
ff0960ac
AM
71 /**
72 * Default constructor
73 */
20658947
FC
74 public TmfTraceStub() {
75 super();
0316808c 76 setParser(new TmfEventParserStub(this));
20658947
FC
77 }
78
d18dd09b 79 /**
ff0960ac
AM
80 * Constructor with which you can specify a custom streaming interval. The
81 * parser and indexer won't be specified.
82 *
3ef62bac 83 * @param path
ff0960ac 84 * The path to the trace file
e31e01e8 85 * @param cacheSize
ff0960ac
AM
86 * The cache size
87 * @param interval
88 * The trace streaming interval
89 * @throws TmfTraceException
90 * If an error occurred opening the trace
8d2e2848 91 */
ff0960ac
AM
92 public TmfTraceStub(final String path,
93 final int cacheSize,
94 final long interval) throws TmfTraceException {
ab186fbb 95 super(null, ITmfEvent.class, path, cacheSize, interval, null);
ff0960ac 96 setupTrace(path);
0316808c 97 setParser(new TmfEventParserStub(this));
1703b536
FC
98 }
99
20658947 100 /**
ff0960ac
AM
101 * Constructor to specify the parser and indexer. The streaming interval
102 * will be 0.
103 *
20658947 104 * @param path
ff0960ac 105 * The path to the trace file
20658947 106 * @param cacheSize
ff0960ac 107 * The cache size
ff4ed569 108 * @param waitForCompletion
ff0960ac 109 * Do we block the caller until the trace is indexed, or not.
ff0960ac
AM
110 * @param parser
111 * The trace parser. If left 'null', it will use a
112 * {@link TmfEventParserStub}.
113 * @throws TmfTraceException
114 * If an error occurred opening the trace
8d2e2848 115 */
ff0960ac
AM
116 public TmfTraceStub(final String path,
117 final int cacheSize,
118 final boolean waitForCompletion,
ff0960ac 119 final ITmfEventParser parser) throws TmfTraceException {
ab186fbb 120 super(null, ITmfEvent.class, path, cacheSize, 0, null);
ff0960ac
AM
121 setupTrace(path);
122 setParser((parser != null) ? parser : new TmfEventParserStub(this));
07671572 123 if (waitForCompletion) {
51e75066 124 indexTrace(true);
07671572 125 }
d18dd09b
ASL
126 }
127
20658947 128 /**
ff0960ac
AM
129 * Copy constructor
130 *
131 * @param trace
132 * The trace to copy
133 * @throws TmfTraceException
134 * If an error occurred opening the trace
20658947 135 */
ff0960ac
AM
136 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
137 super(trace);
138 setupTrace(getPath()); // fPath will be set by the super-constructor
0316808c 139 setParser(new TmfEventParserStub(this));
20658947 140 }
085d898f 141
ff0960ac
AM
142
143 private void setupTrace(String path) throws TmfTraceException {
b4f71e4a 144 try {
a94410d9 145 fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
b4f71e4a
FC
146 } catch (FileNotFoundException e) {
147 throw new TmfTraceException(e.getMessage());
148 }
73005152 149 }
085d898f 150
ff0960ac
AM
151 // ------------------------------------------------------------------------
152 // Initializers
153 // ------------------------------------------------------------------------
085d898f 154
20658947 155 @Override
6256d8ad 156 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
b4f71e4a 157 try {
a94410d9 158 fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
b4f71e4a
FC
159 } catch (FileNotFoundException e) {
160 throw new TmfTraceException(e.getMessage());
161 }
0316808c 162 setParser(new TmfEventParserStub(this));
20658947
FC
163 super.initTrace(resource, path, type);
164 }
f17b2f70 165
1703b536 166 @Override
6256d8ad 167 public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
1703b536
FC
168 super.initialize(resource, path, type);
169 }
170
e31e01e8 171 // ------------------------------------------------------------------------
d18dd09b 172 // Accessors
e31e01e8 173 // ------------------------------------------------------------------------
d18dd09b 174
ff0960ac
AM
175 /**
176 * @return The file stream to the trace
177 */
d18dd09b
ASL
178 public RandomAccessFile getStream() {
179 return fTrace;
180 }
181
ff0960ac
AM
182 /**
183 * Set the initial range offset.
184 *
185 * @param initOffset
186 * The new initial range offset
187 */
66262ad8
BH
188 public void setInitialRangeOffset(ITmfTimestamp initOffset) {
189 fInitialRangeOffset = initOffset;
190 }
191
192 @Override
193 public ITmfTimestamp getInitialRangeOffset() {
194 if (fInitialRangeOffset != null) {
195 return fInitialRangeOffset;
196 }
197 return super.getInitialRangeOffset();
198 }
199
e31e01e8 200 // ------------------------------------------------------------------------
d18dd09b 201 // Operators
e31e01e8 202 // ------------------------------------------------------------------------
d18dd09b 203
085d898f 204 @Override
1e1bef82 205 public TmfContext seekEvent(final ITmfLocation location) {
d18dd09b 206 try {
09e86496
FC
207 fLock.lock();
208 try {
209 if (fTrace != null) {
210 // Position the trace at the requested location and
211 // returns the corresponding context
212 long loc = 0;
213 long rank = 0;
214 if (location != null) {
5976d44a 215 loc = (Long) location.getLocationInfo();
09e86496
FC
216 rank = ITmfContext.UNKNOWN_RANK;
217 }
20658947 218 if (loc != fTrace.getFilePointer()) {
09e86496 219 fTrace.seek(loc);
20658947 220 }
09e86496
FC
221 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
222 return context;
73005152 223 }
09e86496
FC
224 } catch (final IOException e) {
225 e.printStackTrace();
226 } catch (final NullPointerException e) {
227 e.printStackTrace();
73005152 228 }
3427112b
FC
229 finally{
230 fLock.unlock();
231 }
09e86496 232 } catch (final NullPointerException e) {
085d898f
FC
233 e.printStackTrace();
234 }
085d898f 235 return null;
d18dd09b
ASL
236 }
237
c76c54bb 238
085d898f 239 @Override
7e6347b0 240 public TmfContext seekEvent(final double ratio) {
085d898f 241 fLock.lock();
c76c54bb 242 try {
73005152 243 if (fTrace != null) {
15e89960 244 final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
7e6347b0 245 final TmfContext context = seekEvent(location);
73005152
BH
246 context.setRank(ITmfContext.UNKNOWN_RANK);
247 return context;
248 }
085d898f 249 } catch (final IOException e) {
c76c54bb 250 e.printStackTrace();
73005152
BH
251 } finally {
252 fLock.unlock();
c76c54bb 253 }
085d898f 254
c76c54bb
FC
255 return null;
256 }
257
258 @Override
1e1bef82 259 public double getLocationRatio(ITmfLocation location) {
73005152 260 fLock.lock();
c76c54bb 261 try {
0283f7ff 262 if (fTrace != null) {
5976d44a 263 if (location.getLocationInfo() instanceof Long) {
0126a8ca 264 return ((Long) location.getLocationInfo()).doubleValue() / fTrace.length();
0283f7ff
FC
265 }
266 }
085d898f 267 } catch (final IOException e) {
c76c54bb 268 e.printStackTrace();
73005152
BH
269 } finally {
270 fLock.unlock();
c76c54bb
FC
271 }
272 return 0;
273 }
274
4e3aa37d 275 @Override
1e1bef82 276 public ITmfLocation getCurrentLocation() {
73005152 277 fLock.lock();
d18dd09b 278 try {
0283f7ff 279 if (fTrace != null) {
cb8c854e 280 return new TmfLongLocation(fTrace.getFilePointer());
0283f7ff 281 }
085d898f
FC
282 } catch (final IOException e) {
283 e.printStackTrace();
284 } finally {
285 fLock.unlock();
286 }
287 return null;
288 }
289
290 @Override
6256d8ad 291 public ITmfEvent parseEvent(final ITmfContext context) {
085d898f
FC
292 fLock.lock();
293 try {
294 // parseNextEvent will update the context
0316808c 295 if (fTrace != null && getParser() != null && context != null) {
ea271da6 296 final ITmfEvent event = getParser().parseEvent(context);
085d898f 297 return event;
73005152 298 }
73005152
BH
299 } finally {
300 fLock.unlock();
d18dd09b
ASL
301 }
302 return null;
303 }
304
b75d6b65 305 @Override
9b749023 306 public synchronized void setNbEvents(final long nbEvents) {
b75d6b65
FC
307 super.setNbEvents(nbEvents);
308 }
309
085d898f
FC
310 @Override
311 public void setTimeRange(final TmfTimeRange range) {
312 super.setTimeRange(range);
313 }
d18dd09b 314
085d898f
FC
315 @Override
316 public void setStartTime(final ITmfTimestamp startTime) {
317 super.setStartTime(startTime);
ff4ed569
FC
318 }
319
085d898f
FC
320 @Override
321 public void setEndTime(final ITmfTimestamp endTime) {
322 super.setEndTime(endTime);
ff4ed569
FC
323 }
324
1703b536
FC
325 @Override
326 public void setStreamingInterval(final long interval) {
327 super.setStreamingInterval(interval);
328 }
329
085d898f 330 @Override
9b749023 331 public synchronized void dispose() {
085d898f
FC
332 fLock.lock();
333 try {
334 if (fTrace != null) {
335 fTrace.close();
336 fTrace = null;
337 }
338 } catch (final IOException e) {
339 // Ignore
340 } finally {
341 fLock.unlock();
342 }
343 super.dispose();
ff4ed569
FC
344 }
345
2352aed9 346 @Override
a94410d9
MK
347 public IStatus validate(IProject project, String path) {
348 if (fileExists(path)) {
349 return Status.OK_STATUS;
350 }
94d4821e 351 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "File does not exist: " + path);
2352aed9
FC
352 }
353
032ecd45
MAL
354 private static int fCheckpointSize = -1;
355
356 @Override
357 public synchronized int getCheckpointSize() {
358 if (fCheckpointSize == -1) {
359 TmfCheckpoint c = new TmfCheckpoint(new TmfTimestamp(0L), new TmfLongLocation(0L), 0);
360 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
361 b.clear();
362 c.serialize(b);
363 fCheckpointSize = b.position();
364 }
365
366 return fCheckpointSize;
367 }
368
369 @Override
370 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
371 return new TmfLongLocation(bufferIn);
372 }
b5ddd705
GB
373
374 /**
375 * Simulate trace opening, to be called by tests who need an actively opened
376 * trace
377 */
378 public void openTrace() {
379 TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, this, null));
380 selectTrace();
381 }
382
383 /**
384 * Simulate selecting the trace
385 */
386 public void selectTrace() {
387 TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, this));
388 }
9b749023 389}
This page took 0.076846 seconds and 5 git commands to generate.