X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Frps%2Fgnunet-service-rps_sampler.c;h=b51867d28b942e32b8edd392e08ec419451ef767;hb=36fd53ed6ae7c60d4b49cea614d18aa6d58843a1;hp=01d8aa74853341cc31d190afd3ee648b169f9e81;hpb=df3efc93aafc83fadd659ec4b8d49fa9302b0831;p=oweals%2Fgnunet.git diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c index 01d8aa748..b51867d28 100644 --- a/src/rps/gnunet-service-rps_sampler.c +++ b/src/rps/gnunet-service-rps_sampler.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) + Copyright (C) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,8 +14,8 @@ 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. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** @@ -32,7 +32,10 @@ #include #include -#define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__) +#include "rps-test_util.h" + +#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__) + // multiple 'clients'? @@ -102,8 +105,98 @@ struct RPS_SamplerElement * How many times this sampler changed the peer_id. */ uint32_t num_change; + + /** + * The file name this sampler element should log to + */ + #ifdef TO_FILE + char *file_name; + #endif /* TO_FILE */ }; +/** + * Callback that is called from _get_rand_peer() when the PeerID is ready. + * + * @param cls the closure given alongside this function. + * @param id the PeerID that was returned + */ +typedef void +(*RPS_sampler_rand_peer_ready_cont) (void *cls, + const struct GNUNET_PeerIdentity *id); + + +/** + * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer + */ +struct GetPeerCls +{ + /** + * DLL + */ + struct GetPeerCls *next; + + /** + * DLL + */ + struct GetPeerCls *prev; + + /** + * The sampler this function operates on. + */ + struct RPS_Sampler *sampler; + + /** + * The task for this function. + */ + struct GNUNET_SCHEDULER_Task *get_peer_task; + + /** + * The callback + */ + RPS_sampler_rand_peer_ready_cont cont; + + /** + * The closure to the callback @e cont + */ + void *cont_cls; + + /** + * The address of the id to be stored at + */ + struct GNUNET_PeerIdentity *id; +}; + + +/** + * Type of function used to differentiate between modified and not modified + * Sampler. + */ +typedef void +(*RPS_get_peers_type) (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc); + +/** + * Get one random peer out of the sampled peers. + * + * We might want to reinitialise this sampler after giving the + * corrsponding peer to the client. + * Only used internally + */ +static void +sampler_get_rand_peer (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc); + +/** + * Get one random peer out of the sampled peers. + * + * We might want to reinitialise this sampler after giving the + * corrsponding peer to the client. + */ +static void +sampler_mod_get_rand_peer (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc); + + /** * Sampler with its own array of SamplerElements */ @@ -116,36 +209,58 @@ struct RPS_Sampler //size_t size; /** - * All Samplers in one array. + * All sampler elements in one array. */ struct RPS_SamplerElement **sampler_elements; /** - * Max time a round takes + * Number of sampler elements trash can holds. + */ + unsigned int trash_can_size; + + /** + * Trash can for old sampler elements. + * We need this to evaluate the sampler. + * TODO remove after evaluation + * and undo changes in + * sampler_resize + * sampler_empty + * sampler_init + * sampler_remove? + * sampler_reinitialise_by_value + * sampler_update + */ + struct RPS_SamplerElement **trash_can; + + /** + * Maximum time a round takes * * Used in the context of RPS */ struct GNUNET_TIME_Relative max_round_interval; /** - * Callback to be called when a peer gets inserted into a sampler. + * Stores the function to return peers. Which one it is depends on whether + * the Sampler is the modified one or not. */ - RPS_sampler_insert_cb insert_cb; + RPS_get_peers_type get_peers; /** - * Closure to the insert_cb. + * Head for the DLL to store the closures to pending requests. */ - void *insert_cls; + struct GetPeerCls *gpc_head; /** - * Callback to be called when a peer gets inserted into a sampler. + * Tail for the DLL to store the closures to pending requests. */ - RPS_sampler_remove_cb remove_cb; + struct GetPeerCls *gpc_tail; + #ifdef TO_FILE /** - * Closure to the remove_cb. + * File name to log to */ - void *remove_cls; + char *file_name; + #endif /* TO_FILE */ }; /** @@ -179,52 +294,10 @@ struct NRandPeersReadyCls void *cls; }; -/** - * Callback that is called from _get_rand_peer() when the PeerID is ready. - * - * @param cls the closure given alongside this function. - * @param id the PeerID that was returned - */ -typedef void -(*RPS_sampler_rand_peer_ready_cb) (void *cls, - const struct GNUNET_PeerIdentity *id); - -/** - * Closure to #RPS_sampler_get_rand_peer() - */ -struct GetPeerCls -{ - /** - * The task for this function. - */ - struct GNUNET_SCHEDULER_Task *get_peer_task; - - /** - * The callback - */ - RPS_sampler_rand_peer_ready_cb cb; - - /** - * The closure to the callback - */ - void *cb_cls; - - /** - * The address of the id to be stored at - */ - struct GNUNET_PeerIdentity *id; -}; - -/** - * Multihashmap that keeps track of all get_peer_tasks that are still scheduled. - */ -struct GNUNET_CONTAINER_MultiHashMap *get_peer_tasks; - - -/** - * Global sampler variable. - */ -struct RPS_Sampler *sampler; +///** +// * Global sampler variable. +// */ +//struct RPS_Sampler *sampler; /** @@ -254,24 +327,23 @@ static uint32_t client_get_index; * Checks whether all n peers are available. If they are, * give those back. */ - void +static void check_n_peers_ready (void *cls, const struct GNUNET_PeerIdentity *id) { - struct NRandPeersReadyCls *n_peers_cls; - - n_peers_cls = (struct NRandPeersReadyCls *) cls; + struct NRandPeersReadyCls *n_peers_cls = cls; + n_peers_cls->cur_num_peers++; LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: Got %" PRIX32 ". of %" PRIX32 " peers\n", + "Got %" PRIX32 ". of %" PRIX32 " peers\n", n_peers_cls->cur_num_peers, n_peers_cls->num_peers); - if (n_peers_cls->num_peers - 1 == n_peers_cls->cur_num_peers) + if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers) { /* All peers are ready -- return those to the client */ GNUNET_assert (NULL != n_peers_cls->callback); LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: returning %" PRIX32 " peers to the client\n", + "returning %" PRIX32 " peers to the client\n", n_peers_cls->num_peers); n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers); @@ -295,6 +367,15 @@ RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el) &(sampler_el->auth_key.key), GNUNET_CRYPTO_HASH_LENGTH); + #ifdef TO_FILE + /* Create a file(-name) to store internals to */ + char *name_buf; + name_buf = auth_key_to_string (sampler_el->auth_key); + + sampler_el->file_name = create_file (name_buf); + GNUNET_free (name_buf); + #endif /* TO_FILE */ + sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS; sampler_el->birth = GNUNET_TIME_absolute_get (); @@ -319,29 +400,35 @@ RPS_sampler_elem_create (void) s = GNUNET_new (struct RPS_SamplerElement); RPS_sampler_elem_reinit (s); - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with empty PeerID\n"); return s; } /** - * Input an PeerID into the given sampler. + * Input an PeerID into the given sampler element. + * + * @param sampler the sampler the @a s_elem belongs to. + * Needed to know the */ - static void -RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, const struct GNUNET_PeerIdentity *other, - RPS_sampler_insert_cb insert_cb, void *insert_cls, - RPS_sampler_remove_cb remove_cb, void *remove_cls) +static void +RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, + struct RPS_Sampler *sampler, /* TODO remove? */ + const struct GNUNET_PeerIdentity *other) { struct GNUNET_HashCode other_hash; s_elem->num_peers++; - if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)) ) + to_file (s_elem->file_name, + "Got id %s", + GNUNET_i2s_full (other)); + + if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id))) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", GNUNET_i2s (other)); - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n", GNUNET_i2s (&(s_elem->peer_id))); } else @@ -351,62 +438,51 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, const struct GNUNET_Pe sizeof(struct GNUNET_PeerIdentity), &other_hash); - if ( EMPTY == s_elem->is_empty ) + if (EMPTY == s_elem->is_empty) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (was empty previously).\n", - GNUNET_i2s(other)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Got PeerID %s; Simply accepting (was empty previously).\n", + GNUNET_i2s(other)); s_elem->peer_id = *other; s_elem->peer_id_hash = other_hash; - if (NULL != sampler->insert_cb) - sampler->insert_cb (sampler->insert_cls, &(s_elem->peer_id)); - s_elem->num_change++; } - else if ( 0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash) ) + else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash)) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", GNUNET_i2s (other)); - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n", + LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n", GNUNET_i2s (&s_elem->peer_id)); - - if ( NULL != sampler->remove_cb ) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the remove callback.\n", - GNUNET_i2s (&s_elem->peer_id)); - sampler->remove_cb (sampler->remove_cls, &s_elem->peer_id); - } - s_elem->peer_id = *other; s_elem->peer_id_hash = other_hash; - if ( NULL != sampler->insert_cb ) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Inserting new PeerID %s with the insert callback.\n", - GNUNET_i2s (&s_elem->peer_id)); - sampler->insert_cb(sampler->insert_cls, &s_elem->peer_id); - } - s_elem->num_change++; } else { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n", - GNUNET_i2s(other)); - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n", - GNUNET_i2s(&s_elem->peer_id)); + LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", + GNUNET_i2s (other)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n", + GNUNET_i2s (&s_elem->peer_id)); } } s_elem->is_empty = NOT_EMPTY; + + to_file (s_elem->file_name, + "Now holding %s", + GNUNET_i2s_full (&s_elem->peer_id)); } + /** * Get the size of the sampler. * + * @param sampler the sampler to return the size of. * @return the size of the sampler */ unsigned int -RPS_sampler_get_size () +RPS_sampler_get_size (struct RPS_Sampler *sampler) { return sampler->sampler_size; } @@ -415,14 +491,14 @@ RPS_sampler_get_size () /** * Grow or shrink the size of the sampler. * + * @param sampler the sampler to resize. * @param new_size the new size of the sampler */ - void -RPS_sampler_resize (unsigned int new_size) +static void +sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) { unsigned int old_size; uint32_t i; - struct RPS_SamplerElement **rem_list; // TODO check min and max size @@ -430,53 +506,100 @@ RPS_sampler_resize (unsigned int new_size) if (old_size > new_size) { /* Shrinking */ - /* Temporary store those to properly call the removeCB on those later */ - rem_list = GNUNET_malloc ((old_size - new_size) * sizeof (struct RPS_SamplerElement *)); - memcpy (rem_list, - &sampler->sampler_elements[new_size], - (old_size - new_size) * sizeof (struct RPS_SamplerElement *)); - - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Shrinking sampler %d -> %d\n", old_size, new_size); - GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size); + LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: sampler->sampler_elements now points to %p\n", - sampler->sampler_elements); - - for (i = 0 ; i < old_size - new_size ; i++) - {/* Remove unneeded rest */ - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX32 ". sampler\n", i); - if (NULL != sampler->remove_cb) - sampler->remove_cb (sampler->remove_cls, &rem_list[i]->peer_id); - GNUNET_free (rem_list[i]); + "Shrinking sampler %d -> %d\n", + old_size, + new_size); + + to_file (sampler->file_name, + "Shrinking sampler %d -> %d", + old_size, + new_size); + + /* TODO Temporary store those to properly call the removeCB on those later? */ + GNUNET_array_grow (sampler->trash_can, + sampler->trash_can_size, + old_size - new_size); + for (i = new_size ; i < old_size ; i++) + { + to_file (sampler->file_name, + "-%" PRIu32 ": %s", + i, + sampler->sampler_elements[i]->file_name); + to_file (sampler->sampler_elements[i]->file_name, + "--- non-active"); + sampler->trash_can[i - new_size] = sampler->sampler_elements[i]; } - GNUNET_free (rem_list); + + GNUNET_array_grow (sampler->sampler_elements, + sampler->sampler_size, + new_size); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "sampler->sampler_elements now points to %p\n", + sampler->sampler_elements); + } else if (old_size < new_size) { /* Growing */ - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Growing sampler %d -> %d\n", old_size, new_size); - GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size); LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: sampler->sampler_elements now points to %p\n", - sampler->sampler_elements); + "Growing sampler %d -> %d\n", + old_size, + new_size); - for ( i = old_size ; i < new_size ; i++ ) + to_file (sampler->file_name, + "Growing sampler %d -> %d", + old_size, + new_size); + + GNUNET_array_grow (sampler->sampler_elements, + sampler->sampler_size, + new_size); + + for (i = old_size ; i < new_size ; i++) { /* Add new sampler elements */ sampler->sampler_elements[i] = RPS_sampler_elem_create (); - if (NULL != sampler->insert_cb) - sampler->insert_cb (sampler->insert_cls, &sampler->sampler_elements[i]->peer_id); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: Added %" PRIX32 ". sampler, now pointing to %p, contains %s\n", - i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id)); + + to_file (sampler->file_name, + "+%" PRIu32 ": %s", + i, + sampler->sampler_elements[i]->file_name); } } else { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Size remains the same -- nothing to do\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n"); return; } GNUNET_assert (sampler->sampler_size == new_size); - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Finished growing/shrinking.\n"); // remove +} + + +/** + * Grow or shrink the size of the sampler. + * + * @param sampler the sampler to resize. + * @param new_size the new size of the sampler + */ +void +RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) +{ + GNUNET_assert (0 < new_size); + sampler_resize (sampler, new_size); +} + + +/** + * Empty the sampler. + * + * @param sampler the sampler to empty. + * @param new_size the new size of the sampler + */ +static void +sampler_empty (struct RPS_Sampler *sampler) +{ + sampler_resize (sampler, 0); } @@ -484,59 +607,102 @@ RPS_sampler_resize (unsigned int new_size) * Initialise a tuple of sampler elements. * * @param init_size the size the sampler is initialised with - * @param ins_cb the callback that will be called on every PeerID that is - * newly inserted into a sampler element - * @param ins_cls the closure given to #ins_cb - * @param rem_cb the callback that will be called on every PeerID that is - * removed from a sampler element - * @param rem_cls the closure given to #rem_cb + * @param max_round_interval maximum time a round takes + * @return a handle to a sampler that consists of sampler elements. */ - void +struct RPS_Sampler * RPS_sampler_init (size_t init_size, - struct GNUNET_TIME_Relative max_round_interval, - RPS_sampler_insert_cb ins_cb, void *ins_cls, - RPS_sampler_remove_cb rem_cb, void *rem_cls) + struct GNUNET_TIME_Relative max_round_interval) { - //struct RPS_Sampler *sampler; - //uint32_t i; + struct RPS_Sampler *sampler; /* Initialise context around extended sampler */ min_size = 10; // TODO make input to _samplers_init() max_size = 1000; // TODO make input to _samplers_init() sampler = GNUNET_new (struct RPS_Sampler); + + #ifdef TO_FILE + sampler->file_name = create_file ("sampler-"); + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Initialised sampler %s\n", + sampler->file_name); + #endif /* TO_FILE */ + sampler->sampler_size = 0; sampler->sampler_elements = NULL; + sampler->trash_can_size = 0; + sampler->trash_can = NULL; sampler->max_round_interval = max_round_interval; - sampler->insert_cb = ins_cb; - sampler->insert_cls = ins_cls; - sampler->remove_cb = rem_cb; - sampler->remove_cls = rem_cls; - get_peer_tasks = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO); + sampler->get_peers = sampler_get_rand_peer; + sampler->gpc_head = NULL; + sampler->gpc_tail = NULL; //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); - RPS_sampler_resize (init_size); + RPS_sampler_resize (sampler, init_size); client_get_index = 0; //GNUNET_assert (init_size == sampler->sampler_size); + return sampler; +} + +/** + * Initialise a modified tuple of sampler elements. + * + * @param init_size the size the sampler is initialised with + * @param max_round_interval maximum time a round takes + * @return a handle to a sampler that consists of sampler elements. + */ +struct RPS_Sampler * +RPS_sampler_mod_init (size_t init_size, + struct GNUNET_TIME_Relative max_round_interval) +{ + struct RPS_Sampler *sampler; + + sampler = RPS_sampler_init (init_size, max_round_interval); + sampler->get_peers = sampler_mod_get_rand_peer; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Initialised modified sampler %s\n", + sampler->file_name); + to_file (sampler->file_name, + "This is a modified sampler"); + + return sampler; } /** * A fuction to update every sampler in the given list * + * @param sampler the sampler to update. * @param id the PeerID that is put in the sampler */ void -RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id) +RPS_sampler_update (struct RPS_Sampler *sampler, + const struct GNUNET_PeerIdentity *id) { uint32_t i; - for ( i = 0 ; i < sampler->sampler_size ; i++ ) - RPS_sampler_elem_next (sampler->sampler_elements[i], id, - sampler->insert_cb, sampler->insert_cls, - sampler->remove_cb, sampler->remove_cls); + to_file (sampler->file_name, + "Got %s", + GNUNET_i2s_full (id)); + + for (i = 0 ; i < sampler->sampler_size ; i++) + { + RPS_sampler_elem_next (sampler->sampler_elements[i], + sampler, + id); + } + + for (i = 0 ; i < sampler->trash_can_size ; i++) + { + RPS_sampler_elem_next (sampler->trash_can[i], + sampler, + id); + } } @@ -545,18 +711,28 @@ RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id) * * Used to get rid of a PeerID. * + * @param sampler the sampler to reinitialise a sampler element in. * @param id the id of the sampler elements to update. */ void -RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id) +RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler, + const struct GNUNET_PeerIdentity *id) { uint32_t i; + struct RPS_SamplerElement *trash_entry; for ( i = 0 ; i < sampler->sampler_size ; i++ ) { if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) ) { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Reinitialising sampler\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n"); + trash_entry = GNUNET_new (struct RPS_SamplerElement); + *trash_entry = *(sampler->sampler_elements[i]); + GNUNET_array_append (sampler->trash_can, + sampler->trash_can_size, + trash_entry); + to_file (trash_entry->file_name, + "--- non-active"); RPS_sampler_elem_reinit (sampler->sampler_elements[i]); } } @@ -570,41 +746,50 @@ RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id) * corrsponding peer to the client. * Only used internally */ - void -RPS_sampler_get_rand_peer_ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +static void +sampler_get_rand_peer (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct GetPeerCls *gpc; + struct GetPeerCls *gpc = cls; uint32_t r_index; - struct GNUNET_HashCode *hash; - gpc = (struct GetPeerCls *) cls; + gpc->get_peer_task = NULL; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; /**; * Choose the r_index of the peer we want to return * at random from the interval of the gossip list */ - r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, - sampler->sampler_size); + r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, + gpc->sampler->sampler_size); - if ( EMPTY == sampler->sampler_elements[r_index]->is_empty ) + if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty) { - gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply( - GNUNET_TIME_UNIT_SECONDS, - .1), - &RPS_sampler_get_rand_peer_, - cls); + //LOG (GNUNET_ERROR_TYPE_DEBUG, + // "Not returning randomly selected, empty PeerID. - Rescheduling.\n"); + + /* FIXME no active wait - get notified, when new id arrives? + * Might also be a freshly emptied one. Others might still contain ids. + * Counter? + */ + gpc->get_peer_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply ( + GNUNET_TIME_UNIT_SECONDS, 0.1), + &sampler_get_rand_peer, + cls); return; } - *gpc->id = sampler->sampler_elements[r_index]->peer_id; + *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id; + + gpc->cont (gpc->cont_cls, gpc->id); - hash = GNUNET_new (struct GNUNET_HashCode); - GNUNET_CRYPTO_hash (&gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (get_peer_tasks, hash, &gpc->get_peer_task)) - LOG (GNUNET_ERROR_TYPE_WARNING, "SAMPLER: Key to remove is not in the hashmap\n"); - GNUNET_free (gpc->get_peer_task); + GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head, + gpc->sampler->gpc_tail, + gpc); - gpc->cb (gpc->cb_cls, gpc->id); + GNUNET_free (gpc); } @@ -613,93 +798,105 @@ RPS_sampler_get_rand_peer_ (void *cls, const struct GNUNET_SCHEDULER_TaskContext * * We might want to reinitialise this sampler after giving the * corrsponding peer to the client. - * - * @return a random PeerID of the PeerIDs previously put into the sampler. */ - void -RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +static void +sampler_mod_get_rand_peer (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct GetPeerCls *gpc; + struct GetPeerCls *gpc = cls; struct GNUNET_PeerIdentity tmp_id; + unsigned int empty_flag; struct RPS_SamplerElement *s_elem; struct GNUNET_TIME_Relative last_request_diff; - struct GNUNET_HashCode *hash; uint32_t tmp_client_get_index; - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Single peer was requested\n"); + gpc->get_peer_task = NULL; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n"); - gpc = (struct GetPeerCls *) cls; - hash = GNUNET_new (struct GNUNET_HashCode); /* Store the next #client_get_index to check whether we cycled over the whole list */ if (0 < client_get_index) tmp_client_get_index = client_get_index - 1; else - tmp_client_get_index = sampler->sampler_size - 1; + tmp_client_get_index = gpc->sampler->sampler_size - 1; LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: scheduling for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n", - tmp_client_get_index, sampler->sampler_size); + "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n", + tmp_client_get_index, gpc->sampler->sampler_size); do { /* Get first non empty sampler */ if (tmp_client_get_index == client_get_index) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: reached tmp_index %" PRIX32 ".\n", client_get_index); - gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval, - &RPS_sampler_get_rand_peer, - cls); + { /* We once cycled over the whole list */ + LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n", + client_get_index); + GNUNET_assert (NULL == gpc->get_peer_task); + gpc->get_peer_task = + GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval, + &sampler_mod_get_rand_peer, + cls); return; } - tmp_id = sampler->sampler_elements[client_get_index]->peer_id; - RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]); - RPS_sampler_elem_next (sampler->sampler_elements[client_get_index], &tmp_id, - NULL, NULL, NULL, NULL); + tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id; + empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty; + RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]); + if (EMPTY != empty_flag) + RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index], + gpc->sampler, + &tmp_id); /* Cycle the #client_get_index one step further */ - if ( client_get_index == sampler->sampler_size - 1 ) + if ( client_get_index == gpc->sampler->sampler_size - 1 ) client_get_index = 0; else client_get_index++; - LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: incremented index to %" PRIX32 ".\n", client_get_index); - } while (EMPTY == sampler->sampler_elements[client_get_index]->is_empty); + /* LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n", + client_get_index); */ + } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty); - s_elem = sampler->sampler_elements[client_get_index]; + s_elem = gpc->sampler->sampler_elements[client_get_index]; *gpc->id = s_elem->peer_id; /* Check whether we may use this sampler to give it back to the client */ if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us) { - last_request_diff = GNUNET_TIME_absolute_get_difference (s_elem->last_client_request, - GNUNET_TIME_absolute_get ()); - /* We're not going to give it back now if it was already requested by a client this round */ - if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us) + last_request_diff = + GNUNET_TIME_absolute_get_difference (s_elem->last_client_request, + GNUNET_TIME_absolute_get ()); + /* We're not going to give it back now if it was + * already requested by a client this round */ + if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: Last client request on this sampler was less than max round interval ago -- scheduling for later\n"); + "Last client request on this sampler was less than max round interval ago -- scheduling for later\n"); ///* How many time remains untile the next round has started? */ - //inv_last_request_diff = GNUNET_TIME_absolute_get_difference (last_request_diff, - // sampler->max_round_interval); + //inv_last_request_diff = + // GNUNET_TIME_absolute_get_difference (last_request_diff, + // sampler->max_round_interval); // add a little delay /* Schedule it one round later */ - gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval, - &RPS_sampler_get_rand_peer, - cls); + GNUNET_assert (NULL == gpc->get_peer_task); + gpc->get_peer_task = + GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval, + &sampler_mod_get_rand_peer, + cls); return; } // TODO add other reasons to wait here } - GNUNET_CRYPTO_hash (&gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (get_peer_tasks, hash, &gpc->get_peer_task)) - LOG (GNUNET_ERROR_TYPE_WARNING, "SAMPLER: Key to remove is not in the hashmap\n"); - GNUNET_free (gpc->get_peer_task); - s_elem->last_client_request = GNUNET_TIME_absolute_get (); - gpc->cb (gpc->cb_cls, gpc->id); + GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head, + gpc->sampler->gpc_tail, + gpc); + gpc->cont (gpc->cont_cls, gpc->id); + GNUNET_free (gpc); } @@ -710,6 +907,7 @@ RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext * corrsponding peer to the client. * Random with or without consumption? * + * @param sampler the sampler to get peers from. * @param cb callback that will be called once the ids are ready. * @param cls closure given to @a cb * @param for_client #GNUNET_YES if result is used for client, @@ -717,20 +915,18 @@ RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext * @param num_peers the number of peers requested */ void -RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, - void *cls, uint32_t num_peers, int for_client) +RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler, + RPS_sampler_n_rand_peers_ready_cb cb, + void *cls, uint32_t num_peers) { - GNUNET_assert (GNUNET_YES == for_client || - GNUNET_NO == for_client); GNUNET_assert (0 != sampler->sampler_size); + if (0 == num_peers) + return; // TODO check if we have too much (distinct) sampled peers uint32_t i; struct NRandPeersReadyCls *cb_cls; struct GetPeerCls *gpc; - struct GNUNET_HashCode *hash; - - hash = GNUNET_new (struct GNUNET_HashCode); cb_cls = GNUNET_new (struct NRandPeersReadyCls); cb_cls->num_peers = num_peers; @@ -740,23 +936,22 @@ RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, cb_cls->cls = cls; LOG (GNUNET_ERROR_TYPE_DEBUG, - "SAMPLER: Scheduling requests for %" PRIX32 " peers\n", num_peers); + "Scheduling requests for %" PRIu32 " peers\n", num_peers); - for ( i = 0 ; i < num_peers ; i++ ) + for (i = 0 ; i < num_peers ; i++) { gpc = GNUNET_new (struct GetPeerCls); - gpc->cb = check_n_peers_ready; - gpc->cb_cls = cb_cls; + gpc->sampler = sampler; + gpc->cont = check_n_peers_ready; + gpc->cont_cls = cb_cls; gpc->id = &cb_cls->ids[i]; // maybe add a little delay - if (GNUNET_YES == for_client) - gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&RPS_sampler_get_rand_peer, gpc); - else if (GNUNET_NO == for_client) - gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&RPS_sampler_get_rand_peer_, gpc); - GNUNET_CRYPTO_hash (&gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); - (void) GNUNET_CONTAINER_multihashmap_put (get_peer_tasks, hash, &gpc->get_peer_task, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc); + + GNUNET_CONTAINER_DLL_insert (sampler->gpc_head, + sampler->gpc_tail, + gpc); } } @@ -764,12 +959,14 @@ RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, /** * Counts how many Samplers currently hold a given PeerID. * + * @param sampler the sampler to count ids in. * @param id the PeerID to count. * * @return the number of occurrences of id. */ uint32_t -RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id) +RPS_sampler_count_id (struct RPS_Sampler *sampler, + const struct GNUNET_PeerIdentity *id) { uint32_t count; uint32_t i; @@ -785,36 +982,25 @@ RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id) } -/** - * Callback to iterate over the hashmap to cancle the get_peer_tasks. - */ - int -clear_get_peer_tasks (void *cls, const struct GNUNET_HashCode *key, void *value) -{ - struct GNUNET_SCHEDULER_Task *task; - - task = (struct GNUNET_SCHEDULER_Task *) value; - GNUNET_SCHEDULER_cancel (task); - - GNUNET_CONTAINER_multihashmap_remove (get_peer_tasks, key, value); - - return GNUNET_YES; -} - - /** * Cleans the sampler. */ void -RPS_sampler_destroy () +RPS_sampler_destroy (struct RPS_Sampler *sampler) { - if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_iterate (get_peer_tasks, - clear_get_peer_tasks, - NULL)) - LOG (GNUNET_ERROR_TYPE_WARNING, "SAMPLER: iteration over hashmap was cancelled\n"); - GNUNET_CONTAINER_multihashmap_destroy (get_peer_tasks); - RPS_sampler_resize (0); - GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, 0); + struct GetPeerCls *i; + + for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head) + { + GNUNET_CONTAINER_DLL_remove (sampler->gpc_head, + sampler->gpc_tail, + i); + GNUNET_SCHEDULER_cancel (i->get_peer_task); + GNUNET_free (i); + } + + sampler_empty (sampler); + GNUNET_free (sampler); } /* end of gnunet-service-rps.c */