From 967a3bbae2c2a7c6fe5c975f1ba6cdbbd17d183f Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Thu, 8 Apr 2010 13:06:54 +0000 Subject: [PATCH] hostlist client gets notified when hostlist advertisments arrive --- src/hostlist/gnunet-daemon-hostlist.c | 60 +++++++++++++++++++-------- src/hostlist/hostlist-client.c | 33 +++++++++++++-- src/hostlist/hostlist-client.h | 4 +- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/hostlist/gnunet-daemon-hostlist.c b/src/hostlist/gnunet-daemon-hostlist.c index 9f3453890..2197a50b1 100644 --- a/src/hostlist/gnunet-daemon-hostlist.c +++ b/src/hostlist/gnunet-daemon-hostlist.c @@ -74,6 +74,12 @@ static struct GNUNET_STATISTICS_Handle *stats; */ struct GNUNET_CORE_Handle *core; +/** + * Handle to the hostlist client's advertisement handler + */ +GNUNET_CORE_MessageCallback client_adv_handler = NULL; + + /** * gnunet-daemon-hostlist command line options. */ @@ -112,20 +118,41 @@ core_init (void *cls, /** * Core handler for p2p hostlist advertisements */ -static int handle_hostlist_advertisement (void *cls, +static int advertisement_handler (void *cls, const struct GNUNET_PeerIdentity * peer, const struct GNUNET_MessageHeader * message, struct GNUNET_TIME_Relative latency, uint32_t distance) { -#if DEBUG_HOSTLIST_LEARNING - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Recieved hostlist advertisement\n")); -#endif + if (advertising && (NULL != client_adv_handler)) + { + (*client_adv_handler) (cls, peer, message, latency, distance); + return GNUNET_YES; + } + return GNUNET_NO; +} - return GNUNET_OK; +/** + * 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) +{ + /* call hostlist client connection handler*/ + + /* do my own stuff */ } + /** * Last task run during shutdown. Disconnects us from * the other services. @@ -161,7 +188,7 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * service. */ static struct GNUNET_CORE_MessageHandler handlers[] = { - { &handle_hostlist_advertisement, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0}, + { &advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0}, { NULL, 0, 0 } }; @@ -184,14 +211,6 @@ run (void *cls, GNUNET_CORE_ConnectEventHandler ch = NULL; GNUNET_CORE_DisconnectEventHandler dh = NULL; - - - struct GNUNET_CORE_MessageHandler null_handler[] = { - { NULL, 0, 0 } - }; - - struct GNUNET_CORE_MessageHandler *used_handler = null_handler; - if ( (! bootstrapping) && (! learning) && (! provide_hostlist) ) @@ -204,7 +223,7 @@ run (void *cls, if (bootstrapping) { GNUNET_HOSTLIST_client_start (cfg, sched, stats, - &ch, &dh); + &ch, &dh, &client_adv_handler); } if (provide_hostlist) { @@ -212,9 +231,14 @@ run (void *cls, } if (learning) { - used_handler = handlers; + } + + struct GNUNET_TIME_Relative a; + advertisement_handler(NULL,NULL,NULL,a,6); + + core = GNUNET_CORE_connect (sched, cfg, GNUNET_TIME_UNIT_FOREVER_REL, NULL, @@ -222,7 +246,7 @@ run (void *cls, NULL, ch, dh, NULL, GNUNET_NO, NULL, GNUNET_NO, - used_handler); + handlers); GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_FOREVER_REL, diff --git a/src/hostlist/hostlist-client.c b/src/hostlist/hostlist-client.c index 1c0deb511..3824972d8 100644 --- a/src/hostlist/hostlist-client.c +++ b/src/hostlist/hostlist-client.c @@ -32,7 +32,7 @@ #include "gnunet_transport_service.h" #include -#define DEBUG_HOSTLIST_CLIENT GNUNET_NO +#define DEBUG_HOSTLIST_CLIENT GNUNET_YES /** * Number of connections that we must have to NOT download @@ -695,7 +695,7 @@ connect_handler (void *cls, /** - * Method called whenever a given peer connects. + * Method called whenever a given peer disconnects. * * @param cls closure * @param peer peer identity this notification is about @@ -712,6 +712,31 @@ disconnect_handler (void *cls, GNUNET_NO); } +/** + * Method called whenever an advertisement message arrives. + * + * @param cls closure (always NULL) + * @param client identification of the client + * @param message the actual message + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) + */ +static int +advertisement_handler (void *cls, + const struct GNUNET_PeerIdentity * peer, + const struct GNUNET_MessageHeader * message, + struct GNUNET_TIME_Relative latency, + uint32_t distance) +{ +#if DEBUG_HOSTLIST_CLIENT + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Hostlist client recieved advertisement message\n"); +#endif + + /* put code to use message here */ + + return GNUNET_YES; +} /** * Continuation called by the statistics code once @@ -757,7 +782,8 @@ GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SCHEDULER_Handle *s, struct GNUNET_STATISTICS_Handle *st, GNUNET_CORE_ConnectEventHandler *ch, - GNUNET_CORE_DisconnectEventHandler *dh) + GNUNET_CORE_DisconnectEventHandler *dh, + GNUNET_CORE_MessageCallback *msgh) { if (0 != curl_global_init (CURL_GLOBAL_WIN32)) { @@ -781,6 +807,7 @@ GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c, proxy = NULL; *ch = &connect_handler; *dh = &disconnect_handler; + *msgh = &advertisement_handler; GNUNET_STATISTICS_get (stats, "hostlist", gettext_noop("Minimum time between hostlist downloads"), diff --git a/src/hostlist/hostlist-client.h b/src/hostlist/hostlist-client.h index 888d2099a..378a91996 100644 --- a/src/hostlist/hostlist-client.h +++ b/src/hostlist/hostlist-client.h @@ -40,6 +40,7 @@ * @param st hande for publishing statistics * @param ch set to handler for connect notifications * @param dh set to handler for disconnect notifications + * @param msgh set to handler for message handler notifications * @return GNUNET_OK on success */ int @@ -47,7 +48,8 @@ GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SCHEDULER_Handle *s, struct GNUNET_STATISTICS_Handle *st, GNUNET_CORE_ConnectEventHandler *ch, - GNUNET_CORE_DisconnectEventHandler *dh); + GNUNET_CORE_DisconnectEventHandler *dh, + GNUNET_CORE_MessageCallback *msgh); /** -- 2.25.1