2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_nse_service.h"
32 #include "gnunet-service-rps_sampler.h"
37 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
39 // TODO modify @brief in every file
41 // TODO take care that messages are not longer than 64k
43 // TODO check for overflows
45 // TODO align message structs
47 // (TODO api -- possibility of getting weak random peer immideately)
49 // TODO malicious peer
51 // TODO Change API to accept initialisation peers
53 // TODO Change API to accept good peers 'friends'
55 // TODO store peers somewhere
57 // TODO check that every id we get is valid - is it reachable?
61 // hist_size_init, hist_size_max
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
71 static struct GNUNET_PeerIdentity *own_identity;
74 * Closure to the callback cadet calls on each peer it passes to us
79 * The server handle to later listen to client requests
81 struct GNUNET_SERVER_Handle *server;
84 * Counts how many peers cadet already passed to us
90 struct GNUNET_PeerIdentity *
91 get_rand_peer (const struct GNUNET_PeerIdentity *peer_list, unsigned int size);
94 /***********************************************************************
95 * Housekeeping with peers
96 ***********************************************************************/
99 * Struct used to store the context of a connected client.
104 * The message queue to communicate with the client.
106 struct GNUNET_MQ_Handle *mq;
110 * Used to keep track in what lists single peerIDs are.
112 enum in_list_flag // probably unneeded
114 in_other_sampler_list = 0x1,
115 in_other_gossip_list = 0x2, // unneeded?
116 in_own_sampler_list = 0x4,
117 in_own_gossip_list = 0x8 // unneeded?
121 * Struct used to keep track of other peer's status
123 * This is stored in a multipeermap.
128 * In own gossip/sampler list, in other's gossip/sampler list
130 uint32_t in_flags; // unneeded?
133 * Message queue open to client
135 struct GNUNET_MQ_Handle *mq;
138 * Channel open to client.
140 struct GNUNET_CADET_Channel *to_channel;
143 * Channel open from client.
145 struct GNUNET_CADET_Channel *from_channel; // unneeded
148 * This is pobably followed by 'statistical' data (when we first saw
149 * him, how did we get his ID, how many pushes (in a timeinterval),
154 /***********************************************************************
155 * /Housekeeping with peers
156 ***********************************************************************/
158 /***********************************************************************
160 ***********************************************************************/
163 * Set of all peers to keep track of them.
165 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
169 * The gossiped list of peers.
171 static struct GNUNET_PeerIdentity *gossip_list;
174 * Size of the gossiped list
176 //static unsigned int gossip_list_size;
177 static uint32_t gossip_list_size;
181 * The actual size of the sampler
183 static unsigned int sampler_size;
184 //size_t sampler_size;
187 * The size of sampler we need to be able to satisfy the client's need of
190 static unsigned int sampler_size_client_need;
193 * The size of sampler we need to be able to satisfy the Brahms protocol's
194 * need of random peers.
196 * This is directly taken as the #gossip_list_size on update of the
199 * This is one minimum size the sampler grows to.
201 static unsigned int sampler_size_est_need;
205 * Percentage of total peer number in the gossip list
206 * to send random PUSHes to
208 * TODO do not read from configuration
213 * Percentage of total peer number in the gossip list
214 * to send random PULLs to
216 * TODO do not read from configuration
221 * The percentage gamma of history updates.
222 * Simply 1 - alpha - beta
227 * Identifier for the main task that runs periodically.
229 static struct GNUNET_SCHEDULER_Task *do_round_task;
232 * Time inverval the do_round task runs in.
234 static struct GNUNET_TIME_Relative round_interval;
239 * List to store peers received through pushes temporary.
241 * TODO -> multipeermap
243 static struct GNUNET_PeerIdentity *push_list;
246 * Size of the push_list;
248 static unsigned int push_list_size;
249 //size_t push_list_size;
252 * List to store peers received through pulls temporary.
254 * TODO -> multipeermap
256 static struct GNUNET_PeerIdentity *pull_list;
259 * Size of the pull_list;
261 static unsigned int pull_list_size;
262 //size_t pull_list_size;
268 static struct GNUNET_NSE_Handle *nse;
273 static struct GNUNET_CADET_Handle *cadet_handle;
279 * Only needed in the beginning to check how many of the 64 deltas
282 static unsigned int req_counter;
285 * Time of the last request we received.
287 * Used to compute the expected request rate.
289 static struct GNUNET_TIME_Absolute last_request;
292 * Size of #request_deltas.
294 #define REQUEST_DELTAS_SIZE 64
295 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
298 * Last 64 deltas between requests
300 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
303 * The prediction of the rate of requests
305 static struct GNUNET_TIME_Relative request_rate;
308 /***********************************************************************
310 ***********************************************************************/
313 /***********************************************************************
315 ***********************************************************************/
318 * Check if peer is already in peer array.
321 in_arr (const struct GNUNET_PeerIdentity *array,
322 unsigned int arr_size,
323 const struct GNUNET_PeerIdentity *peer)
325 GNUNET_assert (NULL != peer);
330 GNUNET_assert (NULL != array);
335 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
347 * Get random peer from the gossip list.
349 struct GNUNET_PeerIdentity *
350 get_rand_peer (const struct GNUNET_PeerIdentity *peer_list, unsigned int list_size)
353 struct GNUNET_PeerIdentity *peer;
355 peer = GNUNET_new (struct GNUNET_PeerIdentity);
356 // FIXME if we have only NULL in gossip list this will block
357 // but then we might have a problem nevertheless
363 * Choose the r_index of the peer we want to return
364 * at random from the interval of the gossip list
366 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
369 *peer = peer_list[r_index];
370 } while (NULL == peer);
377 * Get the context of a peer. If not existing, create.
380 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
382 struct PeerContext *ctx;
384 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
386 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
390 ctx = GNUNET_new (struct PeerContext);
393 ctx->to_channel = NULL;
394 ctx->from_channel = NULL;
395 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
402 * Get the channel of a peer. If not existing, create.
404 struct GNUNET_CADET_Channel *
405 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
407 struct PeerContext *ctx;
409 ctx = get_peer_ctx (peer_map, peer);
410 if (NULL == ctx->to_channel)
412 ctx->to_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
413 GNUNET_RPS_CADET_PORT,
414 GNUNET_CADET_OPTION_RELIABLE);
415 // do I have to explicitly put it in the peer_map?
416 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
419 return ctx->to_channel;
424 * Get the message queue of a specific peer.
426 * If we already have a message queue open to this client,
427 * simply return it, otherways create one.
429 struct GNUNET_MQ_Handle *
430 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
432 struct PeerContext *ctx;
434 ctx = get_peer_ctx (peer_map, peer_id);
437 (void) get_channel (peer_map, peer_id);
438 ctx->mq = GNUNET_CADET_mq_create (ctx->to_channel);
439 //do I have to explicitly put it in the peer_map?
440 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
441 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
448 * Sum all time relatives of an array.
450 struct GNUNET_TIME_Relative
451 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
453 struct GNUNET_TIME_Relative sum;
456 sum = GNUNET_TIME_UNIT_ZERO;
457 for ( i = 0 ; i < arr_size ; i++ )
459 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
466 * Compute the average of given time relatives.
468 struct GNUNET_TIME_Relative
469 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
471 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size); // FIXME find a way to devide that by arr_size
475 /***********************************************************************
477 ***********************************************************************/
480 * Wrapper around _sampler_resize()
485 uint64_t bigger_size;
489 if (sampler_size_est_need > sampler_size_client_need)
490 bigger_size = sampler_size_client_need;
492 bigger_size = sampler_size_est_need;
494 // TODO respect the request rate, min, max
495 if (sampler_size > bigger_size*4)
497 RPS_sampler_resize (sampler_size/2);
499 else if (sampler_size < bigger_size)
501 RPS_sampler_resize (sampler_size*2);
507 * Function called by NSE.
509 * Updates sizes of sampler list and gossip list and adapt those lists
513 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
516 //double scale; // TODO this might go gloabal/config
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %f)\n",
520 logestimate, std_dev, sampler_size);
522 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
523 // GNUNET_NSE_log_estimate_to_n (logestimate);
524 estimate = pow (estimate, 1./3);
525 // TODO add if std_dev is a number
526 // estimate += (std_dev * scale);
527 if ( 0 < estimate ) {
528 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
529 sampler_size_est_need = estimate;
531 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
533 /* If the NSE has changed adapt the lists accordingly */
539 * Callback called once the requested PeerIDs are ready.
541 * Sends those to the requesting client.
543 void client_respond (void *cls,
544 struct GNUNET_PeerIdentity *ids, uint64_t num_peers)
546 struct GNUNET_MQ_Envelope *ev;
547 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
548 struct GNUNET_SERVER_Client *client;
549 struct client_ctx *cli_ctx;
551 client = (struct GNUNET_SERVER_Client *) cls;
553 ev = GNUNET_MQ_msg_extra (out_msg,
554 num_peers * sizeof (struct GNUNET_PeerIdentity),
555 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
556 out_msg->num_peers = GNUNET_htonll (num_peers);
560 num_peers * sizeof (struct GNUNET_PeerIdentity));
563 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
564 if ( NULL == cli_ctx ) {
565 cli_ctx = GNUNET_new (struct client_ctx);
566 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
567 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
570 GNUNET_MQ_send (cli_ctx->mq, ev);
575 * Handle RPS request from the client.
578 * @param client identification of the client
579 * @param message the actual message
582 handle_client_request (void *cls,
583 struct GNUNET_SERVER_Client *client,
584 const struct GNUNET_MessageHeader *message)
586 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
588 struct GNUNET_RPS_CS_RequestMessage *msg;
590 struct GNUNET_TIME_Relative max_round_duration;
593 /* Estimate request rate */
594 if (request_deltas_size > req_counter)
596 if ( 1 < req_counter)
598 /* Shift last request deltas to the right */
599 memcpy (&request_deltas[1],
601 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
602 /* Add current delta to beginning */
603 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
604 GNUNET_TIME_absolute_get ());
605 request_rate = T_relative_avg (request_deltas, req_counter);
607 max_round_duration = GNUNET_TIME_relative_add (round_interval,
608 GNUNET_TIME_relative_divide (round_interval, 2));
609 sampler_size_client_need = max_round_duration.rel_value_us / request_rate.rel_value_us;
613 last_request = GNUNET_TIME_absolute_get ();
616 // TODO check message size
617 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
619 num_peers = ntohl (msg->num_peers);
621 RPS_sampler_get_n_rand_peers (client_respond, client, num_peers);
623 GNUNET_SERVER_receive_done (client,
629 * Handle seed from the client.
632 * @param client identification of the client
633 * @param message the actual message
636 handle_client_seed (void *cls,
637 struct GNUNET_SERVER_Client *client,
638 const struct GNUNET_MessageHeader *message)
640 struct GNUNET_RPS_CS_SeedMessage *in_msg;
641 struct GNUNET_PeerIdentity *peers;
644 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
647 GNUNET_SERVER_receive_done (client,
650 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
651 if (ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage) /
652 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
655 GNUNET_SERVER_receive_done (client,
659 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
660 peers = (struct GNUNET_PeerIdentity *) &message[1];
662 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
663 RPS_sampler_update_list (&peers[i]);
665 GNUNET_SERVER_receive_done (client,
671 * Handle a PUSH message from another peer.
673 * Check the proof of work and store the PeerID
674 * in the temporary list for pushed PeerIDs.
677 * @param channel The channel the PUSH was received over
678 * @param channel_ctx The context associated with this channel
679 * @param msg The message header
682 handle_peer_push (void *cls,
683 struct GNUNET_CADET_Channel *channel,
685 const struct GNUNET_MessageHeader *msg)
687 const struct GNUNET_PeerIdentity *peer;
689 // (check the proof of work)
691 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
692 // FIXME wait for cadet to change this function
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
695 /* Add the sending peer to the push_list */
696 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
697 GNUNET_array_append (push_list, push_list_size, *peer);
703 * Handle PULL REQUEST request message from another peer.
705 * Reply with the gossip list of PeerIDs.
708 * @param channel The channel the PUSH was received over
709 * @param channel_ctx The context associated with this channel
710 * @param msg The message header
713 handle_peer_pull_request (void *cls,
714 struct GNUNET_CADET_Channel *channel,
716 const struct GNUNET_MessageHeader *msg)
718 struct GNUNET_PeerIdentity *peer;
719 struct GNUNET_MQ_Handle *mq;
720 struct GNUNET_MQ_Envelope *ev;
721 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
724 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
725 // FIXME wait for cadet to change this function
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "PULL REQUEST from peer %s received, going to send %u peers\n",
728 GNUNET_i2s (peer), gossip_list_size);
730 mq = get_mq (peer_map, peer);
732 ev = GNUNET_MQ_msg_extra (out_msg,
733 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
734 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
735 //out_msg->num_peers = GNUNET_htonll (gossip_list_size);
736 out_msg->num_peers = htonl (gossip_list_size);
737 memcpy (&out_msg[1], gossip_list,
738 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
740 GNUNET_MQ_send (mq, ev);
746 * Handle PULL REPLY message from another peer.
748 * Check whether we sent a corresponding request and
749 * whether this reply is the first one.
752 * @param channel The channel the PUSH was received over
753 * @param channel_ctx The context associated with this channel
754 * @param msg The message header
757 handle_peer_pull_reply (void *cls,
758 struct GNUNET_CADET_Channel *channel,
760 const struct GNUNET_MessageHeader *msg)
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
764 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
765 struct GNUNET_PeerIdentity *peers;
768 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
770 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
771 return GNUNET_SYSERR;
773 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
774 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
776 LOG (GNUNET_ERROR_TYPE_ERROR, "message says it sends %" PRIu64 " peers, have space for %i peers\n",
777 ntohl (in_msg->num_peers),
778 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity));
780 return GNUNET_SYSERR;
783 // TODO check that we sent a request and that it is the first reply
785 peers = (struct GNUNET_PeerIdentity *) &msg[1];
786 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
788 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
789 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
792 // TODO check that id is valid - whether it is reachable
799 * Send out PUSHes and PULLs.
801 * This is executed regylary.
804 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
809 //unsigned int *n_arr;
810 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
811 struct GNUNET_MQ_Envelope *ev;
812 const struct GNUNET_PeerIdentity *peer;
813 struct GNUNET_MQ_Handle *mq;
815 // TODO log lists, ...
818 /* Would it make sense to have one shuffeled gossip list and then
819 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
820 * use the rest to update sampler?
821 * in essence get random peers with consumption */
824 //n_arr = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
825 n_peers = round (alpha * gossip_list_size);
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
829 n_peers, alpha, gossip_list_size);
830 for ( i = 0 ; i < n_peers ; i++ )
832 peer = get_rand_peer (gossip_list, gossip_list_size);
833 if (own_identity != peer)
834 { // FIXME if this fails schedule/loop this for later
835 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
837 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
838 // FIXME sometimes it returns a pointer to a freed mq
839 mq = get_mq (peer_map, peer);
840 GNUNET_MQ_send (mq, ev);
845 /* Send PULL requests */
846 //n_arr = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
847 n_peers = round (beta * gossip_list_size);
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
851 n_peers, beta, gossip_list_size);
852 for ( i = 0 ; i < n_peers ; i++ )
854 peer = get_rand_peer (gossip_list, gossip_list_size);
855 if (own_identity != peer)
856 { // FIXME if this fails schedule/loop this for later
857 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
859 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
861 mq = get_mq (peer_map, peer);
862 GNUNET_MQ_send (mq, ev);
867 /* Update gossip list */
870 if ( push_list_size <= alpha * gossip_list_size &&
871 push_list_size != 0 &&
872 pull_list_size != 0 )
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
876 uint64_t first_border;
877 uint64_t second_border;
879 GNUNET_array_grow (gossip_list, gossip_list_size, sampler_size_est_need);
881 first_border = round (alpha * gossip_list_size);
882 for ( i = 0 ; i < first_border ; i++ )
883 { // TODO use RPS_sampler_get_n_rand_peers
884 /* Update gossip list with peers received through PUSHes */
885 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
887 gossip_list[i] = push_list[r_index];
888 // TODO change the in_flags accordingly
891 second_border = first_border + round (beta * gossip_list_size);
892 for ( i = first_border ; i < second_border ; i++ )
894 /* Update gossip list with peers received through PULLs */
895 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
897 gossip_list[i] = pull_list[r_index];
898 // TODO change the in_flags accordingly
901 for ( i = second_border ; i < gossip_list_size ; i++ )
903 /* Update gossip list with peers from history */
904 peer = RPS_sampler_get_n_rand_peers_ (1);
905 gossip_list[i] = *peer;
906 // TODO change the in_flags accordingly
912 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
914 // TODO independent of that also get some peers from CADET_get_peers()?
917 /* Update samplers */
919 for ( i = 0 ; i < push_list_size ; i++ )
921 RPS_sampler_update_list (&push_list[i]);
925 for ( i = 0 ; i < pull_list_size ; i++ )
927 RPS_sampler_update_list (&pull_list[i]);
932 /* Empty push/pull lists */
933 GNUNET_array_grow (push_list, push_list_size, 0);
934 GNUNET_array_grow (pull_list, pull_list_size, 0);
936 struct GNUNET_TIME_Relative time_next_round;
937 struct GNUNET_TIME_Relative half_round_interval;
938 unsigned int rand_delay;
940 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
941 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
945 * Compute random value between (0 and 1) * round_interval
946 * via multiplying round_interval with a 'fraction' (0 to value)/value
948 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
949 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
950 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
951 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
952 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
954 /* Schedule next round */
955 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
956 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
961 * Open a connection to given peer and store channel and mq.
964 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
966 // We open a channel to be notified when this peer goes down.
967 (void) get_channel (peer_map, id);
972 * Close the connection to given peer and delete channel and mq.
975 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
978 struct PeerContext *ctx;
980 s = RPS_sampler_count_id (id);
983 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
985 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
986 if (NULL != ctx->to_channel)
990 GNUNET_MQ_destroy (ctx->mq);
992 // may already be freed at shutdown of cadet
993 //GNUNET_CADET_channel_destroy (ctx->to_channel);
996 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
1002 rps_start (struct GNUNET_SERVER_Handle *server);
1005 * This is called from GNUNET_CADET_get_peers().
1007 * It is called on every peer(ID) that cadet somehow has contact with.
1008 * We use those to initialise the sampler.
1011 init_peer_cb (void *cls,
1012 const struct GNUNET_PeerIdentity *peer,
1013 int tunnel, // "Do we have a tunnel towards this peer?"
1014 unsigned int n_paths, // "Number of known paths towards this peer"
1015 unsigned int best_path) // "How long is the best path?
1016 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1018 struct init_peer_cls *ipc;
1020 ipc = (struct init_peer_cls *) cls;
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "Got %" PRIX32 ". peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1025 ipc->i, GNUNET_i2s (peer), peer, gossip_list_size);
1026 RPS_sampler_update_list (peer);
1027 (void) get_peer_ctx (peer_map, peer); // unneeded? -> insertCB
1029 if (ipc->i < gossip_list_size)
1031 gossip_list[ipc->i] = *peer; // FIXME sometimes we're writing to invalid space here
1032 // not sure whether fixed
1036 // send push/pull to each of those peers?
1040 if (ipc->i < gossip_list_size)
1042 memcpy (&gossip_list[ipc->i],
1043 RPS_sampler_get_n_rand_peers_ (1),
1044 (gossip_list_size - ipc->i) * sizeof (struct GNUNET_PeerIdentity));
1046 rps_start (ipc->server);
1053 * Callback used to clean the multipeermap.
1056 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1058 struct GNUNET_PeerIdentity *tmp_id;
1059 struct PeerContext *peer_ctx;
1061 /* Check if we are starting to again iterate over same peers */
1063 { /* Store first id we see */
1064 tmp_id = GNUNET_new (struct GNUNET_PeerIdentity);
1068 else if (0 == GNUNET_CRYPTO_cmp_peer_identity (key, cls))
1069 { /* Check if we see the first id again */
1074 peer_ctx = (struct PeerContext *) value;
1076 if ( NULL != peer_ctx->mq)
1077 GNUNET_MQ_destroy (peer_ctx->mq);
1079 if ( NULL != peer_ctx->to_channel)
1080 GNUNET_CADET_channel_destroy (peer_ctx->to_channel);
1082 if ( NULL != peer_ctx->from_channel)
1083 GNUNET_CADET_channel_destroy (peer_ctx->from_channel);
1090 * Task run during shutdown.
1096 shutdown_task (void *cls,
1097 const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1103 if ( NULL != do_round_task )
1105 GNUNET_SCHEDULER_cancel (do_round_task);
1106 do_round_task = NULL;
1109 num_peers = GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL);
1110 if (GNUNET_SYSERR == num_peers)
1111 LOG (GNUNET_ERROR_TYPE_WARNING,
1112 "Iterating over peers to disconnect from them was cancelled\n");
1114 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1116 GNUNET_NSE_disconnect (nse);
1117 GNUNET_CADET_disconnect (cadet_handle);
1118 GNUNET_free (own_identity);
1119 RPS_sampler_destroy ();
1120 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
1121 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1122 GNUNET_array_grow (push_list, push_list_size, 0);
1123 GNUNET_array_grow (pull_list, pull_list_size, 0);
1128 * A client disconnected. Remove all of its data structure entries.
1130 * @param cls closure, NULL
1131 * @param client identification of the client
1134 handle_client_disconnect (void *cls,
1135 struct GNUNET_SERVER_Client * client)
1140 * Handle the channel a peer opens to us.
1142 * @param cls The closure
1143 * @param channel The channel the peer wants to establish
1144 * @param initiator The peer's peer ID
1145 * @param port The port the channel is being established over
1146 * @param options Further options
1149 handle_inbound_channel (void *cls,
1150 struct GNUNET_CADET_Channel *channel,
1151 const struct GNUNET_PeerIdentity *initiator,
1153 enum GNUNET_CADET_ChannelOption options)
1155 struct PeerContext *ctx;
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us (Peer %s).\n", GNUNET_i2s (initiator));
1159 GNUNET_assert ( NULL != channel );
1161 // we might not even store the from_channel
1163 ctx = get_peer_ctx (peer_map, initiator);
1164 if (NULL != ctx->from_channel)
1166 ctx->from_channel = channel;
1169 // FIXME there might already be an established channel
1171 //ctx->in_flags = in_other_gossip_list;
1172 ctx->mq = NULL; // TODO create mq?
1174 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
1175 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1176 return NULL; // TODO
1180 * This is called when a remote peer destroys a channel.
1182 * @param cls The closure
1183 * @param channel The channel being closed
1184 * @param channel_ctx The context associated with this channel
1187 cleanup_channel (void *cls,
1188 const struct GNUNET_CADET_Channel *channel,
1191 struct GNUNET_PeerIdentity *peer;
1192 struct PeerContext *peer_ctx;
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
1196 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1197 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1198 // Guess simply casting isn't the nicest way...
1199 // FIXME wait for cadet to change this function
1200 RPS_sampler_reinitialise_by_value (peer);
1202 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1203 /* Somwewhat {ab,re}use the iterator function */
1204 (void) peer_remove_cb (peer, peer, peer_ctx);
1205 GNUNET_CONTAINER_multipeermap_remove_all (peer_map, peer);
1209 * Actually start the service.
1212 rps_start (struct GNUNET_SERVER_Handle *server)
1214 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1215 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1216 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1217 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1221 GNUNET_SERVER_add_handlers (server, handlers);
1222 GNUNET_SERVER_disconnect_notify (server,
1223 &handle_client_disconnect,
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1228 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1231 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1238 * Process statistics requests.
1240 * @param cls closure
1241 * @param server the initialized server
1242 * @param c configuration to use
1246 struct GNUNET_SERVER_Handle *server,
1247 const struct GNUNET_CONFIGURATION_Handle *c)
1249 // TODO check what this does -- copied from gnunet-boss
1250 // - seems to work as expected
1251 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1253 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1255 struct init_peer_cls *ipc;
1261 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1262 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1263 GNUNET_assert (NULL != own_identity);
1264 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s (own_identity), own_identity);
1267 /* Get time interval from the configuration */
1268 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1273 GNUNET_SCHEDULER_shutdown ();
1277 /* Get initial size of sampler/gossip list from the configuration */
1278 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1280 (long long unsigned int *) &sampler_size_est_need))
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1283 GNUNET_SCHEDULER_shutdown ();
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1288 //gossip_list_size = sampler_size; // TODO rename sampler_size
1291 GNUNET_array_grow (gossip_list, gossip_list_size, sampler_size_est_need);
1294 /* connect to NSE */
1295 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1296 // TODO check whether that was successful
1297 // TODO disconnect on shutdown
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1303 // TODO initialise thresholds - ?
1305 /* Get alpha from the configuration */
1306 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1314 /* Get beta from the configuration */
1315 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1323 // TODO check that alpha + beta < 1
1325 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1328 /* Initialise cadet */
1329 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1330 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1331 sizeof (struct GNUNET_MessageHeader)},
1332 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1333 sizeof (struct GNUNET_MessageHeader)},
1334 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1338 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1339 cadet_handle = GNUNET_CADET_connect (cfg,
1341 &handle_inbound_channel,
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1348 /* Initialise sampler */
1349 struct GNUNET_TIME_Relative half_round_interval;
1350 struct GNUNET_TIME_Relative max_round_interval;
1352 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1353 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1355 RPS_sampler_init (sampler_size_est_need, own_identity, max_round_interval,
1356 insertCB, NULL, removeCB, NULL);
1357 sampler_size = sampler_size_est_need;
1359 /* Initialise push and pull maps */
1366 ipc = GNUNET_new (struct init_peer_cls);
1367 ipc->server = server;
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1370 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, ipc);
1372 // TODO send push/pull to each of those peers?
1377 * The main function for the rps service.
1379 * @param argc number of arguments from the command line
1380 * @param argv command line arguments
1381 * @return 0 ok, 1 on error
1384 main (int argc, char *const *argv)
1386 return (GNUNET_OK ==
1387 GNUNET_SERVICE_run (argc,
1390 GNUNET_SERVICE_OPTION_NONE,
1391 &run, NULL)) ? 0 : 1;
1394 /* end of gnunet-service-rps.c */