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