From: Christian Grothoff Date: Thu, 6 Oct 2011 19:27:35 +0000 (+0000) Subject: drafting typemap API X-Git-Tag: initial-import-from-subversion-38251~16728 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=0babf19b445e521fdb78f0e1a67d5548e28405b9;p=oweals%2Fgnunet.git drafting typemap API --- diff --git a/src/core/gnunet-service-core_sessions.c b/src/core/gnunet-service-core_sessions.c index 0db053ff5..b72a0e0b3 100644 --- a/src/core/gnunet-service-core_sessions.c +++ b/src/core/gnunet-service-core_sessions.c @@ -19,7 +19,7 @@ */ /** - * @file core/gnunet-service-core_neighbours.c + * @file core/gnunet-service-core_sessions.c * @brief code for managing of 'encrypted' sessions (key exchange done) * @author Christian Grothoff */ @@ -33,7 +33,7 @@ * Record kept for each request for transmission issued by a * client that is still pending. */ -struct ClientActiveRequest; +struct GSC_ClientActiveRequest; /** * Data kept per session. @@ -49,13 +49,13 @@ struct Session * Head of list of requests from clients for transmission to * this peer. */ - struct ClientActiveRequest *active_client_request_head; + struct GSC_ClientActiveRequest *active_client_request_head; /** * Tail of list of requests from clients for transmission to * this peer. */ - struct ClientActiveRequest *active_client_request_tail; + struct GSC_ClientActiveRequest *active_client_request_tail; /** * Performance data for the peer. @@ -294,8 +294,8 @@ handle_peer_status_change (struct Neighbour *n) static void schedule_peer_messages (struct Neighbour *n) { - struct ClientActiveRequest *car; - struct ClientActiveRequest *pos; + struct GSC_ClientActiveRequest *car; + struct GSC_ClientActiveRequest *pos; struct Client *c; struct MessageEntry *mqe; unsigned int queue_size; @@ -354,7 +354,7 @@ static void free_neighbour (struct Neighbour *n) { struct MessageEntry *m; - struct ClientActiveRequest *car; + struct GSC_ClientActiveRequest *car; #if DEBUG_CORE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -1360,6 +1360,51 @@ GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car, +/** + * Send a message to the neighbour. + * + * @param cls the message + * @param key neighbour's identity + * @param value 'struct Neighbour' of the target + * @return always GNUNET_OK + */ +static int +do_send_message (void *cls, const GNUNET_HashCode * key, void *value) +{ + struct GNUNET_MessageHeader *hdr = cls; + struct Neighbour *n = value; + struct MessageEntry *m; + uint16_t size; + + size = ntohs (hdr->size); + m = GNUNET_malloc (sizeof (struct MessageEntry) + size); + memcpy (&m[1], hdr, size); + m->deadline = GNUNET_TIME_UNIT_FOREVER_ABS; + m->slack_deadline = GNUNET_TIME_UNIT_FOREVER_ABS; + m->priority = UINT_MAX; + m->sender_status = n->status; + m->size = size; + GNUNET_CONTAINER_DLL_insert (n->message_head, + n->message_tail, + m); + return GNUNET_OK; +} + + +/** + * Broadcast a message to all neighbours. + * + * @param msg message to transmit + */ +void +GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg) +{ + if (NULL == sessions) + return; + GNUNET_CONTAINER_multihashmap_iterate (sessions, + &do_send_message, msg); +} + /** * Helper function for GSC_SESSIONS_handle_client_iterate_peers. @@ -1413,9 +1458,60 @@ queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value) } +/** + * End the session with the given peer (we are no longer + * connected). + * + * @param pid identity of peer to kill session with + */ +void +GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid) +{ +} + + +/** + * Traffic is being solicited for the given peer. This means that the + * message queue on the transport-level (NEIGHBOURS subsystem) is now + * empty and it is now OK to transmit another (non-control) message. + * + * @param pid identity of peer ready to receive data + */ +void +GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid) +{ +} + + +/** + * Transmit a message to a particular peer. + * + * @param car original request that was queued and then solicited, + * ownership does not change (dequeue will be called soon). + * @param msg message to transmit + */ +void +GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car, + const struct GNUNET_MessageHeader *msg) +{ +} + /** - * Handle CORE_ITERATE_PEERS request. + * We have a new client, notify it about all current sessions. + * + * @param client the new client + */ +void +GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client) +{ +} + + +/** + * Handle CORE_ITERATE_PEERS request. For this request type, the client + * does not have to have transmitted an INIT request. All current peers + * are returned, regardless of which message types they accept. * * @param cls unused * @param client client sending the iteration request @@ -1439,7 +1535,10 @@ GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client /** - * Handle CORE_PEER_CONNECTED request. Notify client about existing neighbours. + * Handle CORE_PEER_CONNECTED request. Notify client about connection + * to the given neighbour. For this request type, the client does not + * have to have transmitted an INIT request. All current peers are + * returned, regardless of which message types they accept. * * @param cls unused * @param client client sending the iteration request @@ -1466,7 +1565,8 @@ GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *cl /** - * Handle REQUEST_INFO request. + * Handle REQUEST_INFO request. For this request type, the client must + * have transmitted an INIT first. * * @param cls unused * @param client client sending the request @@ -1576,8 +1676,11 @@ GSC_SESSIONS_handle_client_request_info (void *cls, struct GNUNET_SERVER_Client +/** + * Initialize sessions subsystem. + */ int -GSC_NEIGHBOURS_init () +GSC_SESSIONS_init () { neighbours = GNUNET_CONTAINER_multihashmap_create (128); self.public_key = &my_public_key; @@ -1589,8 +1692,11 @@ GSC_NEIGHBOURS_init () } +/** + * Shutdown sessions subsystem. + */ void -GSC_NEIGHBOURS_done () +GSC_SESSIONS_done () { GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper, NULL); diff --git a/src/core/gnunet-service-core_sessions.h b/src/core/gnunet-service-core_sessions.h index 1898aa36a..fc5944cda 100644 --- a/src/core/gnunet-service-core_sessions.h +++ b/src/core/gnunet-service-core_sessions.h @@ -86,6 +86,15 @@ GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car, const struct GNUNET_MessageHeader *msg); +/** + * Broadcast a message to all neighbours. + * + * @param msg message to transmit + */ +void +GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg); + + /** * We have a new client, notify it about all current sessions. * @@ -95,6 +104,48 @@ void GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client); +/** + * Handle CORE_ITERATE_PEERS request. For this request type, the client + * does not have to have transmitted an INIT request. All current peers + * are returned, regardless of which message types they accept. + * + * @param cls unused + * @param client client sending the iteration request + * @param message iteration request message + */ +void +GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message); + + +/** + * Handle CORE_PEER_CONNECTED request. Notify client about connection + * to the given neighbour. For this request type, the client does not + * have to have transmitted an INIT request. All current peers are + * returned, regardless of which message types they accept. + * + * @param cls unused + * @param client client sending the iteration request + * @param message iteration request message + */ +void +GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message); + + +/** + * Handle REQUEST_INFO request. For this request type, the client must have + * transmitted an INIT first. + * + * @param cls unused + * @param client client sending the request + * @param message iteration request message + */ +void +GSC_SESSIONS_handle_client_request_info (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message); + + /** * Initialize sessions subsystem. */ diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c index dfd0b8888..5feb65a53 100644 --- a/src/core/gnunet-service-core_typemap.c +++ b/src/core/gnunet-service-core_typemap.c @@ -1,3 +1,33 @@ +/* + This file is part of GNUnet. + (C) 2011 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file core/gnunet-service-core_typemap.c + * @brief management of map that specifies which message types this peer supports + * @author Christian Grothoff + */ +#include "platform.h" +#include "gnunet_util_lib.h" +#include "gnunet_transport_service.h" +#include "gnunet_service_core.h" + /** * A type map describing which messages a given neighbour is able @@ -15,6 +45,58 @@ struct GSC_TypeMap static uint32_t my_type_map[(UINT16_MAX + 1) / 32]; +/** + * Compute a type map message for this peer. + * + * @return this peers current type map message. + */ +static struct GNUNET_MessageHeader * +compute_type_map_message () +{ + char *tmp; + uLongf dlen; + struct GNUNET_MessageHeader *hdr; + +#ifdef compressBound + dlen = compressBound (sizeof (my_type_map)); +#else + dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20; + /* documentation says 100.1% oldSize + 12 bytes, but we + * should be able to overshoot by more to be safe */ +#endif + hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader)); + hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader)); + tmp = (char *) &hdr[1]; + if ((Z_OK != + compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map, + sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map))) + { + dlen = sizeof (my_type_map); + memcpy (tmp, my_type_map, sizeof (my_type_map)); + hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP); + } + else + { + hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); + } + return hdr; +} + + +/** + * Send my type map to all connected peers (it got changed). + */ +static void +broadcast_my_type_map () +{ + struct GNUNET_MessageHeader *hdr; + + hdr = compute_type_map_message (); + GSC_SESSIONS_broadcast (hdr);x + GNUNET_free (hdr); +} + + /** * Add a set of types to our type map. */ @@ -63,97 +145,20 @@ int GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap, const uint16_t *types, unsigned int tcnt) -{ +{ return GNUNET_YES; /* FIXME */ } -/** - * Compute a type map message for this peer. - * - * @return this peers current type map message. - */ -static struct GNUNET_MessageHeader * -compute_type_map_message () -{ - char *tmp; - uLongf dlen; - struct GNUNET_MessageHeader *hdr; - -#ifdef compressBound - dlen = compressBound (sizeof (my_type_map)); -#else - dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20; - /* documentation says 100.1% oldSize + 12 bytes, but we - * should be able to overshoot by more to be safe */ -#endif - hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader)); - hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader)); - tmp = (char *) &hdr[1]; - if ((Z_OK != - compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map, - sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map))) - { - dlen = sizeof (my_type_map); - memcpy (tmp, my_type_map, sizeof (my_type_map)); - hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP); - } - else - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); - } - return hdr; -} - - -/** - * Send a type map message to the neighbour. - * - * @param cls the type map message - * @param key neighbour's identity - * @param value 'struct Neighbour' of the target - * @return always GNUNET_OK - */ -static int -send_type_map_to_neighbour (void *cls, const GNUNET_HashCode * key, void *value) +void +GSC_TYPEMAP_init () { - struct GNUNET_MessageHeader *hdr = cls; - struct Neighbour *n = value; - struct MessageEntry *m; - uint16_t size; - - if (n == &self) - return GNUNET_OK; - size = ntohs (hdr->size); - m = GNUNET_malloc (sizeof (struct MessageEntry) + size); - memcpy (&m[1], hdr, size); - m->deadline = GNUNET_TIME_UNIT_FOREVER_ABS; - m->slack_deadline = GNUNET_TIME_UNIT_FOREVER_ABS; - m->priority = UINT_MAX; - m->sender_status = n->status; - m->size = size; - m->next = n->messages; - n->messages = m; - return GNUNET_OK; } - -/** - * Send my type map to all connected peers (it got changed). - */ -static void -broadcast_my_type_map () +void +GSC_TYPEMAP_done () { - struct GNUNET_MessageHeader *hdr; - - if (NULL == neighbours) - return; - hdr = compute_type_map_message (); - GNUNET_CONTAINER_multihashmap_iterate (neighbours, - &send_type_map_to_neighbour, hdr); - GNUNET_free (hdr); } - - +/* end of gnunet-service-core_typemap.c */ diff --git a/src/core/gnunet-service-core_typemap.h b/src/core/gnunet-service-core_typemap.h new file mode 100644 index 000000000..4bbd6eb29 --- /dev/null +++ b/src/core/gnunet-service-core_typemap.h @@ -0,0 +1,66 @@ +* + This file is part of GNUnet. + (C) 2011 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file core/gnunet-service-core_typemap.h + * @brief management of map that specifies which message types this peer supports + * @author Christian Grothoff + */ +#ifndef GNUNET_SERVICE_CORE_TYPEMAP_H +#define GNUNET_SERVICE_CORE_TYPEMAP_H + +#include "gnunet_util_lib.h" +#include "gnunet_transport_service.h" +#include "gnunet_service_core.h" + + +/** + * Add a set of types to our type map. + */ +void +GSC_TYPEMAP_add (const uint16_t *types, + unsigned int tlen); + + +/** + * Remove a set of types from our type map. + */ +void +GSC_TYPEMAP_remove (const uint16_t *types, + unsigned int tlen); + + +/** + * Test if any of the types from the types array is in the + * given type map. + * + * @param map map to test + * @param types array of types + * @param tcnt number of entries in types + * @return GNUNET_YES if a type is in the map, GNUNET_NO if not + */ +int +GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap, + const uint16_t *types, + unsigned int tcnt); + + +#endif +/* end of gnunet-service-core_typemap.h */