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))
390 * Check if peer is already in peer array.
393 in_arr (const struct GNUNET_PeerIdentity *array,
394 unsigned int arr_size,
395 const struct GNUNET_PeerIdentity *peer)
397 GNUNET_assert (NULL != peer);
402 GNUNET_assert (NULL != array);
406 for (i = 0; i < arr_size ; i++)
407 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
414 * Print peerlist to log.
417 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
421 LOG (GNUNET_ERROR_TYPE_DEBUG,
422 "Printing peer list of length %u at %p:\n",
425 for (i = 0 ; i < len ; i++)
427 LOG (GNUNET_ERROR_TYPE_DEBUG,
429 i, GNUNET_i2s (&list[i]));
435 * Remove peer from list.
438 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
439 unsigned int *list_size,
440 const struct GNUNET_PeerIdentity *peer)
443 struct GNUNET_PeerIdentity *tmp;
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 "Removing peer %s from list at %p\n",
451 print_peer_list (tmp, *list_size);
453 for ( i = 0 ; i < *list_size ; i++ )
455 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
457 if (i < *list_size -1)
458 { /* Not at the last entry -- shift peers left */
459 memcpy (&tmp[i], &tmp[i +1],
460 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
462 /* Remove last entry (should be now useless PeerID) */
463 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
470 * Get random peer from the given list but don't return one from the @a ignore_list.
472 struct GNUNET_PeerIdentity *
473 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
475 const struct GNUNET_PeerIdentity *ignore_list,
476 uint32_t ignore_size)
480 struct GNUNET_PeerIdentity *tmp_peer_list;
481 struct GNUNET_PeerIdentity *peer;
483 GNUNET_assert (NULL != peer_list);
488 tmp_peer_list = NULL;
489 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
490 memcpy (tmp_peer_list,
492 list_size * sizeof (struct GNUNET_PeerIdentity));
493 peer = GNUNET_new (struct GNUNET_PeerIdentity);
496 * Choose the r_index of the peer we want to return
497 * at random from the interval of the gossip list
499 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
501 *peer = tmp_peer_list[r_index];
503 while (in_arr (ignore_list, ignore_size, peer))
505 rem_from_list (&tmp_peer_list, &tmp_size, peer);
507 print_peer_list (tmp_peer_list, tmp_size);
516 * Choose the r_index of the peer we want to return
517 * at random from the interval of the gossip list
519 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
521 *peer = tmp_peer_list[r_index];
525 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
532 * Get the context of a peer. If not existing, create.
535 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
536 const struct GNUNET_PeerIdentity *peer)
538 struct PeerContext *ctx;
540 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
542 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
546 ctx = GNUNET_new (struct PeerContext);
549 ctx->send_channel = NULL;
550 ctx->recv_channel = NULL;
551 ctx->outstanding_ops = NULL;
552 ctx->num_outstanding_ops = 0;
553 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
561 * Put random peer from sampler into the gossip list as history update.
564 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
566 GNUNET_assert (1 == num_peers);
568 if (gossip_list_size < sampler_size_est_need)
569 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
571 if (0 < num_hist_update_tasks)
572 num_hist_update_tasks--;
577 * Set the peer flag to living and call the outstanding operations on this peer.
580 peer_is_live (struct PeerContext *peer_ctx)
582 struct GNUNET_PeerIdentity *peer;
584 peer = &peer_ctx->peer_id;
585 set_peer_flag (peer_ctx, LIVING);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
589 if (0 != peer_ctx->num_outstanding_ops)
590 { /* Call outstanding operations */
593 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
594 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
595 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
603 * Callback that is called when a channel was effectively established.
604 * This is given to ntfy_tmt_rdy and called when the channel was
605 * successfully established.
608 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
610 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
614 peer_is_live (peer_ctx);
616 //if (NULL != peer_ctx->is_live_task)
618 // LOG (GNUNET_ERROR_TYPE_DEBUG,
619 // "Trying to cancle is_live_task for peer %s\n",
620 // GNUNET_i2s (&peer_ctx->peer_id));
621 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
622 // peer_ctx->is_live_task = NULL;
624 peer_ctx->is_live_task = NULL;
631 * Get the channel of a peer. If not existing, create.
633 struct GNUNET_CADET_Channel *
634 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
635 const struct GNUNET_PeerIdentity *peer)
637 struct PeerContext *ctx;
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Trying to establish channel to peer %s\n",
643 ctx = get_peer_ctx (peer_map, peer);
644 if (NULL == ctx->send_channel)
646 ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle,
649 GNUNET_RPS_CADET_PORT,
650 GNUNET_CADET_OPTION_RELIABLE);
652 /* If we don't know whether peer is live,
653 * get notified when we know it is live. */
654 if (NULL == ctx->recv_channel
655 && NULL == ctx->is_live_task)
657 ctx->peer_id = *peer;
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Get informed about peer %s getting live\n",
662 GNUNET_CADET_notify_transmit_ready (ctx->send_channel,
664 GNUNET_TIME_UNIT_FOREVER_REL,
665 sizeof (struct GNUNET_MessageHeader),
666 cadet_ntfy_tmt_rdy_cb,
669 // FIXME check whether this is NULL
671 // do I have to explicitly put it in the peer_map?
672 (void) GNUNET_CONTAINER_multipeermap_put
676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
678 return ctx->send_channel;
683 * Get the message queue of a specific peer.
685 * If we already have a message queue open to this client,
686 * simply return it, otherways create one.
688 struct GNUNET_MQ_Handle *
689 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
690 const struct GNUNET_PeerIdentity *peer_id)
692 struct PeerContext *ctx;
694 ctx = get_peer_ctx (peer_map, peer_id);
697 (void) get_channel (peer_map, peer_id);
698 ctx->mq = GNUNET_CADET_mq_create (ctx->send_channel);
699 //do I have to explicitly put it in the peer_map?
700 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
708 * Sum all time relatives of an array.
710 struct GNUNET_TIME_Relative
711 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
713 struct GNUNET_TIME_Relative sum;
716 sum = GNUNET_TIME_UNIT_ZERO;
717 for ( i = 0 ; i < arr_size ; i++ )
719 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
726 * Compute the average of given time relatives.
728 struct GNUNET_TIME_Relative
729 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
731 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
736 * Insert PeerID in #pull_list
738 * Called once we know a peer is live.
741 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
743 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
744 GNUNET_array_append (pull_list, pull_list_size, *peer);
748 * Check whether #insert_in_pull_list was already scheduled
751 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
755 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
756 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
763 * Insert PeerID in #gossip_list
765 * Called once we know a peer is live.
768 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
770 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
771 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
775 * Check whether #insert_in_pull_list was already scheduled
778 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
782 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
783 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
790 * Update sampler with given PeerID.
793 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
795 RPS_sampler_update (prot_sampler, peer);
796 RPS_sampler_update (client_sampler, peer);
801 * Check whether #insert_in_sampler was already scheduled
804 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
808 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
809 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
816 * Wrapper around #RPS_sampler_resize()
818 * If we do not have enough sampler elements, double current sampler size
819 * If we have more than enough sampler elements, halv current sampler size
822 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
824 unsigned int sampler_size;
827 // TODO respect the min, max
828 sampler_size = RPS_sampler_get_size (sampler);
829 if (sampler_size > new_size * 4)
831 RPS_sampler_resize (sampler, sampler_size / 2);
833 else if (sampler_size < new_size)
835 RPS_sampler_resize (sampler, sampler_size * 2);
837 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
842 * Wrapper around #RPS_sampler_resize() resizing the client sampler
845 client_resize_wrapper ()
847 uint32_t bigger_size;
848 unsigned int sampler_size;
852 sampler_size = RPS_sampler_get_size (client_sampler);
854 if (sampler_size_est_need > sampler_size_client_need)
855 bigger_size = sampler_size_est_need;
857 bigger_size = sampler_size_client_need;
859 // TODO respect the min, max
860 resize_wrapper (client_sampler, bigger_size);
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
866 * Estimate request rate
868 * Called every time we receive a request from the client.
873 struct GNUNET_TIME_Relative max_round_duration;
875 if (request_deltas_size > req_counter)
877 if ( 1 < req_counter)
879 /* Shift last request deltas to the right */
880 memcpy (&request_deltas[1],
882 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
884 /* Add current delta to beginning */
886 GNUNET_TIME_absolute_get_difference (last_request,
887 GNUNET_TIME_absolute_get ());
888 request_rate = T_relative_avg (request_deltas, req_counter);
890 /* Compute the duration a round will maximally take */
892 GNUNET_TIME_relative_add (round_interval,
893 GNUNET_TIME_relative_divide (round_interval, 2));
895 /* Set the estimated size the sampler has to have to
896 * satisfy the current client request rate */
897 sampler_size_client_need =
898 max_round_duration.rel_value_us / request_rate.rel_value_us;
900 /* Resize the sampler */
901 client_resize_wrapper ();
903 last_request = GNUNET_TIME_absolute_get ();
907 /***********************************************************************
909 ***********************************************************************/
916 * Function called by NSE.
918 * Updates sizes of sampler list and gossip list and adapt those lists
922 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
923 double logestimate, double std_dev)
926 //double scale; // TODO this might go gloabal/config
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
930 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
932 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
933 // GNUNET_NSE_log_estimate_to_n (logestimate);
934 estimate = pow (estimate, 1.0 / 3);
935 // TODO add if std_dev is a number
936 // estimate += (std_dev * scale);
937 if (2 < ceil (estimate))
939 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
940 sampler_size_est_need = estimate;
942 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
944 /* If the NSE has changed adapt the lists accordingly */
945 resize_wrapper (prot_sampler, sampler_size_est_need);
946 client_resize_wrapper ();
951 * Callback called once the requested PeerIDs are ready.
953 * Sends those to the requesting client.
955 void client_respond (void *cls,
956 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
958 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
959 struct GNUNET_MQ_Envelope *ev;
960 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
961 struct GNUNET_SERVER_Client *client;
962 uint32_t size_needed;
963 struct client_ctx *cli_ctx;
965 client = (struct GNUNET_SERVER_Client *) cls;
967 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
968 num_peers * sizeof (struct GNUNET_PeerIdentity);
970 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
972 ev = GNUNET_MQ_msg_extra (out_msg,
973 num_peers * sizeof (struct GNUNET_PeerIdentity),
974 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
975 out_msg->num_peers = htonl (num_peers);
979 num_peers * sizeof (struct GNUNET_PeerIdentity));
982 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
983 if ( NULL == cli_ctx ) {
984 cli_ctx = GNUNET_new (struct client_ctx);
985 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
986 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
989 GNUNET_MQ_send (cli_ctx->mq, ev);
994 * Handle RPS request from the client.
997 * @param client identification of the client
998 * @param message the actual message
1001 handle_client_request (void *cls,
1002 struct GNUNET_SERVER_Client *client,
1003 const struct GNUNET_MessageHeader *message)
1005 struct GNUNET_RPS_CS_RequestMessage *msg;
1007 uint32_t size_needed;
1010 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1012 num_peers = ntohl (msg->num_peers);
1013 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1014 num_peers * sizeof (struct GNUNET_PeerIdentity);
1016 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1022 for (i = 0 ; i < num_peers ; i++)
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
1027 RPS_sampler_get_n_rand_peers (client_sampler, client_respond,
1028 client, num_peers, GNUNET_YES);
1030 GNUNET_SERVER_receive_done (client,
1036 * Handle seed from the client.
1038 * @param cls closure
1039 * @param client identification of the client
1040 * @param message the actual message
1043 handle_client_seed (void *cls,
1044 struct GNUNET_SERVER_Client *client,
1045 const struct GNUNET_MessageHeader *message)
1047 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1048 struct GNUNET_PeerIdentity *peers;
1051 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
1053 GNUNET_break_op (0);
1054 GNUNET_SERVER_receive_done (client,
1057 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1058 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1059 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1061 GNUNET_break_op (0);
1062 GNUNET_SERVER_receive_done (client,
1066 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1067 peers = (struct GNUNET_PeerIdentity *) &message[1];
1069 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1070 RPS_sampler_update (prot_sampler, &peers[i]);
1071 RPS_sampler_update (client_sampler, &peers[i]);
1073 GNUNET_SERVER_receive_done (client,
1079 * Handle a PUSH message from another peer.
1081 * Check the proof of work and store the PeerID
1082 * in the temporary list for pushed PeerIDs.
1084 * @param cls Closure
1085 * @param channel The channel the PUSH was received over
1086 * @param channel_ctx The context associated with this channel
1087 * @param msg The message header
1090 handle_peer_push (void *cls,
1091 struct GNUNET_CADET_Channel *channel,
1093 const struct GNUNET_MessageHeader *msg)
1095 const struct GNUNET_PeerIdentity *peer;
1097 // (check the proof of work)
1099 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1100 // FIXME wait for cadet to change this function
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1103 /* Add the sending peer to the push_list */
1104 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
1105 GNUNET_array_append (push_list, push_list_size, *peer);
1111 * Handle PULL REQUEST request message from another peer.
1113 * Reply with the gossip list of PeerIDs.
1115 * @param cls Closure
1116 * @param channel The channel the PUSH was received over
1117 * @param channel_ctx The context associated with this channel
1118 * @param msg The message header
1121 handle_peer_pull_request (void *cls,
1122 struct GNUNET_CADET_Channel *channel,
1124 const struct GNUNET_MessageHeader *msg)
1126 struct GNUNET_PeerIdentity *peer;
1128 struct GNUNET_MQ_Handle *mq;
1129 struct GNUNET_MQ_Envelope *ev;
1130 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1133 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
1134 GNUNET_CADET_OPTION_PEER);
1135 // FIXME wait for cadet to change this function
1137 /* Compute actual size */
1138 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1139 gossip_list_size * sizeof (struct GNUNET_PeerIdentity);
1141 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1142 /* Compute number of peers to send
1143 * If too long, simply truncate */
1145 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1146 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1147 sizeof (struct GNUNET_PeerIdentity);
1149 send_size = gossip_list_size;
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "PULL REQUEST from peer %s received, going to send %u peers\n",
1153 GNUNET_i2s (peer), send_size);
1155 mq = get_mq (peer_map, peer);
1157 ev = GNUNET_MQ_msg_extra (out_msg,
1158 send_size * sizeof (struct GNUNET_PeerIdentity),
1159 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1160 //out_msg->num_peers = htonl (gossip_list_size);
1161 out_msg->num_peers = htonl (send_size);
1162 memcpy (&out_msg[1], gossip_list,
1163 send_size * sizeof (struct GNUNET_PeerIdentity));
1165 GNUNET_MQ_send (mq, ev);
1171 * Handle PULL REPLY message from another peer.
1173 * Check whether we sent a corresponding request and
1174 * whether this reply is the first one.
1176 * @param cls Closure
1177 * @param channel The channel the PUSH was received over
1178 * @param channel_ctx The context associated with this channel
1179 * @param msg The message header
1182 handle_peer_pull_reply (void *cls,
1183 struct GNUNET_CADET_Channel *channel,
1185 const struct GNUNET_MessageHeader *msg)
1187 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1189 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1190 struct GNUNET_PeerIdentity *peers;
1191 struct PeerContext *peer_ctx;
1192 struct GNUNET_PeerIdentity *sender;
1193 struct PeerContext *sender_ctx;
1194 struct PeerOutstandingOp out_op;
1197 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1199 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
1200 return GNUNET_SYSERR;
1202 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1203 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1205 LOG (GNUNET_ERROR_TYPE_ERROR, "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1206 ntohl (in_msg->num_peers),
1207 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity));
1208 GNUNET_break_op (0);
1209 return GNUNET_SYSERR;
1212 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1213 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1214 // Guess simply casting isn't the nicest way...
1215 // FIXME wait for cadet to change this function
1216 sender_ctx = get_peer_ctx (peer_map, sender);
1218 if (0 == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1220 GNUNET_break_op (0);
1224 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1225 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1227 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1228 if ((0 != (peer_ctx->peer_flags && LIVING)) ||
1229 NULL != peer_ctx->recv_channel)
1231 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1232 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1234 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1236 out_op.op = insert_in_pull_list;
1237 out_op.op_cls = NULL;
1238 GNUNET_array_append (peer_ctx->outstanding_ops,
1239 peer_ctx->num_outstanding_ops,
1244 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1245 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1252 * Send out PUSHes and PULLs.
1254 * This is executed regylary.
1257 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1259 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1262 unsigned int *permut;
1263 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1264 struct GNUNET_MQ_Envelope *ev;
1265 struct GNUNET_PeerIdentity peer;
1266 struct GNUNET_PeerIdentity *tmp_peer;
1267 struct GNUNET_MQ_Handle *mq;
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Printing gossip list:\n");
1271 for (i = 0 ; i < gossip_list_size ; i++)
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1274 // TODO log lists, ...
1276 /* Would it make sense to have one shuffeled gossip list and then
1277 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1278 * use the rest to update sampler?
1279 * in essence get random peers with consumption */
1282 if (0 < gossip_list_size)
1284 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1285 (unsigned int) gossip_list_size);
1286 n_peers = ceil (alpha * gossip_list_size);
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "Going to send pushes to %u ceil (%f * %u) peers.\n",
1289 n_peers, alpha, gossip_list_size);
1290 for (i = 0 ; i < n_peers ; i++)
1292 peer = gossip_list[permut[i]];
1293 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1294 { // FIXME if this fails schedule/loop this for later
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Sending PUSH to peer %s of gossiped list.\n",
1297 GNUNET_i2s (&peer));
1299 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1300 mq = get_mq (peer_map, &peer);
1301 GNUNET_MQ_send (mq, ev);
1304 GNUNET_free (permut);
1308 /* Send PULL requests */
1309 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1310 n_peers = ceil (beta * gossip_list_size);
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "Going to send pulls to %u ceil (%f * %u) peers.\n",
1313 n_peers, beta, gossip_list_size);
1314 for (i = 0 ; i < n_peers ; i++)
1316 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1317 pending_pull_reply_list, pending_pull_reply_list_size);
1318 if (NULL != tmp_peer)
1321 GNUNET_free (tmp_peer);
1323 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, peer);
1325 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
1326 { // FIXME if this fails schedule/loop this for later
1327 LOG (GNUNET_ERROR_TYPE_DEBUG,
1328 "Sending PULL request to peer %s of gossiped list.\n",
1329 GNUNET_i2s (&peer));
1331 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1332 mq = get_mq (peer_map, &peer);
1333 GNUNET_MQ_send (mq, ev);
1339 /* Update gossip list */
1342 if ( push_list_size <= alpha * gossip_list_size &&
1343 push_list_size != 0 &&
1344 pull_list_size != 0 )
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1348 uint32_t first_border;
1349 uint32_t second_border;
1351 first_border = ceil (alpha * sampler_size_est_need);
1352 second_border = first_border + ceil (beta * sampler_size_est_need);
1354 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1356 for (i = 0 ; i < first_border ; i++)
1357 { // TODO use RPS_sampler_get_n_rand_peers
1358 /* Update gossip list with peers received through PUSHes */
1359 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1361 gossip_list[i] = push_list[r_index];
1362 // TODO change the peer_flags accordingly
1365 for (i = first_border ; i < second_border ; i++)
1367 /* Update gossip list with peers received through PULLs */
1368 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1370 gossip_list[i] = pull_list[r_index];
1371 // TODO change the peer_flags accordingly
1374 for (i = second_border ; i < sampler_size_est_need ; i++)
1376 /* Update gossip list with peers from history */
1377 RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
1378 num_hist_update_tasks++;
1379 // TODO change the peer_flags accordingly
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
1387 // TODO independent of that also get some peers from CADET_get_peers()?
1390 /* Update samplers */
1392 for ( i = 0 ; i < push_list_size ; i++ )
1394 RPS_sampler_update (prot_sampler, &push_list[i]);
1395 RPS_sampler_update (client_sampler, &push_list[i]);
1396 // TODO set in_flag?
1399 for ( i = 0 ; i < pull_list_size ; i++ )
1401 RPS_sampler_update (prot_sampler, &push_list[i]);
1402 RPS_sampler_update (client_sampler, &push_list[i]);
1403 // TODO set in_flag?
1407 /* Empty push/pull lists */
1408 GNUNET_array_grow (push_list, push_list_size, 0);
1409 GNUNET_array_grow (pull_list, pull_list_size, 0);
1411 struct GNUNET_TIME_Relative time_next_round;
1412 struct GNUNET_TIME_Relative half_round_interval;
1413 unsigned int rand_delay;
1415 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1416 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1420 * Compute random value between (0 and 1) * round_interval
1421 * via multiplying round_interval with a 'fraction' (0 to value)/value
1423 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1424 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
1425 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
1426 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
1427 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1429 /* Schedule next round */
1430 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1436 * Open a connection to given peer and store channel and mq.
1439 insertCB (void *cls, struct RPS_Sampler *sampler,
1440 const struct GNUNET_PeerIdentity *id)
1442 // We open a channel to be notified when this peer goes down.
1443 (void) get_channel (peer_map, id);
1448 * Close the connection to given peer and delete channel and mq
1449 * if the peer is not anymore in the sampler.
1452 removeCB (void *cls, struct RPS_Sampler *sampler,
1453 const struct GNUNET_PeerIdentity *id)
1456 struct PeerContext *ctx;
1458 s = RPS_sampler_count_id (sampler, id);
1461 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
1463 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
1464 if (NULL != ctx->send_channel)
1466 if (NULL != ctx->mq)
1468 GNUNET_MQ_destroy (ctx->mq);
1470 // may already be freed at shutdown of cadet
1471 //GNUNET_CADET_channel_destroy (ctx->send_channel);
1473 // TODO cleanup peer
1474 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
1480 rps_start (struct GNUNET_SERVER_Handle *server);
1483 * This is called from GNUNET_CADET_get_peers().
1485 * It is called on every peer(ID) that cadet somehow has contact with.
1486 * We use those to initialise the sampler.
1489 init_peer_cb (void *cls,
1490 const struct GNUNET_PeerIdentity *peer,
1491 int tunnel, // "Do we have a tunnel towards this peer?"
1492 unsigned int n_paths, // "Number of known paths towards this peer"
1493 unsigned int best_path) // "How long is the best path?
1494 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1496 struct GNUNET_SERVER_Handle *server;
1497 struct PeerOutstandingOp out_op;
1498 struct PeerContext *peer_ctx;
1500 server = (struct GNUNET_SERVER_Handle *) cls;
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1505 GNUNET_i2s (peer), peer, gossip_list_size);
1507 // maybe create a function for that
1508 peer_ctx = get_peer_ctx (peer_map, peer);
1509 // FIXME this peer might already be marked as LIVE
1510 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1512 out_op.op = insert_in_sampler;
1513 out_op.op_cls = NULL;
1514 GNUNET_array_append (peer_ctx->outstanding_ops,
1515 peer_ctx->num_outstanding_ops,
1519 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1521 out_op.op = insert_in_gossip_list;
1522 out_op.op_cls = NULL;
1523 GNUNET_array_append (peer_ctx->outstanding_ops,
1524 peer_ctx->num_outstanding_ops,
1528 /* Trigger livelyness test on peer */
1529 (void) get_channel (peer_map, peer);
1531 // send push/pull to each of those peers?
1539 * Callback used to clean the multipeermap.
1542 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1544 struct PeerContext *peer_ctx;
1545 const struct GNUNET_CADET_Channel *ch = (const struct GNUNET_CADET_Channel *) cls;
1546 struct GNUNET_CADET_Channel *recv;
1547 struct GNUNET_CADET_Channel *send;
1549 peer_ctx = (struct PeerContext *) value;
1551 if (0 != peer_ctx->num_outstanding_ops)
1552 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
1554 if (NULL != peer_ctx->mq)
1555 GNUNET_MQ_destroy (peer_ctx->mq);
1557 if (NULL != peer_ctx->is_live_task)
1559 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1560 peer_ctx->is_live_task = NULL;
1563 send = peer_ctx->send_channel;
1564 peer_ctx->send_channel = NULL;
1565 recv = peer_ctx->send_channel;
1566 peer_ctx->recv_channel = NULL;
1571 GNUNET_CADET_channel_destroy (send);
1577 GNUNET_CADET_channel_destroy (recv);
1580 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1581 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1583 GNUNET_free (peer_ctx);
1590 * Task run during shutdown.
1596 shutdown_task (void *cls,
1597 const struct GNUNET_SCHEDULER_TaskContext *tc)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1601 if ( NULL != do_round_task )
1603 GNUNET_SCHEDULER_cancel (do_round_task);
1604 do_round_task = NULL;
1609 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1610 LOG (GNUNET_ERROR_TYPE_WARNING,
1611 "Iterating over peers to disconnect from them was cancelled\n");
1614 GNUNET_NSE_disconnect (nse);
1615 GNUNET_CADET_disconnect (cadet_handle);
1616 RPS_sampler_destroy (prot_sampler);
1617 RPS_sampler_destroy (client_sampler);
1618 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
1619 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1620 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1621 GNUNET_array_grow (push_list, push_list_size, 0);
1622 GNUNET_array_grow (pull_list, pull_list_size, 0);
1627 * A client disconnected. Remove all of its data structure entries.
1629 * @param cls closure, NULL
1630 * @param client identification of the client
1633 handle_client_disconnect (void *cls,
1634 struct GNUNET_SERVER_Client * client)
1640 * Handle the channel a peer opens to us.
1642 * @param cls The closure
1643 * @param channel The channel the peer wants to establish
1644 * @param initiator The peer's peer ID
1645 * @param port The port the channel is being established over
1646 * @param options Further options
1649 handle_inbound_channel (void *cls,
1650 struct GNUNET_CADET_Channel *channel,
1651 const struct GNUNET_PeerIdentity *initiator,
1653 enum GNUNET_CADET_ChannelOption options)
1655 struct PeerContext *peer_ctx;
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "New channel was established to us (Peer %s).\n",
1659 GNUNET_i2s (initiator));
1661 GNUNET_assert (NULL != channel);
1663 // we might not even store the recv_channel
1665 peer_ctx = get_peer_ctx (peer_map, initiator);
1666 // FIXME what do we do if a channel is established twice?
1667 // overwrite? Clean old channel? ...?
1668 //if (NULL != peer_ctx->recv_channel)
1670 // peer_ctx->recv_channel = channel;
1672 peer_ctx->recv_channel = channel;
1674 peer_ctx->mq = NULL;
1676 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, peer_ctx,
1677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1679 peer_is_live (peer_ctx);
1681 return NULL; // TODO
1686 * This is called when a remote peer destroys a channel.
1688 * @param cls The closure
1689 * @param channel The channel being closed
1690 * @param channel_ctx The context associated with this channel
1693 cleanup_channel (void *cls,
1694 const struct GNUNET_CADET_Channel *channel,
1697 struct GNUNET_PeerIdentity *peer;
1698 struct PeerContext *peer_ctx;
1700 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1701 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1702 // Guess simply casting isn't the nicest way...
1703 // FIXME wait for cadet to change this function
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up channel to peer %s\n",
1707 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
1708 RPS_sampler_reinitialise_by_value (client_sampler, peer);
1710 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1712 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1714 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
1717 /* Somwewhat {ab,re}use the iterator function */
1718 /* Cast to void is ok, because it's used as void in peer_remove_cb */
1719 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
1725 * Actually start the service.
1728 rps_start (struct GNUNET_SERVER_Handle *server)
1730 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1731 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1732 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1733 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1737 GNUNET_SERVER_add_handlers (server, handlers);
1738 GNUNET_SERVER_disconnect_notify (server,
1739 &handle_client_disconnect,
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1744 num_hist_update_tasks = 0;
1746 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1749 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1756 * Process statistics requests.
1758 * @param cls closure
1759 * @param server the initialized server
1760 * @param c configuration to use
1764 struct GNUNET_SERVER_Handle *server,
1765 const struct GNUNET_CONFIGURATION_Handle *c)
1767 // TODO check what this does -- copied from gnunet-boss
1768 // - seems to work as expected
1769 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1774 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
1775 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1776 "STARTING SERVICE (rps) for peer [%s]\n",
1777 GNUNET_i2s (&own_identity));
1780 /* Get time interval from the configuration */
1781 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1785 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1786 GNUNET_SCHEDULER_shutdown ();
1790 /* Get initial size of sampler/gossip list from the configuration */
1791 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1793 (long long unsigned int *) &sampler_size_est_need))
1795 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1796 GNUNET_SCHEDULER_shutdown ();
1799 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1805 /* connect to NSE */
1806 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1807 // TODO check whether that was successful
1808 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1814 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1817 /* Initialise cadet */
1818 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1819 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1820 sizeof (struct GNUNET_MessageHeader)},
1821 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1822 sizeof (struct GNUNET_MessageHeader)},
1823 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1827 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1828 cadet_handle = GNUNET_CADET_connect (cfg,
1830 &handle_inbound_channel,
1834 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1837 /* Initialise sampler */
1838 struct GNUNET_TIME_Relative half_round_interval;
1839 struct GNUNET_TIME_Relative max_round_interval;
1841 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1842 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1844 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval,
1845 insertCB, NULL, removeCB, NULL);
1846 client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval,
1847 insertCB, NULL, removeCB, NULL);
1849 /* Initialise push and pull maps */
1854 pending_pull_reply_list = NULL;
1855 pending_pull_reply_list_size = 0;
1858 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1859 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, server);
1861 // TODO send push/pull to each of those peers?
1866 * The main function for the rps service.
1868 * @param argc number of arguments from the command line
1869 * @param argv command line arguments
1870 * @return 0 ok, 1 on error
1873 main (int argc, char *const *argv)
1875 return (GNUNET_OK ==
1876 GNUNET_SERVICE_run (argc,
1879 GNUNET_SERVICE_OPTION_NONE,
1880 &run, NULL)) ? 0 : 1;
1883 /* end of gnunet-service-rps.c */