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 ***********************************************************************/
157 * Set of all peers to keep track of them.
159 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
163 * The gossiped list of peers.
165 static struct GNUNET_PeerIdentity *gossip_list;
168 * Size of the gossiped list
170 static unsigned int gossip_list_size;
174 * The estimated size of the network.
176 * Influenced by the stdev.
178 static unsigned int est_size;
183 * Percentage of total peer number in the gossip list
184 * to send random PUSHes to
186 * TODO do not read from configuration
191 * Percentage of total peer number in the gossip list
192 * to send random PULLs to
194 * TODO do not read from configuration
199 * The percentage gamma of history updates.
200 * Simply 1 - alpha - beta
207 * Identifier for the main task that runs periodically.
209 static struct GNUNET_SCHEDULER_Task * do_round_task;
212 * Time inverval the do_round task runs in.
214 static struct GNUNET_TIME_Relative round_interval;
219 * List to store peers received through pushes temporary.
221 * TODO -> multipeermap
223 static struct GNUNET_PeerIdentity *push_list;
226 * Size of the push_list;
228 static unsigned int push_list_size;
229 //size_t push_list_size;
232 * List to store peers received through pulls temporary.
234 * TODO -> multipeermap
236 static struct GNUNET_PeerIdentity *pull_list;
239 * Size of the pull_list;
241 static unsigned int pull_list_size;
242 //size_t pull_list_size;
248 static struct GNUNET_NSE_Handle *nse;
253 static struct GNUNET_CADET_Handle *cadet_handle;
261 /***********************************************************************
263 ***********************************************************************/
266 * Check if peer is already in peer array.
269 in_arr (const struct GNUNET_PeerIdentity *array,
270 unsigned int arr_size,
271 const struct GNUNET_PeerIdentity *peer)
273 GNUNET_assert (NULL != peer);
278 GNUNET_assert (NULL != array);
283 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
295 * Get random peer from the gossip list.
297 struct GNUNET_PeerIdentity *
298 get_rand_peer(const struct GNUNET_PeerIdentity *peer_list, unsigned int list_size)
301 struct GNUNET_PeerIdentity *peer;
303 peer = GNUNET_new(struct GNUNET_PeerIdentity);
304 // FIXME if we have only NULL in gossip list this will block
305 // but then we might have a problem nevertheless
311 * Choose the r_index of the peer we want to return
312 * at random from the interval of the gossip list
314 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
317 *peer = peer_list[r_index];
318 } while (NULL == peer);
325 * Get the context of a peer. If not existing, create.
327 struct peer_context *
328 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
330 struct peer_context *ctx;
332 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
334 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
338 ctx = GNUNET_new (struct peer_context);
341 ctx->to_channel = NULL;
342 ctx->from_channel = NULL;
343 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
350 * Get the channel of a peer. If not existing, create.
352 struct GNUNET_CADET_Channel *
353 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
355 struct peer_context *ctx;
357 ctx = get_peer_ctx (peer_map, peer);
358 if (NULL == ctx->to_channel)
360 ctx->to_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
361 GNUNET_RPS_CADET_PORT,
362 GNUNET_CADET_OPTION_RELIABLE);
363 // do I have to explicitly put it in the peer_map?
364 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
367 return ctx->to_channel;
372 * Get the message queue of a specific peer.
374 * If we already have a message queue open to this client,
375 * simply return it, otherways create one.
377 struct GNUNET_MQ_Handle *
378 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
380 struct peer_context *ctx;
382 ctx = get_peer_ctx (peer_map, peer_id);
385 (void) get_channel (peer_map, peer_id);
386 ctx->mq = GNUNET_CADET_mq_create (ctx->to_channel);
387 //do I have to explicitly put it in the peer_map?
388 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
389 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
395 /***********************************************************************
397 ***********************************************************************/
400 * Function called by NSE.
402 * Updates sizes of sampler list and gossip list and adapt those lists
406 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
409 unsigned int old_est;
410 //double scale; // TODO this might go gloabal/config
414 LOG (GNUNET_ERROR_TYPE_DEBUG,
415 "Received a ns estimate - logest: %f, std_dev: %f (old_est: %f)\n",
416 logestimate, std_dev, old_est);
418 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
419 // GNUNET_NSE_log_estimate_to_n (logestimate);
420 estimate = pow (estimate, 1./3);
421 // TODO add if std_dev is a number
422 // estimate += (std_dev * scale);
423 if ( 0 < estimate ) {
424 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
427 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
429 /* If the NSE has changed adapt the lists accordingly */
430 // TODO respect the request rate, min, max
431 if (old_est > est_size*4)
433 RPS_sampler_resize (old_est/2);
435 else if (old_est < est_size)
437 if (est_size < old_est*2)
438 RPS_sampler_resize (old_est*2);
440 RPS_sampler_resize (est_size);
445 * Handle RPS request from the client.
448 * @param client identification of the client
449 * @param message the actual message
453 handle_cs_request (void *cls,
454 struct GNUNET_SERVER_Client *client,
455 const struct GNUNET_MessageHeader *message)
457 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
459 struct GNUNET_RPS_CS_RequestMessage *msg;
460 //unsigned int n_arr[sampler_list->size];// =
461 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
462 //struct GNUNET_MQ_Handle *mq;
463 struct client_ctx *cli_ctx;
464 struct GNUNET_MQ_Envelope *ev;
465 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
467 const struct GNUNET_PeerIdentity *peers;
470 // TODO check message size
471 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
472 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
473 if ( NULL == cli_ctx ) {
474 cli_ctx = GNUNET_new(struct client_ctx);
475 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
476 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
479 // How many peers do we give back?
480 // Wait until we have enough random peers?
482 num_peers = GNUNET_ntohll (msg->num_peers);
484 ev = GNUNET_MQ_msg_extra (out_msg,
485 num_peers * sizeof (struct GNUNET_PeerIdentity),
486 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
487 out_msg->num_peers = msg->num_peers; // No conversion between network and network order
489 //&out_msg[1] = RPS_sampler_get_n_rand_peers (num_peers);
490 peers = RPS_sampler_get_n_rand_peers (num_peers);
493 num_peers * sizeof (struct GNUNET_PeerIdentity));
495 GNUNET_MQ_send (cli_ctx->mq, ev);
496 //GNUNET_MQ_destroy(mq);
498 GNUNET_SERVER_receive_done (client,
503 * Handle a PUSH message from another peer.
505 * Check the proof of work and store the PeerID
506 * in the temporary list for pushed PeerIDs.
509 * @param channel The channel the PUSH was received over
510 * @param channel_ctx The context associated with this channel
511 * @param msg The message header
514 handle_peer_push (void *cls,
515 struct GNUNET_CADET_Channel *channel,
517 const struct GNUNET_MessageHeader *msg)
519 const struct GNUNET_PeerIdentity *peer;
521 // (check the proof of work)
523 // TODO accept empty message
524 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PushMessage))
526 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
527 return GNUNET_SYSERR;
530 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
531 // FIXME wait for cadet to change this function
532 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
534 /* Add the sending peer to the push_list */
535 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
536 GNUNET_array_append (push_list, push_list_size, *peer);
542 * Handle PULL REQUEST request message from another peer.
544 * Reply with the gossip list of PeerIDs.
547 * @param channel The channel the PUSH was received over
548 * @param channel_ctx The context associated with this channel
549 * @param msg The message header
552 handle_peer_pull_request (void *cls,
553 struct GNUNET_CADET_Channel *channel,
555 const struct GNUNET_MessageHeader *msg)
557 struct GNUNET_PeerIdentity *peer;
558 struct GNUNET_MQ_Handle *mq;
559 struct GNUNET_MQ_Envelope *ev;
560 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
562 // assert that msg->size is 0
564 // TODO accept empty message
565 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PullRequestMessage))
567 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
568 return GNUNET_SYSERR;
571 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
572 // FIXME wait for cadet to change this function
573 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s (peer));
575 mq = get_mq (peer_map, peer);
577 ev = GNUNET_MQ_msg_extra (out_msg,
578 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
579 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
580 out_msg->num_peers = GNUNET_htonll (gossip_list_size);
581 memcpy (&out_msg[1], gossip_list,
582 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
584 GNUNET_MQ_send (mq, ev);
590 * Handle PULL REPLY message from another peer.
592 * Check whether we sent a corresponding request and
593 * whether this reply is the first one.
596 * @param channel The channel the PUSH was received over
597 * @param channel_ctx The context associated with this channel
598 * @param msg The message header
601 handle_peer_pull_reply (void *cls,
602 struct GNUNET_CADET_Channel *channel,
604 const struct GNUNET_MessageHeader *msg)
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
608 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
609 struct GNUNET_PeerIdentity *peers;
612 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) < ntohs (msg->size))
614 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
615 return GNUNET_SYSERR;
617 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
618 if (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) / sizeof (struct GNUNET_PeerIdentity) != GNUNET_ntohll (in_msg->num_peers))
621 return GNUNET_SYSERR;
624 // TODO check that we sent a request and that it is the first reply
626 peers = (struct GNUNET_PeerIdentity *) &msg[1];
627 for ( i = 0 ; i < GNUNET_ntohll (in_msg->num_peers) ; i++ )
629 if (GNUNET_NO == in_arr(pull_list, pull_list_size, &peers[i]))
630 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
633 // TODO check that id is valid - whether it is reachable
640 * Send out PUSHes and PULLs.
642 * This is executed regylary.
645 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
647 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
650 //unsigned int *n_arr;
651 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
652 struct GNUNET_RPS_P2P_PushMessage *push_msg;
653 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
654 struct GNUNET_MQ_Envelope *ev;
655 const struct GNUNET_PeerIdentity *peer;
656 struct GNUNET_MQ_Handle *mq;
658 // TODO print lists, ...
659 // TODO rendomise and spread calls herein over time
662 /* Would it make sense to have one shuffeled gossip list and then
663 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
664 * use the rest to update sampler?
665 * in essence get random peers with consumption */
668 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
669 n_peers = round (alpha * gossip_list_size);
672 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
673 n_peers, alpha, gossip_list_size);
674 for ( i = 0 ; i < n_peers ; i++ )
675 { // TODO compute length
676 peer = get_rand_peer (gossip_list, gossip_list_size);
677 if (own_identity != peer)
678 { // FIXME if this fails schedule/loop this for later
679 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
681 ev = GNUNET_MQ_msg (push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
682 //ev = GNUNET_MQ_msg_extra();
683 /* TODO Compute proof of work here
685 //push_msg->placeholder = 0;
687 // FIXME sometimes it returns a pointer to a freed mq
688 mq = get_mq (peer_map, peer);
689 GNUNET_MQ_send (mq, ev);
691 // modify in_flags of respective peer?
696 /* Send PULL requests */
697 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
698 n_peers = round (beta * gossip_list_size);
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
702 n_peers, beta, gossip_list_size);
703 for ( i = 0 ; i < n_peers ; i++ )
704 { // TODO compute length
705 peer = get_rand_peer (gossip_list, gossip_list_size);
706 if (own_identity != peer)
707 { // FIXME if this fails schedule/loop this for later
708 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
710 ev = GNUNET_MQ_msg (pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
711 //pull_msg->placeholder = 0;
713 mq = get_mq (peer_map, peer);
714 GNUNET_MQ_send (mq, ev);
715 // modify in_flags of respective peer?
720 /* Update gossip list */
723 if ( push_list_size <= alpha * gossip_list_size &&
724 push_list_size != 0 &&
725 pull_list_size != 0 )
727 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
729 uint64_t first_border;
730 uint64_t second_border;
732 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
734 first_border = round(alpha * gossip_list_size);
735 for ( i = 0 ; i < first_border ; i++ )
736 { // TODO use RPS_sampler_get_n_rand_peers
737 /* Update gossip list with peers received through PUSHes */
738 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
740 gossip_list[i] = push_list[r_index];
741 // TODO change the in_flags accordingly
744 second_border = first_border + round(beta * gossip_list_size);
745 for ( i = first_border ; i < second_border ; i++ )
747 /* Update gossip list with peers received through PULLs */
748 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
750 gossip_list[i] = pull_list[r_index];
751 // TODO change the in_flags accordingly
754 for ( i = second_border ; i < gossip_list_size ; i++ )
756 /* Update gossip list with peers from history */
757 peer = RPS_sampler_get_n_rand_peers (1),
758 gossip_list[i] = *peer;
759 // TODO change the in_flags accordingly
765 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
767 // TODO independent of that also get some peers from CADET_get_peers()?
770 /* Update samplers */
772 for ( i = 0 ; i < push_list_size ; i++ )
774 RPS_sampler_update_list (&push_list[i]);
778 for ( i = 0 ; i < pull_list_size ; i++ )
780 RPS_sampler_update_list (&pull_list[i]);
785 /* Empty push/pull lists */
786 GNUNET_array_grow (push_list, push_list_size, 0);
787 GNUNET_array_grow (pull_list, pull_list_size, 0);
790 /* Schedule next round */
791 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
792 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
797 * Open a connection to given peer and store channel and mq.
800 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
802 // We open a channel to be notified when this peer goes down.
803 (void) get_channel (peer_map, id);
808 * Close the connection to given peer and delete channel and mq.
811 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
814 struct peer_context *ctx;
816 s = RPS_sampler_count_id (id);
819 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
821 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
822 if (NULL != ctx->to_channel)
826 GNUNET_MQ_destroy (ctx->mq);
828 // may already be freed at shutdown of cadet
829 //GNUNET_CADET_channel_destroy (ctx->to_channel);
832 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
838 rps_start (struct GNUNET_SERVER_Handle *server);
841 * This is called from GNUNET_CADET_get_peers().
843 * It is called on every peer(ID) that cadet somehow has contact with.
844 * We use those to initialise the sampler.
847 init_peer_cb (void *cls,
848 const struct GNUNET_PeerIdentity *peer,
849 int tunnel, // "Do we have a tunnel towards this peer?"
850 unsigned int n_paths, // "Number of known paths towards this peer"
851 unsigned int best_path) // "How long is the best path?
852 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
854 struct init_peer_cls *ipc;
856 ipc = (struct init_peer_cls *) cls;
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Got %" PRIX32 ". peer %s (at %p) from CADET (gossip_list_size: %u)\n",
861 ipc->i, GNUNET_i2s (peer), peer, gossip_list_size);
862 RPS_sampler_update_list (peer);
863 (void) get_peer_ctx (peer_map, peer); // unneeded? -> insertCB
865 if (ipc->i < gossip_list_size)
867 gossip_list[ipc->i] = *peer; // FIXME sometimes we're writing to invalid space here
868 // not sure whether fixed
872 // send push/pull to each of those peers?
876 if (ipc->i < gossip_list_size)
878 memcpy(&gossip_list[ipc->i],
879 RPS_sampler_get_n_rand_peers (1),
880 (gossip_list_size - ipc->i) * sizeof(struct GNUNET_PeerIdentity));
882 rps_start (ipc->server);
889 * Task run during shutdown.
895 shutdown_task (void *cls,
896 const struct GNUNET_SCHEDULER_TaskContext *tc)
898 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
900 if ( NULL != do_round_task )
902 GNUNET_SCHEDULER_cancel (do_round_task);
903 do_round_task = NULL;
906 GNUNET_NSE_disconnect(nse);
907 GNUNET_CADET_disconnect(cadet_handle);
908 GNUNET_free(own_identity);
909 RPS_sampler_destroy();
910 GNUNET_array_grow(gossip_list, gossip_list_size, 0);
911 GNUNET_array_grow(push_list, push_list_size, 0);
912 GNUNET_array_grow(pull_list, pull_list_size, 0);
917 * A client disconnected. Remove all of its data structure entries.
919 * @param cls closure, NULL
920 * @param client identification of the client
923 handle_client_disconnect (void *cls,
924 struct GNUNET_SERVER_Client * client)
929 * Handle the channel a peer opens to us.
931 * @param cls The closure
932 * @param channel The channel the peer wants to establish
933 * @param initiator The peer's peer ID
934 * @param port The port the channel is being established over
935 * @param options Further options
938 handle_inbound_channel (void *cls,
939 struct GNUNET_CADET_Channel *channel,
940 const struct GNUNET_PeerIdentity *initiator,
942 enum GNUNET_CADET_ChannelOption options)
944 struct peer_context *ctx;
946 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us (Peer %s).\n", GNUNET_i2s(initiator));
948 GNUNET_assert( NULL != channel );
950 // we might not even store the from_channel
952 ctx = get_peer_ctx(peer_map, initiator);
953 if (NULL != ctx->from_channel)
955 ctx->from_channel = channel;
958 // FIXME there might already be an established channel
960 //ctx->in_flags = in_other_gossip_list;
961 ctx->mq = NULL; // TODO create mq?
963 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
969 * This is called when a remote peer destroys a channel.
971 * @param cls The closure
972 * @param channel The channel being closed
973 * @param channel_ctx The context associated with this channel
976 cleanup_channel(void *cls,
977 const struct GNUNET_CADET_Channel *channel,
980 struct GNUNET_PeerIdentity *peer;
981 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
983 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
984 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
985 // Guess simply casting isn't the nicest way...
986 // FIXME wait for cadet to change this function
987 RPS_sampler_reinitialise_by_value (peer);
991 * Actually start the service.
994 rps_start (struct GNUNET_SERVER_Handle *server)
996 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
997 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
998 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1002 GNUNET_SERVER_add_handlers (server, handlers);
1003 GNUNET_SERVER_disconnect_notify (server,
1004 &handle_client_disconnect,
1006 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1009 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1010 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1012 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1019 * Process statistics requests.
1021 * @param cls closure
1022 * @param server the initialized server
1023 * @param c configuration to use
1027 struct GNUNET_SERVER_Handle *server,
1028 const struct GNUNET_CONFIGURATION_Handle *c)
1030 // TODO check what this does -- copied from gnunet-boss
1031 // - seems to work as expected
1032 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1034 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1036 struct init_peer_cls *ipc;
1042 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1043 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1044 GNUNET_assert (NULL != own_identity);
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1048 /* Get time interval from the configuration */
1049 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1054 GNUNET_SCHEDULER_shutdown();
1058 /* Get initial size of sampler/gossip list from the configuration */
1059 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1061 (long long unsigned int *) &est_size))
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1064 GNUNET_SCHEDULER_shutdown ();
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1069 //gossip_list_size = est_size; // TODO rename est_size
1072 GNUNET_array_grow (gossip_list, gossip_list_size, est_size);
1075 /* connect to NSE */
1076 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1077 // TODO check whether that was successful
1078 // TODO disconnect on shutdown
1079 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1084 // TODO initialise thresholds - ?
1086 /* Get alpha from the configuration */
1087 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1091 LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1093 LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1095 /* Get beta from the configuration */
1096 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1104 // TODO check that alpha + beta < 1
1106 peer_map = GNUNET_CONTAINER_multipeermap_create (est_size, GNUNET_NO);
1109 /* Initialise cadet */
1110 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1111 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1112 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1113 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1117 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1118 cadet_handle = GNUNET_CADET_connect (cfg,
1120 &handle_inbound_channel,
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1127 /* Initialise sampler */
1128 RPS_sampler_init (est_size, own_identity, insertCB, NULL, removeCB, NULL);
1130 /* Initialise push and pull maps */
1137 ipc = GNUNET_new (struct init_peer_cls);
1138 ipc->server = server;
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1141 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, ipc);
1143 // TODO send push/pull to each of those peers?
1148 * The main function for the rps service.
1150 * @param argc number of arguments from the command line
1151 * @param argv command line arguments
1152 * @return 0 ok, 1 on error
1155 main (int argc, char *const *argv)
1157 return (GNUNET_OK ==
1158 GNUNET_SERVICE_run (argc,
1161 GNUNET_SERVICE_OPTION_NONE,
1162 &run, NULL)) ? 0 : 1;
1165 /* end of gnunet-service-rps.c */