GNUnet  0.10.x
Data Structures | Macros | Functions
rps-sampler_common.c File Reference

Code common to client and service sampler. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "rps-sampler_common.h"
#include "gnunet-service-rps_sampler_elem.h"
#include <math.h>
#include <inttypes.h>
#include "rps-test_util.h"
Include dependency graph for rps-sampler_common.c:

Go to the source code of this file.

Data Structures

struct  SamplerNotifyUpdateCTX
 Context for a callback. More...
 
struct  RPS_SamplerRequestHandle
 Closure to _get_n_rand_peers_ready_cb() More...
 
struct  RPS_SamplerRequestHandleSingleInfo
 Closure to _get_rand_peer_info() More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "rps-sampler_common", __VA_ARGS__)
 

Functions

void RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler, uint32_t num_peers)
 Update the current estimate of the network size stored at the sampler. More...
 
void RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler, double desired_probability)
 Set the probability that is needed at least with what a sampler element has to have observed all elements from the network. More...
 
void RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler, double deficiency_factor)
 Set the deficiency factor. More...
 
struct SamplerNotifyUpdateCTXsampler_notify_on_update (struct RPS_Sampler *sampler, SamplerNotifyUpdateCB notify_cb, void *cls)
 Add a callback that will be called when the next peer is inserted into the sampler. More...
 
unsigned int RPS_sampler_get_size (struct RPS_Sampler *sampler)
 Get the size of the sampler. More...
 
static void notify_update (struct RPS_Sampler *sampler)
 Notify about update of the sampler. More...
 
void RPS_sampler_update (struct RPS_Sampler *sampler, const struct GNUNET_PeerIdentity *id)
 Update every sampler element of this sampler with given peer. More...
 
void RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler, const struct GNUNET_PeerIdentity *id)
 Reinitialise all previously initialised sampler elements with the given value. More...
 
uint32_t RPS_sampler_count_id (struct RPS_Sampler *sampler, const struct GNUNET_PeerIdentity *id)
 Counts how many Samplers currently hold a given PeerID. More...
 
static void sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
 Grow or shrink the size of the sampler. More...
 
void RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
 Grow or shrink the size of the sampler. More...
 
static void sampler_empty (struct RPS_Sampler *sampler)
 Empty the sampler. More...
 
static void check_n_peers_ready (void *cls, const struct GNUNET_PeerIdentity *id, double probability, uint32_t num_observed)
 Callback to _get_rand_peer() used by _get_n_rand_peers(). More...
 
static void check_peer_info_ready (void *cls, const struct GNUNET_PeerIdentity *id, double probability, uint32_t num_observed)
 Callback to _get_rand_peer() used by _get_rand_peer_info(). More...
 
struct RPS_SamplerRequestHandleRPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler, uint32_t num_peers, RPS_sampler_n_rand_peers_ready_cb cb, void *cls)
 Get n random peers out of the sampled peers. More...
 
struct RPS_SamplerRequestHandleSingleInfoRPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler, RPS_sampler_sinlge_info_ready_cb cb, void *cls)
 Get one random peer with additional information. More...
 
void RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
 Cancle a request issued through RPS_sampler_n_rand_peers_ready_cb. More...
 
void RPS_sampler_request_single_info_cancel (struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle)
 Cancle a request issued through RPS_sampler_sinlge_info_ready_cb. More...
 
void RPS_sampler_destroy (struct RPS_Sampler *sampler)
 Cleans the sampler. More...
 

Detailed Description

Code common to client and service sampler.

Author
Julius B√ľnger

Definition in file rps-sampler_common.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from(kind, "rps-sampler_common", __VA_ARGS__)

Function Documentation

◆ RPS_sampler_update_with_nw_size()

void RPS_sampler_update_with_nw_size ( struct RPS_Sampler sampler,
uint32_t  num_peers 
)

Update the current estimate of the network size stored at the sampler.

Used for computing the condition when to return elements to the client

Only used/useful with the client sampler (Maybe move to rps-sampler_client.{h|c} ?)

Parameters
samplerThe sampler to update
num_peersThe estimated value

Definition at line 166 of file rps-sampler_common.c.

References num_peers, and RPS_Sampler::num_peers_estim.

Referenced by nse_cb().

168 {
169  sampler->num_peers_estim = num_peers;
170 }
uint32_t num_peers_estim
The estimated total number of peers in the network.
static unsigned int num_peers
Here is the caller graph for this function:

◆ RPS_sampler_set_desired_probability()

void RPS_sampler_set_desired_probability ( struct RPS_Sampler sampler,
double  desired_probability 
)

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} ?)

Parameters
sampler
desired_probability

