rps-sampler_common.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/rps-sampler_common.h
  18. * @brief Code common to client and service sampler
  19. * @author Julius Bünger
  20. */
  21. #ifndef RPS_SAMPLER_COMMON_H
  22. #define RPS_SAMPLER_COMMON_H
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet-service-rps_sampler_elem.h"
  27. #include <math.h>
  28. #include <inttypes.h>
  29. #include "rps-test_util.h"
  30. /**
  31. * Callback that is called from _get_rand_peer() when the PeerID is ready.
  32. *
  33. * @param cls the closure given alongside this function.
  34. * @param id the PeerID that was returned
  35. */
  36. typedef void
  37. (*RPS_sampler_rand_peer_ready_cont) (void *cls,
  38. const struct GNUNET_PeerIdentity *id);
  39. /**
  40. * Type of function used to differentiate between modified and not modified
  41. * Sampler.
  42. */
  43. typedef void
  44. (*RPS_get_peers_type) (void *cls);
  45. /**
  46. * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
  47. *
  48. * @param cls the closure given alongside this function.
  49. * @param ids the PeerIDs that were returned
  50. * to be freed
  51. */
  52. typedef void
  53. (*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
  54. uint32_t num_peers,
  55. void *cls);
  56. /**
  57. * @brief Callback called each time a new peer was put into the sampler
  58. *
  59. * @param cls A possibly given closure
  60. */
  61. typedef void
  62. (*SamplerNotifyUpdateCB) (void *cls);
  63. /**
  64. * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
  65. */
  66. struct GetPeerCls
  67. {
  68. /**
  69. * DLL
  70. */
  71. struct GetPeerCls *next;
  72. struct GetPeerCls *prev;
  73. /**
  74. * The #RPS_SamplerRequestHandle this single request belongs to.
  75. */
  76. struct RPS_SamplerRequestHandle *req_handle;
  77. /**
  78. * The task for this function.
  79. */
  80. struct GNUNET_SCHEDULER_Task *get_peer_task;
  81. /**
  82. * @brief Context to the given callback.
  83. */
  84. struct SamplerNotifyUpdateCTX *notify_ctx;
  85. /**
  86. * The callback
  87. */
  88. RPS_sampler_rand_peer_ready_cont cont;
  89. /**
  90. * The closure to the callback @e cont
  91. */
  92. void *cont_cls;
  93. /**
  94. * The address of the id to be stored at
  95. */
  96. struct GNUNET_PeerIdentity *id;
  97. };
  98. /**
  99. * Sampler with its own array of SamplerElements
  100. */
  101. struct RPS_Sampler
  102. {
  103. /**
  104. * Number of sampler elements we hold.
  105. */
  106. unsigned int sampler_size;
  107. //size_t size;
  108. /**
  109. * All sampler elements in one array.
  110. */
  111. struct RPS_SamplerElement **sampler_elements;
  112. /**
  113. * Maximum time a round takes
  114. *
  115. * Used in the context of RPS
  116. */
  117. struct GNUNET_TIME_Relative max_round_interval;
  118. /**
  119. * @brief The estimated total number of peers in the network
  120. */
  121. uint32_t num_peers_estim;
  122. /**
  123. * @brief The desired probability with which we want to have observed all
  124. * peers.
  125. */
  126. double desired_probability;
  127. /**
  128. * @brief A factor that catches the 'bias' of a random stream of peer ids.
  129. *
  130. * As introduced by Brahms: Factor between the number of unique ids in a
  131. * truly random stream and number of unique ids in the gossip stream.
  132. */
  133. double deficiency_factor;
  134. /**
  135. * Stores the function to return peers. Which one it is depends on whether
  136. * the Sampler is the modified one or not.
  137. */
  138. RPS_get_peers_type get_peers;
  139. /**
  140. * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
  141. */
  142. struct RPS_SamplerRequestHandle *req_handle_head;
  143. struct RPS_SamplerRequestHandle *req_handle_tail;
  144. struct SamplerNotifyUpdateCTX *notify_ctx_head;
  145. struct SamplerNotifyUpdateCTX *notify_ctx_tail;
  146. };
  147. /**
  148. * @brief Update the current estimate of the network size stored at the sampler
  149. *
  150. * Used for computing the condition when to return elements to the client
  151. *
  152. * @param sampler The sampler to update
  153. * @param num_peers The estimated value
  154. */
  155. void
  156. RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
  157. uint32_t num_peers);
  158. /**
  159. * @brief Set the probability that is needed at least with what a sampler
  160. * element has to have observed all elements from the network.
  161. *
  162. * Only used/useful with the client sampler
  163. * (Maybe move to rps-sampler_client.{h|c} ?)
  164. *
  165. * @param sampler
  166. * @param desired_probability
  167. */
  168. void
  169. RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
  170. double desired_probability);
  171. /**
  172. * @brief Set the deficiency factor.
  173. *
  174. * Only used/useful with the client sampler
  175. * (Maybe move to rps-sampler_client.{h|c} ?)
  176. *
  177. * @param sampler
  178. * @param desired_probability
  179. */
  180. void
  181. RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
  182. double deficiency_factor);
  183. /**
  184. * @brief Add a callback that will be called when the next peer is inserted
  185. * into the sampler
  186. *
  187. * @param sampler The sampler on which update it will be called
  188. * @param notify_cb The callback
  189. * @param cls Closure given to the callback
  190. *
  191. * @return The context containing callback and closure
  192. */
  193. struct SamplerNotifyUpdateCTX *
  194. sampler_notify_on_update (struct RPS_Sampler *sampler,
  195. SamplerNotifyUpdateCB notify_cb,
  196. void *cls);
  197. /**
  198. * Update every sampler element of this sampler with given peer
  199. *
  200. * @param sampler the sampler to update.
  201. * @param id the PeerID that is put in the sampler
  202. */
  203. void
  204. RPS_sampler_update (struct RPS_Sampler *sampler,
  205. const struct GNUNET_PeerIdentity *id);
  206. /**
  207. * Reinitialise all previously initialised sampler elements with the given value.
  208. *
  209. * Used to get rid of a PeerID.
  210. *
  211. * @param sampler the sampler to reinitialise a sampler element in.
  212. * @param id the id of the sampler elements to update.
  213. */
  214. void
  215. RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
  216. const struct GNUNET_PeerIdentity *id);
  217. /**
  218. * Get the size of the sampler.
  219. *
  220. * @param sampler the sampler to return the size of.
  221. * @return the size of the sampler
  222. */
  223. unsigned int
  224. RPS_sampler_get_size (struct RPS_Sampler *sampler);
  225. /**
  226. * Grow or shrink the size of the sampler.
  227. *
  228. * @param sampler the sampler to resize.
  229. * @param new_size the new size of the sampler
  230. */
  231. void
  232. RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
  233. /**
  234. * Get n random peers out of the sampled peers.
  235. *
  236. * We might want to reinitialise this sampler after giving the
  237. * corrsponding peer to the client.
  238. * Random with or without consumption?
  239. *
  240. * @param sampler the sampler to get peers from.
  241. * @param cb callback that will be called once the ids are ready.
  242. * @param cls closure given to @a cb
  243. * @param for_client #GNUNET_YES if result is used for client,
  244. * #GNUNET_NO if used internally
  245. * @param num_peers the number of peers requested
  246. */
  247. struct RPS_SamplerRequestHandle *
  248. RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
  249. uint32_t num_peers,
  250. RPS_sampler_n_rand_peers_ready_cb cb,
  251. void *cls);
  252. /**
  253. * Counts how many Samplers currently hold a given PeerID.
  254. *
  255. * @param sampler the sampler to count ids in.
  256. * @param id the PeerID to count.
  257. *
  258. * @return the number of occurrences of id.
  259. */
  260. uint32_t
  261. RPS_sampler_count_id (struct RPS_Sampler *sampler,
  262. const struct GNUNET_PeerIdentity *id);
  263. /**
  264. * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
  265. *
  266. * @param req_handle the handle to the request
  267. */
  268. void
  269. RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
  270. /**
  271. * Cleans the sampler.
  272. */
  273. void
  274. RPS_sampler_destroy (struct RPS_Sampler *sampler);
  275. #endif /* RPS_SAMPLER_COMMON_H */
  276. /* end of rps-sampler_common.h */