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