2 This file is part of GNUnet.
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.
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.
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/>.
20 * @file rps/rps-sampler_common.c
21 * @brief Code common to client and service sampler
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
28 #include "rps-sampler_common.h"
29 #include "gnunet-service-rps_sampler_elem.h"
34 #include "rps-test_util.h"
36 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler_common",__VA_ARGS__)
39 * @brief Context for a callback. Contains callback and closure.
41 * Meant to be an entry in an DLL.
43 struct SamplerNotifyUpdateCTX
46 * @brief The Callback to call on updates
48 SamplerNotifyUpdateCB notify_cb;
51 * @brief The according closure.
56 * @brief Next element in DLL.
58 struct SamplerNotifyUpdateCTX *next;
61 * @brief Previous element in DLL.
63 struct SamplerNotifyUpdateCTX *prev;
68 * Closure to _get_n_rand_peers_ready_cb()
70 struct RPS_SamplerRequestHandle
75 struct RPS_SamplerRequestHandle *next;
76 struct RPS_SamplerRequestHandle *prev;
79 * Number of peers we are waiting for.
84 * Number of peers we currently have.
86 uint32_t cur_num_peers;
89 * Pointer to the array holding the ids.
91 struct GNUNET_PeerIdentity *ids;
94 * Head and tail for the DLL to store the tasks for single requests
96 struct GetPeerCls *gpc_head;
97 struct GetPeerCls *gpc_tail;
102 struct RPS_Sampler *sampler;
105 * Callback to be called when all ids are available.
107 RPS_sampler_n_rand_peers_ready_cb callback;
110 * Closure given to the callback
117 * @brief Add a callback that will be called when the next peer is inserted
120 * @param sampler The sampler on which update it will be called
121 * @param notify_cb The callback
122 * @param cls Closure given to the callback
124 * @return The context containing callback and closure
126 struct SamplerNotifyUpdateCTX *
127 sampler_notify_on_update (struct RPS_Sampler *sampler,
128 SamplerNotifyUpdateCB notify_cb,
131 struct SamplerNotifyUpdateCTX *notify_ctx;
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "Inserting new context for notification\n");
135 notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX);
136 notify_ctx->notify_cb = notify_cb;
137 notify_ctx->cls = cls;
138 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
139 sampler->notify_ctx_tail,
146 * Get the size of the sampler.
148 * @param sampler the sampler to return the size of.
149 * @return the size of the sampler
152 RPS_sampler_get_size (struct RPS_Sampler *sampler)
154 return sampler->sampler_size;
159 * @brief Notify about update of the sampler.
161 * Call the callbacks that are waiting for notification on updates to the
164 * @param sampler The sampler the updates are waiting for
167 notify_update (struct RPS_Sampler *sampler)
169 struct SamplerNotifyUpdateCTX *tmp_notify_head;
170 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
172 LOG (GNUNET_ERROR_TYPE_DEBUG,
173 "Calling callbacks waiting for update notification.\n");
174 tmp_notify_head = sampler->notify_ctx_head;
175 tmp_notify_tail = sampler->notify_ctx_tail;
176 sampler->notify_ctx_head = NULL;
177 sampler->notify_ctx_tail = NULL;
178 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
179 NULL != tmp_notify_head;
180 notify_iter = tmp_notify_head)
182 GNUNET_assert (NULL != notify_iter->notify_cb);
183 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
186 notify_iter->notify_cb (notify_iter->cls);
187 GNUNET_free (notify_iter);
193 * Update every sampler element of this sampler with given peer
195 * @param sampler the sampler to update.
196 * @param id the PeerID that is put in the sampler
199 RPS_sampler_update (struct RPS_Sampler *sampler,
200 const struct GNUNET_PeerIdentity *id)
202 to_file (sampler->file_name,
204 GNUNET_i2s_full (id));
206 for (uint32_t i = 0; i < sampler->sampler_size; i++)
208 RPS_sampler_elem_next (sampler->sampler_elements[i],
211 notify_update (sampler);
216 * Reinitialise all previously initialised sampler elements with the given value.
218 * Used to get rid of a PeerID.
220 * @param sampler the sampler to reinitialise a sampler element in.
221 * @param id the id of the sampler elements to update.
224 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
225 const struct GNUNET_PeerIdentity *id)
229 for (i = 0; i < sampler->sampler_size; i++)
231 if (0 == GNUNET_CRYPTO_cmp_peer_identity(id,
232 &(sampler->sampler_elements[i]->peer_id)) )
234 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
235 to_file (sampler->sampler_elements[i]->file_name,
237 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
244 * Counts how many Samplers currently hold a given PeerID.
246 * @param sampler the sampler to count ids in.
247 * @param id the PeerID to count.
249 * @return the number of occurrences of id.
252 RPS_sampler_count_id (struct RPS_Sampler *sampler,
253 const struct GNUNET_PeerIdentity *id)
259 for ( i = 0 ; i < sampler->sampler_size ; i++ )
261 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
262 && EMPTY != sampler->sampler_elements[i]->is_empty)
270 * Grow or shrink the size of the sampler.
272 * @param sampler the sampler to resize.
273 * @param new_size the new size of the sampler
276 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
278 unsigned int old_size;
281 // TODO check min and max size
283 old_size = sampler->sampler_size;
285 if (old_size > new_size)
288 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Shrinking sampler %d -> %d\n",
293 to_file (sampler->file_name,
294 "Shrinking sampler %d -> %d",
298 for (i = new_size ; i < old_size ; i++)
300 to_file (sampler->file_name,
303 sampler->sampler_elements[i]->file_name);
304 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
307 GNUNET_array_grow (sampler->sampler_elements,
308 sampler->sampler_size,
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "sampler->sampler_elements now points to %p\n",
312 sampler->sampler_elements);
315 else if (old_size < new_size)
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Growing sampler %d -> %d\n",
322 to_file (sampler->file_name,
323 "Growing sampler %d -> %d",
327 GNUNET_array_grow (sampler->sampler_elements,
328 sampler->sampler_size,
331 for (i = old_size ; i < new_size ; i++)
332 { /* Add new sampler elements */
333 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
335 to_file (sampler->file_name,
338 sampler->sampler_elements[i]->file_name);
343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
347 GNUNET_assert (sampler->sampler_size == new_size);
352 * Grow or shrink the size of the sampler.
354 * @param sampler the sampler to resize.
355 * @param new_size the new size of the sampler
358 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
360 GNUNET_assert (0 < new_size);
361 sampler_resize (sampler, new_size);
368 * @param sampler the sampler to empty.
369 * @param new_size the new size of the sampler
372 sampler_empty (struct RPS_Sampler *sampler)
374 sampler_resize (sampler, 0);
379 * Callback to _get_rand_peer() used by _get_n_rand_peers().
381 * Checks whether all n peers are available. If they are,
385 check_n_peers_ready (void *cls,
386 const struct GNUNET_PeerIdentity *id)
388 struct RPS_SamplerRequestHandle *req_handle = cls;
390 RPS_sampler_n_rand_peers_ready_cb tmp_cb;
391 struct GNUNET_PeerIdentity *peers;
395 req_handle->cur_num_peers++;
396 LOG (GNUNET_ERROR_TYPE_DEBUG,
397 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
398 req_handle->cur_num_peers, req_handle->num_peers);
400 if (req_handle->num_peers == req_handle->cur_num_peers)
401 { /* All peers are ready -- return those to the client */
402 GNUNET_assert (NULL != req_handle->callback);
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "returning %" PRIX32 " peers to the client\n",
406 req_handle->num_peers);
408 /* Copy pointers and peers temporarily as they
409 * might be deleted from within the callback */
410 tmp_cb = req_handle->callback;
411 num_peers = req_handle->num_peers;
412 peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
413 GNUNET_memcpy (peers,
415 num_peers * sizeof (struct GNUNET_PeerIdentity));
416 cb_cls = req_handle->cls;
417 RPS_sampler_request_cancel (req_handle);
419 tmp_cb (peers, num_peers, cb_cls);
426 * Get n random peers out of the sampled peers.
428 * We might want to reinitialise this sampler after giving the
429 * corrsponding peer to the client.
430 * Random with or without consumption?
432 * @param sampler the sampler to get peers from.
433 * @param cb callback that will be called once the ids are ready.
434 * @param cls closure given to @a cb
435 * @param for_client #GNUNET_YES if result is used for client,
436 * #GNUNET_NO if used internally
437 * @param num_peers the number of peers requested
439 struct RPS_SamplerRequestHandle *
440 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
442 RPS_sampler_n_rand_peers_ready_cb cb,
446 struct RPS_SamplerRequestHandle *req_handle;
447 struct GetPeerCls *gpc;
449 GNUNET_assert (0 != sampler->sampler_size);
453 // TODO check if we have too much (distinct) sampled peers
454 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
455 req_handle->num_peers = num_peers;
456 req_handle->cur_num_peers = 0;
457 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
458 req_handle->sampler = sampler;
459 req_handle->callback = cb;
460 req_handle->cls = cls;
461 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
462 sampler->req_handle_tail,
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
468 for (i = 0; i < num_peers; i++)
470 gpc = GNUNET_new (struct GetPeerCls);
471 gpc->req_handle = req_handle;
472 gpc->cont = check_n_peers_ready;
473 gpc->cont_cls = req_handle;
474 gpc->id = &req_handle->ids[i];
476 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
477 req_handle->gpc_tail,
479 // maybe add a little delay
480 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
487 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
489 * @param req_handle the handle to the request
492 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
494 struct GetPeerCls *i;
496 while (NULL != (i = req_handle->gpc_head) )
498 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
499 req_handle->gpc_tail,
501 if (NULL != i->get_peer_task)
503 GNUNET_SCHEDULER_cancel (i->get_peer_task);
505 if (NULL != i->notify_ctx)
507 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
508 req_handle->sampler->notify_ctx_tail,
510 GNUNET_free (i->notify_ctx);
511 i->notify_ctx = NULL;
515 GNUNET_free (req_handle->ids);
516 req_handle->ids = NULL;
517 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
518 req_handle->sampler->req_handle_tail,
520 GNUNET_free (req_handle);
525 * Cleans the sampler.
528 RPS_sampler_destroy (struct RPS_Sampler *sampler)
530 if (NULL != sampler->req_handle_head)
532 LOG (GNUNET_ERROR_TYPE_WARNING,
533 "There are still pending requests. Going to remove them.\n");
534 while (NULL != sampler->req_handle_head)
536 RPS_sampler_request_cancel (sampler->req_handle_head);
539 sampler_empty (sampler);
540 GNUNET_free (sampler);
544 /* end of rps-sampler_common.c */