~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/core/networkmanager/impl/ByteBucketST.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on Dec 1, 2008
 
3
 * Created by Paul Gardner
 
4
 * 
 
5
 * Copyright 2008 Vuze, Inc.  All rights reserved.
 
6
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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.
 
19
 */
 
20
 
 
21
 
 
22
package com.aelitis.azureus.core.networkmanager.impl;
 
23
 
 
24
import org.gudy.azureus2.core3.util.Debug;
 
25
import org.gudy.azureus2.core3.util.SystemTime;
 
26
 
 
27
import com.aelitis.azureus.core.networkmanager.NetworkManager;
 
28
 
 
29
public class 
 
30
ByteBucketST 
 
31
        implements ByteBucket
 
32
{
 
33
          private int rate;
 
34
          private int burst_rate;
 
35
          private long avail_bytes;
 
36
          private long prev_update_time;
 
37
          
 
38
          
 
39
          /**
 
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
 
43
           */
 
44
          public ByteBucketST( int rate_bytes_per_sec ) {
 
45
            this( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5) );
 
46
          }
 
47
          
 
48
          /**
 
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
 
53
           */
 
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();
 
60
          }
 
61
          
 
62
          
 
63
          /**
 
64
           * Get the number of bytes currently available for use.
 
65
           * @return number of free bytes
 
66
           */
 
67
          public int getAvailableByteCount() {
 
68
                if ( avail_bytes < NetworkManager.UNLIMITED_RATE ){
 
69
                        update_avail_byte_count();
 
70
                }
 
71
                
 
72
            return (int)avail_bytes;
 
73
          }
 
74
          
 
75
          
 
76
          /**
 
77
           * Update the bucket with the number of bytes just used.
 
78
           * @param bytes_used
 
79
           */
 
80
          public void setBytesUsed( int bytes_used ) {
 
81
                if ( avail_bytes >= NetworkManager.UNLIMITED_RATE ){
 
82
                  return;
 
83
                }
 
84
                
 
85
            avail_bytes -= bytes_used;
 
86
            if( avail_bytes < 0 ) Debug.out( "avail_bytes < 0: " + avail_bytes);
 
87
          }
 
88
          
 
89
          
 
90
          /**
 
91
           * Get the configured fill rate.
 
92
           * @return guaranteed rate in bytes per sec
 
93
           */
 
94
          public int getRate() {  return rate;  }
 
95
          
 
96
          
 
97
          /**
 
98
           * Get the configured burst rate.
 
99
           * @return burst rate in bytes per sec
 
100
           */
 
101
          public int getBurstRate() {  return burst_rate;  }
 
102
          
 
103
          
 
104
          /**
 
105
           * Set the current fill/guaranteed rate, with a burst rate of 1.2X the given rate.
 
106
           * @param rate_bytes_per_sec
 
107
           */
 
108
          public void setRate( int rate_bytes_per_sec ) {
 
109
            setRate( rate_bytes_per_sec, rate_bytes_per_sec + (rate_bytes_per_sec/5));
 
110
          }
 
111
          
 
112
          
 
113
          /**
 
114
           * Set the current fill/guaranteed rate, along with the burst rate.
 
115
           * @param rate_bytes_per_sec
 
116
           * @param burst_rate
 
117
           */
 
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;
 
122
            }
 
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;
 
126
            }
 
127
            this.rate = rate_bytes_per_sec;
 
128
            this.burst_rate = burst_rate;
 
129
            if ( avail_bytes > burst_rate ){
 
130
                avail_bytes = burst_rate;
 
131
            }
 
132
            ensureByteBucketMinBurstRate();
 
133
          }
 
134
          
 
135
          
 
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);
 
143
              }
 
144
              else if (prev_update_time >now) { //oops, time went backwards
 
145
                  avail_bytes =burst_rate;
 
146
                  prev_update_time =now;
 
147
              }
 
148
          }
 
149
 
 
150
          
 
151
          /**
 
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.
 
154
           */
 
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
 
159
            }
 
160
          }
 
161
          
 
162
}