added helper for handling the "view" in rps
[oweals/gnunet.git] / src / regex / regex_internal_dht.c
index e8788190212cc2fdd79b248a1bb2352e23c5a658..849ba20d0f324a7796c7caa6d30030eacc569295 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012, 2015 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file src/regex/regex_internal_dht.c
 
 #define LOG(kind,...) GNUNET_log_from (kind,"regex-dht",__VA_ARGS__)
 
+/**
+ * DHT replication level to use.
+ */
 #define DHT_REPLICATION 5
+
+/**
+ * DHT record lifetime to use.
+ */
 #define DHT_TTL         GNUNET_TIME_UNIT_HOURS
+
+/**
+ * DHT options to set.
+ */
 #define DHT_OPT         GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
 
 
@@ -57,7 +68,7 @@ struct REGEX_INTERNAL_Announcement
   /**
    * Automaton representation of the regex (expensive to build).
    */
-  struct REGEX_INTERNAL_Automatondfa;
+  struct REGEX_INTERNAL_Automaton *dfa;
 
   /**
    * Our private key.
@@ -77,7 +88,7 @@ struct REGEX_INTERNAL_Announcement
  * @param cls closure.
  * @param key hash for current state.
  * @param proof proof for current state.
- * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
+ * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
  * @param num_edges number of edges leaving current state.
  * @param edges edges leaving current state.
  */
@@ -95,17 +106,17 @@ regex_iterator (void *cls,
   unsigned int i;
 
   LOG (GNUNET_ERROR_TYPE_INFO,
-       "DHT PUT for state %s with proof `%s' and %u edges\n",
+       "DHT PUT for state %s with proof `%s' and %u edges:\n",
        GNUNET_h2s (key),
        proof,
        num_edges);
   for (i = 0; i < num_edges; i++)
   {
     LOG (GNUNET_ERROR_TYPE_INFO,
-         "  edge %s towards %s (%s)\n",
+         "Edge %u `%s' towards %s\n",
+         i,
          edges[i].label,
-         GNUNET_h2s (&edges[i].destination),
-         proof);
+         GNUNET_h2s (&edges[i].destination));
   }
   if (GNUNET_YES == accepting)
   {
@@ -122,7 +133,7 @@ regex_iterator (void *cls,
     ab.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_DHT_MAX_EXPIRATION));
     ab.key = *key;
     GNUNET_CRYPTO_eddsa_key_get_public (h->priv,
-                                                   &ab.peer.public_key);
+                                        &ab.peer.public_key);
     GNUNET_assert (GNUNET_OK ==
                    GNUNET_CRYPTO_eddsa_sign (h->priv,
                                            &ab.purpose,
@@ -156,9 +167,11 @@ regex_iterator (void *cls,
                   GNUNET_TIME_relative_to_absolute (DHT_TTL),
                   DHT_TTL,
                   NULL, NULL);
-  GNUNET_STATISTICS_update (h->stats, "# regex blocks stored",
+  GNUNET_STATISTICS_update (h->stats,
+                            "# regex blocks stored",
                             1, GNUNET_NO);
-  GNUNET_STATISTICS_update (h->stats, "# regex block bytes stored",
+  GNUNET_STATISTICS_update (h->stats,
+                            "# regex block bytes stored",
                             size, GNUNET_NO);
   GNUNET_free (block);
 }
@@ -166,16 +179,15 @@ regex_iterator (void *cls,
 
 /**
  * Announce a regular expression: put all states of the automaton in the DHT.
- * Does not free resources, must call REGEX_INTERNAL_announce_cancel for that.
+ * Does not free resources, must call #REGEX_INTERNAL_announce_cancel() for that.
  *
  * @param dht An existing and valid DHT service handle. CANNOT be NULL.
  * @param priv our private key, must remain valid until the announcement is cancelled
  * @param regex Regular expression to announce.
  * @param compression How many characters per edge can we squeeze?
  * @param stats Optional statistics handle to report usage. Can be NULL.
- *
  * @return Handle to reuse o free cached resources.
- *         Must be freed by calling REGEX_INTERNAL_announce_cancel.
+ *         Must be freed by calling #REGEX_INTERNAL_announce_cancel().
  */
 struct REGEX_INTERNAL_Announcement *
 REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
@@ -187,7 +199,7 @@ REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
   struct REGEX_INTERNAL_Announcement *h;
 
   GNUNET_assert (NULL != dht);
-  h = GNUNET_malloc (sizeof (struct REGEX_INTERNAL_Announcement));
+  h = GNUNET_new (struct REGEX_INTERNAL_Announcement);
   h->regex = regex;
   h->dht = dht;
   h->stats = stats;
@@ -202,7 +214,7 @@ REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
  * Announce again a regular expression previously announced.
  * Does use caching to speed up process.
  *
- * @param h Handle returned by a previous REGEX_INTERNAL_announce call.
+ * @param h Handle returned by a previous #REGEX_INTERNAL_announce call().
  */
 void
 REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h)
@@ -211,7 +223,9 @@ REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h)
   LOG (GNUNET_ERROR_TYPE_INFO,
        "REGEX_INTERNAL_reannounce: %s\n",
        h->regex);
-  REGEX_INTERNAL_iterate_reachable_edges (h->dfa, &regex_iterator, h);
+  REGEX_INTERNAL_iterate_reachable_edges (h->dfa,
+                                          &regex_iterator,
+                                          h);
 }
 
 
@@ -219,7 +233,7 @@ REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h)
  * Clear all cached data used by a regex announce.
  * Does not close DHT connection.
  *
