diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-03-19 13:54:14 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-03-19 13:54:14 +0000 |
commit | 583e1cc42de7a8b1d846fca7e592d88599b77ebc (patch) | |
tree | b18e730b827af95126443188286e6e6bbfc984c1 /src/testbed/testbed_api_testbed.c | |
parent | b7c97d0ef3729107a38b6ba3ec7f05407a52ae01 (diff) | |
download | gnunet-583e1cc42de7a8b1d846fca7e592d88599b77ebc.tar.gz gnunet-583e1cc42de7a8b1d846fca7e592d88599b77ebc.zip |
- code reordering
- Call GNUNET_SCHEDULER_shutdown() upon errors
Diffstat (limited to 'src/testbed/testbed_api_testbed.c')
-rw-r--r-- | src/testbed/testbed_api_testbed.c | 214 |
1 files changed, 100 insertions, 114 deletions
diff --git a/src/testbed/testbed_api_testbed.c b/src/testbed/testbed_api_testbed.c index 76177b486..85519c1b9 100644 --- a/src/testbed/testbed_api_testbed.c +++ b/src/testbed/testbed_api_testbed.c | |||
@@ -294,88 +294,6 @@ struct RunContext | |||
294 | 294 | ||
295 | 295 | ||
296 | /** | 296 | /** |
297 | * Function to return the string representation of the duration between current | ||
298 | * time and `pstart_time' in `RunContext' | ||
299 | * | ||
300 | * @param rc the RunContext | ||
301 | * @return the representation string; this is NOT reentrant | ||
302 | */ | ||
303 | static const char * | ||
304 | prof_time (struct RunContext *rc) | ||
305 | { | ||
306 | struct GNUNET_TIME_Relative ptime; | ||
307 | |||
308 | ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time); | ||
309 | return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES); | ||
310 | } | ||
311 | |||
312 | |||
313 | /** | ||
314 | * Task for starting peers | ||
315 | * | ||
316 | * @param cls the RunHandle | ||
317 | * @param tc the task context from scheduler | ||
318 | */ | ||
319 | static void | ||
320 | start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
321 | { | ||
322 | struct RunContext *rc = cls; | ||
323 | struct DLLOperation *dll_op; | ||
324 | unsigned int peer; | ||
325 | |||
326 | DEBUG ("Starting Peers\n"); | ||
327 | rc->pstart_time = GNUNET_TIME_absolute_get (); | ||
328 | for (peer = 0; peer < rc->num_peers; peer++) | ||
329 | { | ||
330 | dll_op = GNUNET_malloc (sizeof (struct DLLOperation)); | ||
331 | dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL); | ||
332 | dll_op->cls = rc->peers[peer]; | ||
333 | GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op); | ||
334 | } | ||
335 | rc->peer_count = 0; | ||
336 | } | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Functions of this signature are called when a peer has been successfully | ||
341 | * created | ||
342 | * | ||
343 | * @param cls the closure from GNUNET_TESTBED_peer_create() | ||
344 | * @param peer the handle for the created peer; NULL on any error during | ||
345 | * creation | ||
346 | * @param emsg NULL if peer is not NULL; else MAY contain the error description | ||
347 | */ | ||
348 | static void | ||
349 | peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg) | ||
350 | { | ||
351 | struct DLLOperation *dll_op = cls; | ||
352 | struct RunContext *rc; | ||
353 | |||
354 | GNUNET_assert (NULL != dll_op); | ||
355 | rc = dll_op->rc; | ||
356 | GNUNET_assert (NULL != rc); | ||
357 | GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op); | ||
358 | GNUNET_TESTBED_operation_done (dll_op->op); | ||
359 | GNUNET_free (dll_op); | ||
360 | if (NULL == peer) | ||
361 | { | ||
362 | if (NULL != emsg) | ||
363 | LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n", | ||
364 | emsg); | ||
365 | /* FIXME: GNUNET_TESTBED_shutdown_run()? */ | ||
366 | return; | ||
367 | } | ||
368 | rc->peers[rc->peer_count] = peer; | ||
369 | rc->peer_count++; | ||
370 | if (rc->peer_count < rc->num_peers) | ||
371 | return; | ||
372 | DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc)); | ||
373 | rc->state = RC_PEERS_CREATED; | ||
374 | GNUNET_SCHEDULER_add_now (&start_peers_task, rc); | ||
375 | } | ||
376 | |||
377 | |||
378 | /** | ||
379 | * Assuming all peers have been destroyed cleanup run handle | 297 | * Assuming all peers have been destroyed cleanup run handle |
380 | * | 298 | * |
381 | * @param cls the run handle | 299 | * @param cls the run handle |
@@ -417,32 +335,6 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
417 | * @param tc the task context from scheduler | 335 | * @param tc the task context from scheduler |
418 | */ | 336 | */ |
419 | static void | 337 | static void |
420 | shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
421 | |||
422 | |||
423 | /** | ||
424 | * Function to shutdown now | ||
425 | * | ||
426 | * @param rc the RunContext | ||
427 | */ | ||
428 | static void | ||
429 | shutdown_now (struct RunContext *rc) | ||
430 | { | ||
431 | if (GNUNET_YES == rc->shutdown) | ||
432 | return; | ||
433 | if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task) | ||
434 | GNUNET_SCHEDULER_cancel (rc->shutdown_run_task); | ||
435 | rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc); | ||
436 | } | ||
437 | |||
438 | |||
439 | /** | ||
440 | * Stops the testbed run and releases any used resources | ||
441 | * | ||
442 | * @param cls the tesbed run handle | ||
443 | * @param tc the task context from scheduler | ||
444 | */ | ||
445 | static void | ||
446 | shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 338 | shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
447 | { | 339 | { |
448 | struct RunContext *rc = cls; | 340 | struct RunContext *rc = cls; |
@@ -514,6 +406,105 @@ shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
514 | 406 | ||
515 | 407 | ||
516 | /** | 408 | /** |
409 | * Function to shutdown now | ||
410 | * | ||
411 | * @param rc the RunContext | ||
412 | */ | ||
413 | static void | ||
414 | shutdown_now (struct RunContext *rc) | ||
415 | { | ||
416 | if (GNUNET_YES == rc->shutdown) | ||
417 | return; | ||
418 | if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task) | ||
419 | GNUNET_SCHEDULER_cancel (rc->shutdown_run_task); | ||
420 | GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */ | ||
421 | rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc); | ||
422 | } | ||
423 | |||
424 | |||
425 | /** | ||
426 | * Function to return the string representation of the duration between current | ||
427 | * time and `pstart_time' in `RunContext' | ||
428 | * | ||
429 | * @param rc the RunContext | ||
430 | * @return the representation string; this is NOT reentrant | ||
431 | */ | ||
432 | static const char * | ||
433 | prof_time (struct RunContext *rc) | ||
434 | { | ||
435 | struct GNUNET_TIME_Relative ptime; | ||
436 | |||
437 | ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time); | ||
438 | return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES); | ||
439 | } | ||
440 | |||
441 | |||
442 | /** | ||
443 | * Task for starting peers | ||
444 | * | ||
445 | * @param cls the RunHandle | ||
446 | * @param tc the task context from scheduler | ||
447 | */ | ||
448 | static void | ||
449 | start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
450 | { | ||
451 | struct RunContext *rc = cls; | ||
452 | struct DLLOperation *dll_op; | ||
453 | unsigned int peer; | ||
454 | |||
455 | DEBUG ("Starting Peers\n"); | ||
456 | rc->pstart_time = GNUNET_TIME_absolute_get (); | ||
457 | for (peer = 0; peer < rc->num_peers; peer++) | ||
458 | { | ||
459 | dll_op = GNUNET_malloc (sizeof (struct DLLOperation)); | ||
460 | dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL); | ||
461 | dll_op->cls = rc->peers[peer]; | ||
462 | GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op); | ||
463 | } | ||
464 | rc->peer_count = 0; | ||
465 | } | ||
466 | |||
467 | |||
468 | /** | ||
469 | * Functions of this signature are called when a peer has been successfully | ||
470 | * created | ||
471 | * | ||
472 | * @param cls the closure from GNUNET_TESTBED_peer_create() | ||
473 | * @param peer the handle for the created peer; NULL on any error during | ||
474 | * creation | ||
475 | * @param emsg NULL if peer is not NULL; else MAY contain the error description | ||
476 | */ | ||
477 | static void | ||
478 | peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg) | ||
479 | { | ||
480 | struct DLLOperation *dll_op = cls; | ||
481 | struct RunContext *rc; | ||
482 | |||
483 | GNUNET_assert (NULL != dll_op); | ||
484 | rc = dll_op->rc; | ||
485 | GNUNET_assert (NULL != rc); | ||
486 | GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op); | ||
487 | GNUNET_TESTBED_operation_done (dll_op->op); | ||
488 | GNUNET_free (dll_op); | ||
489 | if (NULL == peer) | ||
490 | { | ||
491 | if (NULL != emsg) | ||
492 | LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n", | ||
493 | emsg); | ||
494 | shutdown_now (rc); | ||
495 | return; | ||
496 | } | ||
497 | rc->peers[rc->peer_count] = peer; | ||
498 | rc->peer_count++; | ||
499 | if (rc->peer_count < rc->num_peers) | ||
500 | return; | ||
501 | DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc)); | ||
502 | rc->state = RC_PEERS_CREATED; | ||
503 | GNUNET_SCHEDULER_add_now (&start_peers_task, rc); | ||
504 | } | ||
505 | |||
506 | |||
507 | /** | ||
517 | * call test master callback | 508 | * call test master callback |
518 | * | 509 | * |
519 | * @param rc the RunContext | 510 | * @param rc the RunContext |
@@ -642,7 +633,7 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event) | |||
642 | case RC_PEERS_CREATED: | 633 | case RC_PEERS_CREATED: |
643 | case RC_READY: | 634 | case RC_READY: |
644 | rc->state = RC_PEERS_SHUTDOWN; | 635 | rc->state = RC_PEERS_SHUTDOWN; |
645 | GNUNET_free (rc->peers); | 636 | GNUNET_free_non_null (rc->peers); |
646 | rc->peers = NULL; | 637 | rc->peers = NULL; |
647 | DEBUG ("Peers shut down in %s\n", prof_time (rc)); | 638 | DEBUG ("Peers shut down in %s\n", prof_time (rc)); |
648 | GNUNET_SCHEDULER_add_now (&cleanup_task, rc); | 639 | GNUNET_SCHEDULER_add_now (&cleanup_task, rc); |
@@ -817,11 +808,6 @@ controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
817 | GNUNET_free (rc->peers); | 808 | GNUNET_free (rc->peers); |
818 | rc->peers = NULL; | 809 | rc->peers = NULL; |
819 | } | 810 | } |
820 | if (NULL != rc->c) | ||
821 | { | ||
822 | GNUNET_TESTBED_controller_disconnect (rc->c); | ||
823 | rc->c = NULL; | ||
824 | } | ||
825 | shutdown_now (rc); | 811 | shutdown_now (rc); |
826 | return; | 812 | return; |
827 | } | 813 | } |