Definition at line 184 of file rps-sampler_common.c.

References RPS_Sampler::desired_probability.

Referenced by GNUNET_RPS_request_peer_info(), and GNUNET_RPS_request_peers().

186 {
187  sampler->desired_probability = desired_probability;
188 }
double desired_probability
The desired probability with which we want to have observed all peers.
Here is the caller graph for this function:

◆ RPS_sampler_set_deficiency_factor()

void RPS_sampler_set_deficiency_factor ( struct RPS_Sampler sampler,
double  deficiency_factor 
)

Set the deficiency factor.

Only used/useful with the client sampler (Maybe move to rps-sampler_client.{h|c} ?)

Parameters
sampler
desired_probability

Definition at line 201 of file rps-sampler_common.c.

References RPS_Sampler::deficiency_factor.

Referenced by GNUNET_RPS_request_peer_info(), and GNUNET_RPS_request_peers().

203 {
204  sampler->deficiency_factor = deficiency_factor;
205 }
double deficiency_factor
A factor that catches the &#39;bias&#39; of a random stream of peer ids.
Here is the caller graph for this function:

◆ sampler_notify_on_update()

struct SamplerNotifyUpdateCTX* sampler_notify_on_update ( struct RPS_Sampler sampler,
SamplerNotifyUpdateCB  notify_cb,
void *  cls 
)

Add a callback that will be called when the next peer is inserted into the sampler.

Parameters
samplerThe sampler on which update it will be called
notify_cbThe callback
clsClosure given to the callback
Returns
The context containing callback and closure

Definition at line 219 of file rps-sampler_common.c.

References SamplerNotifyUpdateCTX::cls, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_new, LOG, SamplerNotifyUpdateCTX::notify_cb, RPS_Sampler::notify_ctx_head, and RPS_Sampler::notify_ctx_tail.

Referenced by sampler_get_rand_peer(), and sampler_mod_get_rand_peer().

222 {
223  struct SamplerNotifyUpdateCTX *notify_ctx;
224 
226  "Inserting new context for notification\n");
227  notify_ctx = GNUNET_new(struct SamplerNotifyUpdateCTX);
228  notify_ctx->notify_cb = notify_cb;
229  notify_ctx->cls = cls;
231  sampler->notify_ctx_tail,
232  notify_ctx);
233  return notify_ctx;
234 }
struct SamplerNotifyUpdateCTX * notify_ctx_tail
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
Context for a callback.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
SamplerNotifyUpdateCB notify_cb
The Callback to call on updates.
#define LOG(kind,...)
void * cls
The according closure.
struct SamplerNotifyUpdateCTX * notify_ctx_head
Here is the caller graph for this function:

◆ RPS_sampler_get_size()

unsigned int RPS_sampler_get_size ( struct RPS_Sampler sampler)

Get the size of the sampler.

Parameters
samplerthe sampler to return the size of.
Returns
the size of the sampler

Definition at line 244 of file rps-sampler_common.c.

References RPS_Sampler::sampler_size.

Referenced by adapt_sizes(), and resize_wrapper().

245 {
246  return sampler->sampler_size;
247 }
unsigned int sampler_size
Number of sampler elements we hold.
Here is the caller graph for this function:

◆ notify_update()

static void notify_update ( struct RPS_Sampler sampler)
static

Notify about update of the sampler.

Call the callbacks that are waiting for notification on updates to the sampler.

Parameters
samplerThe sampler the updates are waiting for

Definition at line 259 of file rps-sampler_common.c.

References GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, LOG, RPS_Sampler::notify_ctx_head, and RPS_Sampler::notify_ctx_tail.

Referenced by RPS_sampler_update().

260 {
261  struct SamplerNotifyUpdateCTX *tmp_notify_head;
262  struct SamplerNotifyUpdateCTX *tmp_notify_tail;
263 
265  "Calling callbacks waiting for update notification.\n");
266  tmp_notify_head = sampler->notify_ctx_head;
267  tmp_notify_tail = sampler->notify_ctx_tail;
268  sampler->notify_ctx_head = NULL;
269  sampler->notify_ctx_tail = NULL;
270  for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
271  NULL != tmp_notify_head;
272  notify_iter = tmp_notify_head)
273  {
274  GNUNET_assert(NULL != notify_iter->notify_cb);
275  GNUNET_CONTAINER_DLL_remove(tmp_notify_head,
276  tmp_notify_tail,
277  notify_iter);
278  notify_iter->notify_cb(notify_iter->cls);
279  GNUNET_free(notify_iter);
280  }
281 }
struct SamplerNotifyUpdateCTX * notify_ctx_tail
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Context for a callback.
#define LOG(kind,...)
struct SamplerNotifyUpdateCTX * notify_ctx_head
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ RPS_sampler_update()

