src/core/net/sf/basedb/util/TransferRateProgressReporter.java

Code
Comments
Other
Rev Date Author Line
6070 21 Jun 12 nicklas 1 package net.sf.basedb.util;
6070 21 Jun 12 nicklas 2
6070 21 Jun 12 nicklas 3 import net.sf.basedb.core.AbsoluteProgressReporter;
6070 21 Jun 12 nicklas 4
6070 21 Jun 12 nicklas 5 /**
6070 21 Jun 12 nicklas 6   A progress reporter implementation that has a "side-effect" 
6070 21 Jun 12 nicklas 7   that limits the transfer rate. The parent progress reporter
6070 21 Jun 12 nicklas 8   is optional.
6070 21 Jun 12 nicklas 9   
6070 21 Jun 12 nicklas 10   @author nicklas
6070 21 Jun 12 nicklas 11   @since 3.2
6070 21 Jun 12 nicklas 12 */
6070 21 Jun 12 nicklas 13 public class TransferRateProgressReporter
6070 21 Jun 12 nicklas 14   implements AbsoluteProgressReporter 
6070 21 Jun 12 nicklas 15 {
6070 21 Jun 12 nicklas 16
6070 21 Jun 12 nicklas 17   private final AbsoluteProgressReporter parent;
6070 21 Jun 12 nicklas 18   private final int maxTicksPerSecond;
6070 21 Jun 12 nicklas 19   private final int maxDelay;
6070 21 Jun 12 nicklas 20   private long startTime;
6070 21 Jun 12 nicklas 21   
6070 21 Jun 12 nicklas 22   /**
6070 21 Jun 12 nicklas 23     Create a new progress reporter. Max delay per progress update is 8 seconds.
6070 21 Jun 12 nicklas 24     
6070 21 Jun 12 nicklas 25     @param parent An optional parent progress reporter
6132 14 Sep 12 nicklas 26     @param maxTicksPerSecond Max allowed ticks-per-second, if the rate goes above this,
6070 21 Jun 12 nicklas 27       the {@link #displayAbsolute(long, String)} method will wait a short time
6070 21 Jun 12 nicklas 28       before returning
6070 21 Jun 12 nicklas 29   */
6070 21 Jun 12 nicklas 30   public TransferRateProgressReporter(AbsoluteProgressReporter parent, int maxTicksPerSecond)
6070 21 Jun 12 nicklas 31   {
6070 21 Jun 12 nicklas 32     this(parent, maxTicksPerSecond, 8000);
6070 21 Jun 12 nicklas 33   }
6070 21 Jun 12 nicklas 34   
6070 21 Jun 12 nicklas 35   
6070 21 Jun 12 nicklas 36   /**
6070 21 Jun 12 nicklas 37     Create a new progress reporter. Max delay per progress update is 8 seconds.
6070 21 Jun 12 nicklas 38     
6070 21 Jun 12 nicklas 39     @param parent An optional parent progress reporter
6132 14 Sep 12 nicklas 40     @param maxTicksPerSecond Max allowed ticks-per-second, if the rate goes above this,
6070 21 Jun 12 nicklas 41       the {@link #displayAbsolute(long, String)} method will wait a short time
6070 21 Jun 12 nicklas 42       before returning
6070 21 Jun 12 nicklas 43     @param maxDelay The max delay in milliseconds for each call to {@link #displayAbsolute(long, String)}
6070 21 Jun 12 nicklas 44   */
6070 21 Jun 12 nicklas 45   public TransferRateProgressReporter(AbsoluteProgressReporter parent, int maxTicksPerSecond, int maxDelay)
6070 21 Jun 12 nicklas 46   {
6070 21 Jun 12 nicklas 47     this.parent = parent;
6070 21 Jun 12 nicklas 48     this.maxTicksPerSecond = maxTicksPerSecond;
6070 21 Jun 12 nicklas 49     this.maxDelay = maxDelay;
6070 21 Jun 12 nicklas 50   }
6070 21 Jun 12 nicklas 51   
6070 21 Jun 12 nicklas 52   
6070 21 Jun 12 nicklas 53   /*
6070 21 Jun 12 nicklas 54     From the AbsoluteProgressReporter interface
6070 21 Jun 12 nicklas 55     -------------------------------------------
6070 21 Jun 12 nicklas 56   */
6070 21 Jun 12 nicklas 57   /**
6070 21 Jun 12 nicklas 58     Forward the call to the parent if one exists. There is no
6070 21 Jun 12 nicklas 59     rate control in this method.
6070 21 Jun 12 nicklas 60   */
6070 21 Jun 12 nicklas 61   @Override
6070 21 Jun 12 nicklas 62   public void display(int percent, String message) 
6070 21 Jun 12 nicklas 63   {
6070 21 Jun 12 nicklas 64     if (parent != null) parent.display(percent, message);
6070 21 Jun 12 nicklas 65   }
6070 21 Jun 12 nicklas 66
6070 21 Jun 12 nicklas 67   /**
6070 21 Jun 12 nicklas 68     Forward the call to the parent if one exists.
6070 21 Jun 12 nicklas 69   */
6070 21 Jun 12 nicklas 70   @Override
6070 21 Jun 12 nicklas 71   public void append(String message)
6070 21 Jun 12 nicklas 72   {
6070 21 Jun 12 nicklas 73     if (parent != null) parent.append(message);
6070 21 Jun 12 nicklas 74   }
6070 21 Jun 12 nicklas 75
6070 21 Jun 12 nicklas 76   /**
6070 21 Jun 12 nicklas 77     Forward the call to the parent if one exists. Then
6070 21 Jun 12 nicklas 78     check the number of completed ticks and time since start
6070 21 Jun 12 nicklas 79     against the max transfer rate. If the max rate has been exceeded
6070 21 Jun 12 nicklas 80     pause the thread for a while.
6070 21 Jun 12 nicklas 81   */
6070 21 Jun 12 nicklas 82   @Override
6070 21 Jun 12 nicklas 83   public void displayAbsolute(long completed, String message) 
6070 21 Jun 12 nicklas 84   {
6070 21 Jun 12 nicklas 85     if (parent != null)  parent.displayAbsolute(completed, message);
6070 21 Jun 12 nicklas 86     
6070 21 Jun 12 nicklas 87     if (startTime == 0) 
6070 21 Jun 12 nicklas 88     {
6070 21 Jun 12 nicklas 89       startTime = System.currentTimeMillis();
6070 21 Jun 12 nicklas 90     }
6070 21 Jun 12 nicklas 91     else 
6070 21 Jun 12 nicklas 92     {
6070 21 Jun 12 nicklas 93       // Calculate actual time and expected time in milliseconds
6070 21 Jun 12 nicklas 94       long actualTimeSinceStart = System.currentTimeMillis() - startTime;
6070 21 Jun 12 nicklas 95       long expectedTimeSinceStart = 1000 * completed / maxTicksPerSecond;
6070 21 Jun 12 nicklas 96       // If actual time is less than expected (with some slack) pause the thread 
6070 21 Jun 12 nicklas 97       long delay = expectedTimeSinceStart - actualTimeSinceStart;
6070 21 Jun 12 nicklas 98       if (delay > 1000)
6070 21 Jun 12 nicklas 99       {
6070 21 Jun 12 nicklas 100         // Do not pause more than 8 seconds
6070 21 Jun 12 nicklas 101         if (delay > 8000) delay = 8000; 
6070 21 Jun 12 nicklas 102         try
6070 21 Jun 12 nicklas 103         {
6070 21 Jun 12 nicklas 104           java.lang.Thread.sleep(delay);
6070 21 Jun 12 nicklas 105         }
6070 21 Jun 12 nicklas 106         catch (InterruptedException ex)
6070 21 Jun 12 nicklas 107         {}
6070 21 Jun 12 nicklas 108       }
6070 21 Jun 12 nicklas 109     }
6070 21 Jun 12 nicklas 110   }
6070 21 Jun 12 nicklas 111
6070 21 Jun 12 nicklas 112 }