diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-04-30 08:17:37 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-04-30 08:17:37 +0000 |
commit | 95f9076a2139f5fb042b944a0658b6cda2fa35db (patch) | |
tree | b0826a2a1dcf812e6b4450fe6b05d47cd53ae49d /src/cadet/gnunet-cadet.c | |
parent | 7746f68db77b9ca3c4aaca24ab2ce5253461240b (diff) | |
download | gnunet-95f9076a2139f5fb042b944a0658b6cda2fa35db.tar.gz gnunet-95f9076a2139f5fb042b944a0658b6cda2fa35db.zip |
implementing new scheduler shutdown semantics
Diffstat (limited to 'src/cadet/gnunet-cadet.c')
-rw-r--r-- | src/cadet/gnunet-cadet.c | 135 |
1 files changed, 71 insertions, 64 deletions
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c index 14b499aae..6453392d8 100644 --- a/src/cadet/gnunet-cadet.c +++ b/src/cadet/gnunet-cadet.c | |||
@@ -72,22 +72,22 @@ static uint32_t listen_port; | |||
72 | /** | 72 | /** |
73 | * Request echo service | 73 | * Request echo service |
74 | */ | 74 | */ |
75 | int echo; | 75 | static int echo; |
76 | 76 | ||
77 | /** | 77 | /** |
78 | * Request a debug dump | 78 | * Request a debug dump |
79 | */ | 79 | */ |
80 | int dump; | 80 | static int dump; |
81 | 81 | ||
82 | /** | 82 | /** |
83 | * Time of last echo request. | 83 | * Time of last echo request. |
84 | */ | 84 | */ |
85 | struct GNUNET_TIME_Absolute echo_time; | 85 | static struct GNUNET_TIME_Absolute echo_time; |
86 | 86 | ||
87 | /** | 87 | /** |
88 | * Task for next echo request. | 88 | * Task for next echo request. |
89 | */ | 89 | */ |
90 | struct GNUNET_SCHEDULER_Task * echo_task; | 90 | static struct GNUNET_SCHEDULER_Task *echo_task; |
91 | 91 | ||
92 | /** | 92 | /** |
93 | * Peer to connect to. | 93 | * Peer to connect to. |
@@ -102,8 +102,7 @@ static uint32_t target_port; | |||
102 | /** | 102 | /** |
103 | * Data pending in netcat mode. | 103 | * Data pending in netcat mode. |
104 | */ | 104 | */ |
105 | size_t data_size; | 105 | static size_t data_size; |
106 | |||
107 | 106 | ||
108 | /** | 107 | /** |
109 | * Cadet handle. | 108 | * Cadet handle. |
@@ -123,8 +122,17 @@ static struct GNUNET_CADET_TransmitHandle *th; | |||
123 | /** | 122 | /** |
124 | * Shutdown task handle. | 123 | * Shutdown task handle. |
125 | */ | 124 | */ |
126 | struct GNUNET_SCHEDULER_Task * sd; | 125 | static struct GNUNET_SCHEDULER_Task *sd; |
127 | 126 | ||
127 | /** | ||
128 | * Task for reading from stdin. | ||
129 | */ | ||
130 | static struct GNUNET_SCHEDULER_Task *rd_task; | ||
131 | |||
132 | /** | ||
133 | * Task for main job. | ||
134 | */ | ||
135 | static struct GNUNET_SCHEDULER_Task *job; | ||
128 | 136 | ||
129 | 137 | ||
130 | static void | 138 | static void |
@@ -195,7 +203,8 @@ conn_2s (uint16_t status) | |||
195 | static void | 203 | static void |
196 | shutdown_task (void *cls) | 204 | shutdown_task (void *cls) |
197 | { | 205 | { |
198 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); | 206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
207 | "Shutdown\n"); | ||
199 | if (NULL != th) | 208 | if (NULL != th) |
200 | { | 209 | { |
201 | GNUNET_CADET_notify_transmit_ready_cancel (th); | 210 | GNUNET_CADET_notify_transmit_ready_cancel (th); |
@@ -209,7 +218,22 @@ shutdown_task (void *cls) | |||
209 | if (NULL != mh) | 218 | if (NULL != mh) |
210 | { | 219 | { |
211 | GNUNET_CADET_disconnect (mh); | 220 | GNUNET_CADET_disconnect (mh); |
212 | mh = NULL; | 221 | mh = NULL; |
222 | } | ||
223 | if (NULL != rd_task) | ||
224 | { | ||
225 | GNUNET_SCHEDULER_cancel (rd_task); | ||
226 | rd_task = NULL; | ||
227 | } | ||
228 | if (NULL != echo_task) | ||
229 | { | ||
230 | GNUNET_SCHEDULER_cancel (echo_task); | ||
231 | echo_task = NULL; | ||
232 | } | ||
233 | if (NULL != job) | ||
234 | { | ||
235 | GNUNET_SCHEDULER_cancel (job); | ||
236 | job = NULL; | ||
213 | } | 237 | } |
214 | } | 238 | } |
215 | 239 | ||
@@ -227,7 +251,7 @@ shutdown_task (void *cls) | |||
227 | * @param buf where the callee should write the message | 251 | * @param buf where the callee should write the message |
228 | * @return number of bytes written to buf | 252 | * @return number of bytes written to buf |
229 | */ | 253 | */ |
230 | size_t | 254 | static size_t |
231 | data_ready (void *cls, size_t size, void *buf) | 255 | data_ready (void *cls, size_t size, void *buf) |
232 | { | 256 | { |
233 | struct GNUNET_MessageHeader *msg; | 257 | struct GNUNET_MessageHeader *msg; |
@@ -271,13 +295,6 @@ static void | |||
271 | read_stdio (void *cls) | 295 | read_stdio (void *cls) |
272 | { | 296 | { |
273 | static char buf[60000]; | 297 | static char buf[60000]; |
274 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
275 | |||
276 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
277 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
278 | { | ||
279 | return; | ||
280 | } | ||
281 | 298 | ||
282 | data_size = read (0, buf, 60000); | 299 | data_size = read (0, buf, 60000); |
283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size); | 300 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size); |
@@ -305,10 +322,10 @@ listen_stdio (void) | |||
305 | 322 | ||
306 | rs = GNUNET_NETWORK_fdset_create (); | 323 | rs = GNUNET_NETWORK_fdset_create (); |
307 | GNUNET_NETWORK_fdset_set_native (rs, 0); | 324 | GNUNET_NETWORK_fdset_set_native (rs, 0); |
308 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 325 | rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
309 | GNUNET_TIME_UNIT_FOREVER_REL, | 326 | GNUNET_TIME_UNIT_FOREVER_REL, |
310 | rs, NULL, | 327 | rs, NULL, |
311 | &read_stdio, NULL); | 328 | &read_stdio, NULL); |
312 | GNUNET_NETWORK_fdset_destroy (rs); | 329 | GNUNET_NETWORK_fdset_destroy (rs); |
313 | } | 330 | } |
314 | 331 | ||
@@ -408,12 +425,8 @@ channel_incoming (void *cls, | |||
408 | static void | 425 | static void |
409 | send_echo (void *cls) | 426 | send_echo (void *cls) |
410 | { | 427 | { |
411 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 428 | if (NULL == ch) |
412 | |||
413 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
414 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || NULL == ch) | ||
415 | return; | 429 | return; |
416 | |||
417 | GNUNET_assert (NULL == th); | 430 | GNUNET_assert (NULL == th); |
418 | th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO, | 431 | th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO, |
419 | GNUNET_TIME_UNIT_FOREVER_REL, | 432 | GNUNET_TIME_UNIT_FOREVER_REL, |
@@ -432,7 +445,8 @@ request_dump (void *cls) | |||
432 | { | 445 | { |
433 | GNUNET_CADET_request_dump (mh); | 446 | GNUNET_CADET_request_dump (mh); |
434 | GNUNET_SCHEDULER_cancel (sd); | 447 | GNUNET_SCHEDULER_cancel (sd); |
435 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &shutdown_task, NULL); | 448 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, |
449 | &shutdown_task, NULL); | ||
436 | } | 450 | } |
437 | 451 | ||
438 | 452 | ||
@@ -466,7 +480,7 @@ create_channel (void *cls) | |||
466 | if (GNUNET_NO == echo) | 480 | if (GNUNET_NO == echo) |
467 | listen_stdio (); | 481 | listen_stdio (); |
468 | else | 482 | else |
469 | GNUNET_SCHEDULER_add_now (send_echo, NULL); | 483 | echo_task = GNUNET_SCHEDULER_add_now (&send_echo, NULL); |
470 | } | 484 | } |
471 | 485 | ||
472 | 486 | ||
@@ -561,8 +575,11 @@ data_callback (void *cls, | |||
561 | * (0 = unknown, 1 = ourselves, 2 = neighbor) | 575 | * (0 = unknown, 1 = ourselves, 2 = neighbor) |
562 | */ | 576 | */ |
563 | static void | 577 | static void |
564 | peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer, | 578 | peers_callback (void *cls, |
565 | int tunnel, unsigned int n_paths, unsigned int best_path) | 579 | const struct GNUNET_PeerIdentity *peer, |
580 | int tunnel, | ||
581 | unsigned int n_paths, | ||
582 | unsigned int best_path) | ||
566 | { | 583 | { |
567 | if (NULL == peer) | 584 | if (NULL == peer) |
568 | { | 585 | { |
@@ -589,7 +606,7 @@ peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
589 | * Each path starts with the local peer. | 606 | * Each path starts with the local peer. |
590 | * Each path ends with the destination peer (given in @c peer). | 607 | * Each path ends with the destination peer (given in @c peer). |
591 | */ | 608 | */ |
592 | void | 609 | static void |
593 | peer_callback (void *cls, | 610 | peer_callback (void *cls, |
594 | const struct GNUNET_PeerIdentity *peer, | 611 | const struct GNUNET_PeerIdentity *peer, |
595 | int tunnel, | 612 | int tunnel, |
@@ -629,7 +646,7 @@ peer_callback (void *cls, | |||
629 | * @param estate Encryption state. | 646 | * @param estate Encryption state. |
630 | * @param cstate Connectivity state. | 647 | * @param cstate Connectivity state. |
631 | */ | 648 | */ |
632 | void | 649 | static void |
633 | tunnels_callback (void *cls, | 650 | tunnels_callback (void *cls, |
634 | const struct GNUNET_PeerIdentity *peer, | 651 | const struct GNUNET_PeerIdentity *peer, |
635 | unsigned int channels, | 652 | unsigned int channels, |
@@ -665,7 +682,7 @@ tunnels_callback (void *cls, | |||
665 | * @param estate Encryption status. | 682 | * @param estate Encryption status. |
666 | * @param cstate Connectivity status. | 683 | * @param cstate Connectivity status. |
667 | */ | 684 | */ |
668 | void | 685 | static void |
669 | tunnel_callback (void *cls, | 686 | tunnel_callback (void *cls, |
670 | const struct GNUNET_PeerIdentity *peer, | 687 | const struct GNUNET_PeerIdentity *peer, |
671 | unsigned int n_channels, | 688 | unsigned int n_channels, |
@@ -691,7 +708,7 @@ tunnel_callback (void *cls, | |||
691 | } | 708 | } |
692 | if (GNUNET_YES != monitor_mode) | 709 | if (GNUNET_YES != monitor_mode) |
693 | { | 710 | { |
694 | GNUNET_SCHEDULER_shutdown(); | 711 | GNUNET_SCHEDULER_shutdown (); |
695 | } | 712 | } |
696 | return; | 713 | return; |
697 | } | 714 | } |
@@ -705,14 +722,7 @@ tunnel_callback (void *cls, | |||
705 | static void | 722 | static void |
706 | get_peers (void *cls) | 723 | get_peers (void *cls) |
707 | { | 724 | { |
708 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 725 | job = NULL; |
709 | |||
710 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
711 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
712 | { | ||
713 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); | ||
714 | return; | ||
715 | } | ||
716 | GNUNET_CADET_get_peers (mh, &peers_callback, NULL); | 726 | GNUNET_CADET_get_peers (mh, &peers_callback, NULL); |
717 | } | 727 | } |
718 | 728 | ||
@@ -727,6 +737,7 @@ show_peer (void *cls) | |||
727 | { | 737 | { |
728 | struct GNUNET_PeerIdentity pid; | 738 | struct GNUNET_PeerIdentity pid; |
729 | 739 | ||
740 | job = NULL; | ||
730 | if (GNUNET_OK != | 741 | if (GNUNET_OK != |
731 | GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id, | 742 | GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id, |
732 | strlen (peer_id), | 743 | strlen (peer_id), |
@@ -741,6 +752,7 @@ show_peer (void *cls) | |||
741 | GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL); | 752 | GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL); |
742 | } | 753 | } |
743 | 754 | ||
755 | |||
744 | /** | 756 | /** |
745 | * Call CADET's meta API, get all tunnels known to a peer. | 757 | * Call CADET's meta API, get all tunnels known to a peer. |
746 | * | 758 | * |
@@ -749,14 +761,7 @@ show_peer (void *cls) | |||
749 | static void | 761 | static void |
750 | get_tunnels (void *cls) | 762 | get_tunnels (void *cls) |
751 | { | 763 | { |
752 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 764 | job = NULL; |
753 | |||
754 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
755 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
756 | { | ||
757 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); | ||
758 | return; | ||
759 | } | ||
760 | GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL); | 765 | GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL); |
761 | } | 766 | } |
762 | 767 | ||
@@ -779,7 +784,7 @@ show_tunnel (void *cls) | |||
779 | fprintf (stderr, | 784 | fprintf (stderr, |
780 | _("Invalid tunnel owner `%s'\n"), | 785 | _("Invalid tunnel owner `%s'\n"), |
781 | tunnel_id); | 786 | tunnel_id); |
782 | GNUNET_SCHEDULER_shutdown(); | 787 | GNUNET_SCHEDULER_shutdown (); |
783 | return; | 788 | return; |
784 | } | 789 | } |
785 | GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL); | 790 | GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL); |
@@ -794,7 +799,8 @@ show_tunnel (void *cls) | |||
794 | static void | 799 | static void |
795 | show_channel (void *cls) | 800 | show_channel (void *cls) |
796 | { | 801 | { |
797 | 802 | job = NULL; | |
803 | GNUNET_break (0); | ||
798 | } | 804 | } |
799 | 805 | ||
800 | 806 | ||
@@ -806,7 +812,8 @@ show_channel (void *cls) | |||
806 | static void | 812 | static void |
807 | show_connection (void *cls) | 813 | show_connection (void *cls) |
808 | { | 814 | { |
809 | 815 | job = NULL; | |
816 | GNUNET_break (0); | ||
810 | } | 817 | } |
811 | 818 | ||
812 | 819 | ||
@@ -819,7 +826,9 @@ show_connection (void *cls) | |||
819 | * @param cfg configuration | 826 | * @param cfg configuration |
820 | */ | 827 | */ |
821 | static void | 828 | static void |
822 | run (void *cls, char *const *args, const char *cfgfile, | 829 | run (void *cls, |
830 | char *const *args, | ||
831 | const char *cfgfile, | ||
823 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 832 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
824 | { | 833 | { |
825 | GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL; | 834 | GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL; |
@@ -871,32 +880,32 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
871 | else if (NULL != peer_id) | 880 | else if (NULL != peer_id) |
872 | { | 881 | { |
873 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); | 882 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); |
874 | GNUNET_SCHEDULER_add_now (&show_peer, NULL); | 883 | job = GNUNET_SCHEDULER_add_now (&show_peer, NULL); |
875 | } | 884 | } |
876 | else if (NULL != tunnel_id) | 885 | else if (NULL != tunnel_id) |
877 | { | 886 | { |
878 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n"); | 887 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n"); |
879 | GNUNET_SCHEDULER_add_now (&show_tunnel, NULL); | 888 | job = GNUNET_SCHEDULER_add_now (&show_tunnel, NULL); |
880 | } | 889 | } |
881 | else if (NULL != channel_id) | 890 | else if (NULL != channel_id) |
882 | { | 891 | { |
883 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); | 892 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); |
884 | GNUNET_SCHEDULER_add_now (&show_channel, NULL); | 893 | job = GNUNET_SCHEDULER_add_now (&show_channel, NULL); |
885 | } | 894 | } |
886 | else if (NULL != conn_id) | 895 | else if (NULL != conn_id) |
887 | { | 896 | { |
888 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); | 897 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); |
889 | GNUNET_SCHEDULER_add_now (&show_connection, NULL); | 898 | job = GNUNET_SCHEDULER_add_now (&show_connection, NULL); |
890 | } | 899 | } |
891 | else if (GNUNET_YES == request_peers) | 900 | else if (GNUNET_YES == request_peers) |
892 | { | 901 | { |
893 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); | 902 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); |
894 | GNUNET_SCHEDULER_add_now (&get_peers, NULL); | 903 | job = GNUNET_SCHEDULER_add_now (&get_peers, NULL); |
895 | } | 904 | } |
896 | else if (GNUNET_YES == request_tunnels) | 905 | else if (GNUNET_YES == request_tunnels) |
897 | { | 906 | { |
898 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); | 907 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); |
899 | GNUNET_SCHEDULER_add_now (&get_tunnels, NULL); | 908 | job = GNUNET_SCHEDULER_add_now (&get_tunnels, NULL); |
900 | } | 909 | } |
901 | else | 910 | else |
902 | { | 911 | { |
@@ -913,11 +922,9 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
913 | ports); | 922 | ports); |
914 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n"); | 923 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n"); |
915 | if (NULL == mh) | 924 | if (NULL == mh) |
916 | GNUNET_SCHEDULER_add_now (shutdown_task, NULL); | 925 | GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); |
917 | else | 926 | else |
918 | sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 927 | sd = GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); |
919 | shutdown_task, NULL); | ||
920 | |||
921 | } | 928 | } |
922 | 929 | ||
923 | 930 | ||