diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-27 14:06:26 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-27 14:06:26 +0000 |
commit | 24680d0c69d4a14e207b20051c34ee48dbafdc1f (patch) | |
tree | fa08440f2b84f8400b41c2f632fd2f9fce0ea26e /src/testbed/testbed_api.c | |
parent | 68a09c195d33239193f160dfabef54a6dbf0d239 (diff) | |
download | gnunet-24680d0c69d4a14e207b20051c34ee48dbafdc1f.tar.gz gnunet-24680d0c69d4a14e207b20051c34ee48dbafdc1f.zip |
move functions unrelated to TESTBED_BarrierWaitHandle out of testbed_api_barriers.c
Diffstat (limited to 'src/testbed/testbed_api.c')
-rw-r--r-- | src/testbed/testbed_api.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/testbed/testbed_api.c b/src/testbed/testbed_api.c index d946b7082..67b1e8c96 100644 --- a/src/testbed/testbed_api.c +++ b/src/testbed/testbed_api.c | |||
@@ -2268,4 +2268,147 @@ GNUNET_TESTBED_get_index (const struct GNUNET_TESTBED_Peer *peer) | |||
2268 | return peer->unique_id; | 2268 | return peer->unique_id; |
2269 | } | 2269 | } |
2270 | 2270 | ||
2271 | |||
2272 | /** | ||
2273 | * Remove a barrier and it was the last one in the barrier hash map, destroy the | ||
2274 | * hash map | ||
2275 | * | ||
2276 | * @param barrier the barrier to remove | ||
2277 | */ | ||
2278 | void | ||
2279 | GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier) | ||
2280 | { | ||
2281 | struct GNUNET_TESTBED_Controller *c = barrier->c; | ||
2282 | |||
2283 | GNUNET_assert (NULL != c->barrier_map); /* No barriers present */ | ||
2284 | GNUNET_assert (GNUNET_OK == | ||
2285 | GNUNET_CONTAINER_multihashmap_remove (c->barrier_map, | ||
2286 | &barrier->key, | ||
2287 | barrier)); | ||
2288 | GNUNET_free (barrier->name); | ||
2289 | GNUNET_free (barrier); | ||
2290 | if (0 == GNUNET_CONTAINER_multihashmap_size (c->barrier_map)) | ||
2291 | { | ||
2292 | GNUNET_CONTAINER_multihashmap_destroy (c->barrier_map); | ||
2293 | c->barrier_map = NULL; | ||
2294 | } | ||
2295 | } | ||
2296 | |||
2297 | |||
2298 | /** | ||
2299 | * Initialise a barrier and call the given callback when the required percentage | ||
2300 | * of peers (quorum) reach the barrier OR upon error. | ||
2301 | * | ||
2302 | * @param controller the handle to the controller | ||
2303 | * @param name identification name of the barrier | ||
2304 | * @param quorum the percentage of peers that is required to reach the barrier. | ||
2305 | * Peers signal reaching a barrier by calling | ||
2306 | * GNUNET_TESTBED_barrier_reached(). | ||
2307 | * @param cb the callback to call when the barrier is reached or upon error. | ||
2308 | * Cannot be NULL. | ||
2309 | * @param cls closure for the above callback | ||
2310 | * @param echo GNUNET_YES to echo the barrier crossed status message back to the | ||
2311 | * controller | ||
2312 | * @return barrier handle; NULL upon error | ||
2313 | */ | ||
2314 | struct GNUNET_TESTBED_Barrier * | ||
2315 | GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, | ||
2316 | const char *name, | ||
2317 | unsigned int quorum, | ||
2318 | GNUNET_TESTBED_barrier_status_cb cb, void *cls, | ||
2319 | int echo) | ||
2320 | { | ||
2321 | struct GNUNET_TESTBED_BarrierInit *msg; | ||
2322 | struct GNUNET_TESTBED_Barrier *barrier; | ||
2323 | struct GNUNET_HashCode key; | ||
2324 | size_t name_len; | ||
2325 | uint16_t msize; | ||
2326 | |||
2327 | GNUNET_assert (quorum <= 100); | ||
2328 | GNUNET_assert (NULL != cb); | ||
2329 | name_len = strlen (name); | ||
2330 | GNUNET_assert (0 < name_len); | ||
2331 | GNUNET_CRYPTO_hash (name, name_len, &key); | ||
2332 | if (NULL == controller->barrier_map) | ||
2333 | controller->barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES); | ||
2334 | if (GNUNET_YES == | ||
2335 | GNUNET_CONTAINER_multihashmap_contains (controller->barrier_map, | ||
2336 | &key)) | ||
2337 | { | ||
2338 | GNUNET_break (0); | ||
2339 | return NULL; | ||
2340 | } | ||
2341 | LOG_DEBUG ("Initialising barrier `%s'\n", name); | ||
2342 | barrier = GNUNET_new (struct GNUNET_TESTBED_Barrier); | ||
2343 | barrier->c = controller; | ||
2344 | barrier->name = GNUNET_strdup (name); | ||
2345 | barrier->cb = cb; | ||
2346 | barrier->cls = cls; | ||
2347 | barrier->echo = echo; | ||
2348 | (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode)); | ||
2349 | GNUNET_assert (GNUNET_OK == | ||
2350 | GNUNET_CONTAINER_multihashmap_put (controller->barrier_map, | ||
2351 | &barrier->key, | ||
2352 | barrier, | ||
2353 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); | ||
2354 | msize = name_len + sizeof (struct GNUNET_TESTBED_BarrierInit); | ||
2355 | msg = GNUNET_malloc (msize); | ||
2356 | msg->header.size = htons (msize); | ||
2357 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT); | ||
2358 | msg->quorum = (uint8_t) quorum; | ||
2359 | (void) memcpy (msg->name, barrier->name, name_len); | ||
2360 | GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header); | ||
2361 | return barrier; | ||
2362 | } | ||
2363 | |||
2364 | |||
2365 | /** | ||
2366 | * Initialise a barrier and call the given callback when the required percentage | ||
2367 | * of peers (quorum) reach the barrier OR upon error. | ||
2368 | * | ||
2369 | * @param controller the handle to the controller | ||
2370 | * @param name identification name of the barrier | ||
2371 | * @param quorum the percentage of peers that is required to reach the barrier. | ||
2372 | * Peers signal reaching a barrier by calling | ||
2373 | * GNUNET_TESTBED_barrier_reached(). | ||
2374 | * @param cb the callback to call when the barrier is reached or upon error. | ||
2375 | * Cannot be NULL. | ||
2376 | * @param cls closure for the above callback | ||
2377 | * @return barrier handle; NULL upon error | ||
2378 | */ | ||
2379 | struct GNUNET_TESTBED_Barrier * | ||
2380 | GNUNET_TESTBED_barrier_init (struct GNUNET_TESTBED_Controller *controller, | ||
2381 | const char *name, | ||
2382 | unsigned int quorum, | ||
2383 | GNUNET_TESTBED_barrier_status_cb cb, void *cls) | ||
2384 | { | ||
2385 | return GNUNET_TESTBED_barrier_init_ (controller, | ||
2386 | name, quorum, cb, cls, GNUNET_YES); | ||
2387 | } | ||
2388 | |||
2389 | |||
2390 | /** | ||
2391 | * Cancel a barrier. | ||
2392 | * | ||
2393 | * @param barrier the barrier handle | ||
2394 | */ | ||
2395 | void | ||
2396 | GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier) | ||
2397 | { | ||
2398 | struct GNUNET_TESTBED_BarrierCancel *msg; | ||
2399 | uint16_t msize; | ||
2400 | |||
2401 | msize = sizeof (struct GNUNET_TESTBED_BarrierCancel) + strlen (barrier->name); | ||
2402 | msg = GNUNET_malloc (msize); | ||
2403 | msg->header.size = htons (msize); | ||
2404 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL); | ||
2405 | (void) memcpy (msg->name, barrier->name, strlen (barrier->name)); | ||
2406 | GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header); | ||
2407 | GNUNET_TESTBED_barrier_remove_ (barrier); | ||
2408 | } | ||
2409 | |||
2410 | |||
2411 | |||
2412 | |||
2413 | |||
2271 | /* end of testbed_api.c */ | 2414 | /* end of testbed_api.c */ |