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 | |
72f1e62a | 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
dfee01ae | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
22 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
23 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
6c13869b FC |
24 | import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser; |
25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; | |
6c13869b FC |
27 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
28 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; | |
29 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; | |
d18dd09b ASL |
30 | |
31 | /** | |
32 | * <b><u>TmfTraceStub</u></b> | |
33 | * <p> | |
8d2e2848 | 34 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 35 | */ |
3b38ea61 | 36 | @SuppressWarnings("nls") |
e31e01e8 | 37 | public class TmfTraceStub extends TmfTrace<TmfEvent> { |
d18dd09b | 38 | |
e31e01e8 | 39 | // ------------------------------------------------------------------------ |
d18dd09b | 40 | // Attributes |
e31e01e8 | 41 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
42 | |
43 | // The actual stream | |
ff4ed569 | 44 | private RandomAccessFile fTrace; |
d18dd09b ASL |
45 | |
46 | // The associated event parser | |
72f1e62a | 47 | private ITmfEventParser<TmfEvent> fParser; |
d18dd09b | 48 | |
73005152 | 49 | // The synchronization lock |
085d898f FC |
50 | private final ReentrantLock fLock = new ReentrantLock(); |
51 | ||
e31e01e8 | 52 | // ------------------------------------------------------------------------ |
d18dd09b | 53 | // Constructors |
e31e01e8 | 54 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
55 | |
56 | /** | |
3ef62bac | 57 | * @param path |
d18dd09b ASL |
58 | * @throws FileNotFoundException |
59 | */ | |
085d898f | 60 | public TmfTraceStub(final String path) throws FileNotFoundException { |
3ef62bac | 61 | this(path, DEFAULT_INDEX_PAGE_SIZE, false); |
8d2e2848 FC |
62 | } |
63 | ||
64 | /** | |
3ef62bac | 65 | * @param path |
e31e01e8 | 66 | * @param cacheSize |
8d2e2848 FC |
67 | * @throws FileNotFoundException |
68 | */ | |
085d898f | 69 | public TmfTraceStub(final String path, final int cacheSize) throws FileNotFoundException { |
3ef62bac | 70 | this(path, cacheSize, false); |
d18dd09b ASL |
71 | } |
72 | ||
73 | /** | |
3ef62bac | 74 | * @param path |
ff4ed569 | 75 | * @param waitForCompletion |
d18dd09b ASL |
76 | * @throws FileNotFoundException |
77 | */ | |
085d898f | 78 | public TmfTraceStub(final String path, final boolean waitForCompletion) throws FileNotFoundException { |
3ef62bac | 79 | this(path, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion); |
8d2e2848 | 80 | } |
085d898f | 81 | |
8d2e2848 | 82 | /** |
3ef62bac | 83 | * @param path |
8d2e2848 | 84 | * @param cacheSize |
ff4ed569 | 85 | * @param waitForCompletion |
8d2e2848 FC |
86 | * @throws FileNotFoundException |
87 | */ | |
085d898f | 88 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException { |
3ef62bac FC |
89 | super(null, TmfEvent.class, path, cacheSize, false); |
90 | fTrace = new RandomAccessFile(path, "r"); | |
d18dd09b ASL |
91 | fParser = new TmfEventParserStub(); |
92 | } | |
93 | ||
085d898f | 94 | |
73005152 | 95 | /** |
3ef62bac | 96 | * @param path |
73005152 BH |
97 | * @param cacheSize |
98 | * @param waitForCompletion | |
99 | * @param parser | |
100 | * @throws FileNotFoundException | |
101 | */ | |
085d898f | 102 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, final ITmfEventParser<TmfEvent> parser) throws FileNotFoundException { |
3ef62bac FC |
103 | super(path, TmfEvent.class, path, cacheSize, false); |
104 | fTrace = new RandomAccessFile(path, "r"); | |
73005152 BH |
105 | fParser = parser; |
106 | } | |
085d898f | 107 | |
ff4ed569 FC |
108 | /** |
109 | */ | |
110 | @Override | |
085d898f FC |
111 | public TmfTraceStub clone() { |
112 | TmfTraceStub clone = null; | |
113 | try { | |
114 | clone = (TmfTraceStub) super.clone(); | |
115 | clone.fTrace = new RandomAccessFile(getPath(), "r"); | |
116 | clone.fParser = new TmfEventParserStub(); | |
117 | } catch (final FileNotFoundException e) { | |
118 | } | |
119 | return clone; | |
ff4ed569 | 120 | } |
085d898f | 121 | |
e31e01e8 | 122 | // ------------------------------------------------------------------------ |
d18dd09b | 123 | // Accessors |
e31e01e8 | 124 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
125 | |
126 | public RandomAccessFile getStream() { | |
127 | return fTrace; | |
128 | } | |
129 | ||
e31e01e8 | 130 | // ------------------------------------------------------------------------ |
d18dd09b | 131 | // Operators |
e31e01e8 | 132 | // ------------------------------------------------------------------------ |
d18dd09b | 133 | |
085d898f FC |
134 | @Override |
135 | @SuppressWarnings("unchecked") | |
136 | public TmfContext seekLocation(final ITmfLocation<?> location) { | |
137 | fLock.lock(); | |
d18dd09b | 138 | try { |
73005152 BH |
139 | if (fTrace != null) { |
140 | // Position the trace at the requested location and | |
141 | // returns the corresponding context | |
142 | long loc = 0; | |
143 | long rank = 0; | |
144 | if (location != null) { | |
145 | loc = ((TmfLocation<Long>) location).getLocation(); | |
146 | rank = ITmfContext.UNKNOWN_RANK; | |
147 | } | |
085d898f | 148 | if (loc != fTrace.getFilePointer()) |
73005152 | 149 | fTrace.seek(loc); |
085d898f | 150 | final TmfContext context = new TmfContext(getCurrentLocation(), rank); |
73005152 BH |
151 | return context; |
152 | } | |
085d898f FC |
153 | } catch (final IOException e) { |
154 | e.printStackTrace(); | |
155 | } | |
73005152 BH |
156 | finally{ |
157 | fLock.unlock(); | |
158 | } | |
085d898f | 159 | return null; |
d18dd09b ASL |
160 | } |
161 | ||
c76c54bb | 162 | |
085d898f FC |
163 | @Override |
164 | public TmfContext seekLocation(final double ratio) { | |
165 | fLock.lock(); | |
c76c54bb | 166 | try { |
73005152 | 167 | if (fTrace != null) { |
085d898f FC |
168 | final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length()))); |
169 | final TmfContext context = seekLocation(location); | |
73005152 BH |
170 | context.setRank(ITmfContext.UNKNOWN_RANK); |
171 | return context; | |
172 | } | |
085d898f | 173 | } catch (final IOException e) { |
c76c54bb | 174 | e.printStackTrace(); |
73005152 BH |
175 | } finally { |
176 | fLock.unlock(); | |
c76c54bb | 177 | } |
085d898f | 178 | |
c76c54bb FC |
179 | return null; |
180 | } | |
181 | ||
182 | @Override | |
12c155f5 | 183 | @SuppressWarnings("rawtypes") |
085d898f | 184 | public double getLocationRatio(final ITmfLocation location) { |
73005152 | 185 | fLock.lock(); |
c76c54bb | 186 | try { |
085d898f FC |
187 | if (fTrace != null) |
188 | if (location.getLocation() instanceof Long) | |
73005152 | 189 | return (double) ((Long) location.getLocation()) / fTrace.length(); |
085d898f | 190 | } catch (final IOException e) { |
c76c54bb | 191 | e.printStackTrace(); |
73005152 BH |
192 | } finally { |
193 | fLock.unlock(); | |
c76c54bb FC |
194 | } |
195 | return 0; | |
196 | } | |
197 | ||
4e3aa37d | 198 | @Override |
085d898f | 199 | public TmfLocation<Long> getCurrentLocation() { |
73005152 | 200 | fLock.lock(); |
d18dd09b | 201 | try { |
085d898f | 202 | if (fTrace != null) |
73005152 | 203 | return new TmfLocation<Long>(fTrace.getFilePointer()); |
085d898f FC |
204 | } catch (final IOException e) { |
205 | e.printStackTrace(); | |
206 | } finally { | |
207 | fLock.unlock(); | |
208 | } | |
209 | return null; | |
210 | } | |
211 | ||
212 | @Override | |
213 | public ITmfEvent parseEvent(final ITmfContext context) { | |
214 | fLock.lock(); | |
215 | try { | |
216 | // parseNextEvent will update the context | |
217 | if (fTrace != null) { | |
218 | final ITmfEvent event = fParser.parseNextEvent(this, context.clone()); | |
219 | return event; | |
73005152 | 220 | } |
085d898f FC |
221 | } |
222 | catch (final IOException e) { | |
d18dd09b | 223 | e.printStackTrace(); |
73005152 BH |
224 | } finally { |
225 | fLock.unlock(); | |
d18dd09b ASL |
226 | } |
227 | return null; | |
228 | } | |
229 | ||
085d898f FC |
230 | @Override |
231 | public void setTimeRange(final TmfTimeRange range) { | |
232 | super.setTimeRange(range); | |
233 | } | |
d18dd09b | 234 | |
085d898f FC |
235 | @Override |
236 | public void setStartTime(final ITmfTimestamp startTime) { | |
237 | super.setStartTime(startTime); | |
ff4ed569 FC |
238 | } |
239 | ||
085d898f FC |
240 | @Override |
241 | public void setEndTime(final ITmfTimestamp endTime) { | |
242 | super.setEndTime(endTime); | |
ff4ed569 FC |
243 | } |
244 | ||
085d898f FC |
245 | @Override |
246 | public void dispose() { | |
247 | fLock.lock(); | |
248 | try { | |
249 | if (fTrace != null) { | |
250 | fTrace.close(); | |
251 | fTrace = null; | |
252 | } | |
253 | } catch (final IOException e) { | |
254 | // Ignore | |
255 | } finally { | |
256 | fLock.unlock(); | |
257 | } | |
258 | super.dispose(); | |
ff4ed569 FC |
259 | } |
260 | ||
e31e01e8 | 261 | } |