/*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.linuxtools.ctf.core.trace;
import java.nio.ByteOrder;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
/**
* @author Matthew Khouzam
* @author Simon Marchi
*/
-public class StreamInputReader {
+public class StreamInputReader implements AutoCloseable {
// ------------------------------------------------------------------------
// Attributes
private CTFTraceReader fParent;
- /** Map of all the event types */
- private final Map<Long, EventDefinition> fEventDefs = new HashMap<>();
+ /**
+ * Live trace reading
+ */
+ private boolean fLive = false;
// ------------------------------------------------------------------------
// Constructors
/**
* Dispose the StreamInputReader
*
- * @since 2.0
+ * @since 3.0
*/
- public void dispose() {
- fPacketReader.dispose();
+ @Override
+ public void close() {
+ fPacketReader.close();
}
// ------------------------------------------------------------------------
return fCurrentEvent;
}
- /**
- * Gets the current packet context
- *
- * @return the current packet context (size, lost events and such)
- */
- public StructDefinition getCurrentPacketContext() {
- return fPacketReader.getStreamPacketContextDef();
- }
-
/**
* Gets the byte order for a trace
*
}
/**
- * Gets the event definition hashmap for this StreamInput
+ * Set the trace to live mode
+ *
+ * @param live
+ * whether the trace is read live or not
+ * @since 3.0
+ */
+ public void setLive(boolean live) {
+ fLive = live;
+ }
+
+ /**
+ * Get if the trace is to read live or not
*
- * @return Unmodifiable map with the event definitions
- * @since 2.1
+ * @return whether the trace is live or not
+ * @since 3.0
*/
- public Map<Long, EventDefinition> getEventDefinitions() {
- return Collections.unmodifiableMap(fEventDefs);
+ public boolean isLive() {
+ return fLive;
}
/**
- * Add an event definition to this stream input reader.
+ * Get the event context of the stream
*
- * @param id
- * The id of the event definition. This will overwrite any
- * existing definition with the same id.
- * @param def
- * The matching event definition
- * @since 2.1
+ * @return the event context declaration of the stream
+ * @since 3.0
*/
- public void addEventDefinition(Long id, EventDefinition def) {
- fEventDefs.put(id, def);
+ public StructDeclaration getStreamEventContextDecl() {
+ return getStreamInput().getStream().getEventContextDecl();
}
// ------------------------------------------------------------------------
* @return If an event has been successfully read.
* @throws CTFReaderException
* if an error occurs
+ * @since 3.0
*/
- public boolean readNextEvent() throws CTFReaderException {
+ public CTFResponse readNextEvent() throws CTFReaderException {
/*
* Change packet if needed
if (!fPacketReader.hasMoreEvents()) {
final StreamInputPacketIndexEntry prevPacket = fPacketReader
.getCurrentPacket();
- if (prevPacket != null) {
+ if (prevPacket != null || fLive) {
goToNextPacket();
}
+
}
/*
* If an event is available, read it.
*/
if (fPacketReader.hasMoreEvents()) {
- this.setCurrentEvent(fPacketReader.readNextEvent());
- return true;
+ setCurrentEvent(fPacketReader.readNextEvent());
+ return CTFResponse.OK;
}
this.setCurrentEvent(null);
- return false;
+ return fLive ? CTFResponse.WAIT : CTFResponse.FINISH;
}
/**
*/
private void goToNextPacket() throws CTFReaderException {
fPacketIndex++;
+ // did we already index the packet?
if (getPacketSize() >= (fPacketIndex + 1)) {
fPacketReader.setCurrentPacket(getPacket());
} else {
+ // go to the next packet if there is one, index it at the same time
if (fStreamInput.addPacketHeaderIndex()) {
fPacketIndex = getPacketSize() - 1;
fPacketReader.setCurrentPacket(getPacket());
} else {
+ // out of packets
fPacketReader.setCurrentPacket(null);
}
}
}
/*
- * Advance until either of these conditions are met
- * <ul>
- * <li> reached the end of the trace file (the given timestamp is after the last event), </li>
- * <li> found the first event with a timestamp greater or equal the given timestamp. </li>
- * </ul>
+ * Advance until either of these conditions are met:
+ *
+ * - reached the end of the trace file (the given timestamp is after the
+ * last event)
+ *
+ * - found the first event with a timestamp greater or equal the given
+ * timestamp.
*/
readNextEvent();
boolean done = (this.getCurrentEvent() == null);
/**
* @param timestamp
+ * the time to seek
* @throws CTFReaderException
* if an error occurs
*/