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