- wip
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
index f172011f8175e93d54ff28fe3079919f2260be4f..b7054e2c705d2246718aec1c03774883e1d76604 100644 (file)
@@ -58,30 +58,15 @@ static struct GNUNET_DHT_Handle *dht_handle;
  */
 static struct GNUNET_REGEX_announce_handle *announce_handle;
 
-/**
- * Hostkey generation context
- */
-static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
-
 /**
  * Periodically reannounce regex.
  */
 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
 
 /**
- * How often reannounce regex.
- */
-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.
+ * What's the maximum reannounce period.
  */
-static struct GNUNET_PeerIdentity my_full_id;
+static struct GNUNET_TIME_Relative reannounce_period_max;
 
 /**
  * Maximal path compression length for regex announcing.
@@ -104,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.
@@ -116,11 +106,6 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
 
-  if (NULL != keygen)
-  {
-    GNUNET_CRYPTO_rsa_key_create_stop (keygen);
-    keygen = NULL;
-  }
   if (NULL != announce_handle)
   {
     GNUNET_REGEX_announce_cancel (announce_handle);
@@ -133,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);
 }
 
 
@@ -146,7 +133,10 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 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;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
@@ -154,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,8 +158,9 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "First time, creating regex: %s\n",
                 regex);
+    memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
     announce_handle = GNUNET_REGEX_announce (dht_handle,
-                                            &my_full_id,
+                                            &id,
                                             regex,
                                             (unsigned int) max_path_compression,
                                             stats_handle);
@@ -172,14 +170,19 @@ reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_assert (NULL != announce_handle);
     GNUNET_REGEX_reannounce (announce_handle);
   }
-  reannounce_task = GNUNET_SCHEDULER_add_delayed (reannounce_freq,
-                                                  &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.
@@ -195,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);
 }
 
 
@@ -264,42 +268,6 @@ load_regexes (const char *filename, char **rx)
 }
 
 
-/**
- * Callback for hostkey read/generation
- *
- * @param cls Closure (not used).
- * @param pk The private key of the local peer.
- * @param emsg Error message if applicable.
- */
-static void
-key_generation_cb (void *cls,
-                   struct GNUNET_CRYPTO_RsaPrivateKey *pk,
-                   const char *emsg)
-{
-  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
-
-  keygen = NULL;
-  if (NULL == pk)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Regexprofiler could not access hostkey: %s. Exiting.\n"),
-                emsg);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  GNUNET_CRYPTO_rsa_key_get_public (pk, &my_public_key);
-  GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
-                      &my_full_id.hashPubKey);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Regexprofiler for peer [%s] starting\n",
-              GNUNET_i2s(&my_full_id));
-  announce_regex (rx_with_pfx);
-  GNUNET_free (rx_with_pfx);
-}
-
-
 /**
  * @brief Main function that will be run by the scheduler.
  *
@@ -314,38 +282,39 @@ run (void *cls, char *const *args GNUNET_UNUSED,
      const struct GNUNET_CONFIGURATION_Handle *cfg_)
 {
   char *regex = NULL;
-  char *keyfile;
+  char *policy_dir;
+  long long unsigned int peer_id;
 
   cfg = cfg_;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
-                                               &keyfile))
+      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
+                                             "MAX_PATH_COMPRESSION",
+                                             &max_path_compression))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "regexprofiler", "hostkey");
+                "regexprofiler", "max_path_compression");
+    global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", "MAX_PATH_COMPRESSION",
-                                             &max_path_compression))
+      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", "max_path_compression");
+                "regexprofiler", "policy_dir");
     global_ret = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "REGEXPROFILER",
-                                               "POLICY_FILE", &policy_filename))
+      GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
+                                             "PEERID", &peer_id))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
@@ -371,17 +340,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);
 
@@ -409,12 +375,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
 
   /* Announcing regexes from policy_filename */
   GNUNET_asprintf (&rx_with_pfx, "%s(%s)", regex_prefix, regex);
+  announce_regex (rx_with_pfx);
   GNUNET_free (regex);
-
-  keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
-                                               &key_generation_cb,
-                                               NULL);
-  GNUNET_free (keyfile);
+  GNUNET_free (rx_with_pfx);
 
   /* Scheduled the task to clean up when shutdown is called */
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,