void RPS_sampler_update ( struct RPS_Sampler sampler,
const struct GNUNET_PeerIdentity id 
)

Update every sampler element of this sampler with given peer.

Parameters
samplerthe sampler to update.
idthe PeerID that is put in the sampler

Definition at line 291 of file rps-sampler_common.c.

References notify_update(), RPS_sampler_elem_next(), RPS_Sampler::sampler_elements, and RPS_Sampler::sampler_size.

Referenced by collect_peers_cb(), collect_peers_info_cb(), and insert_in_sampler().

293 {
294  for (uint32_t i = 0; i < sampler->sampler_size; i++)
295  {
297  id);
298  }
299  notify_update(sampler);
300 }
static void notify_update(struct RPS_Sampler *sampler)
Notify about update of the sampler.
unsigned int sampler_size
Number of sampler elements we hold.
void RPS_sampler_elem_next(struct RPS_SamplerElement *sampler_elem, const struct GNUNET_PeerIdentity *new_ID)
Update a sampler element with a PeerID.
struct RPS_SamplerElement ** sampler_elements
All sampler elements in one array.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_reinitialise_by_value()

void RPS_sampler_reinitialise_by_value ( 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.

FIXME: This should also consider currently pending requests (Pending requests already collect peerids. As long as not all requested IDs have been collected, they are kept. Ideally, the id should be removed from all pending requests. This seems quite complicated.)

Parameters
samplerthe sampler to reinitialise a sampler element in.
idthe id of the sampler elements to update.

Definition at line 318 of file rps-sampler_common.c.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_memcmp, LOG, RPS_SamplerElement::peer_id, RPS_sampler_elem_reinit(), RPS_Sampler::sampler_elements, and RPS_Sampler::sampler_size.

Referenced by remove_peer().

320 {
321  uint32_t i;
322 
323  for (i = 0; i < sampler->sampler_size; i++)
324  {
325  if (0 == GNUNET_memcmp(id,
326  &(sampler->sampler_elements[i]->peer_id)))
327  {
328  LOG(GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
330  }
331  }
332 }
void RPS_sampler_elem_reinit(struct RPS_SamplerElement *sampler_elem)
Reinitialise a previously initialised sampler element.
struct GNUNET_PeerIdentity peer_id
The PeerID this sampler currently samples.
#define LOG(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
unsigned int sampler_size
Number of sampler elements we hold.
struct RPS_SamplerElement ** sampler_elements
All sampler elements in one array.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_count_id()

uint32_t RPS_sampler_count_id ( struct RPS_Sampler sampler,
const struct GNUNET_PeerIdentity id 
)

Counts how many Samplers currently hold a given PeerID.

Parameters
samplerthe sampler to count ids in.
idthe PeerID to count.
Returns
the number of occurrences of id.

Definition at line 344 of file rps-sampler_common.c.

References EMPTY, GNUNET_memcmp, RPS_SamplerElement::is_empty, RPS_SamplerElement::peer_id, RPS_Sampler::sampler_elements, and RPS_Sampler::sampler_size.

Referenced by check_sending_channel_needed(), clean_peer(), and insert_in_sampler().

346 {
347  uint32_t count;
348  uint32_t i;
349 
350  count = 0;
351  for (i = 0; i < sampler->sampler_size; i++)
352  {
353  if (0 == GNUNET_memcmp(&sampler->sampler_elements[i]->peer_id, id)
354  && EMPTY != sampler->sampler_elements[i]->is_empty)
355  count++;
356  }
357  return count;
358 }
enum RPS_SamplerEmpty is_empty
Flag that indicates that we are not holding a valid PeerID right now.
struct GNUNET_PeerIdentity peer_id
The PeerID this sampler currently samples.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
unsigned int sampler_size
Number of sampler elements we hold.
struct RPS_SamplerElement ** sampler_elements
All sampler elements in one array.
Here is the caller graph for this function:

◆ sampler_resize()

static void sampler_resize ( struct RPS_Sampler sampler,
unsigned int  new_size 
)
static

Grow or shrink the size of the sampler.

Parameters
samplerthe sampler to resize.
new_sizethe new size of the sampler

Definition at line 368 of file rps-sampler_common.c.

References GNUNET_array_grow, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, LOG, RPS_sampler_elem_create(), RPS_sampler_elem_destroy(), RPS_Sampler::sampler_elements, and RPS_Sampler::sampler_size.

Referenced by RPS_sampler_resize(), and sampler_empty().

369 {
370  unsigned int old_size;
371  uint32_t i;
372 
373  // TODO check min and max size
374 
375  old_size = sampler->sampler_size;
376 
377  if (old_size > new_size)
378  { /* Shrinking */
380  "Shrinking sampler %d -> %d\n",
381  old_size,
382  new_size);
383 
384  for (i = new_size; i < old_size; i++)
385  {
387  }
388 
390  sampler->sampler_size,
391  new_size);
393  "sampler->sampler_elements now points to %p\n",
394  sampler->sampler_elements);
395  }
396  else if (old_size < new_size)
397  { /* Growing */
399  "Growing sampler %d -> %d\n",
400  old_size,
401  new_size);
402 
404  sampler->sampler_size,
405  new_size);
406 
407  for (i = old_size; i < new_size; i++)
408  { /* Add new sampler elements */
410  }
411  }
412  else
413  {
414  LOG(GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
415  return;
416  }
417 
418  GNUNET_assert(sampler->sampler_size == new_size);
419 }
void RPS_sampler_elem_destroy(struct RPS_SamplerElement *sampler_elem)
Destroy a sampler element.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct RPS_SamplerElement * RPS_sampler_elem_create(void)
Create a sampler element and initialise it.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define LOG(kind,...)
unsigned int sampler_size
Number of sampler elements we hold.
struct RPS_SamplerElement ** sampler_elements
All sampler elements in one array.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_resize()

