- simplify parameters
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
index 3ca57b4f30fd7e701ac8cc42392640744d2160b9..17610992aa06944f75c0fb785bc1be0df4ef8f14 100644 (file)
@@ -64,14 +64,9 @@ static struct GNUNET_REGEX_announce_handle *announce_handle;
 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
 
 /**
- * How often reannounce regex.
+ * What's the maximum reannounce period.
  */
-static struct GNUNET_TIME_Relative reannounce_freq;
-
-/**
- * Random delay to spread out load on the DHT.
- */
-static struct GNUNET_TIME_Relative announce_delay;
+static struct GNUNET_TIME_Relative reannounce_period_max;
 
 /**
  * Maximal path compression length for regex announcing.
@@ -94,6 +89,11 @@ static char * regex_prefix;
  */
 static char *rx_with_pfx;
 
+/**
+ * How many put rounds should we do.
+ */
+static unsigned int rounds = 5;
+
 
 /**
  * Task run during shutdown.
@@ -118,7 +118,9 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     dht_handle = NULL;
   }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Daemon for %s shutting down\n",
+              policy_filename);
 }
 
 
@@ -132,6 +134,7 @@ static void
 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_PeerIdentity id;
+  struct GNUNET_TIME_Relative random_delay;
   char *regex = cls;
 
   reannounce_task = GNUNET_SCHEDULER_NO_TASK;
@@ -141,6 +144,13 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
   }
 
+  if (0 == rounds--)
+  {
+    global_ret = 0;
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (regex);
+    return;
+  }
   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)
@@ -161,21 +171,18 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     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);
+  random_delay =
+    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                   GNUNET_CRYPTO_random_u32 (
+                                     GNUNET_CRYPTO_QUALITY_WEAK,
+                                     reannounce_period_max.rel_value));
+  reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
+                                                  &reannounce_regex, cls);
 }
 
 
 /**
- * Announce the given regular expression using Mesh and the path compression
+ * Announce the given regular expression using regex and the path compression
  * length read from config.
  *
  * @param regex regular expression to announce on this peer's mesh.
@@ -191,11 +198,12 @@ announce_regex (const char * regex)
     return;
   }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Daemon for %s starting\n",
+              policy_filename);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
   copy = GNUNET_strdup (regex);
-  reannounce_task = GNUNET_SCHEDULER_add_delayed (announce_delay,
-                                                  reannounce_regex,
-                                                  (void *) copy);
+  reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
 }
 
 
@@ -259,6 +267,29 @@ load_regexes (const char *filename, char **rx)
   return rx_cnt;
 }
 
+/**
+ * Scan through the policy_dir looking for the n-th filename.
+ *
+ * @param cls Closure (target number n).
+ * @param filename complete filename (absolute path).
+ * @return GNUNET_OK to continue to iterate,
+ *  GNUNET_NO to stop when found
+ */
+static int
+scan (void *cls, const char *filename)
+{
+  long n = (long) cls;
+  static long c = 0;
+
+  if (c == n)
+  {
+    policy_filename = GNUNET_strdup (filename);
+    return GNUNET_NO;
+  }
+  c++;
+  return GNUNET_OK;
+}
+
 
 /**
  * @brief Main function that will be run by the scheduler.
@@ -274,11 +305,14 @@ run (void *cls, char *const *args GNUNET_UNUSED,
      const struct GNUNET_CONFIGURATION_Handle *cfg_)
 {
   char *regex = NULL;
+  char *policy_dir;
+  long long unsigned int peer_id;
 
   cfg = cfg_;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
+      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
+                                             "MAX_PATH_COMPRESSION",
                                              &max_path_compression))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -289,10 +323,21 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
-                                               "POLICY_FILE", &policy_filename))
+      GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
+                                             "POLICY_DIR", &policy_dir))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _
+                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
+                "regexprofiler", "policy_dir");
+    global_ret = GNUNET_SYSERR;
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
+                                             "PEERID", &peer_id))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
@@ -318,17 +363,14 @@ run (void *cls, char *const *args GNUNET_UNUSED,
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
-                                           "REANNOUNCE_FREQ", &reannounce_freq))
+                                           "REANNOUNCE_PERIOD_MAX",
+                                           &reannounce_period_max))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "reannounce_freq not given. Using 10 minutes.\n");
-    reannounce_freq =
+                "reannounce_period_max not given. Using 10 minutes.\n");
+    reannounce_period_max =
       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);
 
@@ -344,6 +386,8 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   }
 
   /* Read regexes from policy files */
+  GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
+                                                   (void *) (long) peer_id));
   if (0 == load_regexes (policy_filename, &regex))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,