From 24d2c11a025e4a96d7fc0cb1ff4f4537e2003026 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20B=C3=BCnger?= Date: Sat, 14 May 2016 19:41:37 +0000 Subject: [PATCH] rps: use stored peers at startup --- src/rps/gnunet-service-rps.c | 28 ++++++++++++- src/rps/gnunet-service-rps_peers.c | 64 +++++++++++++++++++++++++++++- src/rps/gnunet-service-rps_peers.h | 26 ++++++++++++ 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c index c7cdccdfb..f56627a54 100644 --- a/src/rps/gnunet-service-rps.c +++ b/src/rps/gnunet-service-rps.c @@ -698,7 +698,6 @@ got_peer (const struct GNUNET_PeerIdentity *peer) } } - /** * @brief Checks if there is a sending channel and if it is needed * @@ -2019,6 +2018,31 @@ init_peer_cb (void *cls, } } +/** + * @brief Iterator function over stored, valid peers. + * + * We initialise the sampler with those. + * + * @param cls the closure + * @param peer the peer id + * @return #GNUNET_YES if we should continue to + * iterate, + * #GNUNET_NO if not. + */ +static int +valid_peers_iterator (void *cls, + const struct GNUNET_PeerIdentity *peer) +{ + if (NULL != peer) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Got stored, valid peer %s\n", + GNUNET_i2s (peer)); + got_peer (peer); + } + return GNUNET_YES; +} + /** * Iterator over peers from peerinfo. @@ -2316,6 +2340,8 @@ run (void *cls, GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL); // TODO send push/pull to each of those peers? // TODO read stored valid peers from last run + LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n"); + Peers_get_valid_peers (valid_peers_iterator, NULL); peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, diff --git a/src/rps/gnunet-service-rps_peers.c b/src/rps/gnunet-service-rps_peers.c index 93c9bef86..29b08adc8 100644 --- a/src/rps/gnunet-service-rps_peers.c +++ b/src/rps/gnunet-service-rps_peers.c @@ -203,6 +203,22 @@ struct PeerContext */ }; +/** + * @brief Closure to #valid_peer_iterator + */ +struct PeersIteratorCls +{ + /** + * Iterator function + */ + PeersIterator iterator; + + /** + * Closure to iterator + */ + void *cls; +}; + /** * @brief Hashmap of valid peers. */ @@ -919,7 +935,53 @@ Peers_terminate () GNUNET_CONTAINER_multipeermap_destroy (valid_peers); } -// TODO store valid peers + +/** + * Iterator over #valid_peers hash map entries. + * + * @param cls closure - unused + * @param peer current peer id + * @param value value in the hash map - unused + * @return #GNUNET_YES if we should continue to + * iterate, + * #GNUNET_NO if not. + */ +static int +valid_peer_iterator (void *cls, + const struct GNUNET_PeerIdentity *peer, + void *value) +{ + struct PeersIteratorCls *it_cls = cls; + + return it_cls->iterator (it_cls->cls, + peer); +} + + +/** + * @brief Get all currently known, valid peer ids. + * + * @param it function to call on each peer id + * @param it_cls extra argument to @a it + * @return the number of key value pairs processed, + * #GNUNET_SYSERR if it aborted iteration + */ +int +Peers_get_valid_peers (PeersIterator iterator, + void *it_cls) +{ + struct PeersIteratorCls *cls; + int ret; + + cls = GNUNET_new (struct PeersIteratorCls); + cls->iterator = iterator; + cls->cls = it_cls; + ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers, + valid_peer_iterator, + cls); + GNUNET_free (cls); + return ret; +} /** * @brief Add peer to known peers. diff --git a/src/rps/gnunet-service-rps_peers.h b/src/rps/gnunet-service-rps_peers.h index 8a163f8cf..2a7bae6f4 100644 --- a/src/rps/gnunet-service-rps_peers.h +++ b/src/rps/gnunet-service-rps_peers.h @@ -99,6 +99,19 @@ enum Peers_ChannelRole */ typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer); +/** + * @brief Iterator over valid peers. + * + * @param cls closure + * @param peer current public peer id + * @return #GNUNET_YES if we should continue to + * iterate, + * #GNUNET_NO if not. + */ +typedef int +(*PeersIterator) (void *cls, + const struct GNUNET_PeerIdentity *peer); + /** * @brief Initialise storage of peers * @@ -117,6 +130,19 @@ Peers_initialise (char* fn_valid_peers, void Peers_terminate (); + +/** + * @brief Get all currently known, valid peer ids. + * + * @param it function to call on each peer id + * @param it_cls extra argument to @a it + * @return the number of key value pairs processed, + * #GNUNET_SYSERR if it aborted iteration + */ +int +Peers_get_valid_peers (PeersIterator iterator, + void *it_cls); + /** * @brief Add peer to known peers. * -- 2.25.1