void RPS_sampler_resize ( struct RPS_Sampler sampler,
unsigned int  new_size 
)

Grow or shrink the size of the sampler.

Parameters
samplerthe sampler to resize.
new_sizethe new size of the sampler

Definition at line 429 of file rps-sampler_common.c.

References GNUNET_assert, and sampler_resize().

Referenced by resize_wrapper(), RPS_sampler_init(), and RPS_sampler_mod_init().

430 {
431  GNUNET_assert(0 < new_size);
432  sampler_resize(sampler, new_size);
433 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void sampler_resize(struct RPS_Sampler *sampler, unsigned int new_size)
Grow or shrink the size of the sampler.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sampler_empty()

static void sampler_empty ( struct RPS_Sampler sampler)
static

Empty the sampler.

Parameters
samplerthe sampler to empty.
new_sizethe new size of the sampler

Definition at line 443 of file rps-sampler_common.c.

References sampler_resize().

Referenced by RPS_sampler_destroy().

444 {
445  sampler_resize(sampler, 0);
446 }
static void sampler_resize(struct RPS_Sampler *sampler, unsigned int new_size)
Grow or shrink the size of the sampler.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_n_peers_ready()

static void check_n_peers_ready ( void *  cls,
const struct GNUNET_PeerIdentity id,
double  probability,
uint32_t  num_observed 
)
static

Callback to _get_rand_peer() used by _get_n_rand_peers().

Implements RPS_sampler_rand_peer_ready_cont

Checks whether all n peers are available. If they are, give those back.

Parameters
clsClosure
idPeer ID
probabilityThe probability with which this sampler has seen all ids
num_observedHow many ids this sampler has observed

Definition at line 462 of file rps-sampler_common.c.

References RPS_SamplerRequestHandle::callback, SamplerNotifyUpdateCTX::cls, RPS_SamplerRequestHandle::cls, RPS_SamplerRequestHandle::cur_num_peers, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_memcpy, GNUNET_new_array, RPS_SamplerRequestHandle::ids, LOG, num_peers, RPS_SamplerRequestHandle::num_peers, peers, req_handle, and RPS_sampler_request_cancel().

Referenced by RPS_sampler_get_n_rand_peers().

466 {
468 
469  (void)id;
471  struct GNUNET_PeerIdentity *peers;
472  uint32_t num_peers;
473  void *cb_cls;
474  (void)probability;
475  (void)num_observed;
476 
477  req_handle->cur_num_peers++;
479  "Got %" PRIX32 ". of %" PRIX32 " peers\n",
480  req_handle->cur_num_peers, req_handle->num_peers);
481 
482  if (req_handle->num_peers == req_handle->cur_num_peers)
483  { /* All peers are ready -- return those to the client */
484  GNUNET_assert(NULL != req_handle->callback);
485 
487  "returning %" PRIX32 " peers to the client\n",
488  req_handle->num_peers);
489 
490  /* Copy pointers and peers temporarily as they
491  * might be deleted from within the callback */
492  tmp_cb = req_handle->callback;
493  num_peers = req_handle->num_peers;
494  peers = GNUNET_new_array(num_peers, struct GNUNET_PeerIdentity);
495  GNUNET_memcpy(peers,
496  req_handle->ids,
497  num_peers * sizeof(struct GNUNET_PeerIdentity));
498  cb_cls = req_handle->cls;
499  RPS_sampler_request_cancel(req_handle);
500  req_handle = NULL;
501  tmp_cb(peers, num_peers, cb_cls);
502  GNUNET_free(peers);
503  }
504 }
uint32_t num_peers
Number of peers we are waiting for.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void * cls
Closure given to the callback.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
RPS_sampler_n_rand_peers_ready_cb callback
Callback to be called when all ids are available.
void RPS_sampler_request_cancel(struct RPS_SamplerRequestHandle *req_handle)
Cancle a request issued through RPS_sampler_n_rand_peers_ready_cb.
Closure to _get_n_rand_peers_ready_cb()
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
#define LOG(kind,...)
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
struct GNUNET_PeerIdentity * ids
Pointer to the array holding the ids.
static unsigned int num_peers
uint32_t cur_num_peers
Number of peers we currently have.
The identity of the host (wraps the signing key of the peer).
void(* RPS_sampler_n_rand_peers_ready_cb)(const struct GNUNET_PeerIdentity *ids, uint32_t num_peers, void *cls)
Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
static struct CadetPeer * peers
Operation to get peer ids.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_peer_info_ready()

