Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
index cc3aa00809884dda3b78297680f9aa47763602e7..e2cdcbc04450727edb03018a23efef22734d4bc1 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009 Ericsson
+ * Copyright (c) 2009, 2010 Ericsson
  * 
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -15,36 +15,44 @@ package org.eclipse.linuxtools.tmf.trace;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.eclipse.linuxtools.tmf.event.TmfEvent;
+import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
+import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
 
 /**
  * <b><u>TmfTraceStub</u></b>
  * <p>
- * TODO: Implement me. Please.
+ * Dummy test trace. Use in conjunction with TmfEventParserStub.
  */
-public class TmfTraceStub extends TmfTrace {
+@SuppressWarnings("nls")
+public class TmfTraceStub extends TmfTrace<TmfEvent> {
 
-    // ========================================================================
+    // ------------------------------------------------------------------------
     // Attributes
-    // ========================================================================
+    // ------------------------------------------------------------------------
 
     // The actual stream
-    private final RandomAccessFile fTrace;
+    private RandomAccessFile fTrace;
 
     // The associated event parser
-    private final ITmfEventParser fParser;
+    private ITmfEventParser fParser;
 
-    // ========================================================================
+    // The synchronization lock
+    private ReentrantLock fLock = new ReentrantLock();
+    
+    // ------------------------------------------------------------------------
     // Constructors
-    // ========================================================================
+    // ------------------------------------------------------------------------
 
     /**
      * @param filename
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename) throws FileNotFoundException {
-        this(filename, DEFAULT_PAGE_SIZE);
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
     }
 
     /**
@@ -53,74 +61,207 @@ public class TmfTraceStub extends TmfTrace {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
-        super(filename, cacheSize, true);
+        this(filename, cacheSize, false);
+    }
+
+    /**
+     * @param filename
+     * @param waitForCompletion
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
+    }
+    
+    /**
+     * @param filename
+     * @param cacheSize
+     * @param waitForCompletion
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
+        super(null, TmfEvent.class, filename, cacheSize, false);
         fTrace = new RandomAccessFile(filename, "r");
         fParser = new TmfEventParserStub();
-        indexStream();
     }
 
-    // ========================================================================
+    
+    /**
+     * @param filename
+     * @param cacheSize
+     * @param waitForCompletion
+     * @param parser
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion, ITmfEventParser parser) throws FileNotFoundException {
+        super(filename, TmfEvent.class, filename, cacheSize, false);
+        fTrace = new RandomAccessFile(filename, "r");
+        fParser = parser;
+    }
+    
+    /**
+     */
+    @Override
+       public TmfTraceStub clone() {
+       TmfTraceStub clone = null;
+               try {
+                       clone = (TmfTraceStub) super.clone();
+               clone.fTrace  = new RandomAccessFile(getPath(), "r");
+               clone.fParser = new TmfEventParserStub();
+               } catch (CloneNotSupportedException e) {
+               } catch (FileNotFoundException e) {
+               }
+       return clone;
+    }
+    @Override
+       public ITmfTrace copy() {
+               ITmfTrace returnedValue = null;
+               returnedValue = clone();
+               return returnedValue;
+       }
+    
+    // ------------------------------------------------------------------------
     // Accessors
-    // ========================================================================
+    // ------------------------------------------------------------------------
 
     public RandomAccessFile getStream() {
         return fTrace;
     }
 
-    // ========================================================================
+    // ------------------------------------------------------------------------
     // Operators
-    // ========================================================================
+    // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#seekLocation(java.lang.Object)
-     */
-       public TmfTraceContext seekLocation(Object location) {
-       TmfTraceContext context = null;
+       @Override
+       @SuppressWarnings("unchecked")
+       public TmfContext seekLocation(ITmfLocation location) {
+           fLock.lock();
         try {
-               synchronized(fTrace) {
-                       fTrace.seek((location != null) ? (Long) location : 0);
-                       context = new TmfTraceContext(getCurrentLocation(), null, 0);
-               }
+            if (fTrace != null) {
+                // Position the trace at the requested location and
+                // returns the corresponding context
+                long loc  = 0;
+                long rank = 0;
+                if (location != null) {
+                    loc = ((TmfLocation<Long>) location).getLocation();
+                    rank = ITmfContext.UNKNOWN_RANK;
+                }
+                if (loc != fTrace.getFilePointer()) {
+                    fTrace.seek(loc);
+                }
+                TmfContext context = new TmfContext(getCurrentLocation(), rank);
+                return context;
+            }
                } catch (IOException e) {
-                       // TODO Auto-generated catch block
                        e.printStackTrace();
                }
-               return context;
+        finally{
+            fLock.unlock();
+        }
+               return null;
+    }
+
+
+       @Override
+    public TmfContext seekLocation(double ratio) {
+           fLock.lock();
+        try {
+            if (fTrace != null) {
+                ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
+                TmfContext context = seekLocation(location);
+                context.setRank(ITmfContext.UNKNOWN_RANK);
+                return context;
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            fLock.unlock();
+        }
+        
+        return null;
+    }
+
+    @Override
+    @SuppressWarnings("rawtypes")
+    public double getLocationRatio(ITmfLocation location) {
+        fLock.lock();
+        try {
+            if (fTrace != null) {
+                if (location.getLocation() instanceof Long) {
+                    return (double) ((Long) location.getLocation()) / fTrace.length();
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            fLock.unlock();
+        }
+        return 0;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.eventlog.ITmfStreamLocator#getCurrentLocation()
-     */
     @Override
-       public Object getCurrentLocation() {
+       public TmfLocation<Long> getCurrentLocation() {
+        fLock.lock();
         try {
-            return new Long(fTrace.getFilePointer());
+            if (fTrace != null) {
+                return new TmfLocation<Long>(fTrace.getFilePointer());
+            }
         } catch (IOException e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
+        } finally {
+            fLock.unlock();
         }
         return null;
     }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.linuxtools.tmf.trace.TmfTrace#parseEvent()
-        */
        @Override
-       public synchronized TmfEvent parseEvent(TmfTraceContext context) {
+       public TmfEvent parseEvent(TmfContext context) {
+           fLock.lock();
                try {
-                       fTrace.seek((context.location != null) ? (Long) context.location : 0);
-               TmfEvent event = fParser.parseNextEvent(this);
-               context = new TmfTraceContext(getCurrentLocation(), null, 0);
-                       return event;
+                       // parseNextEvent will update the context
+                   if (fTrace != null) {
+                       TmfEvent event = fParser.parseNextEvent(this, context.clone());
+                       return event;
+                   }
                }
                catch (IOException e) {
                        e.printStackTrace();
+               } finally {
+                   fLock.unlock();
                }
                return null;
        }
 
-       // ========================================================================
-    // Helper functions
-    // ========================================================================
+       @Override
+       public void setTimeRange(TmfTimeRange range) {
+       super.setTimeRange(range);
+    }
+
+       @Override
+       public void setStartTime(TmfTimestamp startTime) {
+       super.setStartTime(startTime);
+    }
+
+       @Override
+       public void setEndTime(TmfTimestamp endTime) {
+       super.setEndTime(endTime);
+    }
+       
+       @Override
+       public void dispose() {
+           fLock.lock();
+           try {
+               if (fTrace != null) {
+                   fTrace.close();
+                   fTrace = null;
+               }
+           } catch (IOException e) {
+               // Ignore
+           } finally {
+               fLock.unlock();  
+           }
+           super.dispose();
+       }
 
-}
+}
\ No newline at end of file
This page took 0.026818 seconds and 5 git commands to generate.