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?
59 // hist_size_init, hist_size_max
64 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69 static struct GNUNET_PeerIdentity *own_identity;
72 * Closure to the callback cadet calls on each peer it passes to us
77 * The server handle to later listen to client requests
79 struct GNUNET_SERVER_Handle *server;
82 * Counts how many peers cadet already passed to us
88 struct GNUNET_PeerIdentity *
89 get_rand_peer (const struct GNUNET_PeerIdentity *peer_list, unsigned int size);
92 /***********************************************************************
93 * Housekeeping with peers
94 ***********************************************************************/
97 * Struct used to store the context of a connected client.
102 * The message queue to communicate with the client.
104 struct GNUNET_MQ_Handle *mq;
108 * Used to keep track in what lists single peerIDs are.
110 enum in_list_flag // probably unneeded
112 in_other_sampler_list = 0x1,
113 in_other_gossip_list = 0x2, // unneeded?
114 in_own_sampler_list = 0x4,
115 in_own_gossip_list = 0x8 // unneeded?
119 * Struct used to keep track of other peer's status
121 * This is stored in a multipeermap.
126 * In own gossip/sampler list, in other's gossip/sampler list
128 uint32_t in_flags; // unneeded?
131 * Message queue open to client
133 struct GNUNET_MQ_Handle *mq;
136 * Channel open to client.
138 struct GNUNET_CADET_Channel *to_channel;
141 * Channel open from client.
143 struct GNUNET_CADET_Channel *from_channel; // unneeded
146 * This is pobably followed by 'statistical' data (when we first saw
147 * him, how did we get his ID, how many pushes (in a timeinterval),
152 /***********************************************************************
153 * /Housekeeping with peers
154 ***********************************************************************/
156 /***********************************************************************
158 ***********************************************************************/
161 * Set of all peers to keep track of them.
163 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
167 * The gossiped list of peers.
169 static struct GNUNET_PeerIdentity *gossip_list;
172 * Size of the gossiped list
174 static unsigned int gossip_list_size;
178 * The estimated size of the network.
180 * Influenced by the stdev.
182 static unsigned int est_size;
187 * Percentage of total peer number in the gossip list
188 * to send random PUSHes to
190 * TODO do not read from configuration
195 * Percentage of total peer number in the gossip list
196 * to send random PULLs to
198 * TODO do not read from configuration
203 * The percentage gamma of history updates.
204 * Simply 1 - alpha - beta
209 * Identifier for the main task that runs periodically.
211 static struct GNUNET_SCHEDULER_Task * do_round_task;
214 * Time inverval the do_round task runs in.
216 static struct GNUNET_TIME_Relative round_interval;
221 * List to store peers received through pushes temporary.
223 * TODO -> multipeermap
225 static struct GNUNET_PeerIdentity *push_list;
228 * Size of the push_list;
230 static unsigned int push_list_size;
231 //size_t push_list_size;
234 * List to store peers received through pulls temporary.
236 * TODO -> multipeermap
238 static struct GNUNET_PeerIdentity *pull_list;
241 * Size of the pull_list;
243 static unsigned int pull_list_size;
244 //size_t pull_list_size;
250 static struct GNUNET_NSE_Handle *nse;
255 static struct GNUNET_CADET_Handle *cadet_handle;
266 * Only needed in the beginning to check how many of the 64 deltas
269 static unsigned int req_counter;
272 * Time of the last request we received.
274 * Used to compute the expected request rate.
276 static struct GNUNET_TIME_Absolute last_request;
279 * Size of #request_deltas.
281 #define REQUEST_DELTAS_SIZE 64
282 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
285 * Last 64 deltas between requests
287 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
290 * The prediction of the rate of requests
292 static struct GNUNET_TIME_Relative request_rate;
295 /***********************************************************************
297 ***********************************************************************/
300 /***********************************************************************
302 ***********************************************************************/
305 * Check if peer is already in peer array.
308 in_arr (const struct GNUNET_PeerIdentity *array,
309 unsigned int arr_size,
310 const struct GNUNET_PeerIdentity *peer)
312 GNUNET_assert (NULL != peer);
317 GNUNET_assert (NULL != array);
322 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
334 * Get random peer from the gossip list.
336 struct GNUNET_PeerIdentity *
337 get_rand_peer(const struct GNUNET_PeerIdentity *peer_list, unsigned int list_size)
340 struct GNUNET_PeerIdentity *peer;
342 peer = GNUNET_new(struct GNUNET_PeerIdentity);
343 // FIXME if we have only NULL in gossip list this will block
344 // but then we might have a problem nevertheless
350 * Choose the r_index of the peer we want to return
351 * at random from the interval of the gossip list
353 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
356 *peer = peer_list[r_index];
357 } while (NULL == peer);
364 * Get the context of a peer. If not existing, create.
366 struct peer_context *
367 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
369 struct peer_context *ctx;
371 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
373 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
377 ctx = GNUNET_new (struct peer_context);
380 ctx->to_channel = NULL;
381 ctx->from_channel = NULL;
382 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
389 * Get the channel of a peer. If not existing, create.
391 struct GNUNET_CADET_Channel *
392 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
394 struct peer_context *ctx;
396 ctx = get_peer_ctx (peer_map, peer);
397 if (NULL == ctx->to_channel)
399 ctx->to_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
400 GNUNET_RPS_CADET_PORT,
401 GNUNET_CADET_OPTION_RELIABLE);
402 // do I have to explicitly put it in the peer_map?
403 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
406 return ctx->to_channel;
411 * Get the message queue of a specific peer.
413 * If we already have a message queue open to this client,
414 * simply return it, otherways create one.
416 struct GNUNET_MQ_Handle *
417 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
419 struct peer_context *ctx;
421 ctx = get_peer_ctx (peer_map, peer_id);
424 (void) get_channel (peer_map, peer_id);
425 ctx->mq = GNUNET_CADET_mq_create (ctx->to_channel);
426 //do I have to explicitly put it in the peer_map?
427 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
435 * Sum all time relatives of an array.
437 struct GNUNET_TIME_Relative
438 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
440 struct GNUNET_TIME_Relative sum;
443 sum = GNUNET_TIME_UNIT_ZERO;
444 for ( i = 0 ; i < arr_size ; i++ )
446 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
453 * Compute the average of given time relatives.
455 struct GNUNET_TIME_Relative
456 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
458 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size); // FIXME find a way to devide that by arr_size
462 /***********************************************************************
464 ***********************************************************************/
467 * Function called by NSE.
469 * Updates sizes of sampler list and gossip list and adapt those lists
473 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
476 unsigned int old_est;
477 //double scale; // TODO this might go gloabal/config
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Received a ns estimate - logest: %f, std_dev: %f (old_est: %f)\n",
483 logestimate, std_dev, old_est);
485 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
486 // GNUNET_NSE_log_estimate_to_n (logestimate);
487 estimate = pow (estimate, 1./3);
488 // TODO add if std_dev is a number
489 // estimate += (std_dev * scale);
490 if ( 0 < estimate ) {
491 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
494 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
496 /* If the NSE has changed adapt the lists accordingly */
497 // TODO respect the request rate, min, max
498 if (old_est > est_size*4)
500 RPS_sampler_resize (old_est/2);
502 else if (old_est < est_size)
504 if (est_size < old_est*2)
505 RPS_sampler_resize (old_est*2);
507 RPS_sampler_resize (est_size);
512 * Handle RPS request from the client.
515 * @param client identification of the client
516 * @param message the actual message
520 handle_cs_request (void *cls,
521 struct GNUNET_SERVER_Client *client,
522 const struct GNUNET_MessageHeader *message)
524 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
526 struct GNUNET_RPS_CS_RequestMessage *msg;
527 //unsigned int n_arr[sampler_list->size];// =
528 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
529 //struct GNUNET_MQ_Handle *mq;
530 struct client_ctx *cli_ctx;
531 struct GNUNET_MQ_Envelope *ev;
532 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
534 const struct GNUNET_PeerIdentity *peers;
538 /* Estimate request rate */
539 if (request_deltas_size > req_counter)
541 if ( 1 < req_counter)
543 /* Shift last request deltas to the right */
544 memcpy (&request_deltas[1],
546 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
547 /* Add current delta to beginning */
548 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
549 GNUNET_TIME_absolute_get ());
550 request_rate = T_relative_avg (request_deltas, req_counter);
552 last_request = GNUNET_TIME_absolute_get();
553 // TODO resize the size of the extended_samplers
556 // TODO check message size
557 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
558 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
559 if ( NULL == cli_ctx ) {
560 cli_ctx = GNUNET_new(struct client_ctx);
561 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
562 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
565 // How many peers do we give back?
566 // Wait until we have enough random peers?
568 num_peers = GNUNET_ntohll (msg->num_peers);
570 ev = GNUNET_MQ_msg_extra (out_msg,
571 num_peers * sizeof (struct GNUNET_PeerIdentity),
572 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
573 out_msg->num_peers = msg->num_peers; // No conversion between network and network order
575 //&out_msg[1] = RPS_sampler_get_n_rand_peers (num_peers);
576 peers = RPS_sampler_get_n_rand_peers (num_peers);
579 num_peers * sizeof (struct GNUNET_PeerIdentity));
581 GNUNET_MQ_send (cli_ctx->mq, ev);
582 //GNUNET_MQ_destroy(mq);
584 GNUNET_SERVER_receive_done (client,
589 * Handle a PUSH message from another peer.
591 * Check the proof of work and store the PeerID
592 * in the temporary list for pushed PeerIDs.
595 * @param channel The channel the PUSH was received over
596 * @param channel_ctx The context associated with this channel
597 * @param msg The message header
600 handle_peer_push (void *cls,
601 struct GNUNET_CADET_Channel *channel,
603 const struct GNUNET_MessageHeader *msg)
605 const struct GNUNET_PeerIdentity *peer;
607 // (check the proof of work)
609 // TODO accept empty message
610 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PushMessage))
612 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
613 return GNUNET_SYSERR;
616 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
617 // FIXME wait for cadet to change this function
618 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
620 /* Add the sending peer to the push_list */
621 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
622 GNUNET_array_append (push_list, push_list_size, *peer);
628 * Handle PULL REQUEST request message from another peer.
630 * Reply with the gossip list of PeerIDs.
633 * @param channel The channel the PUSH was received over
634 * @param channel_ctx The context associated with this channel
635 * @param msg The message header
638 handle_peer_pull_request (void *cls,
639 struct GNUNET_CADET_Channel *channel,
641 const struct GNUNET_MessageHeader *msg)
643 struct GNUNET_PeerIdentity *peer;
644 struct GNUNET_MQ_Handle *mq;
645 struct GNUNET_MQ_Envelope *ev;
646 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
648 // assert that msg->size is 0
650 // TODO accept empty message
651 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PullRequestMessage))
653 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
654 return GNUNET_SYSERR;
657 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
658 // FIXME wait for cadet to change this function
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s (peer));
661 mq = get_mq (peer_map, peer);
663 ev = GNUNET_MQ_msg_extra (out_msg,
664 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
665 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
666 out_msg->num_peers = GNUNET_htonll (gossip_list_size);
667 memcpy (&out_msg[1], gossip_list,
668 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
670 GNUNET_MQ_send (mq, ev);
676 * Handle PULL REPLY message from another peer.
678 * Check whether we sent a corresponding request and
679 * whether this reply is the first one.
682 * @param channel The channel the PUSH was received over
683 * @param channel_ctx The context associated with this channel
684 * @param msg The message header
687 handle_peer_pull_reply (void *cls,
688 struct GNUNET_CADET_Channel *channel,
690 const struct GNUNET_MessageHeader *msg)
692 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
694 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
695 struct GNUNET_PeerIdentity *peers;
698 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) < ntohs (msg->size))
700 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
701 return GNUNET_SYSERR;
703 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
704 if (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) / sizeof (struct GNUNET_PeerIdentity) != GNUNET_ntohll (in_msg->num_peers))
707 return GNUNET_SYSERR;
710 // TODO check that we sent a request and that it is the first reply
712 peers = (struct GNUNET_PeerIdentity *) &msg[1];
713 for ( i = 0 ; i < GNUNET_ntohll (in_msg->num_peers) ; i++ )
715 if (GNUNET_NO == in_arr(pull_list, pull_list_size, &peers[i]))
716 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
719 // TODO check that id is valid - whether it is reachable
726 * Send out PUSHes and PULLs.
728 * This is executed regylary.
731 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
733 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
736 //unsigned int *n_arr;
737 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
738 struct GNUNET_RPS_P2P_PushMessage *push_msg;
739 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
740 struct GNUNET_MQ_Envelope *ev;
741 const struct GNUNET_PeerIdentity *peer;
742 struct GNUNET_MQ_Handle *mq;
744 // TODO print lists, ...
745 // TODO randomise and spread calls herein over time
748 /* Would it make sense to have one shuffeled gossip list and then
749 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
750 * use the rest to update sampler?
751 * in essence get random peers with consumption */
754 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
755 n_peers = round (alpha * gossip_list_size);
758 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
759 n_peers, alpha, gossip_list_size);
760 for ( i = 0 ; i < n_peers ; i++ )
762 peer = get_rand_peer (gossip_list, gossip_list_size);
763 if (own_identity != peer)
764 { // FIXME if this fails schedule/loop this for later
765 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
767 ev = GNUNET_MQ_msg (push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
769 // FIXME sometimes it returns a pointer to a freed mq
770 mq = get_mq (peer_map, peer);
771 GNUNET_MQ_send (mq, ev);
776 /* Send PULL requests */
777 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
778 n_peers = round (beta * gossip_list_size);
781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
782 n_peers, beta, gossip_list_size);
783 for ( i = 0 ; i < n_peers ; i++ )
785 peer = get_rand_peer (gossip_list, gossip_list_size);
786 if (own_identity != peer)
787 { // FIXME if this fails schedule/loop this for later
788 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
790 ev = GNUNET_MQ_msg (pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
792 mq = get_mq (peer_map, peer);
793 GNUNET_MQ_send (mq, ev);
798 /* Update gossip list */
801 if ( push_list_size <= alpha * gossip_list_size &&
802 push_list_size != 0 &&
803 pull_list_size != 0 )
805 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
807 uint64_t first_border;
808 uint64_t second_border;
810 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
812 first_border = round(alpha * gossip_list_size);
813 for ( i = 0 ; i < first_border ; i++ )
814 { // TODO use RPS_sampler_get_n_rand_peers
815 /* Update gossip list with peers received through PUSHes */
816 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
818 gossip_list[i] = push_list[r_index];
819 // TODO change the in_flags accordingly
822 second_border = first_border + round(beta * gossip_list_size);
823 for ( i = first_border ; i < second_border ; i++ )
825 /* Update gossip list with peers received through PULLs */
826 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
828 gossip_list[i] = pull_list[r_index];
829 // TODO change the in_flags accordingly
832 for ( i = second_border ; i < gossip_list_size ; i++ )
834 /* Update gossip list with peers from history */
835 peer = RPS_sampler_get_n_rand_peers (1),
836 gossip_list[i] = *peer;
837 // TODO change the in_flags accordingly
843 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
845 // TODO independent of that also get some peers from CADET_get_peers()?
848 /* Update samplers */
850 for ( i = 0 ; i < push_list_size ; i++ )
852 RPS_sampler_update_list (&push_list[i]);
856 for ( i = 0 ; i < pull_list_size ; i++ )
858 RPS_sampler_update_list (&pull_list[i]);
863 /* Empty push/pull lists */
864 GNUNET_array_grow (push_list, push_list_size, 0);
865 GNUNET_array_grow (pull_list, pull_list_size, 0);
867 struct GNUNET_TIME_Relative time_next_round;
868 struct GNUNET_TIME_Relative half_round_interval;
869 unsigned int rand_delay;
871 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
872 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
876 * Compute random value between (0 and 1) * round_interval
877 * via multiplying round_interval with a 'fraction' (0 to value)/value
879 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
880 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
881 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
882 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
883 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
885 /* Schedule next round */
886 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
892 * Open a connection to given peer and store channel and mq.
895 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
897 // We open a channel to be notified when this peer goes down.
898 (void) get_channel (peer_map, id);
903 * Close the connection to given peer and delete channel and mq.
906 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
909 struct peer_context *ctx;
911 s = RPS_sampler_count_id (id);
914 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
916 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
917 if (NULL != ctx->to_channel)
921 GNUNET_MQ_destroy (ctx->mq);
923 // may already be freed at shutdown of cadet
924 //GNUNET_CADET_channel_destroy (ctx->to_channel);
927 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
933 rps_start (struct GNUNET_SERVER_Handle *server);
936 * This is called from GNUNET_CADET_get_peers().
938 * It is called on every peer(ID) that cadet somehow has contact with.
939 * We use those to initialise the sampler.
942 init_peer_cb (void *cls,
943 const struct GNUNET_PeerIdentity *peer,
944 int tunnel, // "Do we have a tunnel towards this peer?"
945 unsigned int n_paths, // "Number of known paths towards this peer"
946 unsigned int best_path) // "How long is the best path?
947 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
949 struct init_peer_cls *ipc;
951 ipc = (struct init_peer_cls *) cls;
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 "Got %" PRIX32 ". peer %s (at %p) from CADET (gossip_list_size: %u)\n",
956 ipc->i, GNUNET_i2s (peer), peer, gossip_list_size);
957 RPS_sampler_update_list (peer);
958 (void) get_peer_ctx (peer_map, peer); // unneeded? -> insertCB
960 if (ipc->i < gossip_list_size)
962 gossip_list[ipc->i] = *peer; // FIXME sometimes we're writing to invalid space here
963 // not sure whether fixed
967 // send push/pull to each of those peers?
971 if (ipc->i < gossip_list_size)
973 memcpy(&gossip_list[ipc->i],
974 RPS_sampler_get_n_rand_peers (1),
975 (gossip_list_size - ipc->i) * sizeof(struct GNUNET_PeerIdentity));
977 rps_start (ipc->server);
984 * Task run during shutdown.
990 shutdown_task (void *cls,
991 const struct GNUNET_SCHEDULER_TaskContext *tc)
993 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
995 if ( NULL != do_round_task )
997 GNUNET_SCHEDULER_cancel (do_round_task);
998 do_round_task = NULL;
1001 GNUNET_NSE_disconnect (nse);
1002 GNUNET_CADET_disconnect (cadet_handle);
1003 GNUNET_free (own_identity);
1004 RPS_sampler_destroy ();
1005 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
1006 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1007 GNUNET_array_grow (push_list, push_list_size, 0);
1008 GNUNET_array_grow (pull_list, pull_list_size, 0);
1013 * A client disconnected. Remove all of its data structure entries.
1015 * @param cls closure, NULL
1016 * @param client identification of the client
1019 handle_client_disconnect (void *cls,
1020 struct GNUNET_SERVER_Client * client)
1025 * Handle the channel a peer opens to us.
1027 * @param cls The closure
1028 * @param channel The channel the peer wants to establish
1029 * @param initiator The peer's peer ID
1030 * @param port The port the channel is being established over
1031 * @param options Further options
1034 handle_inbound_channel (void *cls,
1035 struct GNUNET_CADET_Channel *channel,
1036 const struct GNUNET_PeerIdentity *initiator,
1038 enum GNUNET_CADET_ChannelOption options)
1040 struct peer_context *ctx;
1042 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us (Peer %s).\n", GNUNET_i2s(initiator));
1044 GNUNET_assert( NULL != channel );
1046 // we might not even store the from_channel
1048 ctx = get_peer_ctx(peer_map, initiator);
1049 if (NULL != ctx->from_channel)
1051 ctx->from_channel = channel;
1054 // FIXME there might already be an established channel
1056 //ctx->in_flags = in_other_gossip_list;
1057 ctx->mq = NULL; // TODO create mq?
1059 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
1060 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1061 return NULL; // TODO
1065 * This is called when a remote peer destroys a channel.
1067 * @param cls The closure
1068 * @param channel The channel being closed
1069 * @param channel_ctx The context associated with this channel
1072 cleanup_channel(void *cls,
1073 const struct GNUNET_CADET_Channel *channel,
1076 struct GNUNET_PeerIdentity *peer;
1077 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
1079 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1080 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1081 // Guess simply casting isn't the nicest way...
1082 // FIXME wait for cadet to change this function
1083 RPS_sampler_reinitialise_by_value (peer);
1087 * Actually start the service.
1090 rps_start (struct GNUNET_SERVER_Handle *server)
1092 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1093 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1094 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1098 GNUNET_SERVER_add_handlers (server, handlers);
1099 GNUNET_SERVER_disconnect_notify (server,
1100 &handle_client_disconnect,
1102 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1105 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1106 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1108 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1115 * Process statistics requests.
1117 * @param cls closure
1118 * @param server the initialized server
1119 * @param c configuration to use
1123 struct GNUNET_SERVER_Handle *server,
1124 const struct GNUNET_CONFIGURATION_Handle *c)
1126 // TODO check what this does -- copied from gnunet-boss
1127 // - seems to work as expected
1128 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1130 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1132 struct init_peer_cls *ipc;
1138 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1139 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1140 GNUNET_assert (NULL != own_identity);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1144 /* Get time interval from the configuration */
1145 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1150 GNUNET_SCHEDULER_shutdown();
1154 /* Get initial size of sampler/gossip list from the configuration */
1155 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1157 (long long unsigned int *) &est_size))
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1160 GNUNET_SCHEDULER_shutdown ();
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1165 //gossip_list_size = est_size; // TODO rename est_size
1168 GNUNET_array_grow (gossip_list, gossip_list_size, est_size);
1171 /* connect to NSE */
1172 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1173 // TODO check whether that was successful
1174 // TODO disconnect on shutdown
1175 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1180 // TODO initialise thresholds - ?
1182 /* Get alpha from the configuration */
1183 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1187 LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1189 LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1191 /* Get beta from the configuration */
1192 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1200 // TODO check that alpha + beta < 1
1202 peer_map = GNUNET_CONTAINER_multipeermap_create (est_size, GNUNET_NO);
1205 /* Initialise cadet */
1206 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1207 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1208 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1209 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1213 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1214 cadet_handle = GNUNET_CADET_connect (cfg,
1216 &handle_inbound_channel,
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1223 /* Initialise sampler */
1224 RPS_sampler_init (est_size, own_identity, insertCB, NULL, removeCB, NULL);
1226 /* Initialise push and pull maps */
1233 ipc = GNUNET_new (struct init_peer_cls);
1234 ipc->server = server;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1237 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, ipc);
1239 // TODO send push/pull to each of those peers?
1244 * The main function for the rps service.
1246 * @param argc number of arguments from the command line
1247 * @param argv command line arguments
1248 * @return 0 ok, 1 on error
1251 main (int argc, char *const *argv)
1253 return (GNUNET_OK ==
1254 GNUNET_SERVICE_run (argc,
1257 GNUNET_SERVICE_OPTION_NONE,
1258 &run, NULL)) ? 0 : 1;
1261 /* end of gnunet-service-rps.c */