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