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 * Get random peer from the gossip list.
268 struct GNUNET_PeerIdentity *
269 get_rand_peer(const struct GNUNET_PeerIdentity *peer_list, unsigned int list_size)
272 struct GNUNET_PeerIdentity *peer;
274 peer = GNUNET_new(struct GNUNET_PeerIdentity);
275 // FIXME if we have only NULL in gossip list this will block
276 // but then we might have a problem nevertheless
282 * Choose the r_index of the peer we want to return
283 * at random from the interval of the gossip list
285 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
288 *peer = peer_list[r_index];
289 } while (NULL == peer);
296 * Get the context of a peer. If not existing, create.
298 struct peer_context *
299 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
301 struct peer_context *ctx;
303 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
305 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
309 ctx = GNUNET_new (struct peer_context);
312 ctx->to_channel = NULL;
313 ctx->from_channel = NULL;
314 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
321 * Get the channel of a peer. If not existing, create.
323 struct GNUNET_CADET_Channel *
324 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer)
326 struct peer_context *ctx;
328 ctx = get_peer_ctx (peer_map, peer);
329 if (NULL == ctx->to_channel)
331 ctx->to_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
332 GNUNET_RPS_CADET_PORT,
333 GNUNET_CADET_OPTION_RELIABLE);
334 // do I have to explicitly put it in the peer_map?
335 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
338 return ctx->to_channel;
343 * Get the message queue of a specific peer.
345 * If we already have a message queue open to this client,
346 * simply return it, otherways create one.
348 struct GNUNET_MQ_Handle *
349 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_PeerIdentity *peer_id)
351 struct peer_context *ctx;
353 ctx = get_peer_ctx (peer_map, peer_id);
356 (void) get_channel (peer_map, peer_id);
357 ctx->mq = GNUNET_CADET_mq_create (ctx->to_channel);
358 //do I have to explicitly put it in the peer_map?
359 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
366 /***********************************************************************
368 ***********************************************************************/
371 * Function called by NSE.
373 * Updates sizes of sampler list and gossip list and adapt those lists
377 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
380 unsigned int old_est;
381 //double scale; // TODO this might go gloabal/config
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "Received a ns estimate - logest: %f, std_dev: %f (old_est: %f)\n",
387 logestimate, std_dev, old_est);
389 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
390 // GNUNET_NSE_log_estimate_to_n (logestimate);
391 estimate = pow (estimate, 1./3);
392 // TODO add if std_dev is a number
393 // estimate += (std_dev * scale);
394 if ( 0 < estimate ) {
395 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
398 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
400 /* If the NSE has changed adapt the lists accordingly */
401 // TODO respect the request rate, min, max
402 if (old_est > est_size*4)
404 RPS_sampler_resize (old_est/2);
406 else if (old_est < est_size)
408 if (est_size < old_est*2)
409 RPS_sampler_resize (old_est*2);
411 RPS_sampler_resize (est_size);
416 * Handle RPS request from the client.
419 * @param client identification of the client
420 * @param message the actual message
424 handle_cs_request (void *cls,
425 struct GNUNET_SERVER_Client *client,
426 const struct GNUNET_MessageHeader *message)
428 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
430 struct GNUNET_RPS_CS_RequestMessage *msg;
431 //unsigned int n_arr[sampler_list->size];// =
432 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
433 //struct GNUNET_MQ_Handle *mq;
434 struct client_ctx *cli_ctx;
435 struct GNUNET_MQ_Envelope *ev;
436 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
438 const struct GNUNET_PeerIdentity *peers;
441 // TODO check message size
442 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
443 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
444 if ( NULL == cli_ctx ) {
445 cli_ctx = GNUNET_new(struct client_ctx);
446 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
447 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
450 // How many peers do we give back?
451 // Wait until we have enough random peers?
453 num_peers = GNUNET_ntohll (msg->num_peers);
455 ev = GNUNET_MQ_msg_extra (out_msg,
456 num_peers * sizeof (struct GNUNET_PeerIdentity),
457 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
458 out_msg->num_peers = msg->num_peers; // No conversion between network and network order
460 //&out_msg[1] = RPS_sampler_get_n_rand_peers (num_peers);
461 peers = RPS_sampler_get_n_rand_peers (num_peers);
464 num_peers * sizeof (struct GNUNET_PeerIdentity));
466 GNUNET_MQ_send (cli_ctx->mq, ev);
467 //GNUNET_MQ_destroy(mq);
469 GNUNET_SERVER_receive_done (client,
474 * Handle a PUSH message from another peer.
476 * Check the proof of work and store the PeerID
477 * in the temporary list for pushed PeerIDs.
480 * @param channel The channel the PUSH was received over
481 * @param channel_ctx The context associated with this channel
482 * @param msg The message header
485 handle_peer_push (void *cls,
486 struct GNUNET_CADET_Channel *channel,
488 const struct GNUNET_MessageHeader *msg)
490 const struct GNUNET_PeerIdentity *peer;
492 // (check the proof of work)
494 // TODO accept empty message
495 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PushMessage))
497 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
498 return GNUNET_SYSERR;
501 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
502 // FIXME wait for cadet to change this function
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
505 /* Add the sending peer to the push_list */
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer to push_list of size %u\n", push_list_size);
507 GNUNET_array_append (push_list, push_list_size, *peer);
508 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size of push_list is now %u\n", push_list_size);
514 * Handle PULL REQUEST request message from another peer.
516 * Reply with the gossip list of PeerIDs.
519 * @param channel The channel the PUSH was received over
520 * @param channel_ctx The context associated with this channel
521 * @param msg The message header
524 handle_peer_pull_request (void *cls,
525 struct GNUNET_CADET_Channel *channel,
527 const struct GNUNET_MessageHeader *msg)
529 struct GNUNET_PeerIdentity *peer;
530 struct GNUNET_MQ_Handle *mq;
531 struct GNUNET_MQ_Envelope *ev;
532 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
534 // assert that msg->size is 0
536 // TODO accept empty message
537 if (ntohs(msg->size) != sizeof (struct GNUNET_RPS_P2P_PullRequestMessage))
539 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
540 return GNUNET_SYSERR;
543 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
544 // FIXME wait for cadet to change this function
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s (peer));
547 mq = get_mq (peer_map, peer);
549 ev = GNUNET_MQ_msg_extra (out_msg,
550 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
551 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
552 out_msg->num_peers = GNUNET_htonll (gossip_list_size);
553 memcpy (&out_msg[1], gossip_list,
554 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
556 GNUNET_MQ_send (mq, ev);
562 * Handle PULL REPLY message from another peer.
564 * Check whether we sent a corresponding request and
565 * whether this reply is the first one.
568 * @param channel The channel the PUSH was received over
569 * @param channel_ctx The context associated with this channel
570 * @param msg The message header
573 handle_peer_pull_reply (void *cls,
574 struct GNUNET_CADET_Channel *channel,
576 const struct GNUNET_MessageHeader *msg)
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
580 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
581 struct GNUNET_PeerIdentity *peers;
584 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) < ntohs (msg->size))
586 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
587 return GNUNET_SYSERR;
589 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
590 if (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) / sizeof (struct GNUNET_PeerIdentity) != GNUNET_ntohll (in_msg->num_peers))
593 return GNUNET_SYSERR;
596 // TODO check that we sent a request and that it is the first reply
598 peers = (struct GNUNET_PeerIdentity *) &msg[1];
599 for ( i = 0 ; i < GNUNET_ntohll (in_msg->num_peers) ; i++ )
601 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
604 // TODO check that id is valid - whether it is reachable
611 * Send out PUSHes and PULLs.
613 * This is executed regylary.
616 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
618 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
621 //unsigned int *n_arr;
622 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
623 struct GNUNET_RPS_P2P_PushMessage *push_msg;
624 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
625 struct GNUNET_MQ_Envelope *ev;
626 const struct GNUNET_PeerIdentity *peer;
627 struct GNUNET_MQ_Handle *mq;
629 // TODO print lists, ...
630 // TODO rendomise and spread calls herein over time
633 /* Would it make sense to have one shuffeled gossip list and then
634 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
635 * use the rest to update sampler?
636 * in essence get random peers with consumption */
639 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
640 n_peers = round (alpha * gossip_list_size);
643 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
644 n_peers, alpha, gossip_list_size);
645 for ( i = 0 ; i < n_peers ; i++ )
646 { // TODO compute length
647 peer = get_rand_peer (gossip_list, gossip_list_size);
648 if (own_identity != peer)
649 { // FIXME if this fails schedule/loop this for later
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
652 ev = GNUNET_MQ_msg (push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
653 //ev = GNUNET_MQ_msg_extra();
654 /* TODO Compute proof of work here
656 //push_msg->placeholder = 0;
658 // FIXME sometimes it returns a pointer to a freed mq
659 mq = get_mq (peer_map, peer);
660 GNUNET_MQ_send (mq, ev);
662 // modify in_flags of respective peer?
667 /* Send PULL requests */
668 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
669 n_peers = round (beta * gossip_list_size);
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
673 n_peers, beta, 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 PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
681 ev = GNUNET_MQ_msg (pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
682 //pull_msg->placeholder = 0;
684 mq = get_mq (peer_map, peer);
685 GNUNET_MQ_send (mq, ev);
686 // modify in_flags of respective peer?
691 /* Update gossip list */
694 if ( push_list_size <= alpha * gossip_list_size &&
695 push_list_size != 0 &&
696 pull_list_size != 0 )
698 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
700 uint64_t first_border;
701 uint64_t second_border;
703 GNUNET_array_grow(gossip_list, gossip_list_size, est_size);
705 first_border = round(alpha * gossip_list_size);
706 for ( i = 0 ; i < first_border ; i++ )
707 { // TODO use RPS_sampler_get_n_rand_peers
708 /* Update gossip list with peers received through PUSHes */
709 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
711 gossip_list[i] = push_list[r_index];
712 // TODO change the in_flags accordingly
715 second_border = first_border + round(beta * gossip_list_size);
716 for ( i = first_border ; i < second_border ; i++ )
718 /* Update gossip list with peers received through PULLs */
719 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
721 gossip_list[i] = pull_list[r_index];
722 // TODO change the in_flags accordingly
725 for ( i = second_border ; i < gossip_list_size ; i++ )
727 /* Update gossip list with peers from history */
728 peer = RPS_sampler_get_n_rand_peers (1),
729 gossip_list[i] = *peer;
730 // TODO change the in_flags accordingly
736 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
738 // TODO independent of that also get some peers from CADET_get_peers()?
741 /* Update samplers */
743 for ( i = 0 ; i < push_list_size ; i++ )
745 RPS_sampler_update_list (&push_list[i]);
749 for ( i = 0 ; i < pull_list_size ; i++ )
751 RPS_sampler_update_list (&pull_list[i]);
756 /* Empty push/pull lists */
757 GNUNET_array_grow (push_list, push_list_size, 0);
758 GNUNET_array_grow (pull_list, pull_list_size, 0);
761 /* Schedule next round */
762 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
763 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
768 * Open a connection to given peer and store channel and mq.
771 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
773 // We open a channel to be notified when this peer goes down.
774 (void) get_channel (peer_map, id);
779 * Close the connection to given peer and delete channel and mq.
782 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
785 struct peer_context *ctx;
787 s = RPS_sampler_count_id (id);
790 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
792 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
793 if (NULL != ctx->to_channel)
797 GNUNET_MQ_destroy (ctx->mq);
799 // may already be freed at shutdown of cadet
800 //GNUNET_CADET_channel_destroy (ctx->to_channel);
803 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
809 rps_start (struct GNUNET_SERVER_Handle *server);
812 * This is called from GNUNET_CADET_get_peers().
814 * It is called on every peer(ID) that cadet somehow has contact with.
815 * We use those to initialise the sampler.
818 init_peer_cb (void *cls,
819 const struct GNUNET_PeerIdentity *peer,
820 int tunnel, // "Do we have a tunnel towards this peer?"
821 unsigned int n_paths, // "Number of known paths towards this peer"
822 unsigned int best_path) // "How long is the best path?
823 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
825 struct init_peer_cls *ipc;
827 ipc = (struct init_peer_cls *) cls;
830 LOG (GNUNET_ERROR_TYPE_DEBUG,
831 "Got %" PRIX32 ". peer %s (at %p) from CADET (gossip_list_size: %u)\n",
832 ipc->i, GNUNET_i2s (peer), peer, gossip_list_size);
833 RPS_sampler_update_list (peer);
834 (void) get_peer_ctx (peer_map, peer); // unneeded? -> insertCB
836 if (ipc->i < gossip_list_size)
838 gossip_list[ipc->i] = *peer; // FIXME sometimes we're writing to invalid space here
839 // not sure whether fixed
843 // send push/pull to each of those peers?
847 if (ipc->i < gossip_list_size)
849 memcpy(&gossip_list[ipc->i],
850 RPS_sampler_get_n_rand_peers (1),
851 (gossip_list_size - ipc->i) * sizeof(struct GNUNET_PeerIdentity));
853 rps_start (ipc->server);
860 * Task run during shutdown.
866 shutdown_task (void *cls,
867 const struct GNUNET_SCHEDULER_TaskContext *tc)
869 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
871 if ( NULL != do_round_task )
873 GNUNET_SCHEDULER_cancel (do_round_task);
874 do_round_task = NULL;
877 GNUNET_NSE_disconnect(nse);
878 GNUNET_CADET_disconnect(cadet_handle);
879 GNUNET_free(own_identity);
880 RPS_sampler_destroy();
881 GNUNET_array_grow(gossip_list, gossip_list_size, 0);
882 GNUNET_array_grow(push_list, push_list_size, 0);
883 GNUNET_array_grow(pull_list, pull_list_size, 0);
888 * A client disconnected. Remove all of its data structure entries.
890 * @param cls closure, NULL
891 * @param client identification of the client
894 handle_client_disconnect (void *cls,
895 struct GNUNET_SERVER_Client * client)
900 * Handle the channel a peer opens to us.
902 * @param cls The closure
903 * @param channel The channel the peer wants to establish
904 * @param initiator The peer's peer ID
905 * @param port The port the channel is being established over
906 * @param options Further options
909 handle_inbound_channel (void *cls,
910 struct GNUNET_CADET_Channel *channel,
911 const struct GNUNET_PeerIdentity *initiator,
913 enum GNUNET_CADET_ChannelOption options)
915 struct peer_context *ctx;
917 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us (Peer %s).\n", GNUNET_i2s(initiator));
919 GNUNET_assert( NULL != channel );
921 // we might not even store the from_channel
923 ctx = get_peer_ctx(peer_map, initiator);
924 if (NULL != ctx->from_channel)
926 ctx->from_channel = channel;
929 // FIXME there might already be an established channel
931 //ctx->in_flags = in_other_gossip_list;
932 ctx->mq = NULL; // TODO create mq?
934 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
940 * This is called when a remote peer destroys a channel.
942 * @param cls The closure
943 * @param channel The channel being closed
944 * @param channel_ctx The context associated with this channel
947 cleanup_channel(void *cls,
948 const struct GNUNET_CADET_Channel *channel,
951 struct GNUNET_PeerIdentity *peer;
952 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
954 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
955 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
956 // Guess simply casting isn't the nicest way...
957 // FIXME wait for cadet to change this function
958 RPS_sampler_reinitialise_by_value (peer);
962 * Actually start the service.
965 rps_start (struct GNUNET_SERVER_Handle *server)
967 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
968 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
969 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
973 GNUNET_SERVER_add_handlers (server, handlers);
974 GNUNET_SERVER_disconnect_notify (server,
975 &handle_client_disconnect,
977 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
980 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
981 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
983 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
990 * Process statistics requests.
993 * @param server the initialized server
994 * @param c configuration to use
998 struct GNUNET_SERVER_Handle *server,
999 const struct GNUNET_CONFIGURATION_Handle *c)
1001 // TODO check what this does -- copied from gnunet-boss
1002 // - seems to work as expected
1003 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1005 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1007 struct init_peer_cls *ipc;
1013 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1014 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1015 GNUNET_assert (NULL != own_identity);
1016 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1019 /* Get time interval from the configuration */
1020 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1025 GNUNET_SCHEDULER_shutdown();
1029 /* Get initial size of sampler/gossip list from the configuration */
1030 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1032 (long long unsigned int *) &est_size))
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1035 GNUNET_SCHEDULER_shutdown ();
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1040 //gossip_list_size = est_size; // TODO rename est_size
1043 GNUNET_array_grow (gossip_list, gossip_list_size, est_size);
1046 /* connect to NSE */
1047 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1048 // TODO check whether that was successful
1049 // TODO disconnect on shutdown
1050 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1055 // TODO initialise thresholds - ?
1057 /* Get alpha from the configuration */
1058 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1062 LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1064 LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1066 /* Get beta from the configuration */
1067 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1075 // TODO check that alpha + beta < 1
1077 peer_map = GNUNET_CONTAINER_multipeermap_create (est_size, GNUNET_NO);
1080 /* Initialise cadet */
1081 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1082 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1083 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1084 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1088 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1089 cadet_handle = GNUNET_CADET_connect (cfg,
1091 &handle_inbound_channel,
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1098 /* Initialise sampler */
1099 RPS_sampler_init (est_size, own_identity, insertCB, NULL, removeCB, NULL);
1101 /* Initialise push and pull maps */
1108 ipc = GNUNET_new (struct init_peer_cls);
1109 ipc->server = server;
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1112 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, ipc);
1114 // TODO send push/pull to each of those peers?
1119 * The main function for the rps service.
1121 * @param argc number of arguments from the command line
1122 * @param argv command line arguments
1123 * @return 0 ok, 1 on error
1126 main (int argc, char *const *argv)
1128 return (GNUNET_OK ==
1129 GNUNET_SERVICE_run (argc,
1132 GNUNET_SERVICE_OPTION_NONE,
1133 &run, NULL)) ? 0 : 1;
1136 /* end of gnunet-service-rps.c */