static void check_peer_info_ready ( void *  cls,
const struct GNUNET_PeerIdentity id,
double  probability,
uint32_t  num_observed 
)
static

Callback to _get_rand_peer() used by _get_rand_peer_info().

Implements RPS_sampler_rand_peer_ready_cont

Parameters
clsClosure
idPeer ID
probabilityThe probability with which this sampler has seen all ids
num_observedHow many ids this sampler has observed

Definition at line 518 of file rps-sampler_common.c.

References RPS_SamplerRequestHandleSingleInfo::callback, SamplerNotifyUpdateCTX::cls, RPS_SamplerRequestHandleSingleInfo::cls, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_memcpy, GNUNET_new, RPS_SamplerRequestHandleSingleInfo::id, LOG, peer, req_handle, and RPS_sampler_request_single_info_cancel().

Referenced by RPS_sampler_get_rand_peer_info().

522 {
524 
525  (void)id;
527  struct GNUNET_PeerIdentity *peer;
528  void *cb_cls;
529  (void)probability;
530  (void)num_observed;
531 
533  "Got single peer with additional info\n");
534 
535  GNUNET_assert(NULL != req_handle->callback);
536 
538  "returning single peer with info to the client\n");
539 
540  /* Copy pointers and peers temporarily as they
541  * might be deleted from within the callback */
542  tmp_cb = req_handle->callback;
543  peer = GNUNET_new(struct GNUNET_PeerIdentity);
544  GNUNET_memcpy(peer,
545  req_handle->id,
546  sizeof(struct GNUNET_PeerIdentity));
547  cb_cls = req_handle->cls;
549  req_handle = NULL;
550  tmp_cb(peer, cb_cls, probability, num_observed);
551  GNUNET_free(peer);
552 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void RPS_sampler_request_single_info_cancel(struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle)
Cancle a request issued through RPS_sampler_sinlge_info_ready_cb.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
#define LOG(kind,...)
Closure to _get_rand_peer_info()
void * cls
Closure given to the callback.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_PeerIdentity * id
Pointer to the id.
The identity of the host (wraps the signing key of the peer).
void(* RPS_sampler_sinlge_info_ready_cb)(const struct GNUNET_PeerIdentity *ids, void *cls, double probability, uint32_t num_observed)
Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
RPS_sampler_sinlge_info_ready_cb callback
Callback to be called when all ids are available.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_get_n_rand_peers()

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 
)

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?

Parameters
samplerthe sampler to get peers from.
cbcallback that will be called once the ids are ready.
clsclosure given to cb
num_peersthe number of peers requested

Definition at line 568 of file rps-sampler_common.c.

References RPS_SamplerRequestHandle::callback, check_n_peers_ready(), SamplerNotifyUpdateCTX::cls, RPS_SamplerRequestHandle::cls, GetPeerCls::cont, GetPeerCls::cont_cls, RPS_SamplerRequestHandle::cur_num_peers, GetPeerCls::get_peer_task, RPS_Sampler::get_peers, GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_new, GNUNET_new_array, GNUNET_SCHEDULER_add_now(), RPS_SamplerRequestHandle::gpc_head, RPS_SamplerRequestHandle::gpc_tail, GetPeerCls::id, RPS_SamplerRequestHandle::ids, LOG, num_peers, RPS_SamplerRequestHandle::num_peers, req_handle, GetPeerCls::req_handle, RPS_Sampler::req_handle_head, RPS_Sampler::req_handle_tail, GetPeerCls::req_single_info_handle, RPS_SamplerRequestHandle::sampler, and RPS_Sampler::sampler_size.

