Improve internal signal handling
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramRequest.java
index 7003a3367e983592c3d365012f278316062ad25f..3fa66dbb454a3c458c82766c4670ef9a68f1136b 100644 (file)
@@ -8,12 +8,15 @@
  * 
  * Contributors:
  *   William Bourque - Initial API and implementation
+ *
+ * Modifications:
+ * 2010-07-16 Yuriy Vashchuk - Heritage correction.
  *******************************************************************************/
 package org.eclipse.linuxtools.lttng.ui.views.histogram;
 
 import org.eclipse.linuxtools.lttng.event.LttngEvent;
-import org.eclipse.linuxtools.tmf.event.TmfEvent;
 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
 import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
 
 /**
@@ -23,20 +26,22 @@ import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
  * <p>
  */
 public class HistogramRequest extends TmfEventRequest<LttngEvent> {
+/*     
        private HistogramContent histogramContent = null;
+*/     
        
-       private Integer lastInterval = 0;
-       private Long    lastRangeTime = 0L;
-       private Long    nbEventsInInterval = 1L;
+       private int     lastInterval = 0;
+       private long    lastRangeTime = 0L;
+       private long    nbEventsInInterval = 0L;
        
-       private Integer nbIntervalNotEmpty = 1;
-       private Integer nbEventRead = 0;
+       private int     nbIntervalNotEmpty = 1;
+       private int     nbEventRead = 0;
        
-       private Integer lastDrawPosition = 0;
+       private int     lastDrawPosition = 0;
        
        private HistogramCanvas parentCanvas = null;
        
-       private Boolean requestCompleted = false;
+       private boolean isCompleted = false;
        
        /**
         * Constructor for HistogramRequest.<p>
@@ -49,8 +54,10 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
         * 
         * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
         */
-       public HistogramRequest(TmfTimeRange range, Integer nbRequested, HistogramCanvas newParentCanvas, Long timeInterval) {
-        super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ);
+       public HistogramRequest(TmfTimeRange range, int nbRequested, HistogramCanvas newParentCanvas, long timeInterval, ITmfDataRequest.ExecutionType execType) {
+        super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ, execType);
+        
+       setIsCompleted(false);
         
         // *** FIXME ***
         // This does not work! The request won't be processed or the number of events returned is wrong!
@@ -58,11 +65,10 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
                //super((Class<LttngEvent>)dataType, range);
         
         parentCanvas = newParentCanvas;
-        histogramContent = parentCanvas.getHistogramContent();
         
         // Reset the content of the HistogramContent... the given data better be valid or this will fail.
-        histogramContent.clearContentData();
-        histogramContent.resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval);
+        parentCanvas.getHistogramContent().clearContentData();
+        parentCanvas.getHistogramContent().resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval);
         
         lastRangeTime = range.getStartTime().getValue();
         
@@ -74,39 +80,42 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
         * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
         * Calculation for the content is done here.
         */
