aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_testbed.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-08-31 10:05:09 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-08-31 10:05:09 +0000
commit87a93bc140ad2a527891ad429761343b2522a1e0 (patch)
tree0aa13e245fd937646fdd34c1cc507d918df1ddff /src/testbed/testbed_api_testbed.c
parent1a29a996fa7071cd798fbd75c85edbd6daaaeb3c (diff)
downloadgnunet-87a93bc140ad2a527891ad429761343b2522a1e0.tar.gz
gnunet-87a93bc140ad2a527891ad429761343b2522a1e0.zip
stop peers before destroying them
Diffstat (limited to 'src/testbed/testbed_api_testbed.c')
-rw-r--r--src/testbed/testbed_api_testbed.c98
1 files changed, 78 insertions, 20 deletions
diff --git a/src/testbed/testbed_api_testbed.c b/src/testbed/testbed_api_testbed.c
index e20b3a10a..914523780 100644
--- a/src/testbed/testbed_api_testbed.c
+++ b/src/testbed/testbed_api_testbed.c
@@ -76,6 +76,34 @@ struct DLLOperation
76 76
77 77
78/** 78/**
79 * States of RunContext
80 */
81enum State
82{
83 /**
84 * Initial state
85 */
86 RC_INIT = 0,
87
88 /**
89 * Peers have been started
90 */
91 RC_PEERS_STARTED,
92
93 /**
94 * Peers are stopped
95 */
96 RC_PEERS_STOPPED,
97
98 /**
99 * Peers are destroyed
100 */
101 RC_PEERS_DESTROYED
102
103};
104
105
106/**
79 * Testbed Run Handle 107 * Testbed Run Handle
80 */ 108 */
81struct RunContext 109struct RunContext
@@ -136,6 +164,11 @@ struct RunContext
136 uint64_t event_mask; 164 uint64_t event_mask;
137 165
138 /** 166 /**
167 * State of this context
168 */
169 enum State state;
170
171 /**
139 * Current peer count for an operation; Set this to 0 and increment for each 172 * Current peer count for an operation; Set this to 0 and increment for each
140 * successful operation on a peer 173 * successful operation on a peer
141 */ 174 */
@@ -146,11 +179,6 @@ struct RunContext
146 */ 179 */
147 unsigned int num_peers; 180 unsigned int num_peers;
148 181
149 /**
150 * Are we cleaning up?
151 */
152 int in_shutdown;
153
154}; 182};
155 183
156 184
@@ -306,6 +334,7 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
306 struct DLLOperation *dll_op; 334 struct DLLOperation *dll_op;
307 335
308 GNUNET_assert (NULL == rc->peers); 336 GNUNET_assert (NULL == rc->peers);
337 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
309 if (NULL != rc->c) 338 if (NULL != rc->c)
310 GNUNET_TESTBED_controller_disconnect (rc->c); 339 GNUNET_TESTBED_controller_disconnect (rc->c);
311 if (NULL != rc->cproc) 340 if (NULL != rc->cproc)
@@ -339,13 +368,20 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
339{ 368{
340 struct RunContext *rc = cls; 369 struct RunContext *rc = cls;
341 struct DLLOperation *dll_op; 370 struct DLLOperation *dll_op;
371 unsigned int peer_id;
342 372
343 if ((GNUNET_YES == rc->in_shutdown) && 373
344 (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type)) 374 if ((RC_INIT != rc->state) &&
375 ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type)||
376 (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
345 { 377 {
346 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next) 378 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
347 { 379 {
348 if (event->details.operation_finished.operation == dll_op->op) 380 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
381 (event->details.operation_finished.operation == dll_op->op))
382 break;
383 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
384 (event->details.peer_stop.peer == dll_op->cls))
349 break; 385 break;
350 } 386 }
351 if (NULL == dll_op) 387 if (NULL == dll_op)
@@ -356,11 +392,30 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
356 rc->peer_count++; 392 rc->peer_count++;
357 if (rc->peer_count < rc->num_peers) 393 if (rc->peer_count < rc->num_peers)
358 return; 394 return;
359 GNUNET_free (rc->peers); 395 switch (rc->state)
360 rc->peers = NULL; 396 {
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n"); 397 case RC_PEERS_STARTED:
362 GNUNET_SCHEDULER_add_now (&cleanup_task, rc); 398 rc->state = RC_PEERS_STOPPED;
363 return; 399 rc->peer_count = 0;
400 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
401 {
402 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
403 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
404 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
405 dll_op);
406 }
407 break;
408 case RC_PEERS_STOPPED:
409 rc->state = RC_PEERS_DESTROYED;
410 GNUNET_free (rc->peers);
411 rc->peers = NULL;
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
413 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
414 break;
415 default:
416 GNUNET_assert (0);
417 }
418 return;
364 } 419 }
365 420
366 call_cc: 421 call_cc:
@@ -378,7 +433,8 @@ event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
378 rc->peer_count++; 433 rc->peer_count++;
379 if (rc->peer_count < rc->num_peers) 434 if (rc->peer_count < rc->num_peers)
380 return; 435 return;
381 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n"); 436 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
437 rc->state = RC_PEERS_STARTED;
382 GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls, 438 GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls,
383 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 439 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
384} 440}
@@ -434,10 +490,9 @@ void
434shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 490shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
435{ 491{
436 struct RunContext *rc = cls; 492 struct RunContext *rc = cls;
437 struct DLLOperation *dll_op; 493 struct DLLOperation *dll_op;
438 unsigned int peer; 494 unsigned int peer;
439 495
440 rc->in_shutdown = GNUNET_YES;
441 if (NULL != rc->c) 496 if (NULL != rc->c)
442 { 497 {
443 if (NULL != rc->peers) 498 if (NULL != rc->peers)
@@ -446,13 +501,16 @@ shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
446 for (peer = 0; peer < rc->num_peers; peer++) 501 for (peer = 0; peer < rc->num_peers; peer++)
447 { 502 {
448 dll_op = GNUNET_malloc (sizeof (struct DLLOperation)); 503 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
449 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]); 504 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer]);
505 dll_op->cls = rc->peers[peer];
450 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, 506 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
451 dll_op); 507 dll_op);
452 } 508 }
453 return; 509 return;
454 } 510 }
455 } 511 }
512 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
513 state where all peers are destroyed */
456 GNUNET_SCHEDULER_add_now (&cleanup_task, rc); 514 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
457} 515}
458 516
@@ -508,7 +566,7 @@ GNUNET_TESTBED_run (const char *host_filename,
508 rc->cc_cls = cc_cls; 566 rc->cc_cls = cc_cls;
509 rc->master = master; 567 rc->master = master;
510 rc->master_cls = master_cls; 568 rc->master_cls = master_cls;
511 rc->in_shutdown = GNUNET_NO; 569 rc->state = RC_INIT;
512 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
513 &shutdown_run_task, rc); 571 &shutdown_run_task, rc);
514} 572}