Referenced by do_round(), and GNUNET_RPS_request_peers().

572 {
573  uint32_t i;
575  struct GetPeerCls *gpc;
576 
577  GNUNET_assert(0 != sampler->sampler_size);
578  if (0 == num_peers)
579  return NULL;
580 
581  // TODO check if we have too much (distinct) sampled peers
582  req_handle = GNUNET_new(struct RPS_SamplerRequestHandle);
583  req_handle->num_peers = num_peers;
584  req_handle->cur_num_peers = 0;
585  req_handle->ids = GNUNET_new_array(num_peers, struct GNUNET_PeerIdentity);
586  req_handle->sampler = sampler;
587  req_handle->callback = cb;
588  req_handle->cls = cls;
590  sampler->req_handle_tail,
591  req_handle);
592 
594  "Scheduling requests for %" PRIu32 " peers\n", num_peers);
595 
596  for (i = 0; i < num_peers; i++)
597  {
598  gpc = GNUNET_new(struct GetPeerCls);
599  gpc->req_handle = req_handle;
600  gpc->req_single_info_handle = NULL;
601  gpc->cont = check_n_peers_ready;
602  gpc->cont_cls = req_handle;
603  gpc->id = &req_handle->ids[i];
604 
606  req_handle->gpc_tail,
607  gpc);
608  // maybe add a little delay
610  gpc);
611  }
612  return req_handle;
613 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
RPS_sampler_rand_peer_ready_cont cont
The callback.
uint32_t num_peers
Number of peers we are waiting for.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void * cls
Closure given to the callback.
RPS_sampler_n_rand_peers_ready_cb callback
Callback to be called when all ids are available.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void check_n_peers_ready(void *cls, const struct GNUNET_PeerIdentity *id, double probability, uint32_t num_observed)
Callback to _get_rand_peer() used by _get_n_rand_peers().
struct RPS_SamplerRequestHandle * req_handle
The RPS_SamplerRequestHandle this single request belongs to.
Closure to _get_n_rand_peers_ready_cb()
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
#define LOG(kind,...)
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
struct GetPeerCls * gpc_head
Head and tail for the DLL to store the tasks for single requests.
struct GNUNET_PeerIdentity * ids
Pointer to the array holding the ids.
static unsigned int num_peers
uint32_t cur_num_peers
Number of peers we currently have.
struct RPS_SamplerRequestHandleSingleInfo * req_single_info_handle
The RPS_SamplerRequestHandleSingleInfo this single request belongs to.
void * cont_cls
The closure to the callback cont.
The identity of the host (wraps the signing key of the peer).
Closure for sampler_mod_get_rand_peer() and sampler_get_rand_peer.
struct RPS_SamplerRequestHandle * req_handle_head
Head and tail for the DLL to store the RPS_SamplerRequestHandle.
unsigned int sampler_size
Number of sampler elements we hold.
RPS_get_peers_type get_peers
Stores the function to return peers.
struct RPS_SamplerRequestHandle * req_handle_tail
struct GNUNET_PeerIdentity * id
The address of the id to be stored at.
struct GNUNET_SCHEDULER_Task * get_peer_task
The task for this function.
struct RPS_Sampler * sampler
Sampler.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_get_rand_peer_info()

struct RPS_SamplerRequestHandleSingleInfo* RPS_sampler_get_rand_peer_info ( struct RPS_Sampler sampler,
RPS_sampler_sinlge_info_ready_cb  cb,
void *  cls 
)

Get one random peer with additional information.

Parameters
samplerthe sampler to get peers from.
cbcallback that will be called once the ids are ready.
clsclosure given to cb

Definition at line 624 of file rps-sampler_common.c.

References RPS_SamplerRequestHandleSingleInfo::callback, check_peer_info_ready(), SamplerNotifyUpdateCTX::cls, RPS_SamplerRequestHandleSingleInfo::cls, GetPeerCls::cont, GetPeerCls::cont_cls, GetPeerCls::get_peer_task, RPS_Sampler::get_peers, GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_malloc, GNUNET_new, GNUNET_SCHEDULER_add_now(), RPS_SamplerRequestHandleSingleInfo::gpc_head, RPS_SamplerRequestHandleSingleInfo::gpc_tail, GetPeerCls::id, RPS_SamplerRequestHandleSingleInfo::id, req_handle, GetPeerCls::req_handle, RPS_Sampler::req_handle_single_head, RPS_Sampler::req_handle_single_tail, GetPeerCls::req_single_info_handle, RPS_SamplerRequestHandleSingleInfo::sampler, and RPS_Sampler::sampler_size.

