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 | ||
d4011df2 FC |
98 | @Override |
99 | public ITmfTrace createTraceCopy() { | |
ff4ed569 | 100 | ITmfTrace returnedValue = null; |
cb866e08 | 101 | returnedValue = clone(); |
ff4ed569 FC |
102 | return returnedValue; |
103 | } | |
104 | ||
e31e01e8 | 105 | // ------------------------------------------------------------------------ |
d18dd09b | 106 | // Accessors |
e31e01e8 | 107 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
108 | |
109 | public RandomAccessFile getStream() { | |
110 | return fTrace; | |
111 | } | |
112 | ||
e31e01e8 | 113 | // ------------------------------------------------------------------------ |
d18dd09b | 114 | // Operators |
e31e01e8 | 115 | // ------------------------------------------------------------------------ |
d18dd09b | 116 | |
452ad365 | 117 | @Override |
9f584e4c | 118 | @SuppressWarnings("unchecked") |
452ad365 | 119 | public TmfContext seekLocation(ITmfLocation<?> location) { |
d18dd09b | 120 | try { |
4e3aa37d | 121 | synchronized(fTrace) { |
9f584e4c FC |
122 | // Position the trace at the requested location and |
123 | // returns the corresponding context | |
ff4ed569 FC |
124 | long loc = 0; |
125 | long rank = 0; | |
126 | if (location != null) { | |
127 | loc = ((TmfLocation<Long>) location).getLocation(); | |
128 | rank = ITmfContext.UNKNOWN_RANK; | |
129 | } | |
9f584e4c FC |
130 | if (loc != fTrace.getFilePointer()) { |
131 | fTrace.seek(loc); | |
132 | } | |
ff4ed569 | 133 | TmfContext context = new TmfContext(getCurrentLocation(), rank); |
e31e01e8 | 134 | return context; |
4e3aa37d | 135 | } |
d18dd09b ASL |
136 | } catch (IOException e) { |
137 | e.printStackTrace(); | |
138 | } | |
e31e01e8 | 139 | return null; |
d18dd09b ASL |
140 | } |
141 | ||
4e3aa37d | 142 | @Override |
9f584e4c | 143 | public TmfLocation<Long> getCurrentLocation() { |
d18dd09b | 144 | try { |
9f584e4c | 145 | return new TmfLocation<Long>(fTrace.getFilePointer()); |
d18dd09b ASL |
146 | } catch (IOException e) { |
147 | e.printStackTrace(); | |
148 | } | |
149 | return null; | |
150 | } | |
151 | ||
4e3aa37d | 152 | @Override |
9f584e4c | 153 | public TmfEvent parseEvent(TmfContext context) { |
cc6eec3e | 154 | try { |
cb866e08 FC |
155 | // parseNextEvent will update the context |
156 | TmfEvent event = fParser.parseNextEvent(this, context.clone()); | |
cc6eec3e FC |
157 | return event; |
158 | } | |
159 | catch (IOException e) { | |
160 | e.printStackTrace(); | |
161 | } | |
162 | return null; | |
d18dd09b ASL |
163 | } |
164 | ||
ff4ed569 FC |
165 | @Override |
166 | public void setTimeRange(TmfTimeRange range) { | |
167 | super.setTimeRange(range); | |
168 | } | |
169 | ||
170 | @Override | |
171 | public void setStartTime(TmfTimestamp startTime) { | |
172 | super.setStartTime(startTime); | |
173 | } | |
174 | ||
175 | @Override | |
176 | public void setEndTime(TmfTimestamp endTime) { | |
177 | super.setEndTime(endTime); | |
178 | } | |
179 | ||
e31e01e8 | 180 | } |