-       @Override
-    public void handleData() {
-        TmfEvent[] result = getData();
-        TmfEvent[] evt = new TmfEvent[1];
-        
-        evt[0] = (result.length > 0) ? result[0] : null;
+//     @Override
+//    public void handleData() {
+//        LttngEvent[] result = getData();
+//        LttngEvent event = (result.length > 0) ? result[0] : null;
         
+       @Override
+    public void handleData(LttngEvent event) {
+               super.handleData(event);
+
         // *** FIXME ***
        // *** EVIL BUG ***
         // The request by timerange only does not work! (see constructor above) 
        //      However, the request with number of events will loop until it reach its number or EOF
        //  We have to filter out ourself the extra useless events!
        //
-        if ( (evt[0] != null) && (requestCompleted == false) ) {
-               LttngEvent tmpEvent = (LttngEvent)evt[0];
+        if (event != null) {
+        
+//                     Tracer.trace("Hst: " + event.getTimestamp());
                
                // This check is linked to the evil fix mentionned above
-               if ( ( tmpEvent.getTimestamp().getValue() >= histogramContent.getStartTime() ) &&
-                        ( tmpEvent.getTimestamp().getValue() <= histogramContent.getEndTime() ) )
+               if ( ( event.getTimestamp().getValue() >= parentCanvas.getHistogramContent().getStartTime() ) &&
+                        ( event.getTimestamp().getValue() <= parentCanvas.getHistogramContent().getEndTime() ) )
                {
                        
                        // Distance (in time) between this event and the last one we read
-                       long distance = ( tmpEvent.getTimestamp().getValue() - lastRangeTime );
+                       long distance = ( event.getTimestamp().getValue() - lastRangeTime );
                                
                        // Check if we changed of interval (the distance is higher than the interval time)
-                               if  ( distance > histogramContent.getElementsTimeInterval() ) {
+                               if  ( distance > parentCanvas.getHistogramContent().getElementsTimeInterval() ) {
                                        
-                                       histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
-                                       lastRangeTime = tmpEvent.getTimestamp().getValue();
+                                       parentCanvas.getHistogramContent().getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
+                                       lastRangeTime = event.getTimestamp().getValue();
                                        
                                        // * NOTE *
                                        // We can skip several interval at once, so we need to find what was our interval now
-                                       lastInterval = (int)((lastRangeTime - histogramContent.getStartTime()) / histogramContent.getElementsTimeInterval() );
+                                       lastInterval = (int)((lastRangeTime - parentCanvas.getHistogramContent().getStartTime()) / parentCanvas.getHistogramContent().getElementsTimeInterval() );
                                        
                                        // *** HACK ***
                                        // Because of the threads, weird phenomenons seem to happen here, like a position after the 
@@ -115,14 +124,14 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
                                        if ( lastInterval < 0 ) {
                                                lastInterval = 0;
                                        }
-                                       else if ( lastInterval >= histogramContent.getNbElement() ) {
-                                               lastInterval = (histogramContent.getNbElement()-1);
+                                       else if ( lastInterval >= parentCanvas.getHistogramContent().getNbElement() ) {
+                                               lastInterval = (parentCanvas.getHistogramContent().getNbElement()-1);
                                        }
                                        
                                        // * NOTE * 
                                        // We save the time we have here. This mean only the FIRST time read in an interval will be saved. 
-                                       histogramContent.getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime;
-                                       histogramContent.setReadyUpToPosition(lastInterval);
+                                       parentCanvas.getHistogramContent().getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime;
+                                       parentCanvas.getHistogramContent().setReadyUpToPosition(lastInterval);
                                        
                                        nbIntervalNotEmpty++;
                                        nbEventsInInterval = 1L;
@@ -130,11 +139,11 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
                                // We are still in the same interval, just keep counting
                                else {
                                        nbEventsInInterval++;
-                                       if ( nbEventsInInterval > histogramContent.getHeighestEventCount() ) {
-                                               histogramContent.setHeighestEventCount(nbEventsInInterval);
-                                       }
                                }
                                
+                               if ( nbEventsInInterval > parentCanvas.getHistogramContent().getHeighestEventCount() ) {
+                                       parentCanvas.getHistogramContent().setHeighestEventCount(nbEventsInInterval);
+                               }
                                nbEventRead++;
                                
                                // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
@@ -143,19 +152,20 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
                                        redrawAsyncronously();
                                }
                }
-               else {
-                       //System.out.println("Requested Timerange is : " + histogramContent.getStartTime() + " / " + histogramContent.getEndTime());
-                       //System.out.println("Time is : " + tmpEvent.getTimestamp().getValue());
-                       // *** FIXME ***
-               // *** EVIL FIX ***
-                // Because of the other evil bug (see above), we have to ignore extra useless events we will get
-                       // However, we might be far away from the end so we better start a redraw now
-                       if (tmpEvent.getTimestamp().getValue() >= histogramContent.getEndTime()) {
-                               redrawAsyncronously();
-                               requestCompleted = true;
-                       }
-               }
                }
+        // We got a null event! This mean we reach the end of the request. 
+        // Save the last interval we had, so we won't miss the very last events at the end. 
+        else {
+               // Save the last events
+               parentCanvas.getHistogramContent().getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
+               // We reached the end of the request, so assume we fill up the content as well
+               parentCanvas.getHistogramContent().setReadyUpToPosition(parentCanvas.getHistogramContent().getNbElement());
+                       
+                       // If the interval wasn't null, count this as a "non empty" interval
+                       if (nbEventsInInterval > 0) {
+                               nbIntervalNotEmpty++;
+                       }
+        }
     }
        
        /**
@@ -164,25 +174,28 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
         */
     @Override
     public void handleCompleted() {
+       setIsCompleted(true);
        parentCanvas.notifyParentUpdatedInformationAsynchronously();
                redrawAsyncronously();
+               super.handleCompleted();
+//             System.out.println(System.currentTimeMillis() + ": HistogramView (" + ((getExecType() == ExecutionType.LONG) ? "long" : "short") + ") completed");
     }
     
-    /**
-        * Function that is called when the request completed successfully.<p>
-        */
-    @Override
-    public void handleSuccess() {
-       // Nothing different from completed.
-    }
+//    /**
+//      * Function that is called when the request completed successfully.<p>
+//      */
+//    @Override
+//    public void handleSuccess() {
+//     // Nothing different from completed.
+//    }
     
-    /**
-        * Function that is called when the request completed in failure.<p>
-        */
-    @Override
-    public void handleFailure() {
-       // Nothing different from cancel.
-    }
+//    /**
+//      * Function that is called when the request completed in failure.<p>
+//      */
+//    @Override
+//    public void handleFailure() {
+//     // Nothing different from cancel.
+//    }
     
     /**
         * Function that is called when the request was cancelled.<p>
@@ -191,7 +204,7 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
     @Override
     public void handleCancel() {
        redrawAsyncronously();
-               requestCompleted = true;
+               super.handleCancel();
     }
        
     /**
@@ -203,26 +216,27 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
        // The average number of event is calculated while skipping empty interval if asked
        int averageNumberOfEvents = 0;
        if ( HistogramConstant.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE ) {
-               averageNumberOfEvents = nbEventRead / nbIntervalNotEmpty;
+               averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)nbIntervalNotEmpty);
        }
        else {
-               averageNumberOfEvents = nbEventRead / histogramContent.getNbElement();
+               averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)parentCanvas.getHistogramContent().getNbElement());
        }
-       histogramContent.setAverageNumberOfEvents(averageNumberOfEvents);
+       
+       parentCanvas.getHistogramContent().setAverageNumberOfEvents(averageNumberOfEvents);
        
        // It is possible that the height factor didn't change; 
        //              If not, we only need to redraw the updated section, no the whole content
        // Save the actual height, recalculate the height and check if there was any changes
-       double previousHeightFactor = histogramContent.getHeightFactor();
-       histogramContent.recalculateHeightFactor();
-       if ( histogramContent.getHeightFactor() != previousHeightFactor ) {
-                       histogramContent.recalculateEventHeight();
+       double previousHeightFactor = parentCanvas.getHistogramContent().getHeightFactor();
+       parentCanvas.getHistogramContent().recalculateHeightFactor();
+       if ( parentCanvas.getHistogramContent().getHeightFactor() != previousHeightFactor ) {
+               parentCanvas.getHistogramContent().recalculateEventHeight();
        }
        else {
-               histogramContent.recalculateEventHeightInInterval(lastDrawPosition, histogramContent.getReadyUpToPosition());
+               parentCanvas.getHistogramContent().recalculateEventHeightInInterval(lastDrawPosition, parentCanvas.getHistogramContent().getReadyUpToPosition());
        }
        
-       lastDrawPosition = histogramContent.getReadyUpToPosition();
+       lastDrawPosition = parentCanvas.getHistogramContent().getReadyUpToPosition();
     }
     
     /**
@@ -233,5 +247,21 @@ public class HistogramRequest extends TmfEventRequest<LttngEvent> {
        // Canvas redraw is already asynchronous
        parentCanvas.redrawAsynchronously();
     }
+
+       /**
+        * Getter for isCompleted variable
+        * @return true if the request is completed
+        */
+       public boolean getIsCompleted() {
+               return isCompleted;
+       }
+
+       /**
+        * Setter for isCompleted variable
+        * @param isCompleted value to set the completed flag
+        */
+       public void setIsCompleted(boolean isCompleted) {
+               this.isCompleted = isCompleted;
+       }
     
 }
This page took 0.041498 seconds and 5 git commands to generate.