gnunet-service-rps_sampler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/gnunet-service-rps_sampler.h
  18. * @brief sampler implementation
  19. * @author Julius Bünger
  20. */
  21. #ifndef RPS_SAMPLER_H
  22. #define RPS_SAMPLER_H
  23. #include <inttypes.h>
  24. #include "rps-sampler_common.h"
  25. /**
  26. * A sampler sampling a stream of PeerIDs.
  27. */
  28. struct RPS_Sampler;
  29. /**
  30. * A handle to cancel a request.
  31. */
  32. struct RPS_SamplerRequestHandle;
  33. /**
  34. * Get the size of the sampler.
  35. *
  36. * @param sampler the sampler to return the size of.
  37. * @return the size of the sampler
  38. */
  39. unsigned int
  40. RPS_sampler_get_size (struct RPS_Sampler *sampler);
  41. /**
  42. * Grow or shrink the size of the sampler.
  43. *
  44. * @param sampler the sampler to resize.
  45. * @param new_size the new size of the sampler (not 0)
  46. */
  47. void
  48. RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
  49. /**
  50. * Initialise a tuple of samplers.
  51. *
  52. * @param init_size the size the sampler is initialised with
  53. * @param max_round_interval maximum time a round takes
  54. * @return a handle to a sampler that consists of sampler elements.
  55. */
  56. struct RPS_Sampler *
  57. RPS_sampler_init (size_t init_size,
  58. struct GNUNET_TIME_Relative max_round_interval);
  59. /**
  60. * Update every sampler element of this sampler with given peer
  61. *
  62. * @param sampler the sampler to update.
  63. * @param id the PeerID that is put in the sampler
  64. */
  65. void
  66. RPS_sampler_update (struct RPS_Sampler *sampler,
  67. const struct GNUNET_PeerIdentity *id);
  68. /**
  69. * Reinitialise all previously initialised sampler elements with the given
  70. * value.
  71. *
  72. * Used to get rid of a PeerID.
  73. *
  74. * FIXME: This should also consider currently pending requests
  75. * (Pending requests already collect peerids. As long as not all
  76. * requested IDs have been collected, they are kept.
  77. * Ideally, the @p id should be removed from all pending requests. This
  78. * seems quite complicated.)
  79. *
  80. * @param sampler the sampler to reinitialise a sampler in.
  81. * @param id the id of the samplers to update.
  82. */
  83. void
  84. RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
  85. const struct GNUNET_PeerIdentity *id);
  86. /**
  87. * Get n random peers out of the sampled peers.
  88. *
  89. * We might want to reinitialise this sampler after giving the
  90. * corrsponding peer to the client.
  91. * Random with or without consumption?
  92. *
  93. * @param sampler the sampler to get peers from.
  94. * @param cb callback that will be called once the ids are ready.
  95. * @param cls closure given to @a cb
  96. * @param for_client #GNUNET_YES if result is used for client,
  97. * #GNUNET_NO if used internally
  98. * @param num_peers the number of peers requested
  99. */
  100. struct RPS_SamplerRequestHandle *
  101. RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
  102. uint32_t num_peers,
  103. RPS_sampler_n_rand_peers_ready_cb cb,
  104. void *cls);
  105. /**
  106. * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
  107. *
  108. * @param req_handle the handle to the request
  109. */
  110. void
  111. RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
  112. /**
  113. * Counts how many Samplers currently hold a given PeerID.
  114. *
  115. * @param sampler the sampler to count ids in.
  116. * @param id the PeerID to count.
  117. * @return the number of occurrences of id.
  118. */
  119. uint32_t
  120. RPS_sampler_count_id (struct RPS_Sampler *sampler,
  121. const struct GNUNET_PeerIdentity *id);
  122. /**
  123. * Cleans the samplers.
  124. *
  125. * @param sampler the sampler to destroy.
  126. */
  127. void
  128. RPS_sampler_destroy (struct RPS_Sampler *sampler);
  129. #endif
  130. /* end of gnunet-service-rps.c */