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 check for overflows
43 // TODO align message structs
45 // (TODO api -- possibility of getting weak random peer immideately)
47 // TODO malicious peer
49 // TODO connect to friends
51 // TODO store peers somewhere
55 // hist_size_init, hist_size_max
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_PeerIdentity own_identity;
68 struct GNUNET_PeerIdentity *
69 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
70 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
73 /***********************************************************************
74 * Housekeeping with peers
75 ***********************************************************************/
78 * Struct used to store the context of a connected client.
83 * The message queue to communicate with the client.
85 struct GNUNET_MQ_Handle *mq;
89 * Used to keep track in what lists single peerIDs are.
93 PULL_REPLY_PENDING = 0x01,
94 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
95 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
96 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
99 * We set this bit when we can be sure the other peer is/was live.
106 * Functions of this type can be used to be stored at a peer for later execution.
108 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
111 * Outstanding operation on peer consisting of callback and closure
113 struct PeerOutstandingOp
128 * Struct used to keep track of other peer's status
130 * This is stored in a multipeermap.
135 * In own gossip/sampler list, in other's gossip/sampler list
140 * Message queue open to client
142 struct GNUNET_MQ_Handle *mq;
145 * Channel open to client.
147 struct GNUNET_CADET_Channel *send_channel;
150 * Channel open from client.
152 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
155 * Array of outstanding operations on this peer.
157 struct PeerOutstandingOp *outstanding_ops;
160 * Number of outstanding operations.
162 unsigned int num_outstanding_ops;
163 //size_t num_outstanding_ops;
166 * Handle to the callback given to cadet_ntfy_tmt_rdy()
168 * To be canceled on shutdown.
170 struct GNUNET_CADET_TransmitHandle *is_live_task;
173 * Identity of the peer
175 struct GNUNET_PeerIdentity peer_id;
178 * This is pobably followed by 'statistical' data (when we first saw
179 * him, how did we get his ID, how many pushes (in a timeinterval),
184 /***********************************************************************
185 * /Housekeeping with peers
186 ***********************************************************************/
192 /***********************************************************************
194 ***********************************************************************/
197 * Sampler used for the Brahms protocol itself.
199 static struct RPS_Sampler *prot_sampler;
202 * Sampler used for the clients.
204 static struct RPS_Sampler *client_sampler;
207 * Set of all peers to keep track of them.
209 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
213 * The gossiped list of peers.
215 static struct GNUNET_PeerIdentity *gossip_list;
218 * Size of the gossiped list
220 //static unsigned int gossip_list_size;
221 static uint32_t gossip_list_size;
225 * The size of sampler we need to be able to satisfy the client's need of
228 static unsigned int sampler_size_client_need;
231 * The size of sampler we need to be able to satisfy the Brahms protocol's
232 * need of random peers.
234 * This is directly taken as the #gossip_list_size on update of the
237 * This is one minimum size the sampler grows to.
239 static unsigned int sampler_size_est_need;
243 * Percentage of total peer number in the gossip list
244 * to send random PUSHes to
249 * Percentage of total peer number in the gossip list
250 * to send random PULLs to
255 * The percentage gamma of history updates.
256 * Simply 1 - alpha - beta
261 * Identifier for the main task that runs periodically.
263 static struct GNUNET_SCHEDULER_Task *do_round_task;
266 * Time inverval the do_round task runs in.
268 static struct GNUNET_TIME_Relative round_interval;
273 * List to store peers received through pushes temporary.
275 * TODO -> multipeermap
277 static struct GNUNET_PeerIdentity *push_list;
280 * Size of the push_list;
282 static unsigned int push_list_size;
283 //size_t push_list_size;
286 * List to store peers received through pulls temporary.
288 * TODO -> multipeermap
290 static struct GNUNET_PeerIdentity *pull_list;
293 * Size of the pull_list;
295 static unsigned int pull_list_size;
296 //size_t pull_list_size;
302 static struct GNUNET_NSE_Handle *nse;
307 static struct GNUNET_CADET_Handle *cadet_handle;
313 * Only needed in the beginning to check how many of the 64 deltas
316 static unsigned int req_counter;
319 * Time of the last request we received.
321 * Used to compute the expected request rate.
323 static struct GNUNET_TIME_Absolute last_request;
326 * Size of #request_deltas.
328 #define REQUEST_DELTAS_SIZE 64
329 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
332 * Last 64 deltas between requests
334 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
337 * The prediction of the rate of requests
339 static struct GNUNET_TIME_Relative request_rate;
343 * List with the peers we sent requests to.
345 struct GNUNET_PeerIdentity *pending_pull_reply_list;
348 * Size of #pending_pull_reply_list.
350 uint32_t pending_pull_reply_list_size;
354 * Number of history update tasks.
356 uint32_t num_hist_update_tasks;
359 /***********************************************************************
361 ***********************************************************************/
368 /***********************************************************************
370 ***********************************************************************/
373 * Set a peer flag of given peer context.
375 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
378 * Get peer flag of given peer context.
380 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
383 * Unset flag of given peer context.
385 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
389 * Clean the send channel of a peer
392 peer_clean (const struct GNUNET_PeerIdentity *peer);
396 * Check if peer is already in peer array.
399 in_arr (const struct GNUNET_PeerIdentity *array,
400 unsigned int arr_size,
401 const struct GNUNET_PeerIdentity *peer)
403 GNUNET_assert (NULL != peer);
408 GNUNET_assert (NULL != array);
412 for (i = 0; i < arr_size ; i++)
413 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
420 * Print peerlist to log.
423 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
427 LOG (GNUNET_ERROR_TYPE_DEBUG,
428 "Printing peer list of length %u at %p:\n",
431 for (i = 0 ; i < len ; i++)
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 i, GNUNET_i2s (&list[i]));
441 * Remove peer from list.
444 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
445 unsigned int *list_size,
446 const struct GNUNET_PeerIdentity *peer)
449 struct GNUNET_PeerIdentity *tmp;
453 LOG (GNUNET_ERROR_TYPE_DEBUG,
454 "Removing peer %s from list at %p\n",
457 print_peer_list (tmp, *list_size);
459 for ( i = 0 ; i < *list_size ; i++ )
461 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
463 if (i < *list_size -1)
464 { /* Not at the last entry -- shift peers left */
465 memcpy (&tmp[i], &tmp[i +1],
466 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
468 /* Remove last entry (should be now useless PeerID) */
469 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
476 * Get random peer from the given list but don't return one from the @a ignore_list.
478 struct GNUNET_PeerIdentity *
479 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
481 const struct GNUNET_PeerIdentity *ignore_list,
482 uint32_t ignore_size)
486 struct GNUNET_PeerIdentity *tmp_peer_list;
487 struct GNUNET_PeerIdentity *peer;
489 GNUNET_assert (NULL != peer_list);
494 tmp_peer_list = NULL;
495 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
496 memcpy (tmp_peer_list,
498 list_size * sizeof (struct GNUNET_PeerIdentity));
499 peer = GNUNET_new (struct GNUNET_PeerIdentity);
502 * Choose the r_index of the peer we want to return
503 * at random from the interval of the gossip list
505 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
507 *peer = tmp_peer_list[r_index];
509 while (in_arr (ignore_list, ignore_size, peer))
511 rem_from_list (&tmp_peer_list, &tmp_size, peer);
513 print_peer_list (tmp_peer_list, tmp_size);
522 * Choose the r_index of the peer we want to return
523 * at random from the interval of the gossip list
525 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
527 *peer = tmp_peer_list[r_index];
531 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
538 * Get the context of a peer. If not existing, create.
541 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
542 const struct GNUNET_PeerIdentity *peer)
544 struct PeerContext *ctx;
546 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
548 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
552 ctx = GNUNET_new (struct PeerContext);
555 ctx->send_channel = NULL;
556 ctx->recv_channel = NULL;
557 ctx->outstanding_ops = NULL;
558 ctx->num_outstanding_ops = 0;
559 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
567 * Put random peer from sampler into the gossip list as history update.
570 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
572 GNUNET_assert (1 == num_peers);
574 if (gossip_list_size < sampler_size_est_need)
575 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
577 if (0 < num_hist_update_tasks)
578 num_hist_update_tasks--;
583 * Set the peer flag to living and call the outstanding operations on this peer.
586 peer_is_live (struct PeerContext *peer_ctx)
588 struct GNUNET_PeerIdentity *peer;
590 peer = &peer_ctx->peer_id;
591 set_peer_flag (peer_ctx, VALID);
593 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
595 if (0 != peer_ctx->num_outstanding_ops)
596 { /* Call outstanding operations */
599 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
600 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
601 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
609 * Callback that is called when a channel was effectively established.
610 * This is given to ntfy_tmt_rdy and called when the channel was
611 * successfully established.
614 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
616 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
620 peer_is_live (peer_ctx);
622 //if (NULL != peer_ctx->is_live_task)
624 // LOG (GNUNET_ERROR_TYPE_DEBUG,
625 // "Trying to cancle is_live_task for peer %s\n",
626 // GNUNET_i2s (&peer_ctx->peer_id));
627 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
628 // peer_ctx->is_live_task = NULL;
630 peer_ctx->is_live_task = NULL;
637 * Get the channel of a peer. If not existing, create.
639 struct GNUNET_CADET_Channel *
640 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
641 const struct GNUNET_PeerIdentity *peer)
643 struct PeerContext *ctx;
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Trying to establish channel to peer %s\n",
649 ctx = get_peer_ctx (peer_map, peer);
650 if (NULL == ctx->send_channel)
652 ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle,
655 GNUNET_RPS_CADET_PORT,
656 GNUNET_CADET_OPTION_RELIABLE);
658 /* If we don't know whether peer is live,
659 * get notified when we know it is live. */
660 if (NULL == ctx->recv_channel
661 && NULL == ctx->is_live_task)
663 ctx->peer_id = *peer;
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "Get informed about peer %s getting live\n",
668 GNUNET_CADET_notify_transmit_ready (ctx->send_channel,
670 GNUNET_TIME_UNIT_FOREVER_REL,
671 sizeof (struct GNUNET_MessageHeader),
672 cadet_ntfy_tmt_rdy_cb,
675 // FIXME check whether this is NULL
677 // do I have to explicitly put it in the peer_map?
678 (void) GNUNET_CONTAINER_multipeermap_put
682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
684 return ctx->send_channel;
689 * Get the message queue of a specific peer.
691 * If we already have a message queue open to this client,
692 * simply return it, otherways create one.
694 struct GNUNET_MQ_Handle *
695 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
696 const struct GNUNET_PeerIdentity *peer_id)
698 struct PeerContext *peer_ctx;
700 peer_ctx = get_peer_ctx (peer_map, peer_id);
702 GNUNET_assert (NULL == peer_ctx->is_live_task);
704 if (NULL == peer_ctx->mq)
706 (void) get_channel (peer_map, peer_id);
707 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
708 //do I have to explicitly put it in the peer_map?
709 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
710 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
717 * Sum all time relatives of an array.
719 struct GNUNET_TIME_Relative
720 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
722 struct GNUNET_TIME_Relative sum;
725 sum = GNUNET_TIME_UNIT_ZERO;
726 for ( i = 0 ; i < arr_size ; i++ )
728 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
735 * Compute the average of given time relatives.
737 struct GNUNET_TIME_Relative
738 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
740 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
745 * Insert PeerID in #pull_list
747 * Called once we know a peer is live.
750 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
752 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
753 GNUNET_array_append (pull_list, pull_list_size, *peer);
759 * Check whether #insert_in_pull_list was already scheduled
762 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
766 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
767 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
774 * Insert PeerID in #gossip_list
776 * Called once we know a peer is live.
779 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
781 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
782 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
786 * Check whether #insert_in_pull_list was already scheduled
789 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
793 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
794 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
801 * Update sampler with given PeerID.
804 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
806 RPS_sampler_update (prot_sampler, peer);
807 RPS_sampler_update (client_sampler, peer);
812 * Check whether #insert_in_sampler was already scheduled
815 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
819 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
820 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
827 * Wrapper around #RPS_sampler_resize()
829 * If we do not have enough sampler elements, double current sampler size
830 * If we have more than enough sampler elements, halv current sampler size
833 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
835 unsigned int sampler_size;
838 // TODO respect the min, max
839 sampler_size = RPS_sampler_get_size (sampler);
840 if (sampler_size > new_size * 4)
842 RPS_sampler_resize (sampler, sampler_size / 2);
844 else if (sampler_size < new_size)
846 RPS_sampler_resize (sampler, sampler_size * 2);
848 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
853 * Wrapper around #RPS_sampler_resize() resizing the client sampler
856 client_resize_wrapper ()
858 uint32_t bigger_size;
859 unsigned int sampler_size;
863 sampler_size = RPS_sampler_get_size (client_sampler);
865 if (sampler_size_est_need > sampler_size_client_need)
866 bigger_size = sampler_size_est_need;
868 bigger_size = sampler_size_client_need;
870 // TODO respect the min, max
871 resize_wrapper (client_sampler, bigger_size);
872 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
877 * Estimate request rate
879 * Called every time we receive a request from the client.
884 struct GNUNET_TIME_Relative max_round_duration;
886 if (request_deltas_size > req_counter)
888 if ( 1 < req_counter)
890 /* Shift last request deltas to the right */
891 memcpy (&request_deltas[1],
893 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
895 /* Add current delta to beginning */
897 GNUNET_TIME_absolute_get_difference (last_request,
898 GNUNET_TIME_absolute_get ());
899 request_rate = T_relative_avg (request_deltas, req_counter);
901 /* Compute the duration a round will maximally take */
903 GNUNET_TIME_relative_add (round_interval,
904 GNUNET_TIME_relative_divide (round_interval, 2));
906 /* Set the estimated size the sampler has to have to
907 * satisfy the current client request rate */
908 sampler_size_client_need =
909 max_round_duration.rel_value_us / request_rate.rel_value_us;
911 /* Resize the sampler */
912 client_resize_wrapper ();
914 last_request = GNUNET_TIME_absolute_get ();
918 /***********************************************************************
920 ***********************************************************************/
927 * Function called by NSE.
929 * Updates sizes of sampler list and gossip list and adapt those lists
933 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
934 double logestimate, double std_dev)
937 //double scale; // TODO this might go gloabal/config
939 LOG (GNUNET_ERROR_TYPE_DEBUG,
940 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
941 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
943 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
944 // GNUNET_NSE_log_estimate_to_n (logestimate);
945 estimate = pow (estimate, 1.0 / 3);
946 // TODO add if std_dev is a number
947 // estimate += (std_dev * scale);
948 if (2 < ceil (estimate))
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
951 sampler_size_est_need = estimate;
953 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
955 /* If the NSE has changed adapt the lists accordingly */
956 resize_wrapper (prot_sampler, sampler_size_est_need);
957 client_resize_wrapper ();
962 * Callback called once the requested PeerIDs are ready.
964 * Sends those to the requesting client.
966 void client_respond (void *cls,
967 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
969 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
970 struct GNUNET_MQ_Envelope *ev;
971 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
972 struct GNUNET_SERVER_Client *client;
973 uint32_t size_needed;
974 struct client_ctx *cli_ctx;
976 client = (struct GNUNET_SERVER_Client *) cls;
978 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
979 num_peers * sizeof (struct GNUNET_PeerIdentity);
981 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
983 ev = GNUNET_MQ_msg_extra (out_msg,
984 num_peers * sizeof (struct GNUNET_PeerIdentity),
985 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
986 out_msg->num_peers = htonl (num_peers);
990 num_peers * sizeof (struct GNUNET_PeerIdentity));
993 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
994 if ( NULL == cli_ctx ) {
995 cli_ctx = GNUNET_new (struct client_ctx);
996 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
997 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
1000 GNUNET_MQ_send (cli_ctx->mq, ev);
1005 * Handle RPS request from the client.
1007 * @param cls closure
1008 * @param client identification of the client
1009 * @param message the actual message
1012 handle_client_request (void *cls,
1013 struct GNUNET_SERVER_Client *client,
1014 const struct GNUNET_MessageHeader *message)
1016 struct GNUNET_RPS_CS_RequestMessage *msg;
1018 uint32_t size_needed;
1021 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1023 num_peers = ntohl (msg->num_peers);
1024 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1025 num_peers * sizeof (struct GNUNET_PeerIdentity);
1027 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1033 for (i = 0 ; i < num_peers ; i++)
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
1038 RPS_sampler_get_n_rand_peers (client_sampler, client_respond,
1039 client, num_peers, GNUNET_YES);
1041 GNUNET_SERVER_receive_done (client,
1047 * Handle seed from the client.
1049 * @param cls closure
1050 * @param client identification of the client
1051 * @param message the actual message
1054 handle_client_seed (void *cls,
1055 struct GNUNET_SERVER_Client *client,
1056 const struct GNUNET_MessageHeader *message)
1058 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1059 struct GNUNET_PeerIdentity *peers;
1062 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
1064 GNUNET_break_op (0);
1065 GNUNET_SERVER_receive_done (client,
1068 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1069 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1070 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1072 GNUNET_break_op (0);
1073 GNUNET_SERVER_receive_done (client,
1077 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1078 peers = (struct GNUNET_PeerIdentity *) &message[1];
1080 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1081 RPS_sampler_update (prot_sampler, &peers[i]);
1082 RPS_sampler_update (client_sampler, &peers[i]);
1084 GNUNET_SERVER_receive_done (client,
1090 * Handle a PUSH message from another peer.
1092 * Check the proof of work and store the PeerID
1093 * in the temporary list for pushed PeerIDs.
1095 * @param cls Closure
1096 * @param channel The channel the PUSH was received over
1097 * @param channel_ctx The context associated with this channel
1098 * @param msg The message header
1101 handle_peer_push (void *cls,
1102 struct GNUNET_CADET_Channel *channel,
1104 const struct GNUNET_MessageHeader *msg)
1106 const struct GNUNET_PeerIdentity *peer;
1108 // (check the proof of work)
1110 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1111 // FIXME wait for cadet to change this function
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1114 /* Add the sending peer to the push_list */
1115 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
1116 GNUNET_array_append (push_list, push_list_size, *peer);
1122 * Handle PULL REQUEST request message from another peer.
1124 * Reply with the gossip list of PeerIDs.
1126 * @param cls Closure
1127 * @param channel The channel the PUSH was received over
1128 * @param channel_ctx The context associated with this channel
1129 * @param msg The message header
1132 handle_peer_pull_request (void *cls,
1133 struct GNUNET_CADET_Channel *channel,
1135 const struct GNUNET_MessageHeader *msg)
1137 struct GNUNET_PeerIdentity *peer;
1139 struct GNUNET_MQ_Handle *mq;
1140 struct GNUNET_MQ_Envelope *ev;
1141 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1144 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
1145 GNUNET_CADET_OPTION_PEER);
1146 // FIXME wait for cadet to change this function
1148 /* Compute actual size */
1149 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1150 gossip_list_size * sizeof (struct GNUNET_PeerIdentity);
1152 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1153 /* Compute number of peers to send
1154 * If too long, simply truncate */
1156 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1157 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1158 sizeof (struct GNUNET_PeerIdentity);
1160 send_size = gossip_list_size;
1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1163 "PULL REQUEST from peer %s received, going to send %u peers\n",
1164 GNUNET_i2s (peer), send_size);
1166 mq = get_mq (peer_map, peer);
1168 ev = GNUNET_MQ_msg_extra (out_msg,
1169 send_size * sizeof (struct GNUNET_PeerIdentity),
1170 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1171 //out_msg->num_peers = htonl (gossip_list_size);
1172 out_msg->num_peers = htonl (send_size);
1173 memcpy (&out_msg[1], gossip_list,
1174 send_size * sizeof (struct GNUNET_PeerIdentity));
1176 GNUNET_MQ_send (mq, ev);
1182 * Handle PULL REPLY message from another peer.
1184 * Check whether we sent a corresponding request and
1185 * whether this reply is the first one.
1187 * @param cls Closure
1188 * @param channel The channel the PUSH was received over
1189 * @param channel_ctx The context associated with this channel
1190 * @param msg The message header
1193 handle_peer_pull_reply (void *cls,
1194 struct GNUNET_CADET_Channel *channel,
1196 const struct GNUNET_MessageHeader *msg)
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1200 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1201 struct GNUNET_PeerIdentity *peers;
1202 struct PeerContext *peer_ctx;
1203 struct GNUNET_PeerIdentity *sender;
1204 struct PeerContext *sender_ctx;
1205 struct PeerOutstandingOp out_op;
1208 /* Check for protocol violation */
1209 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1211 GNUNET_break_op (0);
1212 return GNUNET_SYSERR;
1214 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1215 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1216 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1218 LOG (GNUNET_ERROR_TYPE_ERROR,
1219 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1220 ntohl (in_msg->num_peers),
1221 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1222 sizeof (struct GNUNET_PeerIdentity));
1223 GNUNET_break_op (0);
1224 return GNUNET_SYSERR;
1227 /* Do actual logic */
1228 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1229 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1230 // Guess simply casting isn't the nicest way...
1231 // FIXME wait for cadet to change this function
1232 sender_ctx = get_peer_ctx (peer_map, sender);
1234 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1236 GNUNET_break_op (0);
1240 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1241 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1243 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1244 if (NULL != peer_ctx->send_channel
1245 || NULL != peer_ctx->recv_channel)
1247 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1248 && GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1249 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1251 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1253 out_op.op = insert_in_pull_list;
1254 out_op.op_cls = NULL;
1255 GNUNET_array_append (peer_ctx->outstanding_ops,
1256 peer_ctx->num_outstanding_ops,
1261 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1262 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1269 * Send out PUSHes and PULLs.
1271 * This is executed regylary.
1274 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1276 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1279 unsigned int *permut;
1280 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1281 struct GNUNET_MQ_Envelope *ev;
1282 struct GNUNET_PeerIdentity peer;
1283 struct GNUNET_PeerIdentity *tmp_peer;
1284 struct GNUNET_MQ_Handle *mq;
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "Printing gossip list:\n");
1288 for (i = 0 ; i < gossip_list_size ; i++)
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1291 // TODO log lists, ...
1293 /* Would it make sense to have one shuffeled gossip list and then
1294 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1295 * use the rest to update sampler?
1296 * in essence get random peers with consumption */
1299 if (0 < gossip_list_size)
1301 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1302 (unsigned int) gossip_list_size);
1303 n_peers = ceil (alpha * gossip_list_size);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "Going to send pushes to %u ceil (%f * %u) peers.\n",
1306 n_peers, alpha, gossip_list_size);
1307 for (i = 0 ; i < n_peers ; i++)
1309 peer = gossip_list[permut[i]];
1310 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1311 { // FIXME if this fails schedule/loop this for later
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "Sending PUSH to peer %s of gossiped list.\n",
1314 GNUNET_i2s (&peer));
1316 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1317 mq = get_mq (peer_map, &peer);
1318 GNUNET_MQ_send (mq, ev);
1321 GNUNET_free (permut);
1325 /* Send PULL requests */
1326 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1327 n_peers = ceil (beta * gossip_list_size);
1328 LOG (GNUNET_ERROR_TYPE_DEBUG,
1329 "Going to send pulls to %u ceil (%f * %u) peers.\n",
1330 n_peers, beta, gossip_list_size);
1331 for (i = 0 ; i < n_peers ; i++)
1333 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1334 pending_pull_reply_list, pending_pull_reply_list_size);
1335 if (NULL != tmp_peer)
1338 GNUNET_free (tmp_peer);
1340 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, peer);
1342 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
1343 { // FIXME if this fails schedule/loop this for later
1344 LOG (GNUNET_ERROR_TYPE_DEBUG,
1345 "Sending PULL request to peer %s of gossiped list.\n",
1346 GNUNET_i2s (&peer));
1348 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1349 mq = get_mq (peer_map, &peer);
1350 GNUNET_MQ_send (mq, ev);
1356 /* Update gossip list */
1358 if ( push_list_size <= alpha * gossip_list_size &&
1359 push_list_size != 0 &&
1360 pull_list_size != 0 )
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1364 uint32_t first_border;
1365 uint32_t second_border;
1367 uint32_t peers_to_clean_size;
1368 struct GNUNET_PeerIdentity *peers_to_clean;
1370 peers_to_clean = NULL;
1371 peers_to_clean_size = 0;
1372 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
1373 memcpy (peers_to_clean,
1375 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
1377 first_border = ceil (alpha * sampler_size_est_need);
1378 second_border = first_border + ceil (beta * sampler_size_est_need);
1380 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1382 for (i = 0 ; i < first_border ; i++)
1383 { // TODO use RPS_sampler_get_n_rand_peers
1384 /* Update gossip list with peers received through PUSHes */
1385 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1387 gossip_list[i] = push_list[r_index];
1388 // TODO change the peer_flags accordingly
1391 for (i = first_border ; i < second_border ; i++)
1393 /* Update gossip list with peers received through PULLs */
1394 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1396 gossip_list[i] = pull_list[r_index];
1397 // TODO change the peer_flags accordingly
1400 for (i = second_border ; i < sampler_size_est_need ; i++)
1402 /* Update gossip list with peers from history */
1403 RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
1404 num_hist_update_tasks++;
1405 // TODO change the peer_flags accordingly
1408 for (i = 0 ; i < gossip_list_size ; i++)
1409 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
1411 for (i = 0 ; i < peers_to_clean_size ; i++)
1412 peer_clean (&peers_to_clean[i]);
1414 GNUNET_free (peers_to_clean);
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
1420 // TODO independent of that also get some peers from CADET_get_peers()?
1423 /* Update samplers */
1424 for ( i = 0 ; i < push_list_size ; i++ )
1426 RPS_sampler_update (prot_sampler, &push_list[i]);
1427 RPS_sampler_update (client_sampler, &push_list[i]);
1428 // TODO set in_flag?
1431 for ( i = 0 ; i < pull_list_size ; i++ )
1433 RPS_sampler_update (prot_sampler, &push_list[i]);
1434 RPS_sampler_update (client_sampler, &push_list[i]);
1435 // TODO set in_flag?
1439 /* Empty push/pull lists */
1440 GNUNET_array_grow (push_list, push_list_size, 0);
1441 GNUNET_array_grow (pull_list, pull_list_size, 0);
1443 struct GNUNET_TIME_Relative time_next_round;
1444 struct GNUNET_TIME_Relative half_round_interval;
1445 unsigned int rand_delay;
1448 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1449 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1453 * Compute random value between (0 and 1) * round_interval
1454 * via multiplying round_interval with a 'fraction' (0 to value)/value
1456 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1457 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
1458 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
1459 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
1460 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1462 /* Schedule next round */
1463 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1469 rps_start (struct GNUNET_SERVER_Handle *server);
1473 * This is called from GNUNET_CADET_get_peers().
1475 * It is called on every peer(ID) that cadet somehow has contact with.
1476 * We use those to initialise the sampler.
1479 init_peer_cb (void *cls,
1480 const struct GNUNET_PeerIdentity *peer,
1481 int tunnel, // "Do we have a tunnel towards this peer?"
1482 unsigned int n_paths, // "Number of known paths towards this peer"
1483 unsigned int best_path) // "How long is the best path?
1484 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1486 struct GNUNET_SERVER_Handle *server;
1487 struct PeerOutstandingOp out_op;
1488 struct PeerContext *peer_ctx;
1490 server = (struct GNUNET_SERVER_Handle *) cls;
1492 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer))
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1496 GNUNET_i2s (peer), peer, gossip_list_size);
1498 // maybe create a function for that
1499 peer_ctx = get_peer_ctx (peer_map, peer);
1500 // FIXME this peer might already be marked as LIVE
1501 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1503 out_op.op = insert_in_sampler;
1504 out_op.op_cls = NULL;
1505 GNUNET_array_append (peer_ctx->outstanding_ops,
1506 peer_ctx->num_outstanding_ops,
1510 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1512 out_op.op = insert_in_gossip_list;
1513 out_op.op_cls = NULL;
1514 GNUNET_array_append (peer_ctx->outstanding_ops,
1515 peer_ctx->num_outstanding_ops,
1519 /* Trigger livelyness test on peer */
1520 (void) get_channel (peer_map, peer);
1522 // send push/pull to each of those peers?
1524 else if (NULL == peer)
1530 * Clean the send channel of a peer
1533 peer_clean (const struct GNUNET_PeerIdentity *peer)
1535 struct PeerContext *peer_ctx;
1536 struct GNUNET_CADET_Channel *channel;
1538 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
1539 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1541 peer_ctx = get_peer_ctx (peer_map, peer);
1542 if (NULL != peer_ctx->send_channel)
1544 channel = peer_ctx->send_channel;
1545 peer_ctx->send_channel = NULL;
1546 GNUNET_CADET_channel_destroy (channel);
1553 * Callback used to remove peers from the multipeermap.
1556 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1558 struct PeerContext *peer_ctx;
1559 const struct GNUNET_CADET_Channel *channel =
1560 (const struct GNUNET_CADET_Channel *) cls;
1561 struct GNUNET_CADET_Channel *recv;
1562 struct GNUNET_CADET_Channel *send;
1564 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, value))
1566 peer_ctx = (struct PeerContext *) value;
1568 if (0 != peer_ctx->num_outstanding_ops)
1569 GNUNET_array_grow (peer_ctx->outstanding_ops,
1570 peer_ctx->num_outstanding_ops,
1573 if (NULL != peer_ctx->mq)
1574 GNUNET_MQ_destroy (peer_ctx->mq);
1576 if (NULL != peer_ctx->is_live_task)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG,
1579 "Trying to cancle is_live_task for peer %s\n",
1581 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1582 peer_ctx->is_live_task = NULL;
1585 send = peer_ctx->send_channel;
1586 peer_ctx->send_channel = NULL;
1587 recv = peer_ctx->send_channel;
1588 peer_ctx->recv_channel = NULL;
1593 GNUNET_CADET_channel_destroy (send);
1599 GNUNET_CADET_channel_destroy (recv);
1602 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1603 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1605 GNUNET_free (peer_ctx);
1613 * Task run during shutdown.
1619 shutdown_task (void *cls,
1620 const struct GNUNET_SCHEDULER_TaskContext *tc)
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1624 if ( NULL != do_round_task )
1626 GNUNET_SCHEDULER_cancel (do_round_task);
1627 do_round_task = NULL;
1632 if (GNUNET_SYSERR ==
1633 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1634 LOG (GNUNET_ERROR_TYPE_WARNING,
1635 "Iterating over peers to disconnect from them was cancelled\n");
1638 GNUNET_NSE_disconnect (nse);
1639 GNUNET_CADET_disconnect (cadet_handle);
1640 RPS_sampler_destroy (prot_sampler);
1641 RPS_sampler_destroy (client_sampler);
1642 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
1643 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1644 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1645 GNUNET_array_grow (push_list, push_list_size, 0);
1646 GNUNET_array_grow (pull_list, pull_list_size, 0);
1651 * A client disconnected. Remove all of its data structure entries.
1653 * @param cls closure, NULL
1654 * @param client identification of the client
1657 handle_client_disconnect (void *cls,
1658 struct GNUNET_SERVER_Client * client)
1664 * Handle the channel a peer opens to us.
1666 * @param cls The closure
1667 * @param channel The channel the peer wants to establish
1668 * @param initiator The peer's peer ID
1669 * @param port The port the channel is being established over
1670 * @param options Further options
1673 handle_inbound_channel (void *cls,
1674 struct GNUNET_CADET_Channel *channel,
1675 const struct GNUNET_PeerIdentity *initiator,
1677 enum GNUNET_CADET_ChannelOption options)
1679 struct PeerContext *peer_ctx;
1681 LOG (GNUNET_ERROR_TYPE_DEBUG,
1682 "New channel was established to us (Peer %s).\n",
1683 GNUNET_i2s (initiator));
1685 GNUNET_assert (NULL != channel);
1687 // we might not even store the recv_channel
1689 peer_ctx = get_peer_ctx (peer_map, initiator);
1690 // FIXME what do we do if a channel is established twice?
1691 // overwrite? Clean old channel? ...?
1692 //if (NULL != peer_ctx->recv_channel)
1694 // peer_ctx->recv_channel = channel;
1696 peer_ctx->recv_channel = channel;
1698 peer_ctx->mq = NULL;
1700 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, peer_ctx,
1701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1703 peer_is_live (peer_ctx);
1705 return NULL; // TODO
1710 * This is called when a remote peer destroys a channel.
1712 * @param cls The closure
1713 * @param channel The channel being closed
1714 * @param channel_ctx The context associated with this channel
1717 cleanup_channel (void *cls,
1718 const struct GNUNET_CADET_Channel *channel,
1721 struct GNUNET_PeerIdentity *peer;
1722 struct PeerContext *peer_ctx;
1724 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1725 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1726 // Guess simply casting isn't the nicest way...
1727 // FIXME wait for cadet to change this function
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up channel to peer %s\n",
1731 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1733 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1735 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
1738 if (channel == peer_ctx->send_channel)
1739 { /* Peer probably went down */
1740 rem_from_list (&gossip_list, &gossip_list_size, peer);
1741 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
1743 /* Somwewhat {ab,re}use the iterator function */
1744 /* Cast to void is ok, because it's used as void in peer_remove_cb */
1745 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
1748 peer_ctx->recv_channel = NULL;
1754 * Actually start the service.
1757 rps_start (struct GNUNET_SERVER_Handle *server)
1759 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1760 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1761 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1762 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1766 GNUNET_SERVER_add_handlers (server, handlers);
1767 GNUNET_SERVER_disconnect_notify (server,
1768 &handle_client_disconnect,
1770 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1773 num_hist_update_tasks = 0;
1775 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1776 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1778 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1785 * Process statistics requests.
1787 * @param cls closure
1788 * @param server the initialized server
1789 * @param c configuration to use
1793 struct GNUNET_SERVER_Handle *server,
1794 const struct GNUNET_CONFIGURATION_Handle *c)
1796 // TODO check what this does -- copied from gnunet-boss
1797 // - seems to work as expected
1798 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1803 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
1804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1805 "STARTING SERVICE (rps) for peer [%s]\n",
1806 GNUNET_i2s (&own_identity));
1809 /* Get time interval from the configuration */
1810 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1815 GNUNET_SCHEDULER_shutdown ();
1819 /* Get initial size of sampler/gossip list from the configuration */
1820 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1822 (long long unsigned int *) &sampler_size_est_need))
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1825 GNUNET_SCHEDULER_shutdown ();
1828 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1834 /* connect to NSE */
1835 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1836 // TODO check whether that was successful
1837 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1843 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1846 /* Initialise cadet */
1847 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1848 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1849 sizeof (struct GNUNET_MessageHeader)},
1850 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1851 sizeof (struct GNUNET_MessageHeader)},
1852 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1856 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1857 cadet_handle = GNUNET_CADET_connect (cfg,
1859 &handle_inbound_channel,
1863 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1866 /* Initialise sampler */
1867 struct GNUNET_TIME_Relative half_round_interval;
1868 struct GNUNET_TIME_Relative max_round_interval;
1870 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1871 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1873 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
1874 client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
1876 /* Initialise push and pull maps */
1881 pending_pull_reply_list = NULL;
1882 pending_pull_reply_list_size = 0;
1885 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1886 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, server);
1888 // TODO send push/pull to each of those peers?
1893 * The main function for the rps service.
1895 * @param argc number of arguments from the command line
1896 * @param argv command line arguments
1897 * @return 0 ok, 1 on error
1900 main (int argc, char *const *argv)
1902 return (GNUNET_OK ==
1903 GNUNET_SERVICE_run (argc,
1906 GNUNET_SERVICE_OPTION_NONE,
1907 &run, NULL)) ? 0 : 1;
1910 /* end of gnunet-service-rps.c */