try new method for sending find peer requests
[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 /**
42  * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
43  */
44 struct GNUNET_BANDWIDTH_Value32NBO
45 {
46   /**
47    * The actual value (bytes per second).
48    */
49   uint32_t value__ GNUNET_PACKED;
50 };
51
52
53 /**
54  * Struct to track available bandwidth.  Combines a time stamp with a
55  * number of bytes transmitted, a quota and a maximum amount that
56  * carries over.  Not opaque so that it can be inlined into data
57  * structures (reducing malloc-ing); however, values should not be
58  * accessed directly by clients (hence the '__').
59  */
60 struct GNUNET_BANDWIDTH_Tracker
61 {
62   /**
63    * Number of bytes consumed since we last updated the tracker.
64    */
65   int64_t consumption_since_last_update__;
66
67   /**
68    * Time when we last updated the tracker.
69    */
70   struct GNUNET_TIME_Absolute last_update__;
71
72   /**
73    * Bandwidth limit to enforce in bytes per s.
74    */
75   uint32_t available_bytes_per_s__;
76
77   /**
78    * Maximum number of seconds over which bandwidth may "accumulate".
79    * Note that additionally, we also always allow at least
80    * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.
81    */
82   uint32_t max_carry_s__;
83 };
84
85
86 /**
87  * Create a new bandwidth value.
88  *
89  * @param bytes_per_second value to create
90  * @return the new bandwidth value
91  */
92 struct GNUNET_BANDWIDTH_Value32NBO
93 GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second);
94
95
96 /**
97  * At the given bandwidth, calculate how much traffic will be
98  * available until the given deadline.
99  *
100  * @param bps bandwidth
101  * @param deadline when is the deadline
102  * @return number of bytes available at bps until deadline
103  */
104 uint64_t 
105 GNUNET_BANDWIDTH_value_get_available_until (struct GNUNET_BANDWIDTH_Value32NBO bps,
106                                             struct GNUNET_TIME_Relative deadline);
107
108
109 /**
110  * At the given bandwidth, calculate how long it would take for
111  * 'size' bytes to be transmitted.
112  *
113  * @param bps bandwidth
114  * @param size number of bytes we want to have available
115  * @return how long it would take
116  */
117 struct GNUNET_TIME_Relative
118 GNUNET_BANDWIDTH_value_get_delay_for (struct GNUNET_BANDWIDTH_Value32NBO bps,
119                                       uint64_t size);
120
121
122
123 /**
124  * Compute the MIN of two bandwidth values.
125  *
126  * @param b1 first value
127  * @param b2 second value
128  * @return the min of b1 and b2
129  */
130 struct GNUNET_BANDWIDTH_Value32NBO
131 GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1,
132                             struct GNUNET_BANDWIDTH_Value32NBO b2);
133
134
135 /**
136  * Initialize bandwidth tracker.  Note that in addition to the
137  * 'max_carry_s' limit, we also always allow at least
138  * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
139  * bytes-per-second limit is so small that within 'max_carry_s' not
140  * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
141  * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
142  * bytes).
143  *
144  * @param av tracker to initialize
145  * @param bytes_per_second_limit initial limit to assume
146  * @param max_carry_s maximum number of seconds unused bandwidth
147  *        may accumulate before it expires
148  */
149 void
150 GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av,
151                                struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit,
152                                uint32_t max_carry_s);
153
154
155 /**
156  * Notify the tracker that a certain number of bytes of bandwidth have
157  * been consumed.  Note that it is legal to consume bytes even if not
158  * enough bandwidth is available (in that case,
159  * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values
160  * even for a size of zero for a while).
161  *
162  * @param av tracker to update
163  * @param size number of bytes consumed
164  * @return GNUNET_YES if this consumption is above the limit
165  */
166 int
167 GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av,
168                                   ssize_t size);
169
170
171 /**
172  * Compute how long we should wait until consuming 'size'
173  * bytes of bandwidth in order to stay within the given
174  * quota.
175  *
176  * @param av tracker to query
177  * @param size number of bytes we would like to consume
178  * @return time to wait for consumption to be OK
179  */
180 struct GNUNET_TIME_Relative
181 GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av,
182                                     size_t size);
183
184
185 /**
186  * Compute how many bytes are available for consumption right now.
187  * quota.
188  *
189  * @param av tracker to query
190  * @return number of bytes available for consumption right now
191  */
192 int64_t 
193 GNUNET_BANDWIDTH_tracker_get_available (struct GNUNET_BANDWIDTH_Tracker *av);
194
195
196 /**
197  * Update quota of bandwidth tracker.
198  *
199  * @param av tracker to initialize
200  * @param bytes_per_second_limit new limit to assume
201  */
202 void
203 GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av,
204                                        struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit);
205
206
207 #if 0                           /* keep Emacsens' auto-indent happy */
208 {
209 #endif
210 #ifdef __cplusplus
211 }
212 #endif
213
214 /* ifndef GNUNET_BANDWIDTH_LIB_H */
215 #endif
216 /* end of gnunet_bandwidth_lib.h */