Referenced by GNUNET_RPS_request_peer_info().

627 {
629  struct GetPeerCls *gpc;
630 
631  GNUNET_assert(0 != sampler->sampler_size);
632 
633  // TODO check if we have too much (distinct) sampled peers
634  req_handle = GNUNET_new(struct RPS_SamplerRequestHandleSingleInfo);
635  req_handle->id = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
636  req_handle->sampler = sampler;
637  req_handle->callback = cb;
638  req_handle->cls = cls;
640  sampler->req_handle_single_tail,
641  req_handle);
642 
643  gpc = GNUNET_new(struct GetPeerCls);
644  gpc->req_handle = NULL;
647  gpc->cont_cls = req_handle;
648  gpc->id = req_handle->id;
649 
651  req_handle->gpc_tail,
652  gpc);
653  // maybe add a little delay
655  gpc);
656  return req_handle;
657 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
RPS_sampler_rand_peer_ready_cont cont
The callback.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct RPS_SamplerRequestHandleSingleInfo * req_handle_single_head
Head and tail for the DLL to store the RPS_SamplerRequestHandleSingleInfo.
static void check_peer_info_ready(void *cls, const struct GNUNET_PeerIdentity *id, double probability, uint32_t num_observed)
Callback to _get_rand_peer() used by _get_rand_peer_info().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct RPS_SamplerRequestHandle * req_handle
The RPS_SamplerRequestHandle this single request belongs to.
struct RPS_Sampler * sampler
Sampler.
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
Closure to _get_rand_peer_info()
void * cls
Closure given to the callback.
struct RPS_SamplerRequestHandleSingleInfo * req_handle_single_tail
struct GNUNET_PeerIdentity * id
Pointer to the id.
struct RPS_SamplerRequestHandleSingleInfo * req_single_info_handle
The RPS_SamplerRequestHandleSingleInfo this single request belongs to.
void * cont_cls
The closure to the callback cont.
The identity of the host (wraps the signing key of the peer).
Closure for sampler_mod_get_rand_peer() and sampler_get_rand_peer.
unsigned int sampler_size
Number of sampler elements we hold.
RPS_get_peers_type get_peers
Stores the function to return peers.
struct GNUNET_PeerIdentity * id
The address of the id to be stored at.
struct GNUNET_SCHEDULER_Task * get_peer_task
The task for this function.
#define GNUNET_malloc(size)
Wrapper around malloc.
RPS_sampler_sinlge_info_ready_cb callback
Callback to be called when all ids are available.
struct GetPeerCls * gpc_head
Head and tail for the DLL to store the tasks for single requests.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_request_cancel()

void RPS_sampler_request_cancel ( struct RPS_SamplerRequestHandle req_handle)

Cancle a request issued through RPS_sampler_n_rand_peers_ready_cb.

Parameters
req_handlethe handle to the request

Definition at line 666 of file rps-sampler_common.c.

References GetPeerCls::get_peer_task, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_SCHEDULER_cancel(), RPS_SamplerRequestHandle::gpc_head, RPS_SamplerRequestHandle::gpc_tail, RPS_SamplerRequestHandle::ids, GetPeerCls::notify_ctx, RPS_Sampler::notify_ctx_head, RPS_Sampler::notify_ctx_tail, RPS_Sampler::req_handle_head, RPS_Sampler::req_handle_tail, and RPS_SamplerRequestHandle::sampler.

Referenced by check_n_peers_ready(), GNUNET_RPS_request_cancel(), and RPS_sampler_destroy().

667 {
668  struct GetPeerCls *i;
669 
670  while (NULL != (i = req_handle->gpc_head))
671  {
673  req_handle->gpc_tail,
674  i);
675  if (NULL != i->get_peer_task)
676  {
678  }
679  if (NULL != i->notify_ctx)
680  {
682  req_handle->sampler->notify_ctx_tail,
683  i->notify_ctx);
685  i->notify_ctx = NULL;
686  }
687  GNUNET_free(i);
688  }
689  GNUNET_free(req_handle->ids);
690  req_handle->ids = NULL;
692  req_handle->sampler->req_handle_tail,
693  req_handle);
694  GNUNET_free(req_handle);
695 }
struct SamplerNotifyUpdateCTX * notify_ctx_tail
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct SamplerNotifyUpdateCTX * notify_ctx
Context to the given callback.
struct GetPeerCls * gpc_head
Head and tail for the DLL to store the tasks for single requests.
struct GNUNET_PeerIdentity * ids
Pointer to the array holding the ids.
Closure for sampler_mod_get_rand_peer() and sampler_get_rand_peer.
struct RPS_SamplerRequestHandle * req_handle_head
Head and tail for the DLL to store the RPS_SamplerRequestHandle.
struct SamplerNotifyUpdateCTX * notify_ctx_head
struct RPS_SamplerRequestHandle * req_handle_tail
struct GNUNET_SCHEDULER_Task * get_peer_task
The task for this function.
#define GNUNET_free(ptr)
Wrapper around free.
struct RPS_Sampler * sampler
Sampler.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_request_single_info_cancel()

