diff options
Diffstat (limited to 'src/rps/gnunet-service-rps_view.c')
-rw-r--r-- | src/rps/gnunet-service-rps_view.c | 201 |
1 files changed, 100 insertions, 101 deletions
diff --git a/src/rps/gnunet-service-rps_view.c b/src/rps/gnunet-service-rps_view.c index 8a0cae21e..bedd2ae6c 100644 --- a/src/rps/gnunet-service-rps_view.c +++ b/src/rps/gnunet-service-rps_view.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file rps/gnunet-service-rps_view.c | 22 | * @file rps/gnunet-service-rps_view.c |
@@ -28,8 +28,7 @@ | |||
28 | #include "gnunet-service-rps_view.h" | 28 | #include "gnunet-service-rps_view.h" |
29 | #include <inttypes.h> | 29 | #include <inttypes.h> |
30 | 30 | ||
31 | struct View | 31 | struct View { |
32 | { | ||
33 | /** | 32 | /** |
34 | * Array containing the peers | 33 | * Array containing the peers |
35 | */ | 34 | */ |
@@ -54,16 +53,16 @@ struct View | |||
54 | * @return The newly created view | 53 | * @return The newly created view |
55 | */ | 54 | */ |
56 | struct View * | 55 | struct View * |
57 | View_create (uint32_t len) | 56 | View_create(uint32_t len) |
58 | { | 57 | { |
59 | struct View *view; | 58 | struct View *view; |
60 | 59 | ||
61 | view = GNUNET_new (struct View); | 60 | view = GNUNET_new(struct View); |
62 | view->length = len; | 61 | view->length = len; |
63 | view->array = GNUNET_new_array (len, struct GNUNET_PeerIdentity); | 62 | view->array = GNUNET_new_array(len, struct GNUNET_PeerIdentity); |
64 | view->mpm = | 63 | view->mpm = |
65 | GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be | 64 | GNUNET_CONTAINER_multipeermap_create(len, GNUNET_NO); /* might even be |
66 | * set to _YES */ | 65 | * set to _YES */ |
67 | return view; | 66 | return view; |
68 | } | 67 | } |
69 | 68 | ||
@@ -77,38 +76,38 @@ View_create (uint32_t len) | |||
77 | * @param len the (maximum) length for the view | 76 | * @param len the (maximum) length for the view |
78 | */ | 77 | */ |
79 | void | 78 | void |
80 | View_change_len (struct View *view, | 79 | View_change_len(struct View *view, |
81 | uint32_t len) | 80 | uint32_t len) |
82 | { | 81 | { |
83 | uint32_t i; | 82 | uint32_t i; |
84 | uint32_t *index; | 83 | uint32_t *index; |
85 | 84 | ||
86 | if (GNUNET_CONTAINER_multipeermap_size (view->mpm) < len) | 85 | if (GNUNET_CONTAINER_multipeermap_size(view->mpm) < len) |
87 | { /* Simply shrink */ | 86 | { /* Simply shrink */ |
88 | /* We might simply clear and free the left over space */ | 87 | /* We might simply clear and free the left over space */ |
89 | GNUNET_array_grow (view->array, view->length, len); | 88 | GNUNET_array_grow(view->array, view->length, len); |
90 | } | ||
91 | else /* We have to remove elements */ | ||
92 | { | ||
93 | /* TODO find a way to preserve indices */ | ||
94 | for (i = 0; i < len; i++) | ||
95 | { | ||
96 | index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]); | ||
97 | GNUNET_assert (NULL != index); | ||
98 | GNUNET_free (index); | ||
99 | } | 89 | } |
100 | GNUNET_array_grow (view->array, view->length, len); | 90 | else /* We have to remove elements */ |
101 | GNUNET_CONTAINER_multipeermap_destroy (view->mpm); | ||
102 | view->mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); | ||
103 | for (i = 0; i < len; i++) | ||
104 | { | 91 | { |
105 | index = GNUNET_new (uint32_t); | 92 | /* TODO find a way to preserve indices */ |
106 | *index = i; | 93 | for (i = 0; i < len; i++) |
107 | GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index, | 94 | { |
108 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | 95 | index = GNUNET_CONTAINER_multipeermap_get(view->mpm, &view->array[i]); |
96 | GNUNET_assert(NULL != index); | ||
97 | GNUNET_free(index); | ||
98 | } | ||
99 | GNUNET_array_grow(view->array, view->length, len); | ||
100 | GNUNET_CONTAINER_multipeermap_destroy(view->mpm); | ||
101 | view->mpm = GNUNET_CONTAINER_multipeermap_create(len, GNUNET_NO); | ||
102 | for (i = 0; i < len; i++) | ||
103 | { | ||
104 | index = GNUNET_new(uint32_t); | ||
105 | *index = i; | ||
106 | GNUNET_CONTAINER_multipeermap_put(view->mpm, &view->array[i], index, | ||
107 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
108 | } | ||
109 | } | 109 | } |
110 | } | 110 | GNUNET_assert(view->length == len); |
111 | GNUNET_assert (view->length == len); | ||
112 | } | 111 | } |
113 | 112 | ||
114 | 113 | ||
@@ -119,7 +118,7 @@ View_change_len (struct View *view, | |||
119 | * @return the view in array representation | 118 | * @return the view in array representation |
120 | */ | 119 | */ |
121 | const struct GNUNET_PeerIdentity * | 120 | const struct GNUNET_PeerIdentity * |
122 | View_get_as_array (const struct View *view) | 121 | View_get_as_array(const struct View *view) |
123 | { | 122 | { |
124 | return view->array; | 123 | return view->array; |
125 | } | 124 | } |
@@ -132,9 +131,9 @@ View_get_as_array (const struct View *view) | |||
132 | * @return current number of actually contained peers | 131 | * @return current number of actually contained peers |
133 | */ | 132 | */ |
134 | unsigned int | 133 | unsigned int |
135 | View_size (const struct View *view) | 134 | View_size(const struct View *view) |
136 | { | 135 | { |
137 | return GNUNET_CONTAINER_multipeermap_size (view->mpm); | 136 | return GNUNET_CONTAINER_multipeermap_size(view->mpm); |
138 | } | 137 | } |
139 | 138 | ||
140 | 139 | ||
@@ -148,25 +147,25 @@ View_size (const struct View *view) | |||
148 | * GNUNET_NO if peer was not inserted | 147 | * GNUNET_NO if peer was not inserted |
149 | */ | 148 | */ |
150 | int | 149 | int |
151 | View_put (struct View *view, | 150 | View_put(struct View *view, |
152 | const struct GNUNET_PeerIdentity *peer) | 151 | const struct GNUNET_PeerIdentity *peer) |
153 | { | 152 | { |
154 | uint32_t *index; | 153 | uint32_t *index; |
155 | 154 | ||
156 | if ((view->length <= View_size (view)) || /* If array is 'full' */ | 155 | if ((view->length <= View_size(view)) || /* If array is 'full' */ |
157 | (GNUNET_YES == View_contains_peer (view, peer))) | 156 | (GNUNET_YES == View_contains_peer(view, peer))) |
158 | { | 157 | { |
159 | return GNUNET_NO; | 158 | return GNUNET_NO; |
160 | } | 159 | } |
161 | else | 160 | else |
162 | { | 161 | { |
163 | index = GNUNET_new (uint32_t); | 162 | index = GNUNET_new(uint32_t); |
164 | *index = (uint32_t) View_size (view); | 163 | *index = (uint32_t)View_size(view); |
165 | view->array[*index] = *peer; | 164 | view->array[*index] = *peer; |
166 | GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index, | 165 | GNUNET_CONTAINER_multipeermap_put(view->mpm, peer, index, |
167 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | 166 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); |
168 | return GNUNET_OK; | 167 | return GNUNET_OK; |
169 | } | 168 | } |
170 | } | 169 | } |
171 | 170 | ||
172 | 171 | ||
@@ -180,10 +179,10 @@ View_put (struct View *view, | |||
180 | * GNUNET_NO otherwise | 179 | * GNUNET_NO otherwise |
181 | */ | 180 | */ |
182 | int | 181 | int |
183 | View_contains_peer (const struct View *view, | 182 | View_contains_peer(const struct View *view, |
184 | const struct GNUNET_PeerIdentity *peer) | 183 | const struct GNUNET_PeerIdentity *peer) |
185 | { | 184 | { |
186 | return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer); | 185 | return GNUNET_CONTAINER_multipeermap_contains(view->mpm, peer); |
187 | } | 186 | } |
188 | 187 | ||
189 | 188 | ||
@@ -197,32 +196,32 @@ View_contains_peer (const struct View *view, | |||
197 | * GNUNET_NO if view does not contain peer | 196 | * GNUNET_NO if view does not contain peer |
198 | */ | 197 | */ |
199 | int | 198 | int |
200 | View_remove_peer (struct View *view, | 199 | View_remove_peer(struct View *view, |
201 | const struct GNUNET_PeerIdentity *peer) | 200 | const struct GNUNET_PeerIdentity *peer) |
202 | { | 201 | { |
203 | uint32_t *index; | 202 | uint32_t *index; |
204 | uint32_t *swap_index; | 203 | uint32_t *swap_index; |
205 | uint32_t last_index; | 204 | uint32_t last_index; |
206 | 205 | ||
207 | if (GNUNET_NO == View_contains_peer (view, peer)) | 206 | if (GNUNET_NO == View_contains_peer(view, peer)) |
208 | { | 207 | { |
209 | return GNUNET_NO; | 208 | return GNUNET_NO; |
210 | } | 209 | } |
211 | index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer); | 210 | index = GNUNET_CONTAINER_multipeermap_get(view->mpm, peer); |
212 | GNUNET_assert (NULL != index); | 211 | GNUNET_assert(NULL != index); |
213 | last_index = View_size (view) - 1; | 212 | last_index = View_size(view) - 1; |
214 | if (*index < last_index) | 213 | if (*index < last_index) |
215 | { /* Fill the 'gap' in the array with the last peer */ | 214 | { /* Fill the 'gap' in the array with the last peer */ |
216 | view->array[*index] = view->array[last_index]; | 215 | view->array[*index] = view->array[last_index]; |
217 | GNUNET_assert (GNUNET_YES == View_contains_peer (view, | 216 | GNUNET_assert(GNUNET_YES == View_contains_peer(view, |
218 | &view->array[last_index])); | 217 | &view->array[last_index])); |
219 | swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm, | 218 | swap_index = GNUNET_CONTAINER_multipeermap_get(view->mpm, |
220 | &view->array[last_index]); | 219 | &view->array[last_index]); |
221 | GNUNET_assert (NULL != swap_index); | 220 | GNUNET_assert(NULL != swap_index); |
222 | *swap_index = *index; | 221 | *swap_index = *index; |
223 | GNUNET_free (index); | 222 | GNUNET_free(index); |
224 | } | 223 | } |
225 | GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, peer); | 224 | GNUNET_CONTAINER_multipeermap_remove_all(view->mpm, peer); |
226 | return GNUNET_OK; | 225 | return GNUNET_OK; |
227 | } | 226 | } |
228 | 227 | ||
@@ -237,17 +236,17 @@ View_remove_peer (struct View *view, | |||
237 | * NULL if this index is not known | 236 | * NULL if this index is not known |
238 | */ | 237 | */ |
239 | const struct GNUNET_PeerIdentity * | 238 | const struct GNUNET_PeerIdentity * |
240 | View_get_peer_by_index (const struct View *view, | 239 | View_get_peer_by_index(const struct View *view, |
241 | uint32_t index) | 240 | uint32_t index) |
242 | { | 241 | { |
243 | if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm)) | 242 | if (index < GNUNET_CONTAINER_multipeermap_size(view->mpm)) |
244 | { | 243 | { |
245 | return &view->array[index]; | 244 | return &view->array[index]; |
246 | } | 245 | } |
247 | else | 246 | else |
248 | { | 247 | { |
249 | return NULL; | 248 | return NULL; |
250 | } | 249 | } |
251 | } | 250 | } |
252 | 251 | ||
253 | 252 | ||
@@ -257,20 +256,20 @@ View_get_peer_by_index (const struct View *view, | |||
257 | * @param view The view to clear | 256 | * @param view The view to clear |
258 | */ | 257 | */ |
259 | void | 258 | void |
260 | View_clear (struct View *view) | 259 | View_clear(struct View *view) |
261 | { | 260 | { |
262 | for (uint32_t i = 0; 0 < View_size (view); i++) | 261 | for (uint32_t i = 0; 0 < View_size(view); i++) |
263 | { /* Need to free indices stored at peers */ | 262 | { /* Need to free indices stored at peers */ |
264 | uint32_t *index; | 263 | uint32_t *index; |
265 | 264 | ||
266 | GNUNET_assert (GNUNET_YES == | 265 | GNUNET_assert(GNUNET_YES == |
267 | GNUNET_CONTAINER_multipeermap_contains (view->mpm, &view->array[i])); | 266 | GNUNET_CONTAINER_multipeermap_contains(view->mpm, &view->array[i])); |
268 | index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]); | 267 | index = GNUNET_CONTAINER_multipeermap_get(view->mpm, &view->array[i]); |
269 | GNUNET_assert (NULL != index); | 268 | GNUNET_assert(NULL != index); |
270 | GNUNET_free (index); | 269 | GNUNET_free(index); |
271 | GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, &view->array[i]); | 270 | GNUNET_CONTAINER_multipeermap_remove_all(view->mpm, &view->array[i]); |
272 | } | 271 | } |
273 | GNUNET_assert (0 == View_size (view)); | 272 | GNUNET_assert(0 == View_size(view)); |
274 | } | 273 | } |
275 | 274 | ||
276 | 275 | ||
@@ -280,13 +279,13 @@ View_clear (struct View *view) | |||
280 | * @param view the view to destroy | 279 | * @param view the view to destroy |
281 | */ | 280 | */ |
282 | void | 281 | void |
283 | View_destroy (struct View *view) | 282 | View_destroy(struct View *view) |
284 | { | 283 | { |
285 | View_clear (view); | 284 | View_clear(view); |
286 | GNUNET_free (view->array); | 285 | GNUNET_free(view->array); |
287 | view->array = NULL; | 286 | view->array = NULL; |
288 | GNUNET_CONTAINER_multipeermap_destroy (view->mpm); | 287 | GNUNET_CONTAINER_multipeermap_destroy(view->mpm); |
289 | GNUNET_free (view); | 288 | GNUNET_free(view); |
290 | } | 289 | } |
291 | 290 | ||
292 | /* end of gnunet-service-rps_view.c */ | 291 | /* end of gnunet-service-rps_view.c */ |