- implementation for mantis 0002485
[oweals/gnunet.git] / src / topology / gnunet-daemon-topology.c
index a9dbd3573bd2a03997f05a52071918ad74031127..3578b9e9be3e9b1c03841a8c9a664114c0044245 100644 (file)
 #include "gnunet_util_lib.h"
 
 
+/**
+ * Minimum required delay between calls to GNUNET_TRANSPORT_try_connect.
+ */
+#define MAX_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
+
 /**
  * For how long do we blacklist a peer after a failed connection
  * attempt?  This is the baseline factor which is then multiplied by
@@ -121,6 +126,11 @@ struct Peer
    */
   GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
 
+  /**
+   * Task for issuing GNUNET_TRANSPORT_try_connect for this peer.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task;
+
   /**
    * ID of task we use to clear peers from the greylist.
    */
@@ -187,6 +197,11 @@ static struct GNUNET_STATISTICS_Handle *stats;
  */
 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
 
+/**
+ * When can we next ask transport to create a connection?
+ */
+static struct GNUNET_TIME_Absolute next_connect_attempt;
+
 /**
  * Task scheduled to try to add peers.
  */
@@ -301,7 +316,7 @@ is_connection_allowed (struct Peer *peer)
  * @return GNUNET_YES (always: continue to iterate)
  */
 static int
-free_peer (void *cls, const GNUNET_HashCode * pid, void *value)
+free_peer (void *cls, const struct GNUNET_HashCode * pid, void *value)
 {
   struct Peer *pos = value;
 
@@ -312,6 +327,8 @@ free_peer (void *cls, const GNUNET_HashCode * pid, void *value)
     GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
   if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
+  if (pos->attempt_connect_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (pos->attempt_connect_task);
   if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
   GNUNET_free_non_null (pos->hello);
@@ -378,6 +395,51 @@ attempt_connect (struct Peer *pos)
 }
 
 
+/**
+ * Try to connect to the specified peer.
+ *
+ * @param cls peer to connect to
+ * @param tc scheduler context
+ */
+static void
+do_attempt_connect (void *cls,
+                   const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Peer *pos = cls;
+  struct GNUNET_TIME_Relative delay;
+
+  pos->attempt_connect_task = GNUNET_SCHEDULER_NO_TASK;
+  if (GNUNET_YES == pos->is_connected)
+    return;
+  delay = GNUNET_TIME_absolute_get_remaining (next_connect_attempt);
+  if (delay.rel_value > 0)
+  {
+    pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                             &do_attempt_connect,
+                                                             pos);
+    return;
+  }
+  next_connect_attempt = GNUNET_TIME_relative_to_absolute (MAX_CONNECT_FREQUENCY_DELAY);
+  attempt_connect (pos);
+}
+
+
+/**
+ * Schedule a task to try to connect to the specified peer.
+ *
+ * @param pos peer to connect to
+ */
+static void
+schedule_attempt_connect (struct Peer *pos)
+{
+  if (GNUNET_SCHEDULER_NO_TASK != pos->attempt_connect_task)
+    return;
+  pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
+                                                           &do_attempt_connect,
+                                                           pos);
+}
+
+
 /**
  * Discard peer entries for greylisted peers
  * where the greylisting has expired.
@@ -395,7 +457,7 @@ remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
   if (rem.rel_value == 0)
   {
-    attempt_connect (pos);
+    schedule_attempt_connect (pos);
   }
   else
   {
@@ -511,7 +573,7 @@ struct FindAdvHelloContext
  * @return GNUNET_YES (continue iteration)
  */
 static int
-find_advertisable_hello (void *cls, const GNUNET_HashCode * pid, void *value)
+find_advertisable_hello (void *cls, const struct GNUNET_HashCode * pid, void *value)
 {
   struct FindAdvHelloContext *fah = cls;
   struct Peer *pos = value;
@@ -597,7 +659,7 @@ schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return GNUNET_YES (always)
  */
 static int
-reschedule_hellos (void *cls, const GNUNET_HashCode * pid, void *value)
+reschedule_hellos (void *cls, const struct GNUNET_HashCode * pid, void *value)
 {
   struct Peer *peer = value;
   struct Peer *skip = cls;
@@ -681,11 +743,11 @@ connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @return GNUNET_YES (continue to iterate)
  */
 static int
-try_add_peers (void *cls, const GNUNET_HashCode * pid, void *value)
+try_add_peers (void *cls, const struct GNUNET_HashCode * pid, void *value)
 {
   struct Peer *pos = value;
 
-  attempt_connect (pos);
+  schedule_attempt_connect (pos);
   return GNUNET_YES;
 }
 
@@ -705,6 +767,7 @@ add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
 }
 
+
 /**
  * Method called whenever a peer disconnects.
  *
@@ -910,7 +973,7 @@ process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
               GNUNET_i2s (peer));
-  attempt_connect (pos);
+  schedule_attempt_connect (pos);
 }
 
 
@@ -1256,7 +1319,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
   transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
   handle =
-      GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_notify,
+      GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify,
                            &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
                            handlers);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
@@ -1293,6 +1356,9 @@ main (int argc, char *const *argv)
   };
   int ret;
 
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+
   ret =
       (GNUNET_OK ==
        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",