adding Ludo's gnunet-download-manager.scm back to SVN HEAD
[oweals/gnunet.git] / src / include / gnunet_bandwidth_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_bandwidth_lib.h
23  * @brief functions related to bandwidth (unit)
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_BANDWIDTH_LIB_H
28 #define GNUNET_BANDWIDTH_LIB_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_common.h"
39 #include "gnunet_time_lib.h"
40
41 GNUNET_NETWORK_STRUCT_BEGIN
42
43 /**
44  * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
45  */
46 struct GNUNET_BANDWIDTH_Value32NBO
47 {
48   /**
49    * The actual value (bytes per second).
50    */
51   uint32_t value__ GNUNET_PACKED;
52 };
53 GNUNET_NETWORK_STRUCT_END
54
55 /**
56  * Struct to track available bandwidth.  Combines a time stamp with a
57  * number of bytes transmitted, a quota and a maximum amount that
58  * carries over.  Not opaque so that it can be inlined into data
59  * structures (reducing malloc-ing); however, values should not be
60  * accessed directly by clients (hence the '__').
61  */
62 struct GNUNET_BANDWIDTH_Tracker
63 {
64   /**
65    * Number of bytes consumed since we last updated the tracker.
66    */
67   int64_t consumption_since_last_update__;
68
69   /**
70    * Time when we last updated the tracker.
71    */
72   struct GNUNET_TIME_Absolute last_update__;
73
74   /**
75    * Bandwidth limit to enforce in bytes per s.
76    */
77   uint32_t available_bytes_per_s__;
78
79   /**
80    * Maximum number of seconds over which bandwidth may "accumulate".
81    * Note that additionally, we also always allow at least
82    * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.
83    */
84   uint32_t max_carry_s__;
85 };
86
87
88 /**
89  * Create a new bandwidth value.
90  *
91  * @param bytes_per_second value to create
92  * @return the new bandwidth value
93  */
94 struct GNUNET_BANDWIDTH_Value32NBO
95 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second);
96
97
98 /**
99  * Maximum possible bandwidth value.
100  */
101 #define GNUNET_BANDWIDTH_VALUE_MAX GNUNET_BANDWIDTH_value_init(UINT32_MAX)
102
103
104 /**
105  * At the given bandwidth, calculate how much traffic will be
106  * available until the given deadline.
107  *
108  * @param bps bandwidth
109  * @param deadline when is the deadline
110  * @return number of bytes available at bps until deadline
111  */
112 uint64_t
113 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO
114                                             bps,
115                                             struct GNUNET_TIME_Relative
116                                             deadline);
117
118
119 /**
120  * At the given bandwidth, calculate how long it would take for
121  * 'size' bytes to be transmitted.
122  *
123  * @param bps bandwidth
124  * @param size number of bytes we want to have available
125  * @return how long it would take
126  */
127 struct GNUNET_TIME_Relative
128 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
129                                       uint64_t size);
130
131
132
133 /**
134  * Compute the MIN of two bandwidth values.
135  *
136  * @param b1 first value
137  * @param b2 second value
138  * @return the min of b1 and b2
139  */
140 struct GNUNET_BANDWIDTH_Value32NBO
141 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
142                             struct GNUNET_BANDWIDTH_Value32NBO b2);
143
144
145 /**
146  * Initialize bandwidth tracker.  Note that in addition to the
147  * 'max_carry_s' limit, we also always allow at least
148  * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
149  * bytes-per-second limit is so small that within 'max_carry_s' not
150  * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
151  * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
152  * bytes).
153  *
154  * @param av tracker to initialize
155  * @param bytes_per_second_limit initial limit to assume
156  * @param max_carry_s maximum number of seconds unused bandwidth
157  *        may accumulate before it expires
158  */
159 void
160 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
161                                struct GNUNET_BANDWIDTH_Value32NBO
162                                bytes_per_second_limit, uint32_t max_carry_s);
163
164
165 /**
166  * Notify the tracker that a certain number of bytes of bandwidth have
167  * been consumed.  Note that it is legal to consume bytes even if not
168  * enough bandwidth is available (in that case,
169  * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
170  * even for a size of zero for a while).
171  *
172  * @param av tracker to update
173  * @param size number of bytes consumed
174  * @return GNUNET_YES if this consumption is above the limit
175  */
176 int
177 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
178                                   ssize_t size);
179
180
181 /**
182  * Compute how long we should wait until consuming 'size'
183  * bytes of bandwidth in order to stay within the given
184  * quota.
185  *
186  * @param av tracker to query
187  * @param size number of bytes we would like to consume
188  * @return time to wait for consumption to be OK
189  */
190 struct GNUNET_TIME_Relative
191 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
192                                     size_t size);
193
194
195 /**
196  * Compute how many bytes are available for consumption right now.
197  * quota.
198  *
199  * @param av tracker to query
200  * @return number of bytes available for consumption right now
201  */
202 int64_t
203 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
204
205
206 /**
207  * Update quota of bandwidth tracker.
208  *
209  * @param av tracker to initialize
210  * @param bytes_per_second_limit new limit to assume
211  */
212 void
213 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
214                                        struct GNUNET_BANDWIDTH_Value32NBO
215                                        bytes_per_second_limit);
216
217
218 #if 0                           /* keep Emacsens' auto-indent happy */
219 {
220 #endif
221 #ifdef __cplusplus
222 }
223 #endif
224
225 /* ifndef GNUNET_BANDWIDTH_LIB_H */
226 #endif
227 /* end of gnunet_bandwidth_lib.h */