summaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps_custommap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-service-rps_custommap.c')
-rw-r--r--src/rps/gnunet-service-rps_custommap.c253
1 files changed, 129 insertions, 124 deletions
diff --git a/src/rps/gnunet-service-rps_custommap.c b/src/rps/gnunet-service-rps_custommap.c
index b842bc0a5..8fcfc5558 100644
--- a/src/rps/gnunet-service-rps_custommap.c
+++ b/src/rps/gnunet-service-rps_custommap.c
@@ -28,7 +28,7 @@
28#include "gnunet-service-rps_custommap.h" 28#include "gnunet-service-rps_custommap.h"
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "rps-peers", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "rps-peers", __VA_ARGS__)
32 32
33 33
34/** 34/**
@@ -54,7 +54,8 @@
54 * - contain continous indices 0 <= i < len 54 * - contain continous indices 0 <= i < len
55 * - not contain duplicate peers 55 * - not contain duplicate peers
56 */ 56 */
57struct CustomPeerMap { 57struct CustomPeerMap
58{
58 /** 59 /**
59 * Multihashmap to be able to access a random index 60 * Multihashmap to be able to access a random index
60 */ 61 */
@@ -75,14 +76,14 @@ struct CustomPeerMap {
75 * @return the newly created custom peer map 76 * @return the newly created custom peer map
76 */ 77 */
77struct CustomPeerMap * 78struct CustomPeerMap *
78CustomPeerMap_create(unsigned int len) 79CustomPeerMap_create (unsigned int len)
79{ 80{
80 struct CustomPeerMap *c_peer_map; 81 struct CustomPeerMap *c_peer_map;
81 82
82 c_peer_map = GNUNET_new(struct CustomPeerMap); 83 c_peer_map = GNUNET_new (struct CustomPeerMap);
83 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create(len); 84 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create (len);
84 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create(len, 85 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create (len,
85 GNUNET_NO); 86 GNUNET_NO);
86 return c_peer_map; 87 return c_peer_map;
87} 88}
88 89
@@ -94,11 +95,11 @@ CustomPeerMap_create(unsigned int len)
94 * @return size of the map 95 * @return size of the map
95 */ 96 */
96unsigned int 97unsigned int
97CustomPeerMap_size(const struct CustomPeerMap *c_peer_map) 98CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
98{ 99{
99 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 100 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
100 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 101 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
101 return GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map); 102 return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map);
102} 103}
103 104
104/** 105/**
@@ -111,39 +112,40 @@ CustomPeerMap_size(const struct CustomPeerMap *c_peer_map)
111 * GNUNET_NO if map did contain peer previously 112 * GNUNET_NO if map did contain peer previously
112 */ 113 */
113int 114int
114CustomPeerMap_put(const struct CustomPeerMap *c_peer_map, 115CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
115 const struct GNUNET_PeerIdentity *peer) 116 const struct GNUNET_PeerIdentity *peer)
116{ 117{
117 uint32_t *index; 118 uint32_t *index;
118 struct GNUNET_PeerIdentity *p; 119 struct GNUNET_PeerIdentity *p;
119 120
120 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 121 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
121 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 122 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
122 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(c_peer_map->peer_map, 123 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map,
123 peer)) 124 peer))
124 { 125 {
125 /* Need to store the index of the peer in the peermap to be able to remove 126 /* Need to store the index of the peer in the peermap to be able to remove
126 * it properly */ 127 * it properly */
127 index = GNUNET_new(uint32_t); 128 index = GNUNET_new (uint32_t);
128 *index = CustomPeerMap_size(c_peer_map); 129 *index = CustomPeerMap_size (c_peer_map);
129 p = GNUNET_new(struct GNUNET_PeerIdentity); 130 p = GNUNET_new (struct GNUNET_PeerIdentity);
130 *p = *peer; 131 *p = *peer;
131 GNUNET_assert(p != peer); 132 GNUNET_assert (p != peer);
132 GNUNET_assert(0 == memcmp(p, 133 GNUNET_assert (0 == memcmp (p,
133 peer, 134 peer,
134 sizeof(struct GNUNET_PeerIdentity))); 135 sizeof(struct GNUNET_PeerIdentity)));
135 GNUNET_CONTAINER_multipeermap_put(c_peer_map->peer_map, 136 GNUNET_CONTAINER_multipeermap_put (c_peer_map->peer_map,
136 p, 137 p,
137 index, 138 index,
138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 139 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
139 GNUNET_CONTAINER_multihashmap32_put(c_peer_map->hash_map, 140 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map,
140 *index, 141 *index,
141 p, 142 p,
142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
143 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 144 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (
144 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 145 c_peer_map->hash_map) ==
145 return GNUNET_OK; 146 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
146 } 147 return GNUNET_OK;
148 }
147 return GNUNET_NO; 149 return GNUNET_NO;
148} 150}
149 151
@@ -157,10 +159,10 @@ CustomPeerMap_put(const struct CustomPeerMap *c_peer_map,
157 * GNUNET_NO otherwise 159 * GNUNET_NO otherwise
158 */ 160 */
159int 161int
160CustomPeerMap_contains_peer(const struct CustomPeerMap *c_peer_map, 162CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
161 const struct GNUNET_PeerIdentity *peer) 163 const struct GNUNET_PeerIdentity *peer)
162{ 164{
163 return GNUNET_CONTAINER_multipeermap_contains(c_peer_map->peer_map, peer); 165 return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer);
164} 166}
165 167
166/** 168/**
@@ -172,13 +174,13 @@ CustomPeerMap_contains_peer(const struct CustomPeerMap *c_peer_map,
172 * @return the index 174 * @return the index
173 */ 175 */
174static uint32_t * 176static uint32_t *
175CustomPeerMap_get_index_pointer(const struct CustomPeerMap *c_peer_map, 177CustomPeerMap_get_index_pointer (const struct CustomPeerMap *c_peer_map,
176 const struct GNUNET_PeerIdentity *peer) 178 const struct GNUNET_PeerIdentity *peer)
177{ 179{
178 uint32_t *index; 180 uint32_t *index;
179 181
180 GNUNET_assert(GNUNET_YES == CustomPeerMap_contains_peer(c_peer_map, peer)); 182 GNUNET_assert (GNUNET_YES == CustomPeerMap_contains_peer (c_peer_map, peer));
181 index = GNUNET_CONTAINER_multipeermap_get(c_peer_map->peer_map, peer); 183 index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer);
182 return index; 184 return index;
183} 185}
184 186
@@ -192,52 +194,52 @@ CustomPeerMap_get_index_pointer(const struct CustomPeerMap *c_peer_map,
192 * GNUNET_NO if map does not contain peer 194 * GNUNET_NO if map does not contain peer
193 */ 195 */
194int 196int
195CustomPeerMap_remove_peer(const struct CustomPeerMap *c_peer_map, 197CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
196 const struct GNUNET_PeerIdentity *peer) 198 const struct GNUNET_PeerIdentity *peer)
197{ 199{
198 uint32_t *index; 200 uint32_t *index;
199 struct GNUNET_PeerIdentity *p; 201 struct GNUNET_PeerIdentity *p;
200 uint32_t *last_index; 202 uint32_t *last_index;
201 struct GNUNET_PeerIdentity *last_p; 203 struct GNUNET_PeerIdentity *last_p;
202 204
203 if (GNUNET_NO == CustomPeerMap_contains_peer(c_peer_map, 205 if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map,
204 peer)) 206 peer))
205 { 207 {
206 return GNUNET_NO; 208 return GNUNET_NO;
207 } 209 }
208 index = CustomPeerMap_get_index_pointer(c_peer_map, 210 index = CustomPeerMap_get_index_pointer (c_peer_map,
209 peer); 211 peer);
210 GNUNET_assert(*index < CustomPeerMap_size(c_peer_map)); 212 GNUNET_assert (*index < CustomPeerMap_size (c_peer_map));
211 /* Need to get the pointer stored in the hashmap to free it */ 213 /* Need to get the pointer stored in the hashmap to free it */
212 p = GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map, 214 p = GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map,
213 *index); 215 *index);
214 GNUNET_assert(NULL != p); 216 GNUNET_assert (NULL != p);
215 GNUNET_CONTAINER_multihashmap32_remove_all(c_peer_map->hash_map, 217 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map,
216 *index); 218 *index);
217 // TODO wrong peerid? 219 // TODO wrong peerid?
218 GNUNET_CONTAINER_multipeermap_remove_all(c_peer_map->peer_map, 220 GNUNET_CONTAINER_multipeermap_remove_all (c_peer_map->peer_map,
219 peer); 221 peer);
220 if (*index != CustomPeerMap_size(c_peer_map)) 222 if (*index != CustomPeerMap_size (c_peer_map))
221 { /* fill 'gap' with peer at last index */ 223 { /* fill 'gap' with peer at last index */
222 last_p = 224 last_p =
223 GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map, 225 GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map,
224 CustomPeerMap_size(c_peer_map)); 226 CustomPeerMap_size (c_peer_map));
225 GNUNET_assert(NULL != last_p); 227 GNUNET_assert (NULL != last_p);
226 last_index = GNUNET_CONTAINER_multipeermap_get(c_peer_map->peer_map, 228 last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map,
227 last_p); 229 last_p);
228 GNUNET_assert(NULL != last_index); 230 GNUNET_assert (NULL != last_index);
229 GNUNET_assert(CustomPeerMap_size(c_peer_map) == *last_index); 231 GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index);
230 GNUNET_CONTAINER_multihashmap32_put(c_peer_map->hash_map, 232 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map,
231 *index, last_p, 233 *index, last_p,
232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
233 GNUNET_CONTAINER_multihashmap32_remove_all(c_peer_map->hash_map, 235 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map,
234 *last_index); 236 *last_index);
235 *last_index = *index; 237 *last_index = *index;
236 } 238 }
237 GNUNET_free(index); 239 GNUNET_free (index);
238 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 240 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
239 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 241 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
240 GNUNET_free(p); 242 GNUNET_free (p);
241 return GNUNET_OK; 243 return GNUNET_OK;
242} 244}
243 245
@@ -251,14 +253,14 @@ CustomPeerMap_remove_peer(const struct CustomPeerMap *c_peer_map,
251 * if this index is not known, return NULL 253 * if this index is not known, return NULL
252 */ 254 */
253struct GNUNET_PeerIdentity * 255struct GNUNET_PeerIdentity *
254CustomPeerMap_get_peer_by_index(const struct CustomPeerMap *c_peer_map, 256CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map,
255 uint32_t index) 257 uint32_t index)
256{ 258{
257 if (GNUNET_YES == 259 if (GNUNET_YES ==
258 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map, index)) 260 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
259 { 261 {
260 return GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map, index); 262 return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index);
261 } 263 }
262 return NULL; 264 return NULL;
263} 265}
264 266
@@ -272,30 +274,30 @@ CustomPeerMap_get_peer_by_index(const struct CustomPeerMap *c_peer_map,
272 * GNUNET_NO if map does not contain (index of) peer 274 * GNUNET_NO if map does not contain (index of) peer
273 */ 275 */
274int 276int
275CustomPeerMap_remove_peer_by_index(const struct CustomPeerMap *c_peer_map, 277CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map,
276 uint32_t index) 278 uint32_t index)
277{ 279{
278 uint32_t *index_p; 280 uint32_t *index_p;
279 struct GNUNET_PeerIdentity *peer; 281 struct GNUNET_PeerIdentity *peer;
280 282
281 if (index >= CustomPeerMap_size(c_peer_map)) 283 if (index >= CustomPeerMap_size (c_peer_map))
282 { 284 {
283 return GNUNET_NO; 285 return GNUNET_NO;
284 } 286 }
285 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 287 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
286 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 288 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
287 if (GNUNET_NO == 289 if (GNUNET_NO ==
288 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map, index)) 290 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
289 { 291 {
290 return GNUNET_NO; 292 return GNUNET_NO;
291 } 293 }
292 peer = CustomPeerMap_get_peer_by_index(c_peer_map, index); 294 peer = CustomPeerMap_get_peer_by_index (c_peer_map, index);
293 GNUNET_assert(NULL != peer); 295 GNUNET_assert (NULL != peer);
294 index_p = CustomPeerMap_get_index_pointer(c_peer_map, peer); 296 index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer);
295 GNUNET_assert(index == *index_p); 297 GNUNET_assert (index == *index_p);
296 CustomPeerMap_remove_peer(c_peer_map, peer); 298 CustomPeerMap_remove_peer (c_peer_map, peer);
297 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) == 299 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
298 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map)); 300 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
299 return GNUNET_OK; 301 return GNUNET_OK;
300} 302}
301 303
@@ -307,18 +309,21 @@ CustomPeerMap_remove_peer_by_index(const struct CustomPeerMap *c_peer_map,
307 * @return size of the map 309 * @return size of the map
308 */ 310 */
309void 311void
310CustomPeerMap_clear(const struct CustomPeerMap *c_peer_map) 312CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
311{ 313{
312 while (0 < CustomPeerMap_size(c_peer_map)) 314 while (0 < CustomPeerMap_size (c_peer_map))
313 { 315 {
314 GNUNET_assert(GNUNET_YES == 316 GNUNET_assert (GNUNET_YES ==
315 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map, 317 GNUNET_CONTAINER_multihashmap32_contains (
316 CustomPeerMap_size(c_peer_map) - 1)); 318 c_peer_map->hash_map,
317 GNUNET_assert(GNUNET_OK == 319 CustomPeerMap_size (
318 CustomPeerMap_remove_peer_by_index(c_peer_map, 320 c_peer_map) - 1));
319 CustomPeerMap_size(c_peer_map) - 1)); 321 GNUNET_assert (GNUNET_OK ==
320 } 322 CustomPeerMap_remove_peer_by_index (c_peer_map,
321 GNUNET_assert(0 == CustomPeerMap_size(c_peer_map)); 323 CustomPeerMap_size (
324 c_peer_map) - 1));
325 }
326 GNUNET_assert (0 == CustomPeerMap_size (c_peer_map));
322} 327}
323 328
324/** 329/**
@@ -327,12 +332,12 @@ CustomPeerMap_clear(const struct CustomPeerMap *c_peer_map)
327 * @param c_peer_map the map to destroy 332 * @param c_peer_map the map to destroy
328 */ 333 */
329void 334void
330CustomPeerMap_destroy(struct CustomPeerMap *c_peer_map) 335CustomPeerMap_destroy (struct CustomPeerMap *c_peer_map)
331{ 336{
332 CustomPeerMap_clear(c_peer_map); 337 CustomPeerMap_clear (c_peer_map);
333 GNUNET_CONTAINER_multihashmap32_destroy(c_peer_map->hash_map); 338 GNUNET_CONTAINER_multihashmap32_destroy (c_peer_map->hash_map);
334 GNUNET_CONTAINER_multipeermap_destroy(c_peer_map->peer_map); 339 GNUNET_CONTAINER_multipeermap_destroy (c_peer_map->peer_map);
335 GNUNET_free(c_peer_map); 340 GNUNET_free (c_peer_map);
336} 341}
337 342
338/* end of gnunet-service-rps_custommap.c */ 343/* end of gnunet-service-rps_custommap.c */