ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
index 9ea692ff7dc63c0420d9f942306a87e13a9a170c..bcc65573d567d5d98d46b177ca02e6eef691cb7b 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * 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;
 
 /**
@@ -28,7 +25,7 @@ import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntr
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StreamInputReader {
+public class StreamInputReader implements AutoCloseable {
 
     // ------------------------------------------------------------------------
     // Attributes
@@ -59,8 +56,10 @@ public class StreamInputReader {
 
     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
@@ -91,10 +90,11 @@ public class StreamInputReader {
     /**
      * Dispose the StreamInputReader
      *
-     * @since 2.0
+     * @since 3.0
      */
-    public void dispose() {
-        fPacketReader.dispose();
+    @Override
+    public void close() {
+        fPacketReader.close();
     }
 
     // ------------------------------------------------------------------------
@@ -111,15 +111,6 @@ public class StreamInputReader {
         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
      *
@@ -175,27 +166,34 @@ public class StreamInputReader {
     }
 
     /**
-     * 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();
     }
 
     // ------------------------------------------------------------------------
@@ -207,8 +205,9 @@ public class StreamInputReader {
      * @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
@@ -216,20 +215,21 @@ public class StreamInputReader {
         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;
     }
 
     /**
@@ -240,13 +240,16 @@ public class StreamInputReader {
      */
     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);
             }
         }
@@ -291,11 +294,13 @@ public class StreamInputReader {
         }
 
         /*
-         * 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);
@@ -309,6 +314,7 @@ public class StreamInputReader {
 
     /**
      * @param timestamp
+     *            the time to seek
      * @throws CTFReaderException
      *             if an error occurs
      */
This page took 0.028834 seconds and 5 git commands to generate.