123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- /*
- This file is part of GNUnet.
- Copyright (C)
- GNUnet is free software: you can redistribute it and/or modify it
- under the terms of the GNU Affero General Public License as published
- by the Free Software Foundation, either version 3 of the License,
- 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
- Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- SPDX-License-Identifier: AGPL3.0-or-later
- */
- /**
- * @file rps/rps-sampler_common.h
- * @brief Code common to client and service sampler
- * @author Julius Bünger
- */
- #ifndef RPS_SAMPLER_COMMON_H
- #define RPS_SAMPLER_COMMON_H
- #include "platform.h"
- #include "gnunet_util_lib.h"
- #include "gnunet_statistics_service.h"
- #include "gnunet-service-rps_sampler_elem.h"
- #include <math.h>
- #include <inttypes.h>
- #include "rps-test_util.h"
- /**
- * 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);
- /**
- * Type of function used to differentiate between modified and not modified
- * Sampler.
- */
- typedef void
- (*RPS_get_peers_type) (void *cls);
- /**
- * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
- *
- * @param cls the closure given alongside this function.
- * @param ids the PeerIDs that were returned
- * to be freed
- */
- typedef void
- (*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
- uint32_t num_peers,
- void *cls);
- /**
- * @brief Callback called each time a new peer was put into the sampler
- *
- * @param cls A possibly given closure
- */
- typedef void
- (*SamplerNotifyUpdateCB) (void *cls);
- /**
- * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
- */
- struct GetPeerCls
- {
- /**
- * DLL
- */
- struct GetPeerCls *next;
- struct GetPeerCls *prev;
- /**
- * The #RPS_SamplerRequestHandle this single request belongs to.
- */
- struct RPS_SamplerRequestHandle *req_handle;
- /**
- * The task for this function.
- */
- struct GNUNET_SCHEDULER_Task *get_peer_task;
- /**
- * @brief Context to the given callback.
- */
- struct SamplerNotifyUpdateCTX *notify_ctx;
- /**
- * 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;
- };
- /**
- * Sampler with its own array of SamplerElements
- */
- struct RPS_Sampler
- {
- /**
- * Number of sampler elements we hold.
- */
- unsigned int sampler_size;
- //size_t size;
- /**
- * All sampler elements in one array.
- */
- struct RPS_SamplerElement **sampler_elements;
- /**
- * Maximum time a round takes
- *
- * Used in the context of RPS
- */
- struct GNUNET_TIME_Relative max_round_interval;
- /**
- * @brief The estimated total number of peers in the network
- */
- uint32_t num_peers_estim;
- /**
- * @brief The desired probability with which we want to have observed all
- * peers.
- */
- double desired_probability;
- /**
- * @brief A factor that catches the 'bias' of a random stream of peer ids.
- *
- * As introduced by Brahms: Factor between the number of unique ids in a
- * truly random stream and number of unique ids in the gossip stream.
- */
- double deficiency_factor;
- /**
- * Stores the function to return peers. Which one it is depends on whether
- * the Sampler is the modified one or not.
- */
- RPS_get_peers_type get_peers;
- /**
- * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
- */
- struct RPS_SamplerRequestHandle *req_handle_head;
- struct RPS_SamplerRequestHandle *req_handle_tail;
- struct SamplerNotifyUpdateCTX *notify_ctx_head;
- struct SamplerNotifyUpdateCTX *notify_ctx_tail;
- };
- /**
- * @brief Update the current estimate of the network size stored at the sampler
- *
- * Used for computing the condition when to return elements to the client
- *
- * @param sampler The sampler to update
- * @param num_peers The estimated value
- */
- void
- RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
- uint32_t num_peers);
- /**
- * @brief Set the probability that is needed at least with what a sampler
- * element has to have observed all elements from the network.
- *
- * Only used/useful with the client sampler
- * (Maybe move to rps-sampler_client.{h|c} ?)
- *
- * @param sampler
- * @param desired_probability
- */
- void
- RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
- double desired_probability);
- /**
- * @brief Set the deficiency factor.
- *
- * Only used/useful with the client sampler
- * (Maybe move to rps-sampler_client.{h|c} ?)
- *
- * @param sampler
- * @param desired_probability
- */
- void
- RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
- double deficiency_factor);
- /**
- * @brief Add a callback that will be called when the next peer is inserted
- * into the sampler
- *
- * @param sampler The sampler on which update it will be called
- * @param notify_cb The callback
- * @param cls Closure given to the callback
- *
- * @return The context containing callback and closure
- */
- struct SamplerNotifyUpdateCTX *
- sampler_notify_on_update (struct RPS_Sampler *sampler,
- SamplerNotifyUpdateCB notify_cb,
- void *cls);
- /**
- * Update every sampler element of this sampler with given peer
- *
- * @param sampler the sampler to update.
- * @param id the PeerID that is put in the sampler
- */
- void
- RPS_sampler_update (struct RPS_Sampler *sampler,
- const struct GNUNET_PeerIdentity *id);
- /**
- * Reinitialise all previously initialised sampler elements with the given value.
- *
- * 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 (struct RPS_Sampler *sampler,
- const struct GNUNET_PeerIdentity *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 (struct RPS_Sampler *sampler);
- /**
- * 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);
- /**
- * Get n random peers out of the sampled peers.
- *
- * We might want to reinitialise this sampler after giving the
- * 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,
- * #GNUNET_NO if used internally
- * @param num_peers the number of peers requested
- */
- struct RPS_SamplerRequestHandle *
- RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
- uint32_t num_peers,
- RPS_sampler_n_rand_peers_ready_cb cb,
- void *cls);
- /**
- * 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 (struct RPS_Sampler *sampler,
- const struct GNUNET_PeerIdentity *id);
- /**
- * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
- *
- * @param req_handle the handle to the request
- */
- void
- RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
- /**
- * Cleans the sampler.
- */
- void
- RPS_sampler_destroy (struct RPS_Sampler *sampler);
- #endif /* RPS_SAMPLER_COMMON_H */
- /* end of rps-sampler_common.h */
|