2
* Created on Dec 1, 2008
3
* Created by Paul Gardner
5
* Copyright 2008 Vuze, Inc. All rights reserved.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License only.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22
package com.aelitis.azureus.core.networkmanager.impl;
24
import org.gudy.azureus2.core3.util.Debug;
25
import org.gudy.azureus2.core3.util.SystemTime;
27
import com.aelitis.azureus.core.networkmanager.NetworkManager;
34
private int burst_rate;
35
private long avail_bytes;
36
private long prev_update_time;
40
* Create a new byte-bucket with the given byte fill (guaranteed) rate.
41
* Burst rate is set to default 1.2X of given fill rate.
42
* @param rate_bytes_per_sec fill rate
44
public ByteBucketST( int rate_bytes_per_sec ) {
45
this( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5) );
49
* Create a new byte-bucket with the given byte fill (guaranteed) rate
50
* and the given burst rate.
51
* @param rate_bytes_per_sec fill rate
52
* @param burst_rate max rate
54
private ByteBucketST( int rate_bytes_per_sec, int burst_rate ) {
55
this.rate = rate_bytes_per_sec;
56
this.burst_rate = burst_rate;
57
avail_bytes = 0; //start bucket empty
58
prev_update_time = SystemTime.getCurrentTime();
59
ensureByteBucketMinBurstRate();
64
* Get the number of bytes currently available for use.
65
* @return number of free bytes
67
public int getAvailableByteCount() {
68
if ( avail_bytes < NetworkManager.UNLIMITED_RATE ){
69
update_avail_byte_count();
72
return (int)avail_bytes;
77
* Update the bucket with the number of bytes just used.
80
public void setBytesUsed( int bytes_used ) {
81
if ( avail_bytes >= NetworkManager.UNLIMITED_RATE ){
85
avail_bytes -= bytes_used;
86
if( avail_bytes < 0 ) Debug.out( "avail_bytes < 0: " + avail_bytes);
91
* Get the configured fill rate.
92
* @return guaranteed rate in bytes per sec
94
public int getRate() { return rate; }
98
* Get the configured burst rate.
99
* @return burst rate in bytes per sec
101
public int getBurstRate() { return burst_rate; }
105
* Set the current fill/guaranteed rate, with a burst rate of 1.2X the given rate.
106
* @param rate_bytes_per_sec
108
public void setRate( int rate_bytes_per_sec ) {
109
setRate( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5));
114
* Set the current fill/guaranteed rate, along with the burst rate.
115
* @param rate_bytes_per_sec
118
public void setRate( int rate_bytes_per_sec, int burst_rate ) {
119
if( rate_bytes_per_sec < 0 ) {
120
Debug.out("rate_bytes_per_sec [" +rate_bytes_per_sec+ "] < 0");
121
rate_bytes_per_sec = 0;
123
if( burst_rate < rate_bytes_per_sec ) {
124
Debug.out("burst_rate [" +burst_rate+ "] < rate_bytes_per_sec [" +rate_bytes_per_sec+ "]");
125
burst_rate = rate_bytes_per_sec;
127
this.rate = rate_bytes_per_sec;
128
this.burst_rate = burst_rate;
129
if ( avail_bytes > burst_rate ){
130
avail_bytes = burst_rate;
132
ensureByteBucketMinBurstRate();
136
private void update_avail_byte_count() {
137
final long now =SystemTime.getCurrentTime();
138
if (prev_update_time <now) {
139
avail_bytes +=((now -prev_update_time) * rate) / 1000;
140
prev_update_time =now;
141
if( avail_bytes > burst_rate ) avail_bytes = burst_rate;
142
else if( avail_bytes < 0 ) Debug.out("ERROR: avail_bytes < 0: " + avail_bytes);
144
else if (prev_update_time >now) { //oops, time went backwards
145
avail_bytes =burst_rate;
146
prev_update_time =now;
152
* Make sure the bucket's burst rate is at least MSS-sized,
153
* otherwise it will never allow a full packet's worth of data.
155
private void ensureByteBucketMinBurstRate() {
156
int mss = NetworkManager.getMinMssSize();
157
if( burst_rate < mss ) { //oops, this won't ever allow a full packet
158
burst_rate = mss; //so increase the max byte size