uncrustify as demanded.
[oweals/gnunet.git] / src / rps / gnunet-service-rps_sampler.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file rps/gnunet-service-rps_sampler.c
23  * @brief sampler implementation
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "rps.h"
30
31 #include "rps-sampler_common.h"
32 #include "gnunet-service-rps_sampler.h"
33 #include "gnunet-service-rps_sampler_elem.h"
34
35 #include <math.h>
36 #include <inttypes.h>
37
38 #include "rps-test_util.h"
39
40 #define LOG(kind, ...) GNUNET_log_from(kind, "rps-sampler", __VA_ARGS__)
41
42
43 // multiple 'clients'?
44
45 // TODO check for overflows
46
47 // TODO align message structs
48
49 // hist_size_init, hist_size_max
50
51 /***********************************************************************
52 * WARNING: This section needs to be reviewed regarding the use of
53 * functions providing (pseudo)randomness!
54 ***********************************************************************/
55
56 // TODO care about invalid input of the caller (size 0 or less...)
57
58 /**
59  * @brief Callback called each time a new peer was put into the sampler
60  *
61  * @param cls A possibly given closure
62  */
63 typedef void
64 (*SamplerNotifyUpdateCB) (void *cls);
65
66 /**
67  * @brief Context for a callback. Contains callback and closure.
68  *
69  * Meant to be an entry in an DLL.
70  */
71 struct SamplerNotifyUpdateCTX {
72   /**
73    * @brief The Callback to call on updates
74    */
75   SamplerNotifyUpdateCB notify_cb;
76
77   /**
78    * @brief The according closure.
79    */
80   void *cls;
81
82   /**
83    * @brief Next element in DLL.
84    */
85   struct SamplerNotifyUpdateCTX *next;
86
87   /**
88    * @brief Previous element in DLL.
89    */
90   struct SamplerNotifyUpdateCTX *prev;
91 };
92
93
94 /**
95  * Type of function used to differentiate between modified and not modified
96  * Sampler.
97  */
98 typedef void
99 (*RPS_get_peers_type) (void *cls);
100
101 /**
102  * Get one random peer out of the sampled peers.
103  *
104  * We might want to reinitialise this sampler after giving the
105  * corrsponding peer to the client.
106  * Only used internally
107  */
108 static void
109 sampler_get_rand_peer(void *cls);
110
111
112 /**
113  * Closure to _get_n_rand_peers_ready_cb()
114  */
115 struct RPS_SamplerRequestHandle {
116   /**
117    * DLL
118    */
119   struct RPS_SamplerRequestHandle *next;
120   struct RPS_SamplerRequestHandle *prev;
121
122   /**
123    * Number of peers we are waiting for.
124    */
125   uint32_t num_peers;
126
127   /**
128    * Number of peers we currently have.
129    */
130   uint32_t cur_num_peers;
131
132   /**
133    * Pointer to the array holding the ids.
134    */
135   struct GNUNET_PeerIdentity *ids;
136
137   /**
138    * Head and tail for the DLL to store the tasks for single requests
139    */
140   struct GetPeerCls *gpc_head;
141   struct GetPeerCls *gpc_tail;
142
143   /**
144    * Sampler.
145    */
146   struct RPS_Sampler *sampler;
147
148   /**
149    * Callback to be called when all ids are available.
150    */
151   RPS_sampler_n_rand_peers_ready_cb callback;
152
153   /**
154    * Closure given to the callback
155    */
156   void *cls;
157 };
158
159 ///**
160 // * Global sampler variable.
161 // */
162 //struct RPS_Sampler *sampler;
163
164
165 /**
166  * The minimal size for the extended sampler elements.
167  */
168 static size_t min_size;
169
170 /**
171  * The maximal size the extended sampler elements should grow to.
172  */
173 static size_t max_size;
174
175 /**
176  * The size the extended sampler elements currently have.
177  */
178 //static size_t extra_size;
179
180 /**
181  * Inedex to the sampler element that is the next to be returned
182  */
183 static uint32_t client_get_index;
184
185
186 /**
187  * Initialise a tuple of sampler elements.
188  *
189  * @param init_size the size the sampler is initialised with
190  * @param max_round_interval maximum time a round takes
191  * @return a handle to a sampler that consists of sampler elements.
192  */
193 struct RPS_Sampler *
194 RPS_sampler_init(size_t init_size,
195                  struct GNUNET_TIME_Relative max_round_interval)
196 {
197   struct RPS_Sampler *sampler;
198
199   /* Initialise context around extended sampler */
200   min_size = 10; // TODO make input to _samplers_init()
201   max_size = 1000; // TODO make input to _samplers_init()
202
203   sampler = GNUNET_new(struct RPS_Sampler);
204
205   sampler->max_round_interval = max_round_interval;
206   sampler->get_peers = sampler_get_rand_peer;
207   //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
208   //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
209   RPS_sampler_resize(sampler, init_size);
210
211   client_get_index = 0;
212
213   //GNUNET_assert (init_size == sampler->sampler_size);
214   return sampler;
215 }
216
217 /**
218  * Get one random peer out of the sampled peers.
219  *
220  * We might want to reinitialise this sampler after giving the
221  * corrsponding peer to the client.
222  * Only used internally
223  */
224 static void
225 sampler_get_rand_peer(void *cls)
226 {
227   struct GetPeerCls *gpc = cls;
228   uint32_t r_index;
229   struct RPS_Sampler *sampler;
230
231   gpc->get_peer_task = NULL;
232   gpc->notify_ctx = NULL;
233   sampler = gpc->req_handle->sampler;
234
235   /**;
236    * Choose the r_index of the peer we want to return
237    * at random from the interval of the gossip list
238    */
239   r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
240                                      sampler->sampler_size);
241
242   if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
243     {
244       //LOG (GNUNET_ERROR_TYPE_DEBUG,
245       //     "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
246
247       gpc->notify_ctx =
248         sampler_notify_on_update(sampler,
249                                  &sampler_get_rand_peer,
250                                  gpc);
251       return;
252     }
253
254   GNUNET_CONTAINER_DLL_remove(gpc->req_handle->gpc_head,
255                               gpc->req_handle->gpc_tail,
256                               gpc);
257   *gpc->id = sampler->sampler_elements[r_index]->peer_id;
258   gpc->cont(gpc->cont_cls, gpc->id, 0, sampler->sampler_elements[r_index]->num_peers);
259
260   GNUNET_free(gpc);
261 }
262
263
264 /* end of gnunet-service-rps.c */