FileDocCategorySizeDatePackage
ByteBucket.javaAPI DocAzureus 3.0.3.44852Fri Jun 23 07:06:08 BST 2006com.aelitis.azureus.core.networkmanager.impl

ByteBucket

public class ByteBucket extends Object
Byte-bucket implementation based on the token bucket algorithm. Buckets can be configured with a guaranteed normal rate, along with a burst rate.

Fields Summary
private int
rate
private int
burst_rate
private long
avail_bytes
private long
prev_update_time
Constructors Summary
public ByteBucket(int rate_bytes_per_sec)
Create a new byte-bucket with the given byte fill (guaranteed) rate. Burst rate is set to default 1.2X of given fill rate.

param
rate_bytes_per_sec fill rate

    this( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5) );
  
public ByteBucket(int rate_bytes_per_sec, int burst_rate)
Create a new byte-bucket with the given byte fill (guaranteed) rate and the given burst rate.

param
rate_bytes_per_sec fill rate
param
burst_rate max rate

    this.rate = rate_bytes_per_sec;
    this.burst_rate = burst_rate;
    avail_bytes = 0; //start bucket empty
    prev_update_time = SystemTime.getCurrentTime();
    ensureByteBucketMinBurstRate();
  
Methods Summary
private voidensureByteBucketMinBurstRate()
Make sure the bucket's burst rate is at least MSS-sized, otherwise it will never allow a full packet's worth of data.

    int mss = NetworkManager.getMinMssSize();
    if( burst_rate < mss ) {  //oops, this won't ever allow a full packet
      burst_rate = mss;  //so increase the max byte size
    }
  
public intgetAvailableByteCount()
Get the number of bytes currently available for use.

return
number of free bytes

    update_avail_byte_count();
    return (int)avail_bytes;
  
public intgetBurstRate()
Get the configured burst rate.

return
burst rate in bytes per sec

  return burst_rate;  
public intgetRate()
Get the configured fill rate.

return
guaranteed rate in bytes per sec

  return rate;  
public voidsetBytesUsed(int bytes_used)
Update the bucket with the number of bytes just used.

param
bytes_used

    avail_bytes -= bytes_used;
    if( avail_bytes < 0 ) Debug.out( "avail_bytes < 0: " + avail_bytes);
  
public voidsetRate(int rate_bytes_per_sec)
Set the current fill/guaranteed rate, with a burst rate of 1.2X the given rate.

param
rate_bytes_per_sec

    setRate( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5));
  
public voidsetRate(int rate_bytes_per_sec, int burst_rate)
Set the current fill/guaranteed rate, along with the burst rate.

param
rate_bytes_per_sec
param
burst_rate

    if( rate_bytes_per_sec < 0 ) {
      Debug.out("rate_bytes_per_sec [" +rate_bytes_per_sec+ "] < 0");
      rate_bytes_per_sec = 0;
    }
    if( burst_rate < rate_bytes_per_sec ) {
      Debug.out("burst_rate [" +burst_rate+ "] < rate_bytes_per_sec [" +rate_bytes_per_sec+ "]");
      burst_rate = rate_bytes_per_sec;
    }
    this.rate = rate_bytes_per_sec;
    this.burst_rate = burst_rate;
    ensureByteBucketMinBurstRate();
  
private voidupdate_avail_byte_count()

      final long now =SystemTime.getCurrentTime();
      if (prev_update_time <now) {
          avail_bytes +=((now -prev_update_time) * rate) / 1000;
          prev_update_time =now;
          if( avail_bytes > burst_rate ) avail_bytes = burst_rate;
          else if( avail_bytes < 0 )  Debug.out("ERROR: avail_bytes < 0: " + avail_bytes);
      }
      else if (prev_update_time >now) {	//oops, time went backwards
          avail_bytes =burst_rate;
          prev_update_time =now;
      }