tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
index ea18b442fd081056e97b60f3b34293dd22f89a34..ecee99cd7ce6001dd8d4cff183144485db80845c 100644 (file)
 package org.eclipse.linuxtools.tmf.core.trace;
 
 import java.io.File;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
@@ -25,6 +29,11 @@ import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
+import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
 import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
 import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
@@ -75,7 +84,7 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
     private long fNbEvents = 0;
 
     // The time span of the event stream
-    private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
+    private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
     private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
 
     // The trace streaming interval (0 = no streaming)
@@ -90,6 +99,22 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
     // The trace's statistics
     private ITmfStatistics fStatistics;
 
+    // The current selected time
+    private ITmfTimestamp fCurrentTime = TmfTimestamp.ZERO;
+
+    // The current selected range
+    private TmfTimeRange fCurrentRange = TmfTimeRange.NULL_RANGE;
+
+    /**
+     * The collection of state systems that are registered with this trace. Each
+     * sub-class can decide to add its (one or many) state system to this map
+     * during their {@link #buildStateSystem()}.
+     *
+     * @since 2.0
+     */
+    protected final Map<String, ITmfStateSystem> fStateSystems =
+            new HashMap<String, ITmfStateSystem>();
+
     // ------------------------------------------------------------------------
     // Construction
     // ------------------------------------------------------------------------
@@ -229,8 +254,6 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
             }
         }
         super.init(traceName, type);
-
-        buildStatistics();
     }
 
     /**
@@ -269,6 +292,25 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         fStatistics = (fResource == null ? null : new TmfStateStatistics(this) );
     }
 
+    /**
+     * Build the state system(s) associated with this trace type.
+     *
+     * Suppressing the warning, because the 'throws' will usually happen in
+     * sub-classes.
+     *
+     * @throws TmfTraceException
+     *             If there is a problem during the build
+     * @since 2.0
+     */
+    @SuppressWarnings("unused")
+    protected void buildStateSystem() throws TmfTraceException {
+        /*
+         * Nothing is done in the base implementation, please specify
+         * how/if to register a new state system in derived classes.
+         */
+        return;
+    }
+
     /**
      * Clears the trace
      */
@@ -283,6 +325,12 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         if (fStatistics != null) {
             fStatistics.dispose();
         }
+
+        /* Clean up the state systems */
+        for (ITmfStateSystem ss : fStateSystems.values()) {
+            ss.dispose();
+        }
+
         super.dispose();
     }
 
@@ -356,12 +404,16 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
      * @since 2.0
      */
     @Override
