diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-02-02 23:16:13 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-02-02 23:16:13 +0000 |
commit | 8727c5c69097aeb474ce6039bf31650400b6286a (patch) | |
tree | 7159a82133c6d5d411a02a513aa247df7031a263 /src/transport/gnunet-service-transport_neighbours.c | |
parent | fee260b50973a0e26eaa10a5736a3e16ee6201ae (diff) | |
download | gnunet-8727c5c69097aeb474ce6039bf31650400b6286a.tar.gz gnunet-8727c5c69097aeb474ce6039bf31650400b6286a.zip |
reduce callback insanity for callbacks that are always bound to only one value
Diffstat (limited to 'src/transport/gnunet-service-transport_neighbours.c')
-rw-r--r-- | src/transport/gnunet-service-transport_neighbours.c | 210 |
1 files changed, 127 insertions, 83 deletions
diff --git a/src/transport/gnunet-service-transport_neighbours.c b/src/transport/gnunet-service-transport_neighbours.c index 401e6eb2b..f1095095a 100644 --- a/src/transport/gnunet-service-transport_neighbours.c +++ b/src/transport/gnunet-service-transport_neighbours.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "gnunet-service-transport_blacklist.h" | 29 | #include "gnunet-service-transport_blacklist.h" |
30 | #include "gnunet-service-transport_clients.h" | 30 | #include "gnunet-service-transport_clients.h" |
31 | #include "gnunet-service-transport_neighbours.h" | 31 | #include "gnunet-service-transport_neighbours.h" |
32 | #include "gnunet-service-transport_manipulation.h" | ||
32 | #include "gnunet-service-transport_plugins.h" | 33 | #include "gnunet-service-transport_plugins.h" |
33 | #include "gnunet-service-transport_validation.h" | 34 | #include "gnunet-service-transport_validation.h" |
34 | #include "gnunet-service-transport.h" | 35 | #include "gnunet-service-transport.h" |
@@ -507,26 +508,6 @@ static struct BlacklistCheckSwitchContext *pending_bc_head; | |||
507 | static struct BlacklistCheckSwitchContext *pending_bc_tail; | 508 | static struct BlacklistCheckSwitchContext *pending_bc_tail; |
508 | 509 | ||
509 | /** | 510 | /** |
510 | * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb | ||
511 | */ | ||
512 | static void *callback_cls; | ||
513 | |||
514 | /** | ||
515 | * Function to call when we connected to a neighbour. | ||
516 | */ | ||
517 | static NotifyConnect connect_notify_cb; | ||
518 | |||
519 | /** | ||
520 | * Function to call when we disconnected from a neighbour. | ||
521 | */ | ||
522 | static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb; | ||
523 | |||
524 | /** | ||
525 | * Function to call when a neighbour changed address, state or bandwidth. | ||
526 | */ | ||
527 | static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb; | ||
528 | |||
529 | /** | ||
530 | * counter for connected neighbours | 511 | * counter for connected neighbours |
531 | */ | 512 | */ |
532 | static unsigned int neighbours_connected; | 513 | static unsigned int neighbours_connected; |
@@ -566,6 +547,93 @@ print_ack_state (enum GST_ACK_State s) | |||
566 | } | 547 | } |
567 | } | 548 | } |
568 | 549 | ||
550 | |||
551 | /** | ||
552 | * Notify our clients that another peer connected to us. | ||
553 | * | ||
554 | * @param peer the peer that connected | ||
555 | * @param bandwidth_in inbound bandwidth in NBO | ||
556 | * @param bandwidth_out outbound bandwidth in NBO | ||
557 | */ | ||
558 | static void | ||
559 | neighbours_connect_notification (const struct GNUNET_PeerIdentity *peer, | ||
560 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, | ||
561 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) | ||
562 | { | ||
563 | size_t len = sizeof(struct ConnectInfoMessage); | ||
564 | char buf[len] GNUNET_ALIGN; | ||
565 | struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf; | ||
566 | |||
567 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
568 | "We are now connected to peer `%s'\n", | ||
569 | GNUNET_i2s (peer)); | ||
570 | connect_msg->header.size = htons (sizeof(buf)); | ||
571 | connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT); | ||
572 | connect_msg->id = *peer; | ||
573 | connect_msg->quota_in = bandwidth_in; | ||
574 | connect_msg->quota_out = bandwidth_out; | ||
575 | GST_clients_broadcast (&connect_msg->header, GNUNET_NO); | ||
576 | } | ||
577 | |||
578 | |||
579 | /** | ||
580 | * Notify our clients (and manipulation) that a peer disconnected from | ||
581 | * us. | ||
582 | * | ||
583 | * @param peer the peer that disconnected | ||
584 | */ | ||
585 | static void | ||
586 | neighbours_disconnect_notification (const struct GNUNET_PeerIdentity *peer) | ||
587 | { | ||
588 | struct DisconnectInfoMessage disconnect_msg; | ||
589 | |||
590 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
591 | "Peer `%s' disconnected\n", | ||
592 | GNUNET_i2s (peer)); | ||
593 | GST_manipulation_peer_disconnect (peer); | ||
594 | disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage)); | ||
595 | disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT); | ||
596 | disconnect_msg.reserved = htonl (0); | ||
597 | disconnect_msg.peer = *peer; | ||
598 | GST_clients_broadcast (&disconnect_msg.header, | ||
599 | GNUNET_NO); | ||
600 | } | ||
601 | |||
602 | |||
603 | /** | ||
604 | * Notify transport clients that a neighbour peer changed its active | ||
605 | * address. | ||
606 | * | ||
607 | * @param peer identity of the peer | ||
608 | * @param address address possibly NULL if peer is not connected | ||
609 | * @param state current state this peer is in | ||
610 | * @param state_timeout timeout for the current state of the peer | ||
611 | * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect | ||
612 | * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect | ||
613 | */ | ||
614 | static void | ||
615 | neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer, | ||
616 | const struct GNUNET_HELLO_Address *address, | ||
617 | enum GNUNET_TRANSPORT_PeerState state, | ||
618 | struct GNUNET_TIME_Absolute state_timeout, | ||
619 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, | ||
620 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) | ||
621 | { | ||
622 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
623 | "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n", | ||
624 | GNUNET_i2s (peer), | ||
625 | GST_plugins_a2s (address), | ||
626 | GNUNET_TRANSPORT_ps2s (state), | ||
627 | GNUNET_STRINGS_absolute_time_to_string (state_timeout)); | ||
628 | /* FIXME: include bandwidth in notification! */ | ||
629 | GST_clients_broadcast_peer_notification (peer, | ||
630 | address, | ||
631 | state, | ||
632 | state_timeout); | ||
633 | } | ||
634 | |||
635 | |||
636 | |||
569 | /** | 637 | /** |
570 | * Lookup a neighbour entry in the neighbours hash map. | 638 | * Lookup a neighbour entry in the neighbours hash map. |
571 | * | 639 | * |
@@ -666,13 +734,12 @@ set_state (struct NeighbourMapEntry *n, | |||
666 | "Neighbour `%s' changed state to %s\n", | 734 | "Neighbour `%s' changed state to %s\n", |
667 | GNUNET_i2s (&n->id), | 735 | GNUNET_i2s (&n->id), |
668 | GNUNET_TRANSPORT_ps2s(s)); | 736 | GNUNET_TRANSPORT_ps2s(s)); |
669 | neighbour_change_cb (callback_cls, | 737 | neighbours_changed_notification (&n->id, |
670 | &n->id, | 738 | n->primary_address.address, |
671 | n->primary_address.address, | 739 | n->state, |
672 | n->state, | 740 | n->timeout, |
673 | n->timeout, | 741 | n->primary_address.bandwidth_in, |
674 | n->primary_address.bandwidth_in, | 742 | n->primary_address.bandwidth_out); |
675 | n->primary_address.bandwidth_out); | ||
676 | } | 743 | } |
677 | 744 | ||
678 | 745 | ||
@@ -695,13 +762,12 @@ set_state_and_timeout (struct NeighbourMapEntry *n, | |||
695 | GNUNET_i2s (&n->id), | 762 | GNUNET_i2s (&n->id), |
696 | GNUNET_TRANSPORT_ps2s(s), | 763 | GNUNET_TRANSPORT_ps2s(s), |
697 | GNUNET_STRINGS_absolute_time_to_string (timeout)); | 764 | GNUNET_STRINGS_absolute_time_to_string (timeout)); |
698 | neighbour_change_cb (callback_cls, | 765 | neighbours_changed_notification (&n->id, |
699 | &n->id, | 766 | n->primary_address.address, |
700 | n->primary_address.address, | 767 | n->state, |
701 | n->state, | 768 | n->timeout, |
702 | n->timeout, | 769 | n->primary_address.bandwidth_in, |
703 | n->primary_address.bandwidth_in, | 770 | n->primary_address.bandwidth_out); |
704 | n->primary_address.bandwidth_out); | ||
705 | } | 771 | } |
706 | 772 | ||
707 | 773 | ||
@@ -720,12 +786,11 @@ set_timeout (struct NeighbourMapEntry *n, | |||
720 | "Neighbour `%s' changed timeout %s\n", | 786 | "Neighbour `%s' changed timeout %s\n", |
721 | GNUNET_i2s (&n->id), | 787 | GNUNET_i2s (&n->id), |
722 | GNUNET_STRINGS_absolute_time_to_string (timeout)); | 788 | GNUNET_STRINGS_absolute_time_to_string (timeout)); |
723 | neighbour_change_cb (callback_cls, | 789 | neighbours_changed_notification (&n->id, |
724 | &n->id, | 790 | n->primary_address.address, |
725 | n->primary_address.address, | 791 | n->state, n->timeout, |
726 | n->state, n->timeout, | 792 | n->primary_address.bandwidth_in, |
727 | n->primary_address.bandwidth_in, | 793 | n->primary_address.bandwidth_out); |
728 | n->primary_address.bandwidth_out); | ||
729 | } | 794 | } |
730 | 795 | ||
731 | 796 | ||
@@ -875,13 +940,12 @@ set_primary_address (struct NeighbourMapEntry *n, | |||
875 | GNUNET_i2s (&n->id), | 940 | GNUNET_i2s (&n->id), |
876 | GST_plugins_a2s(address)); | 941 | GST_plugins_a2s(address)); |
877 | 942 | ||
878 | neighbour_change_cb (callback_cls, | 943 | neighbours_changed_notification (&n->id, |
879 | &n->id, | 944 | n->primary_address.address, |
880 | n->primary_address.address, | 945 | n->state, |
881 | n->state, | 946 | n->timeout, |
882 | n->timeout, | 947 | n->primary_address.bandwidth_in, |
883 | n->primary_address.bandwidth_in, | 948 | n->primary_address.bandwidth_out); |
884 | n->primary_address.bandwidth_out); | ||
885 | } | 949 | } |
886 | 950 | ||
887 | 951 | ||
@@ -895,13 +959,12 @@ static void | |||
895 | unset_primary_address (struct NeighbourMapEntry *n) | 959 | unset_primary_address (struct NeighbourMapEntry *n) |
896 | { | 960 | { |
897 | /* Notify monitoring about change */ | 961 | /* Notify monitoring about change */ |
898 | neighbour_change_cb (callback_cls, | 962 | neighbours_changed_notification (&n->id, |
899 | &n->id, | 963 | n->primary_address.address, |
900 | n->primary_address.address, | 964 | n->state, |
901 | n->state, | 965 | n->timeout, |
902 | n->timeout, | 966 | GNUNET_BANDWIDTH_value_init (0), |
903 | GNUNET_BANDWIDTH_value_init (0), | 967 | GNUNET_BANDWIDTH_value_init (0)); |
904 | GNUNET_BANDWIDTH_value_init (0)); | ||
905 | /* Unset primary address */ | 968 | /* Unset primary address */ |
906 | free_address (&n->primary_address); | 969 | free_address (&n->primary_address); |
907 | } | 970 | } |
@@ -957,7 +1020,7 @@ free_neighbour (struct NeighbourMapEntry *n, | |||
957 | gettext_noop ("# peers connected"), | 1020 | gettext_noop ("# peers connected"), |
958 | --neighbours_connected, | 1021 | --neighbours_connected, |
959 | GNUNET_NO); | 1022 | GNUNET_NO); |
960 | disconnect_notify_cb (callback_cls, &n->id); | 1023 | neighbours_disconnect_notification (&n->id); |
961 | } | 1024 | } |
962 | 1025 | ||
963 | /* Mark peer as disconnected */ | 1026 | /* Mark peer as disconnected */ |
@@ -1184,7 +1247,7 @@ disconnect_neighbour (struct NeighbourMapEntry *n) | |||
1184 | gettext_noop ("# peers connected"), | 1247 | gettext_noop ("# peers connected"), |
1185 | --neighbours_connected, | 1248 | --neighbours_connected, |
1186 | GNUNET_NO); | 1249 | GNUNET_NO); |
1187 | disconnect_notify_cb (callback_cls, &n->id); | 1250 | neighbours_disconnect_notification (&n->id); |
1188 | set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT); | 1251 | set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT); |
1189 | break; | 1252 | break; |
1190 | case GNUNET_TRANSPORT_PS_RECONNECT_ATS: | 1253 | case GNUNET_TRANSPORT_PS_RECONNECT_ATS: |
@@ -3213,10 +3276,9 @@ GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *messag | |||
3213 | gettext_noop ("# peers connected"), | 3276 | gettext_noop ("# peers connected"), |
3214 | ++neighbours_connected, | 3277 | ++neighbours_connected, |
3215 | GNUNET_NO); | 3278 | GNUNET_NO); |
3216 | connect_notify_cb (callback_cls, | 3279 | neighbours_connect_notification (&n->id, |
3217 | &n->id, | 3280 | n->primary_address.bandwidth_in, |
3218 | n->primary_address.bandwidth_in, | 3281 | n->primary_address.bandwidth_out); |
3219 | n->primary_address.bandwidth_out); | ||
3220 | /* Tell ATS that the outbound session we created to send SYN was successful */ | 3282 | /* Tell ATS that the outbound session we created to send SYN was successful */ |
3221 | set_primary_address (n, | 3283 | set_primary_address (n, |
3222 | n->primary_address.address, | 3284 | n->primary_address.address, |
@@ -3502,10 +3564,9 @@ GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message, | |||
3502 | if (GNUNET_NO == GST_neighbours_test_connected(&n->id)) | 3564 | if (GNUNET_NO == GST_neighbours_test_connected(&n->id)) |
3503 | { | 3565 | { |
3504 | /* Notify about connection */ | 3566 | /* Notify about connection */ |
3505 | connect_notify_cb (callback_cls, | 3567 | neighbours_connect_notification (&n->id, |
3506 | &n->id, | 3568 | n->primary_address.bandwidth_in, |
3507 | n->primary_address.bandwidth_in, | 3569 | n->primary_address.bandwidth_out); |
3508 | n->primary_address.bandwidth_out); | ||
3509 | GNUNET_STATISTICS_set (GST_stats, | 3570 | GNUNET_STATISTICS_set (GST_stats, |
3510 | gettext_noop ("# peers connected"), | 3571 | gettext_noop ("# peers connected"), |
3511 | ++neighbours_connected, | 3572 | ++neighbours_connected, |
@@ -3833,24 +3894,11 @@ GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer) | |||
3833 | /** | 3894 | /** |
3834 | * Initialize the neighbours subsystem. | 3895 | * Initialize the neighbours subsystem. |
3835 | * | 3896 | * |
3836 | * @param cls closure for callbacks | ||
3837 | * @param connect_cb function to call if we connect to a peer | ||
3838 | * @param disconnect_cb function to call if we disconnect from a peer | ||
3839 | * @param peer_address_cb function to call if we change an active address | ||
3840 | * of a neighbour | ||
3841 | * @param max_fds maximum number of fds to use | 3897 | * @param max_fds maximum number of fds to use |
3842 | */ | 3898 | */ |
3843 | void | 3899 | void |
3844 | GST_neighbours_start (void *cls, | 3900 | GST_neighbours_start (unsigned int max_fds) |
3845 | NotifyConnect connect_cb, | 3901 | { |
3846 | GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb, | ||
3847 | GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb, | ||
3848 | unsigned int max_fds) | ||
3849 | { | ||
3850 | callback_cls = cls; | ||
3851 | connect_notify_cb = connect_cb; | ||
3852 | disconnect_notify_cb = disconnect_cb; | ||
3853 | neighbour_change_cb = peer_address_cb; | ||
3854 | neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); | 3902 | neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); |
3855 | registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); | 3903 | registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); |
3856 | util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL, | 3904 | util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL, |
@@ -3926,10 +3974,6 @@ GST_neighbours_stop () | |||
3926 | registered_quota_notifications = NULL; | 3974 | registered_quota_notifications = NULL; |
3927 | 3975 | ||
3928 | neighbours = NULL; | 3976 | neighbours = NULL; |
3929 | callback_cls = NULL; | ||
3930 | connect_notify_cb = NULL; | ||
3931 | disconnect_notify_cb = NULL; | ||
3932 | neighbour_change_cb = NULL; | ||
3933 | } | 3977 | } |
3934 | 3978 | ||
3935 | 3979 | ||