diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-03-05 15:43:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-03-05 15:43:20 +0000 |
commit | 3caf199795bd1c7b475f9cb7941186ee4b66eaec (patch) | |
tree | 51bac749422d8d376efdc6dbfb8c636d4d09f581 /src/include/gnunet_bandwidth_lib.h | |
parent | 441d62754d26edab61ef7fd9bc5c3f6c3c59af5a (diff) | |
download | gnunet-3caf199795bd1c7b475f9cb7941186ee4b66eaec.tar.gz gnunet-3caf199795bd1c7b475f9cb7941186ee4b66eaec.zip |
bwlib
Diffstat (limited to 'src/include/gnunet_bandwidth_lib.h')
-rw-r--r-- | src/include/gnunet_bandwidth_lib.h | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/include/gnunet_bandwidth_lib.h b/src/include/gnunet_bandwidth_lib.h new file mode 100644 index 000000000..2dbe6e562 --- /dev/null +++ b/src/include/gnunet_bandwidth_lib.h | |||
@@ -0,0 +1,178 @@ | |||
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 | * | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #ifndef GNUNET_BANDWIDTH_LIB_H | ||
29 | #define GNUNET_BANDWIDTH_LIB_H | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" | ||
33 | { | ||
34 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | #include "gnunet_common.h" | ||
40 | #include "gnunet_time_lib.h" | ||
41 | |||
42 | /** | ||
43 | * 32-bit bandwidth used for network exchange by GNUnet, in bytes per second. | ||
44 | */ | ||
45 | struct GNUNET_BANDWIDTH_Value32NBO | ||
46 | { | ||
47 | /** | ||
48 | * The actual value (bytes per second). | ||
49 | */ | ||
50 | uint32_t value__ GNUNET_PACKED; | ||
51 | }; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Struct to track available bandwidth. Combines a time stamp with a | ||
56 | * number of bytes transmitted, a quota and a maximum amount that | ||
57 | * carries over. Not opaque so that it can be inlined into data | ||
58 | * structures (reducing malloc-ing); however, values should not be | ||
59 | * accessed directly by clients (hence the '__'). | ||
60 | */ | ||
61 | struct GNUNET_BANDWIDTH_Tracker | ||
62 | { | ||
63 | /** | ||
64 | * Number of bytes consumed since we last updated the tracker. | ||
65 | */ | ||
66 | uint64_t consumption_since_last_update__; | ||
67 | |||
68 | /** | ||
69 | * Time when we last updated the tracker. | ||
70 | */ | ||
71 | struct GNUNET_TIME_Absolute last_update__; | ||
72 | |||
73 | /** | ||
74 | * Bandwidth limit to enforce in bytes per s. | ||
75 | */ | ||
76 | uint32_t available_bytes_per_s__; | ||
77 | |||
78 | /** | ||
79 | * Maximum number of seconds over which bandwidth may "accumulate". | ||
80 | * Note that additionally, we also always allow at least | ||
81 | * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. | ||
82 | */ | ||
83 | uint32_t max_carry_s__; | ||
84 | }; | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Create a new bandwidth value. | ||
89 | * | ||
90 | * @param bytes_per_second value to create | ||
91 | * @return the new bandwidth value | ||
92 | */ | ||
93 | struct GNUNET_BANDWIDTH_Value32NBO | ||
94 | GNUNET_BANDWIDTH_value_init (uint32_t bytes_per_second); | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Compute the MIN of two bandwidth values. | ||
99 | * | ||
100 | * @param b1 first value | ||
101 | * @param b2 second value | ||
102 | * @return the min of b1 and b2 | ||
103 | */ | ||
104 | struct GNUNET_BANDWIDTH_Value32NBO | ||
105 | GNUNET_BANDWIDTH_value_min (struct GNUNET_BANDWIDTH_Value32NBO b1, | ||
106 | struct GNUNET_BANDWIDTH_Value32NBO b2); | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Initialize bandwidth tracker. Note that in addition to the | ||
111 | * 'max_carry_s' limit, we also always allow at least | ||
112 | * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate. So if the | ||
113 | * bytes-per-second limit is so small that within 'max_carry_s' not | ||
114 | * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is | ||
115 | * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in | ||
116 | * bytes). | ||
117 | * | ||
118 | * @param av tracker to initialize | ||
119 | * @param bytes_per_second_limit initial limit to assume | ||
120 | * @param max_carry_s maximum number of seconds unused bandwidth | ||
121 | * may accumulate before it expires | ||
122 | */ | ||
123 | void | ||
124 | GNUNET_BANDWIDTH_tracker_init (struct GNUNET_BANDWIDTH_Tracker *av, | ||
125 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit, | ||
126 | uint32_t max_carry_s); | ||
127 | |||
128 | |||
129 | /** | ||
130 | * Notify the tracker that a certain number of bytes of bandwidth have | ||
131 | * been consumed. Note that it is legal to consume bytes even if not | ||
132 | * enough bandwidth is available (in that case, | ||
133 | * GNUNET_BANDWIDTH_tracker_get_delay may return non-zero delay values | ||
134 | * even for a size of zero for a while). | ||
135 | * | ||
136 | * @param av tracker to update | ||
137 | * @param size number of bytes consumed | ||
138 | */ | ||
139 | void | ||
140 | GNUNET_BANDWIDTH_tracker_consume (struct GNUNET_BANDWIDTH_Tracker *av, | ||
141 | size_t size); | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Compute how long we should wait until consuming 'size' | ||
146 | * bytes of bandwidth in order to stay within the given | ||
147 | * quota. | ||
148 | * | ||
149 | * @param av tracker to query | ||
150 | * @param size number of bytes we would like to consume | ||
151 | * @return time to wait for consumption to be OK | ||
152 | */ | ||
153 | struct GNUNET_TIME_Relative | ||
154 | GNUNET_BANDWIDTH_tracker_get_delay (struct GNUNET_BANDWIDTH_Tracker *av, | ||
155 | size_t size); | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Update quota of bandwidth tracker. | ||
160 | * | ||
161 | * @param av tracker to initialize | ||
162 | * @param bytes_per_second_limit new limit to assume | ||
163 | */ | ||
164 | void | ||
165 | GNUNET_BANDWIDTH_tracker_update_quota (struct GNUNET_BANDWIDTH_Tracker *av, | ||
166 | struct GNUNET_BANDWIDTH_Value32NBO bytes_per_second_limit); | ||
167 | |||
168 | |||
169 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
170 | { | ||
171 | #endif | ||
172 | #ifdef __cplusplus | ||
173 | } | ||
174 | #endif | ||
175 | |||
176 | /* ifndef GNUNET_BANDWIDTH_LIB_H */ | ||
177 | #endif | ||
178 | /* end of gnunet_bandwidth_lib.h */ | ||