Tmf: Trace synchronization using network events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
index 817d85254701bcf6e7ad3143bbea4de063197804..ea3f7b59f41e1af5ede9060cf549613cc292426c 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2013 Ericsson
+ * Copyright (c) 2009, 2013 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  *   Francois Chouinard - Initial API and implementation
  *   Francois Chouinard - Updated as per TMF Trace Model 1.0
  *   Patrick Tasse - Updated for removal of context clone
+ *   Geneviève Bastien  - Added timestamp transforms, its saving to file and
+ *                        timestamp creation functions
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
@@ -26,6 +35,7 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.MultiStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.linuxtools.internal.tmf.core.Activator;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
@@ -38,6 +48,8 @@ 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;
+import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
+import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform;
 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
@@ -116,6 +128,10 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
     protected final Map<String, ITmfStateSystem> fStateSystems =
             new LinkedHashMap<String, ITmfStateSystem>();
 
+    private ITmfTimestampTransform fTsTransform;
+
+    private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Construction
     // ------------------------------------------------------------------------
@@ -737,6 +753,102 @@ public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
         }
     }
 
+    /**
+     * Returns the file resource used to store synchronization formula. The file
+     * may not exist.
+     *
+     * @return the synchronization file
+     */
+    private File getSyncFormulaFile() {
+        File file = null;
+        if (fResource instanceof IFolder) {
+            try {
+                String supplDirectory;
+
+                supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
+
+                file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
+
+            } catch (CoreException e) {
+
+            }
+        }
+        return file;
+    }
+
+    // ------------------------------------------------------------------------
+    // Timestamp transformation functions
+    // ------------------------------------------------------------------------
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public ITmfTimestampTransform getTimestampTransform() {
+        if (fTsTransform == null) {
+            /* Check if a formula is stored somewhere in the resources */
+            File sync_file = getSyncFormulaFile();
+            if (sync_file != null && sync_file.exists()) {
+
+                try {
+                    FileInputStream fis = new FileInputStream(sync_file);
+                    ObjectInputStream ois = new ObjectInputStream(fis);
+                    fTsTransform = (ITmfTimestampTransform) ois.readObject();
+
+                    ois.close();
+                    fis.close();
+                } catch (ClassNotFoundException e1) {
+                    fTsTransform = TmfTimestampTransform.IDENTITY;
+                } catch (FileNotFoundException e1) {
+                    fTsTransform = TmfTimestampTransform.IDENTITY;
+                } catch (IOException e1) {
+                    fTsTransform = TmfTimestampTransform.IDENTITY;
+                }
+            } else {
+                fTsTransform = TmfTimestampTransform.IDENTITY;
+            }
+        }
+        return fTsTransform;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public void setTimestampTransform(final ITmfTimestampTransform tt) {
+        fTsTransform = tt;
+
+        /* Save the timestamp transform to a file */
+        File sync_file = getSyncFormulaFile();
+        if (sync_file != null) {
+            if (sync_file.exists()) {
+                sync_file.delete();
+            }
+            FileOutputStream fos;
+            ObjectOutputStream oos;
+
+            /* Save the header of the file */
+            try {
+                fos = new FileOutputStream(sync_file, false);
+                oos = new ObjectOutputStream(fos);
+
+                oos.writeObject(fTsTransform);
+                oos.close();
+                fos.close();
+            } catch (IOException e1) {
+                Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
+            }
+        }
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public ITmfTimestamp createTimestamp(long ts) {
+        return new TmfTimestamp(getTimestampTransform().transform(ts));
+    }
+
     // ------------------------------------------------------------------------
     // toString
     // ------------------------------------------------------------------------
This page took 0.027029 seconds and 5 git commands to generate.