add completion callback for overlay topology configure functions
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
index 75d3f9c7ec29c99cc70b38be775b36098c87d5a9..591cda331071d1cdd8c5b58f24eee57729fbf631 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012,2013 Christian Grothoff
+     (C) 2012, 2013 Christian Grothoff
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -19,7 +19,7 @@
 */
 
 /**
- * @file mesh/gnunet-daemon-regexprofiler.c
+ * @file regex/gnunet-daemon-regexprofiler.c
  * @brief daemon that uses mesh to announce a regular expression. Used in
  * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
  * without the need to explicitly connect to the mesh service running on the
@@ -73,6 +73,11 @@ static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
  */
 static struct GNUNET_TIME_Relative reannounce_freq;
 
+/**
+ * Random delay to spread out load on the DHT.
+ */
+static struct GNUNET_TIME_Relative announce_delay;
+
 /**
  * Local peer's PeerID.
  */
@@ -135,18 +140,49 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Announce a previously announced regex re-using cached data.
  * 
- * @param cls Clocuse (not used).
+ * @param cls Closure (regex to announce if needed).
  * @param tc TaskContext.
  */
 static void
 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  char *regex = cls;
   reannounce_task = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    GNUNET_free (regex);
     return;
-  reannounce_task = GNUNET_SCHEDULER_add_delayed(reannounce_freq,
-                                                 &reannounce_regex,
-                                                 cls);
+  }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
+  GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
+  if (NULL == announce_handle && NULL != regex)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "First time, creating regex: %s\n",
+                regex);
+    announce_handle = GNUNET_REGEX_announce (dht_handle,
+                                            &my_full_id,
+                                            regex,
+                                            (unsigned int) max_path_compression,
+                                            stats_handle);
+  }
+  else
+  {
+    GNUNET_assert (NULL != announce_handle);
+    GNUNET_REGEX_reannounce (announce_handle);
+  }
+
+  reannounce_task = 
+    GNUNET_SCHEDULER_add_delayed (
+      GNUNET_TIME_relative_add (reannounce_freq,
+                                GNUNET_TIME_relative_multiply (
+                                  GNUNET_TIME_UNIT_SECONDS,
+                                  GNUNET_CRYPTO_random_u32 (
+                                    GNUNET_CRYPTO_QUALITY_WEAK,
+                                    600))),
+      &reannounce_regex,
+      cls);
 }
 
 
@@ -159,23 +195,19 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 announce_regex (const char * regex)
 {
+  char *copy;
+
   if (NULL == regex || 0 == strlen (regex))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
     return;
   }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
-  GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
-  announce_handle = GNUNET_REGEX_announce (dht_handle,
-                                           NULL,
-                                           regex,
-                                           (unsigned int) max_path_compression,
-                                           stats_handle);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
-  reannounce_task = GNUNET_SCHEDULER_add_delayed (reannounce_freq,
+  copy = GNUNET_strdup (regex);
+  reannounce_task = GNUNET_SCHEDULER_add_delayed (announce_delay,
                                                   reannounce_regex,
-                                                  NULL);
+                                                  (void *) copy);
 }
 
 
@@ -349,12 +381,15 @@ run (void *cls, char *const *args GNUNET_UNUSED,
       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
                                            "REANNOUNCE_FREQ", &reannounce_freq))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR
                 "reannounce_freq not given. Using 10 minutes.\n");
     reannounce_freq =
-      GNUNET_TIME_relative_multiply(GNUNET_TIME_relative_get_minute_(), 10);
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
 
   }
+    announce_delay =
+    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+                                   GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 600));
 
   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
 
@@ -411,7 +446,6 @@ main (int argc, char *const *argv)
 
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
     return 2;
-  sleep (60);
   return (GNUNET_OK ==
           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
                               gettext_noop