-    public ITmfStateSystem getStateSystem() {
-        /*
-         * By default, no state system is used. Sub-classes can specify their
-         * own behaviour.
-         */
-        return null;
+    public final ITmfStateSystem getStateSystem(String id) {
+        return fStateSystems.get(id);
+    }
+
+    /**
+     * @since 2.0
+     */
+    @Override
+    public final Collection<String> listStateSystems() {
+        return fStateSystems.keySet();
     }
 
     // ------------------------------------------------------------------------
@@ -400,8 +452,43 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         return fEndTime;
     }
 
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentTime()
+     */
+    /**
+     * @since 2.0
+     */
+    @Override
+    public ITmfTimestamp getCurrentTime() {
+        return fCurrentTime;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentRange()
+     */
+    /**
+     * @since 2.0
+     */
+    @Override
+    public TmfTimeRange getCurrentRange() {
+        return fCurrentRange;
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getInitialRangeOffset()
+     */
+    /**
+     * @since 2.0
+     */
+    @Override
+    public ITmfTimestamp getInitialRangeOffset() {
+        final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
+        return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
+    }
+
     // ------------------------------------------------------------------------
-    // Convenience setters/getters
+    // Convenience setters
     // ------------------------------------------------------------------------
 
     /**
@@ -584,6 +671,13 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
             fEndTime = timestamp;
         }
+        if (fCurrentRange == TmfTimeRange.NULL_RANGE) {
+            fCurrentTime = timestamp;
+            ITmfTimestamp initialOffset = getInitialRangeOffset();
+            long endValue = timestamp.getValue() + initialOffset.normalize(0, timestamp.getScale()).getValue();
+            ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, timestamp.getScale());
+            fCurrentRange = new TmfTimeRange(timestamp, endTimestamp);
+        }
         if (context.hasValidRank()) {
             long rank = context.getRank();
             if (fNbEvents <= rank) {
@@ -599,11 +693,14 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
     // TmfDataProvider
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
+    /**
+     * @since 2.0
      */
     @Override
-    protected ITmfContext armRequest(final ITmfDataRequest request) {
+    public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
+        if (executorIsShutdown()) {
+            return null;
+        }
         if ((request instanceof ITmfEventRequest)
             && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
             && (request.getIndex() == 0))
@@ -616,6 +713,113 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         return seekEvent(request.getIndex());
     }
 
+    // ------------------------------------------------------------------------
+    // Signal handlers
+    // ------------------------------------------------------------------------
+
+    /**
+     * Handler for the Trace Opened signal
+     *
+     * @param signal
+     *            The incoming signal
+     * @since 2.0
+     */
+    @TmfSignalHandler
+    public void traceOpened(TmfTraceOpenedSignal signal) {
+        ITmfTrace trace = signal.getTrace();
+        if (signal.getTrace() instanceof TmfExperiment) {
+            TmfExperiment experiment = (TmfExperiment) signal.getTrace();
+            for (ITmfTrace expTrace : experiment.getTraces()) {
+                if (expTrace == this) {
+                    trace = expTrace;
+                    break;
+                }
+            }
+        }
+        if (trace == this) {
+            /* the signal is for this trace or for an experiment containing this trace */
+            try {
+                buildStatistics();
+            } catch (TmfTraceException e) {
+                e.printStackTrace();
+            }
+            try {
+                buildStateSystem();
+            } catch (TmfTraceException e) {
+                e.printStackTrace();
+            }
+
+            /* Refresh the project, so it can pick up new files that got created. */
+            try {
+                if (fResource != null) {
+                    fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
+                }
+            } catch (CoreException e) {
+                e.printStackTrace();
+            }
+        }
+        if (signal.getTrace() == this) {
+            /* the signal is for this trace or experiment */
+            if (getNbEvents() == 0) {
+                return;
+            }
+
+            final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
+            final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
+
+            // Broadcast in separate thread to prevent deadlock
+            new Thread() {
+                @Override
+                public void run() {
+                    broadcast(rangeUpdatedsignal);
+                }
+            }.start();
+            return;
+        }
+    }
+
+    /**
+     * Signal handler for the TmfTraceRangeUpdatedSignal signal
+     *
+     * @param signal The incoming signal
+     * @since 2.0
+     */
+    @TmfSignalHandler
+    public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
+        if (signal.getTrace() == this) {
+            getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
+        }
+    }
+
+    /**
+     * Signal handler for the TmfTimeSynchSignal signal
+     *
+     * @param signal The incoming signal
+     * @since 2.0
+     */
+    @TmfSignalHandler
+    public void synchToTime(final TmfTimeSynchSignal signal) {
+        if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
+            fCurrentTime = signal.getCurrentTime();
+        }
+    }
+
+    /**
+     * Signal handler for the TmfRangeSynchSignal signal
+     *
+     * @param signal The incoming signal
+     * @since 2.0
+     */
+    @TmfSignalHandler
+    public void synchToRange(final TmfRangeSynchSignal signal) {
+        if (signal.getCurrentTime().compareTo(fStartTime) >= 0 && signal.getCurrentTime().compareTo(fEndTime) <= 0) {
+            fCurrentTime = signal.getCurrentTime();
+        }
+        if (signal.getCurrentRange().getIntersection(getTimeRange()) != null) {
+            fCurrentRange = signal.getCurrentRange().getIntersection(getTimeRange());
+        }
+    }
+
     // ------------------------------------------------------------------------
     // toString
     // ------------------------------------------------------------------------
This page took 0.035105 seconds and 5 git commands to generate.