Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
13package org.eclipse.linuxtools.tmf.trace;
14
15import java.io.FileNotFoundException;
16import java.io.IOException;
17import java.io.RandomAccessFile;
18import java.util.concurrent.locks.ReentrantLock;
19
20import org.eclipse.linuxtools.tmf.event.TmfEvent;
21import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
22import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
23import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
24
25/**
26 * <b><u>TmfTraceStub</u></b>
27 * <p>
28 * Dummy test trace. Use in conjunction with TmfEventParserStub.
29 */
30@SuppressWarnings("nls")
31public class TmfTraceStub extends TmfTrace<TmfEvent> {
32
33 // ------------------------------------------------------------------------
34 // Attributes
35 // ------------------------------------------------------------------------
36
37 // The actual stream
38 private RandomAccessFile fTrace;
39
40 // The associated event parser
41 private ITmfEventParser fParser;
42
43 // The synchronization lock
44 private ReentrantLock fLock = new ReentrantLock();
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 /**
51 * @param filename
52 * @throws FileNotFoundException
53 */
54 public TmfTraceStub(String filename) throws FileNotFoundException {
55 this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
56 }
57
58 /**
59 * @param filename
60 * @param cacheSize
61 * @throws FileNotFoundException
62 */
63 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
64 this(filename, cacheSize, false);
65 }
66
67 /**
68 * @param filename
69 * @param waitForCompletion
70 * @throws FileNotFoundException
71 */
72 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
73 this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
74 }
75
76 /**
77 * @param filename
78 * @param cacheSize
79 * @param waitForCompletion
80 * @throws FileNotFoundException
81 */
82 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
83 super(null, TmfEvent.class, filename, cacheSize, false);
84 fTrace = new RandomAccessFile(filename, "r");
85 fParser = new TmfEventParserStub();
86 }
87
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
102 /**
103 */
104 @Override
105 public TmfTraceStub clone() {
106 TmfTraceStub clone = null;
107 try {
108 clone = (TmfTraceStub) super.clone();
109 clone.fTrace = new RandomAccessFile(getPath(), "r");
110 clone.fParser = new TmfEventParserStub();
111 } catch (CloneNotSupportedException e) {
112 } catch (FileNotFoundException e) {
113 }
114 return clone;
115 }
116
117 @Override
118 public ITmfTrace copy() {
119 ITmfTrace returnedValue = null;
120 returnedValue = clone();
121 return returnedValue;
122 }
123
124 // ------------------------------------------------------------------------
125 // Accessors
126 // ------------------------------------------------------------------------
127
128 public RandomAccessFile getStream() {
129 return fTrace;
130 }
131
132 // ------------------------------------------------------------------------
133 // Operators
134 // ------------------------------------------------------------------------
135
136 @Override
137 @SuppressWarnings("unchecked")
138 public TmfContext seekLocation(ITmfLocation location) {
139 fLock.lock();
140 try {
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 }
156 } catch (IOException e) {
157 e.printStackTrace();
158 }
159 finally{
160 fLock.unlock();
161 }
162 return null;
163 }
164
165
166 @Override
167 public TmfContext seekLocation(double ratio) {
168 fLock.lock();
169 try {
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 }
176 } catch (IOException e) {
177 e.printStackTrace();
178 } finally {
179 fLock.unlock();
180 }
181
182 return null;
183 }
184
185 @Override
186 @SuppressWarnings("rawtypes")
187 public double getLocationRatio(ITmfLocation location) {
188 fLock.lock();
189 try {
190 if (fTrace != null) {
191 if (location.getLocation() instanceof Long) {
192 return (double) ((Long) location.getLocation()) / fTrace.length();
193 }
194 }
195 } catch (IOException e) {
196 e.printStackTrace();
197 } finally {
198 fLock.unlock();
199 }
200 return 0;
201 }
202
203 @Override
204 public TmfLocation<Long> getCurrentLocation() {
205 fLock.lock();
206 try {
207 if (fTrace != null) {
208 return new TmfLocation<Long>(fTrace.getFilePointer());
209 }
210 } catch (IOException e) {
211 e.printStackTrace();
212 } finally {
213 fLock.unlock();
214 }
215 return null;
216 }
217
218 @Override
219 public TmfEvent parseEvent(TmfContext context) {
220 fLock.lock();
221 try {
222 // parseNextEvent will update the context
223 if (fTrace != null) {
224 TmfEvent event = fParser.parseNextEvent(this, context.clone());
225 return event;
226 }
227 }
228 catch (IOException e) {
229 e.printStackTrace();
230 } finally {
231 fLock.unlock();
232 }
233 return null;
234 }
235
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 }
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 }
266
267}
This page took 0.024046 seconds and 5 git commands to generate.