rps: use stored peers at startup
authorJulius Bünger <buenger@mytum.de>
Sat, 14 May 2016 19:41:37 +0000 (19:41 +0000)
committerJulius Bünger <buenger@mytum.de>
Sat, 14 May 2016 19:41:37 +0000 (19:41 +0000)
src/rps/gnunet-service-rps.c
src/rps/gnunet-service-rps_peers.c
src/rps/gnunet-service-rps_peers.h

index c7cdccdfb7f61f5b8ff79c080ace1a9c8fcd7bd4..f56627a54ca67f39b2dba5124781ab89ed89a9d2 100644 (file)
@@ -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,
index 93c9bef867f79d41bfcd639ab5235e26c858aa5f..29b08adc84116973b67030dfdcf1e10a1625682d 100644 (file)
@@ -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.
index 8a163f8cf7b1804b5f9e277a17a5860956b6e1ee..2a7bae6f41250e26f08605d24266af7f4ed7bbf5 100644 (file)
@@ -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.
  *