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 | ||
13 | package org.eclipse.linuxtools.tmf.trace; | |
14 | ||
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | ||
19 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
e31e01e8 | 20 | import org.eclipse.linuxtools.tmf.parser.ITmfEventParser; |
d18dd09b ASL |
21 | |
22 | /** | |
23 | * <b><u>TmfTraceStub</u></b> | |
24 | * <p> | |
8d2e2848 | 25 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 26 | */ |
e31e01e8 | 27 | public class TmfTraceStub extends TmfTrace<TmfEvent> { |
d18dd09b | 28 | |
e31e01e8 | 29 | // ------------------------------------------------------------------------ |
d18dd09b | 30 | // Attributes |
e31e01e8 | 31 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
32 | |
33 | // The actual stream | |
146a887c | 34 | private final RandomAccessFile fTrace; |
d18dd09b ASL |
35 | |
36 | // The associated event parser | |
146a887c | 37 | private final ITmfEventParser fParser; |
d18dd09b | 38 | |
e31e01e8 | 39 | // ------------------------------------------------------------------------ |
d18dd09b | 40 | // Constructors |
e31e01e8 | 41 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
42 | |
43 | /** | |
44 | * @param filename | |
45 | * @throws FileNotFoundException | |
46 | */ | |
47 | public TmfTraceStub(String filename) throws FileNotFoundException { | |
8d2e2848 FC |
48 | this(filename, DEFAULT_CACHE_SIZE, false); |
49 | } | |
50 | ||
51 | /** | |
52 | * @param filename | |
e31e01e8 | 53 | * @param cacheSize |
8d2e2848 FC |
54 | * @throws FileNotFoundException |
55 | */ | |
e31e01e8 FC |
56 | public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException { |
57 | this(filename, cacheSize, false); | |
d18dd09b ASL |
58 | } |
59 | ||
60 | /** | |
61 | * @param filename | |
d18dd09b ASL |
62 | * @throws FileNotFoundException |
63 | */ | |
e31e01e8 FC |
64 | public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException { |
65 | this(filename, DEFAULT_CACHE_SIZE, waitForCompletion); | |
8d2e2848 FC |
66 | } |
67 | ||
68 | /** | |
69 | * @param filename | |
70 | * @param cacheSize | |
71 | * @throws FileNotFoundException | |
72 | */ | |
73 | public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException { | |
e31e01e8 | 74 | super(TmfEvent.class, filename, cacheSize); |
d18dd09b ASL |
75 | fTrace = new RandomAccessFile(filename, "r"); |
76 | fParser = new TmfEventParserStub(); | |
e31e01e8 | 77 | indexTrace(waitForCompletion); |
d18dd09b ASL |
78 | } |
79 | ||
e31e01e8 | 80 | // ------------------------------------------------------------------------ |
d18dd09b | 81 | // Accessors |
e31e01e8 | 82 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
83 | |
84 | public RandomAccessFile getStream() { | |
85 | return fTrace; | |
86 | } | |
87 | ||
e31e01e8 | 88 | // ------------------------------------------------------------------------ |
d18dd09b | 89 | // Operators |
e31e01e8 | 90 | // ------------------------------------------------------------------------ |
d18dd09b | 91 | |
146a887c FC |
92 | /* (non-Javadoc) |
93 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#seekLocation(java.lang.Object) | |
94 | */ | |
4e3aa37d | 95 | public TmfTraceContext seekLocation(Object location) { |
d18dd09b | 96 | try { |
4e3aa37d | 97 | synchronized(fTrace) { |
e31e01e8 FC |
98 | // Position the trace, read the event (to obtain its timestamp) |
99 | // and then re-position the trace (not great...) | |
4e3aa37d | 100 | fTrace.seek((location != null) ? (Long) location : 0); |
e31e01e8 FC |
101 | TmfTraceContext context = new TmfTraceContext(getCurrentLocation()); |
102 | return context; | |
4e3aa37d | 103 | } |
d18dd09b ASL |
104 | } catch (IOException e) { |
105 | e.printStackTrace(); | |
106 | } | |
e31e01e8 | 107 | return null; |
d18dd09b ASL |
108 | } |
109 | ||
146a887c FC |
110 | /* (non-Javadoc) |
111 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#getCurrentLocation() | |
112 | */ | |
4e3aa37d FC |
113 | @Override |
114 | public Object getCurrentLocation() { | |
d18dd09b | 115 | try { |
146a887c | 116 | return new Long(fTrace.getFilePointer()); |
d18dd09b | 117 | } catch (IOException e) { |
146a887c | 118 | // TODO Auto-generated catch block |
d18dd09b ASL |
119 | e.printStackTrace(); |
120 | } | |
121 | return null; | |
122 | } | |
123 | ||
146a887c FC |
124 | /* (non-Javadoc) |
125 | * @see org.eclipse.linuxtools.tmf.trace.TmfTrace#parseEvent() | |
126 | */ | |
4e3aa37d | 127 | @Override |
8d2e2848 | 128 | public TmfEvent parseEvent(TmfTraceContext context) { |
cc6eec3e | 129 | try { |
8d2e2848 FC |
130 | // paserNextEvent updates the context |
131 | TmfEvent event = fParser.parseNextEvent(this, context); | |
cc6eec3e FC |
132 | return event; |
133 | } | |
134 | catch (IOException e) { | |
135 | e.printStackTrace(); | |
136 | } | |
137 | return null; | |
d18dd09b ASL |
138 | } |
139 | ||
e31e01e8 | 140 | } |