From ab44ab06bbd602e11035e9109c9f7d5b9a244cb0 Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Thu, 8 Apr 2010 14:29:44 +0000 Subject: [PATCH] Modifications so hostlist client and server will notified when new peers connect --- src/hostlist/gnunet-daemon-hostlist.c | 68 +++++++++++++++---- src/hostlist/hostlist-server.c | 42 +++++++++++- src/hostlist/hostlist-server.h | 4 +- src/hostlist/learning_peer1.conf | 6 +- src/hostlist/learning_peer2.conf | 8 +-- .../test_gnunet_daemon_hostlist_learning.c | 3 +- 6 files changed, 106 insertions(+), 25 deletions(-) diff --git a/src/hostlist/gnunet-daemon-hostlist.c b/src/hostlist/gnunet-daemon-hostlist.c index 2197a50b1..cf4be4798 100644 --- a/src/hostlist/gnunet-daemon-hostlist.c +++ b/src/hostlist/gnunet-daemon-hostlist.c @@ -42,6 +42,8 @@ #include "gnunet_time_lib.h" #include "gnunet_util_lib.h" +#define DEBUG_HOSTLIST GNUNET_YES + /** * Set if we are allowed to advertise our hostlist to others. */ @@ -79,6 +81,25 @@ struct GNUNET_CORE_Handle *core; */ GNUNET_CORE_MessageCallback client_adv_handler = NULL; +/** + * Handle to hostlist client's connect handler + */ +GNUNET_CORE_ConnectEventHandler client_ch = NULL; + +/** + * Handle to hostlist client's disconnect handler + */ +GNUNET_CORE_DisconnectEventHandler client_dh = NULL; + +/** + * Handle to hostlist server's connect handler + */ +GNUNET_CORE_ConnectEventHandler server_ch = NULL; + +/** + * Handle to hostlist server's disconnect handler + */ +GNUNET_CORE_DisconnectEventHandler server_dh = NULL; /** * gnunet-daemon-hostlist command line options. @@ -133,7 +154,7 @@ static int advertisement_handler (void *cls, } /** - * Method called whenever a given peer connects. + * Method called whenever a given peer connects. Wrapper to call both client's and server's functions * * @param cls closure * @param peer peer identity this notification is about @@ -147,11 +168,36 @@ connect_handler (void *cls, struct GNUNET_TIME_Relative latency, uint32_t distance) { - /* call hostlist client connection handler*/ - - /* do my own stuff */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "A new peer connected, notifying client and server\n"); + if ( NULL != client_ch) + (*client_ch) (cls, peer, latency, distance); + if ( NULL != server_ch) + (*server_ch) (cls, peer, latency, distance); } +/** + * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions + * + * @param cls closure + * @param peer peer identity this notification is about + * @param latency reported latency of the connection with 'other' + * @param distance reported distance (DV) to 'other' + */ +static void +disconnect_handler (void *cls, + const struct + GNUNET_PeerIdentity * peer) +{ + + /* call hostlist client disconnect handler*/ + if ( NULL != client_dh) + (*client_dh) (cls, peer); + + /* call hostlist server disconnect handler*/ + if ( NULL != server_dh) + (*server_dh) (cls, peer); +} /** * Last task run during shutdown. Disconnects us from @@ -208,9 +254,6 @@ run (void *cls, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle * cfg) { - GNUNET_CORE_ConnectEventHandler ch = NULL; - GNUNET_CORE_DisconnectEventHandler dh = NULL; - if ( (! bootstrapping) && (! learning) && (! provide_hostlist) ) @@ -223,27 +266,22 @@ run (void *cls, if (bootstrapping) { GNUNET_HOSTLIST_client_start (cfg, sched, stats, - &ch, &dh, &client_adv_handler); + &client_ch, &client_dh, &client_adv_handler); } if (provide_hostlist) { - GNUNET_HOSTLIST_server_start (cfg, sched, stats); + GNUNET_HOSTLIST_server_start (cfg, sched, stats, &server_ch, &server_dh); } if (learning) { } - - struct GNUNET_TIME_Relative a; - advertisement_handler(NULL,NULL,NULL,a,6); - - core = GNUNET_CORE_connect (sched, cfg, GNUNET_TIME_UNIT_FOREVER_REL, NULL, &core_init, - NULL, ch, dh, + NULL, &connect_handler, &disconnect_handler, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers); diff --git a/src/hostlist/hostlist-server.c b/src/hostlist/hostlist-server.c index bd4203475..6c7c5a8f1 100644 --- a/src/hostlist/hostlist-server.c +++ b/src/hostlist/hostlist-server.c @@ -349,6 +349,40 @@ access_handler_callback (void *cls, return MHD_queue_response (connection, MHD_HTTP_OK, response); } +/** + * Method called whenever a given peer connects. + * + * @param cls closure + * @param peer peer identity this notification is about + * @param latency reported latency of the connection with 'other' + * @param distance reported distance (DV) to 'other' + */ +static void +connect_handler (void *cls, + const struct + GNUNET_PeerIdentity * peer, + struct GNUNET_TIME_Relative latency, + uint32_t distance) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "A new peer connected to the server, preparing to send hostlist advertisement\n"); +} + + +/** + * Method called whenever a given peer disconnects. + * + * @param cls closure + * @param peer peer identity this notification is about + */ +static void +disconnect_handler (void *cls, + const struct + GNUNET_PeerIdentity * peer) +{ + +} + /** * Function that queries MHD's select sets and @@ -446,7 +480,9 @@ prepare_daemon (struct MHD_Daemon *daemon_handle) int GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SCHEDULER_Handle *s, - struct GNUNET_STATISTICS_Handle *st) + struct GNUNET_STATISTICS_Handle *st, + GNUNET_CORE_ConnectEventHandler *server_ch, + GNUNET_CORE_DisconnectEventHandler *server_dh) { unsigned long long port; @@ -500,6 +536,10 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, (unsigned short) port); return GNUNET_SYSERR; } + + *server_ch = &connect_handler; + *server_dh = &disconnect_handler; + if (daemon_handle_v4 != NULL) hostlist_task_v4 = prepare_daemon (daemon_handle_v4); if (daemon_handle_v6 != NULL) diff --git a/src/hostlist/hostlist-server.h b/src/hostlist/hostlist-server.h index c6c6337e7..7251ce11c 100644 --- a/src/hostlist/hostlist-server.h +++ b/src/hostlist/hostlist-server.h @@ -40,7 +40,9 @@ int GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SCHEDULER_Handle *s, - struct GNUNET_STATISTICS_Handle *st); + struct GNUNET_STATISTICS_Handle *st, + GNUNET_CORE_ConnectEventHandler *server_ch, + GNUNET_CORE_DisconnectEventHandler *server_dh); /** diff --git a/src/hostlist/learning_peer1.conf b/src/hostlist/learning_peer1.conf index b9680e5a1..95b0d1850 100644 --- a/src/hostlist/learning_peer1.conf +++ b/src/hostlist/learning_peer1.conf @@ -8,13 +8,13 @@ PORT = 12964 [transport] PORT = 12965 PLUGINS = tcp -#DEBUG = NO +DEBUG = YES [arm] PORT = 12966 DEFAULTSERVICES = resolver transport core statistics topology #GLOBAL_PREFIX = xterm -e gdb -x cmd --args -DEBUG=NO +#DEBUG=NO [statistics] PORT = 12967 @@ -37,7 +37,7 @@ WEAKRANDOM = YES HTTPPORT = 12980 SERVERS = http://localhost:12981/ OPTIONS = -b -p -e -a -DEBUG = NO +DEBUG = YES #BINARY = /home/grothoff/bin/gnunet-daemon-hostlist [topology] diff --git a/src/hostlist/learning_peer2.conf b/src/hostlist/learning_peer2.conf index a026a7fc1..dc1216a86 100644 --- a/src/hostlist/learning_peer2.conf +++ b/src/hostlist/learning_peer2.conf @@ -8,13 +8,13 @@ PORT = 22964 [transport] PORT = 22965 PLUGINS = tcp -#DEBUG = YES +DEBUG = YES [arm] PORT = 22966 DEFAULTSERVICES = resolver transport core statistics topology #GLOBAL_PREFIX = xterm -e gdb -x cmd --args -DEBUG=NO +#DEBUG=NO [statistics] PORT = 22967 @@ -27,7 +27,7 @@ PORT = 22969 [core] PORT = 22970 -#DEBUG = YES +DEBUG = YES #PREFIX = valgrind --tool=memcheck [testing] @@ -37,7 +37,7 @@ WEAKRANDOM = YES HTTPPORT = 12981 SERVERS = http://localhost:12980/ OPTIONS = -b -p -e -a -#DEBUG = YES +DEBUG = YES #BINARY = /home/grothoff/bin/gnunet-daemon-hostlist [topology] diff --git a/src/hostlist/test_gnunet_daemon_hostlist_learning.c b/src/hostlist/test_gnunet_daemon_hostlist_learning.c index c468623e8..9ed891969 100644 --- a/src/hostlist/test_gnunet_daemon_hostlist_learning.c +++ b/src/hostlist/test_gnunet_daemon_hostlist_learning.c @@ -30,7 +30,8 @@ #define START_ARM GNUNET_YES -#define VERBOSE GNUNET_NO +#define VERBOSE GNUNET_YES +#define DEBUG GNUNET_YES /** * How long until we give up on transmitting the message? -- 2.25.1