- * @param h Handle returned by a previous REGEX_INTERNAL_announce call.
+ * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
  */
 void
 REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h)
@@ -263,7 +277,7 @@ struct RegexSearchContext
 
 
 /**
- * Type of values in 'dht_get_results'.
+ * Type of values in `dht_get_results`.
  */
 struct Result
 {
@@ -393,7 +407,7 @@ dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
 
 
 /**
- * Find a path to a peer that offers a regex servcie compatible
+ * Find a path to a peer that offers a regex service compatible
  * with a given string.
  *
  * @param key The key of the accepting state.
@@ -406,8 +420,9 @@ regex_find_path (const struct GNUNET_HashCode *key,
   struct GNUNET_DHT_GetHandle *get_h;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "regex finds path for %s\n",
-       GNUNET_h2s (key));
+       "Accept state found, now searching for paths to %s\n",
+       GNUNET_h2s (key),
+       (unsigned int) ctx->position);
   get_h = GNUNET_DHT_get_start (ctx->info->dht,    /* handle */
                                 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, /* type */
                                 key,     /* key to search */
@@ -432,9 +447,9 @@ regex_find_path (const struct GNUNET_HashCode *key,
  * @param exp when will this value expire
  * @param key key of the result
  * @param get_path path of the get request (not used)
- * @param get_path_length lenght of get_path (not used)
+ * @param get_path_length length of @a get_path (not used)
  * @param put_path path of the put request (not used)
- * @param put_path_length length of the put_path (not used)
+ * @param put_path_length length of the @a put_path (not used)
  * @param type type of the result
  * @param size number of bytes in data
  * @param data pointer to the result data
@@ -487,12 +502,12 @@ dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
 
 
 /**
- * Iterator over found existing mesh regex blocks that match an ongoing search.
+ * Iterator over found existing cadet regex blocks that match an ongoing search.
  *
  * @param cls Closure (current context)-
  * @param key Current key code (key for cached block).
  * @param value Value in the hash map (cached RegexBlock).
- * @return GNUNET_YES: we should always continue to iterate.
+ * @return #GNUNET_YES: we should always continue to iterate.
  */
 static int
 regex_result_iterator (void *cls,
@@ -519,7 +534,7 @@ regex_result_iterator (void *cls,
        GNUNET_BLOCK_is_accepting (block, result->size));
   regex_next_edge (block, result->size, ctx);
 
-  GNUNET_STATISTICS_update (ctx->info->stats, "# regex mesh blocks iterated",
+  GNUNET_STATISTICS_update (ctx->info->stats, "# regex cadet blocks iterated",
                             1, GNUNET_NO);
 
   return GNUNET_YES;
@@ -533,8 +548,7 @@ regex_result_iterator (void *cls,
  * @param token Token that follows to next state.
  * @param len Lenght of token.
  * @param key Hash of next state.
- *
- * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
+ * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise.
  */
 static int
 regex_edge_iterator (void *cls,
@@ -614,7 +628,7 @@ regex_next_edge (const struct RegexBlock *block,
   }
 
   hash = &ctx->hash;
-  new_ctx = GNUNET_malloc (sizeof (struct RegexSearchContext));
+  new_ctx = GNUNET_new (struct RegexSearchContext);
   new_ctx->info = info;
   new_ctx->position = ctx->position + ctx->longest_match;
   GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
@@ -636,9 +650,11 @@ regex_next_edge (const struct RegexBlock *block,
   GNUNET_STATISTICS_update (info->stats, "# regex nodes traversed",
                             1, GNUNET_NO);
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
-       "looking for %s\n",
-       GNUNET_h2s (hash));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Following edges at %s for offset %u in `%s'\n",
+       GNUNET_h2s (hash),
+       (unsigned int) ctx->position,
+       info->description);
   rest = &new_ctx->info->description[new_ctx->position];
   get_h =
       GNUNET_DHT_get_start (info->dht,    /* handle */
@@ -663,7 +679,7 @@ regex_next_edge (const struct RegexBlock *block,
 
 /**
  * Search for a peer offering a regex matching certain string in the DHT.
- * The search runs until REGEX_INTERNAL_search_cancel is called, even if results
+ * The search runs until #REGEX_INTERNAL_search_cancel() is called, even if results
  * are returned.
  *
  * @param dht An existing and valid DHT service handle.
@@ -671,16 +687,15 @@ regex_next_edge (const struct RegexBlock *block,
  * @param callback Callback for found peers.
  * @param callback_cls Closure for @c callback.
  * @param stats Optional statistics handle to report usage. Can be NULL.
- *
  * @return Handle to stop search and free resources.
- *         Must be freed by calling REGEX_INTERNAL_search_cancel.
+ *         Must be freed by calling #REGEX_INTERNAL_search_cancel().
  */
 struct REGEX_INTERNAL_Search *
 REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
-                     const char *string,
-                     REGEX_INTERNAL_Found callback,
-                     void *callback_cls,
-                     struct GNUNET_STATISTICS_Handle *stats)
+                       const char *string,
+                       REGEX_INTERNAL_Found callback,
+                       void *callback_cls,
+                       struct GNUNET_STATISTICS_Handle *stats)
 {
   struct REGEX_INTERNAL_Search *h;
   struct GNUNET_DHT_GetHandle *get_h;
@@ -690,10 +705,9 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
   size_t len;
 
   /* Initialize handle */
-  LOG (GNUNET_ERROR_TYPE_INFO, "REGEX_INTERNAL_search: %s\n", string);
   GNUNET_assert (NULL != dht);
   GNUNET_assert (NULL != callback);
-  h = GNUNET_malloc (sizeof (struct REGEX_INTERNAL_Search));
+  h = GNUNET_new (struct REGEX_INTERNAL_Search);
   h->dht = dht;
   h->description = GNUNET_strdup (string);
   h->callback = callback;
@@ -706,17 +720,17 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
   len = strlen (string);
   size = REGEX_INTERNAL_get_first_key (string, len, &key);
   LOG (GNUNET_ERROR_TYPE_INFO,
-       "  initial key for %s: %s (%.*s)\n",
-       string, GNUNET_h2s (&key), size, string);
-  ctx = GNUNET_malloc (sizeof (struct RegexSearchContext));
+       "Initial key for `%s' is %s (based on `%.*s')\n",
+       string,
+       GNUNET_h2s (&key),
+       size,
+       string);
+  ctx = GNUNET_new (struct RegexSearchContext);
   ctx->position = size;
   ctx->info = h;
-  GNUNET_array_append (h->contexts, h->n_contexts, ctx);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "consumed %u bits out of %u, now looking for %s\n",
-       size, len,
-       GNUNET_h2s (&key));
-
+  GNUNET_array_append (h->contexts,
+                       h->n_contexts,
+                       ctx);
   /* Start search in DHT */
   get_h = GNUNET_DHT_get_start (h->dht,    /* handle */
                                 GNUNET_BLOCK_TYPE_REGEX, /* type */
@@ -747,8 +761,8 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
  * @param cls Closure (unused).
  * @param key Current key code (unused).
  * @param value Value in the hash map (get handle).
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
+ * @return #GNUNET_YES if we should continue to iterate,
+ *         #GNUNET_NO if not.
  */
 static int
 regex_cancel_dht_get (void *cls,
@@ -763,14 +777,14 @@ regex_cancel_dht_get (void *cls,
 
 
 /**
- * Iterator over hash map entries to free MeshRegexBlocks stored during the
+ * Iterator over hash map entries to free CadetRegexBlocks stored during the
  * search for connect_by_string.
  *
  * @param cls Closure (unused).
  * @param key Current key code (unused).
- * @param value MeshRegexBlock in the hash map.
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
+ * @param value CadetRegexBlock in the hash map.
+ * @return #GNUNET_YES if we should continue to iterate,
+ *         #GNUNET_NO if not.
  */
 static int
 regex_free_result (void *cls,