internalize some CTF API
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.trace;
14
15 import java.util.ListIterator;
16
17 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
18 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
19 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
20
21 /**
22 * <b><u>StreamInputReader</u></b>
23 * <p>
24 * Reads the events of a trace file.
25 */
26 public class StreamInputReader {
27
28 // ------------------------------------------------------------------------
29 // Attributes
30 // ------------------------------------------------------------------------
31
32 /**
33 * The StreamInput we are reading.
34 */
35 private final StreamInput streamInput;
36
37 /**
38 * The packet reader used to read packets from this trace file.
39 */
40 private final StreamInputPacketReader packetReader;
41
42 /**
43 * Iterator on the packet index
44 */
45 private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
46
47
48 /**
49 * Reference to the current event of this trace file (iow, the last on that
50 * was read, the next one to be returned)
51 */
52 private EventDefinition currentEvent = null;
53
54 private int name;
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 /**
61 * Constructs a StreamInputReader that reads a StreamInput.
62 *
63 * @param streamInput
64 * The StreamInput to read.
65 */
66 public StreamInputReader(StreamInput streamInput) {
67 this.streamInput = streamInput;
68 this.packetReader = new StreamInputPacketReader(this);
69
70 /*
71 * Get the iterator on the packet index.
72 */
73 this.packetIndexIt = streamInput.getIndex().listIterator();
74
75 /*
76 * Make first packet the current one.
77 */
78 goToNextPacket();
79 }
80
81 // ------------------------------------------------------------------------
82 // Getters/Setters/Predicates
83 // ------------------------------------------------------------------------
84
85 public EventDefinition getCurrentEvent() {
86 return this.currentEvent;
87 }
88
89 public StructDefinition getCurrentPacketContext() {
90 return this.packetReader.getStreamPacketContextDef();
91 }
92
93 public StreamInput getStreamInput() {
94 return this.streamInput;
95 }
96
97 public int getName() {
98 return this.name;
99 }
100
101 public void setName(int name) {
102 this.name = name;
103 }
104
105 public int getCPU() {
106 return this.packetReader.getCPU();
107 }
108
109 // ------------------------------------------------------------------------
110 // Operations
111 // ------------------------------------------------------------------------
112 /**
113 * Reads the next event in the current event variable.
114 *
115 * @return If an event has been successfully read.
116 */
117 public boolean readNextEvent() {
118 /*
119 * Change packet if needed
120 */
121 if (!this.packetReader.hasMoreEvents()) {
122 goToNextPacket();
123 }
124
125 /*
126 * If an event is available, read it.
127 */
128 if (this.packetReader.hasMoreEvents()) {
129 try {
130 this.setCurrentEvent(this.packetReader.readNextEvent());
131 } catch (CTFReaderException e) {
132 /* Some problem happened, we'll assume there is no more events */
133 return false;
134 }
135 return true;
136 }
137 this.setCurrentEvent(null);
138 return false;
139 }
140
141 /**
142 * Change the current packet of the packet reader to the next one.
143 */
144 private void goToNextPacket() {
145 if (getPacketIndexIt().hasNext()) {
146 StreamInputPacketIndexEntry nextPacket = getPacketIndexIt().next();
147 this.packetReader.setCurrentPacket(nextPacket);
148 } else {
149 this.packetReader.setCurrentPacket(null);
150 }
151 }
152
153 /**
154 * Changes the location of the trace file reader so that the current event
155 * is the first event with a timestamp greater than the given timestamp.
156 *
157 * @param timestamp
158 * The timestamp to seek to.
159 */
160 public long seek(long timestamp) {
161 long offset = 0 ;
162 /*
163 * Search in the index for the packet to search in.
164 */
165 this.packetIndexIt = this.streamInput.getIndex().search(timestamp);
166
167 /*
168 * Switch to this packet.
169 */
170 goToNextPacket();
171
172 /*
173 * Advance until A. we reached the end of the trace file (which means
174 * the given timestamp is after the last event), or B. we found the
175 * first event with a timestamp greater than the given timestamp.
176 */
177 readNextEvent();
178 boolean done = (this.getCurrentEvent() == null);
179 while (!done && (this.getCurrentEvent().timestamp < timestamp)) {
180 readNextEvent();
181 done = (this.getCurrentEvent() == null);
182 offset++ ;
183 }
184 return offset;
185 }
186
187
188 public long seekIndex(long index) throws CTFReaderException
189 {
190 /*
191 * Search in the index for the packet to search in.
192 */
193 this.packetIndexIt = this.streamInput.getIndex().searchIndex(index);
194 /*
195 * Switch to this packet.
196 */
197 goToNextPacket();
198 /*
199 * Read the first packet
200 */
201 readNextEvent();
202 /*
203 * get the current index
204 */
205 if(this.packetReader.getCurrentPacket() == null){
206 throw new CTFReaderException("Current packet null in index seek, did you index your trace yet?");
207 }
208 return this.packetReader.getCurrentPacket().getIndexBegin();
209
210 }
211
212 public void goToLastEvent() throws CTFReaderException {
213 /*
214 * Search in the index for the packet to search in.
215 */
216 int len = this.streamInput.getIndex().getEntries().size();
217 StreamInputPacketIndexEntry entry = this.streamInput.getIndex().getEntries().get(len-1);
218 seek(entry.getTimestampEnd() - 1 );
219 /*
220 * Go until the end of that packet
221 */
222 while (this.packetReader.hasMoreEvents()) {
223 this.packetReader.readNextEvent();
224 }
225 }
226
227 public void setCurrentEvent(EventDefinition currentEvent) {
228 this.currentEvent = currentEvent;
229 }
230
231 /**
232 * @return the packetIndexIt
233 */
234 private ListIterator<StreamInputPacketIndexEntry> getPacketIndexIt() {
235 return packetIndexIt;
236 }
237
238 /**
239 * @return the packetReader
240 */
241 public StreamInputPacketReader getPacketReader() {
242 return packetReader;
243 }
244
245 }
This page took 0.036817 seconds and 6 git commands to generate.