void RPS_sampler_request_single_info_cancel ( struct RPS_SamplerRequestHandleSingleInfo req_single_info_handle)

Cancle a request issued through RPS_sampler_sinlge_info_ready_cb.

Cancle a request issued through RPS_sampler_n_rand_peers_ready_cb.

Parameters
req_handlethe handle to the request

Definition at line 704 of file rps-sampler_common.c.

References GetPeerCls::get_peer_task, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_SCHEDULER_cancel(), RPS_SamplerRequestHandleSingleInfo::gpc_head, RPS_SamplerRequestHandleSingleInfo::gpc_tail, RPS_SamplerRequestHandleSingleInfo::id, GetPeerCls::notify_ctx, RPS_Sampler::notify_ctx_head, RPS_Sampler::notify_ctx_tail, RPS_Sampler::req_handle_single_head, RPS_Sampler::req_handle_single_tail, and RPS_SamplerRequestHandleSingleInfo::sampler.

Referenced by check_peer_info_ready(), and GNUNET_RPS_request_single_info_cancel().

706 {
707  struct GetPeerCls *i;
708 
709  while (NULL != (i = req_single_info_handle->gpc_head))
710  {
711  GNUNET_CONTAINER_DLL_remove(req_single_info_handle->gpc_head,
712  req_single_info_handle->gpc_tail,
713  i);
714  if (NULL != i->get_peer_task)
715  {
717  }
718  if (NULL != i->notify_ctx)
719  {
720  GNUNET_CONTAINER_DLL_remove(req_single_info_handle->sampler->notify_ctx_head,
721  req_single_info_handle->sampler->notify_ctx_tail,
722  i->notify_ctx);
724  i->notify_ctx = NULL;
725  }
726  GNUNET_free(i);
727  }
728  GNUNET_free(req_single_info_handle->id);
729  req_single_info_handle->id = NULL;
731  req_single_info_handle->sampler->req_handle_single_tail,
732  req_single_info_handle);
733  GNUNET_free(req_single_info_handle);
734 }
struct SamplerNotifyUpdateCTX * notify_ctx_tail
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct RPS_SamplerRequestHandleSingleInfo * req_handle_single_head
Head and tail for the DLL to store the RPS_SamplerRequestHandleSingleInfo.
struct SamplerNotifyUpdateCTX * notify_ctx
Context to the given callback.
struct RPS_Sampler * sampler
Sampler.
struct RPS_SamplerRequestHandleSingleInfo * req_handle_single_tail
struct GNUNET_PeerIdentity * id
Pointer to the id.
Closure for sampler_mod_get_rand_peer() and sampler_get_rand_peer.
struct SamplerNotifyUpdateCTX * notify_ctx_head
struct GNUNET_SCHEDULER_Task * get_peer_task
The task for this function.
struct GetPeerCls * gpc_head
Head and tail for the DLL to store the tasks for single requests.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPS_sampler_destroy()

void RPS_sampler_destroy ( struct RPS_Sampler sampler)

Cleans the sampler.

Cleans the samplers.

Definition at line 741 of file rps-sampler_common.c.

References GNUNET_ERROR_TYPE_WARNING, GNUNET_free, LOG, RPS_Sampler::req_handle_head, RPS_sampler_request_cancel(), and sampler_empty().

Referenced by destroy_sub(), GNUNET_RPS_request_cancel(), and GNUNET_RPS_request_single_info_cancel().

742 {
743  if (NULL != sampler->req_handle_head)
744  {
746  "There are still pending requests. Going to remove them.\n");
747  while (NULL != sampler->req_handle_head)
748  {
750  }
751  }
752  sampler_empty(sampler);
753  GNUNET_free(sampler);
754 }
void RPS_sampler_request_cancel(struct RPS_SamplerRequestHandle *req_handle)
Cancle a request issued through RPS_sampler_n_rand_peers_ready_cb.
#define LOG(kind,...)
static void sampler_empty(struct RPS_Sampler *sampler)
Empty the sampler.
struct RPS_SamplerRequestHandle * req_handle_head
Head and tail for the DLL to store the RPS_SamplerRequestHandle.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: