diff options
author | Bart Polot <bart.polot+voyager@gmail.com> | 2017-01-31 04:57:50 +0100 |
---|---|---|
committer | Bart Polot <bart.polot+voyager@gmail.com> | 2017-01-31 05:08:53 +0100 |
commit | e6f317e8393c4fb22a8ee5371f155820790e1e39 (patch) | |
tree | 162851984c8037dd52ab0a6472453cc37341df86 /src/cadet/cadet_api.c | |
parent | bdfdbcc9d8ad8f46131c0836a9d489005d6a7bd0 (diff) | |
download | gnunet-e6f317e8393c4fb22a8ee5371f155820790e1e39.tar.gz gnunet-e6f317e8393c4fb22a8ee5371f155820790e1e39.zip |
Refactor reconnect code
Diffstat (limited to 'src/cadet/cadet_api.c')
-rw-r--r-- | src/cadet/cadet_api.c | 104 |
1 files changed, 62 insertions, 42 deletions
diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c index 50306b73b..9448cc7a5 100644 --- a/src/cadet/cadet_api.c +++ b/src/cadet/cadet_api.c | |||
@@ -380,6 +380,41 @@ struct CadetMQState | |||
380 | }; | 380 | }; |
381 | 381 | ||
382 | 382 | ||
383 | |||
384 | /******************************************************************************/ | ||
385 | /********************* FUNCTION DECLARATIONS *************************/ | ||
386 | /******************************************************************************/ | ||
387 | |||
388 | /** | ||
389 | * Reconnect to the service, retransmit all infomation to try to restore the | ||
390 | * original state. | ||
391 | * | ||
392 | * @param h Handle to the CADET service. | ||
393 | */ | ||
394 | static void | ||
395 | schedule_reconnect (struct GNUNET_CADET_Handle *h); | ||
396 | |||
397 | |||
398 | /** | ||
399 | * Reconnect callback: tries to reconnect again after a failer previous | ||
400 | * reconnection. | ||
401 | * | ||
402 | * @param cls Closure (cadet handle). | ||
403 | */ | ||
404 | static void | ||
405 | reconnect_cbk (void *cls); | ||
406 | |||
407 | |||
408 | /** | ||
409 | * Reconnect to the service, retransmit all infomation to try to restore the | ||
410 | * original state. | ||
411 | * | ||
412 | * @param h handle to the cadet | ||
413 | */ | ||
414 | static void | ||
415 | reconnect (struct GNUNET_CADET_Handle *h); | ||
416 | |||
417 | |||
383 | /******************************************************************************/ | 418 | /******************************************************************************/ |
384 | /*********************** AUXILIARY FUNCTIONS *************************/ | 419 | /*********************** AUXILIARY FUNCTIONS *************************/ |
385 | /******************************************************************************/ | 420 | /******************************************************************************/ |
@@ -1003,27 +1038,6 @@ handle_local_ack (void *cls, | |||
1003 | } | 1038 | } |
1004 | } | 1039 | } |
1005 | 1040 | ||
1006 | /** | ||
1007 | * Reconnect to the service, retransmit all infomation to try to restore the | ||
1008 | * original state. | ||
1009 | * | ||
1010 | * @param h handle to the cadet | ||
1011 | * | ||
1012 | * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...) | ||
1013 | */ | ||
1014 | static void | ||
1015 | reconnect (struct GNUNET_CADET_Handle *h); | ||
1016 | |||
1017 | |||
1018 | /** | ||
1019 | * Reconnect callback: tries to reconnect again after a failer previous | ||
1020 | * reconnection. | ||
1021 | * | ||
1022 | * @param cls closure (cadet handle) | ||
1023 | */ | ||
1024 | static void | ||
1025 | reconnect_cbk (void *cls); | ||
1026 | |||
1027 | 1041 | ||
1028 | /** | 1042 | /** |
1029 | * Generic error handler, called with the appropriate error code and | 1043 | * Generic error handler, called with the appropriate error code and |
@@ -1455,16 +1469,14 @@ handle_get_tunnel (void *cls, | |||
1455 | } | 1469 | } |
1456 | 1470 | ||
1457 | 1471 | ||
1458 | |||
1459 | /** | 1472 | /** |
1460 | * Reconnect to the service, retransmit all infomation to try to restore the | 1473 | * Reconnect to the service, retransmit all infomation to try to restore the |
1461 | * original state. | 1474 | * original state. |
1462 | * | 1475 | * |
1463 | * @param h handle to the cadet | 1476 | * @param h handle to the cadet |
1464 | * @return #GNUNET_YES in case of success, #GNUNET_NO otherwise (service down...) | ||
1465 | */ | 1477 | */ |
1466 | static int | 1478 | static void |
1467 | do_reconnect (struct GNUNET_CADET_Handle *h) | 1479 | reconnect (struct GNUNET_CADET_Handle *h) |
1468 | { | 1480 | { |
1469 | struct GNUNET_MQ_MessageHandler handlers[] = { | 1481 | struct GNUNET_MQ_MessageHandler handlers[] = { |
1470 | GNUNET_MQ_hd_fixed_size (channel_created, | 1482 | GNUNET_MQ_hd_fixed_size (channel_created, |
@@ -1499,16 +1511,28 @@ do_reconnect (struct GNUNET_CADET_Handle *h) | |||
1499 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL, | 1511 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL, |
1500 | struct GNUNET_CADET_LocalInfoTunnel, | 1512 | struct GNUNET_CADET_LocalInfoTunnel, |
1501 | h), | 1513 | h), |
1502 | // FIXME | 1514 | // FIXME |
1503 | // GNUNET_MQ_hd_fixed_Y size (channel_destroyed, | 1515 | // GNUNET_MQ_hd_fixed_Y size (channel_destroyed, |
1504 | // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED, | 1516 | // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED, |
1505 | // struct GNUNET_CADET_ChannelDestroyMessage); | 1517 | // struct GNUNET_CADET_ChannelDestroyMessage); |
1506 | GNUNET_MQ_handler_end () | 1518 | GNUNET_MQ_handler_end () |
1507 | }; | 1519 | }; |
1520 | struct GNUNET_CADET_Channel *ch; | ||
1521 | |||
1522 | while (NULL != (ch = h->channels_head)) | ||
1523 | { | ||
1524 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1525 | "Destroying channel due to a reconnect\n"); | ||
1526 | destroy_channel (ch); | ||
1527 | } | ||
1508 | 1528 | ||
1509 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n"); | 1529 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n"); |
1510 | 1530 | ||
1511 | GNUNET_assert (NULL == h->mq); | 1531 | if (NULL != h->mq) |
1532 | { | ||
1533 | GNUNET_MQ_destroy (h->mq); | ||
1534 | h->mq = NULL; | ||
1535 | } | ||
1512 | h->mq = GNUNET_CLIENT_connect (h->cfg, | 1536 | h->mq = GNUNET_CLIENT_connect (h->cfg, |
1513 | "cadet", | 1537 | "cadet", |
1514 | handlers, | 1538 | handlers, |
@@ -1516,14 +1540,13 @@ do_reconnect (struct GNUNET_CADET_Handle *h) | |||
1516 | h); | 1540 | h); |
1517 | if (NULL == h->mq) | 1541 | if (NULL == h->mq) |
1518 | { | 1542 | { |
1519 | reconnect (h); | 1543 | schedule_reconnect (h); |
1520 | return GNUNET_NO; | 1544 | return; |
1521 | } | 1545 | } |
1522 | else | 1546 | else |
1523 | { | 1547 | { |
1524 | h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; | 1548 | h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; |
1525 | } | 1549 | } |
1526 | return GNUNET_YES; | ||
1527 | } | 1550 | } |
1528 | 1551 | ||
1529 | /** | 1552 | /** |
@@ -1538,7 +1561,7 @@ reconnect_cbk (void *cls) | |||
1538 | struct GNUNET_CADET_Handle *h = cls; | 1561 | struct GNUNET_CADET_Handle *h = cls; |
1539 | 1562 | ||
1540 | h->reconnect_task = NULL; | 1563 | h->reconnect_task = NULL; |
1541 | do_reconnect (h); | 1564 | reconnect (h); |
1542 | } | 1565 | } |
1543 | 1566 | ||
1544 | 1567 | ||
@@ -1551,17 +1574,14 @@ reconnect_cbk (void *cls) | |||
1551 | * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...) | 1574 | * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...) |
1552 | */ | 1575 | */ |
1553 | static void | 1576 | static void |
1554 | reconnect (struct GNUNET_CADET_Handle *h) | 1577 | schedule_reconnect (struct GNUNET_CADET_Handle *h) |
1555 | { | 1578 | { |
1556 | struct GNUNET_CADET_Channel *ch; | ||
1557 | |||
1558 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1559 | "Requested RECONNECT, destroying all channels\n"); | ||
1560 | while (NULL != (ch = h->channels_head)) | ||
1561 | destroy_channel (ch); | ||
1562 | if (NULL == h->reconnect_task) | 1579 | if (NULL == h->reconnect_task) |
1580 | { | ||
1563 | h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time, | 1581 | h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time, |
1564 | &reconnect_cbk, h); | 1582 | &reconnect_cbk, h); |
1583 | h->reconnect_time = GNUNET_TIME_STD_BACKOFF (h->reconnect_time); | ||
1584 | } | ||
1565 | } | 1585 | } |
1566 | 1586 | ||
1567 | 1587 | ||
@@ -1584,7 +1604,7 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1584 | h->cfg = cfg; | 1604 | h->cfg = cfg; |
1585 | h->cleaner = cleaner; | 1605 | h->cleaner = cleaner; |
1586 | h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); | 1606 | h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); |
1587 | do_reconnect (h); | 1607 | reconnect (h); |
1588 | if (h->mq == NULL) | 1608 | if (h->mq == NULL) |
1589 | { | 1609 | { |
1590 | GNUNET_break (0); | 1610 | GNUNET_break (0); |
@@ -2371,7 +2391,7 @@ GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
2371 | h->cfg = cfg; | 2391 | h->cfg = cfg; |
2372 | h->mq_api = GNUNET_YES; | 2392 | h->mq_api = GNUNET_YES; |
2373 | h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); | 2393 | h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); |
2374 | do_reconnect (h); | 2394 | reconnect (h); |
2375 | if (NULL == h->mq) | 2395 | if (NULL == h->mq) |
2376 | { | 2396 | { |
2377 | GNUNET_break (0); | 2397 | GNUNET_break (0); |