adding DEBUG logic for #3863
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_datacache.c
index dd6810f451c5510f52ab103c5d751d054655b185..e10a2e2c3da2a166324eef5c97f6ac3fa559256c 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2011, 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
 
      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 dht/gnunet-service-dht_datacache.c
+ * @file dht/gnunet-service-xdht_datacache.c
  * @brief GNUnet DHT service's datacache integration
  * @author Christian Grothoff
  * @author Nathan Evans
@@ -34,6 +33,8 @@
 
 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
 
+#define DEBUG(...)                                           \
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
 /**
  * Handle to the datacache service (for inserting/retrieving data)
@@ -47,18 +48,19 @@ static struct GNUNET_DATACACHE_Handle *datacache;
  *
  * @param expiration when will the reply expire
  * @param key the query this reply is for
- * @param put_path_length number of peers in 'put_path'
+ * @param put_path_length number of peers in @a put_path
  * @param put_path path the reply took on put
  * @param type type of the reply
- * @param data_size number of bytes in 'data'
+ * @param data_size number of bytes in @a data
  * @param data application payload data
  */
 void
 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
-                          const struct GNUNET_HashCode * key,
+                          const struct GNUNET_HashCode *key,
                           unsigned int put_path_length,
                           const struct GNUNET_PeerIdentity *put_path,
-                          enum GNUNET_BLOCK_Type type, size_t data_size,
+                          enum GNUNET_BLOCK_Type type,
+                          size_t data_size,
                           const void *data)
 {
   int r;
@@ -66,7 +68,7 @@ GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
   if (NULL == datacache)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("%s request received, but have no datacache!\n"), "PUT");
+                "PUT request received, but have no datacache!\n");
     return;
   }
   if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
@@ -74,19 +76,31 @@ GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
     GNUNET_break (0);
     return;
   }
+
   /* Put size is actual data size plus struct overhead plus path length (if any) */
-  GNUNET_STATISTICS_update (GDS_stats,
-                            gettext_noop ("# ITEMS stored in datacache"), 1,
-                            GNUNET_NO);
-  r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
-                            put_path_length, put_path);
+  r = GNUNET_DATACACHE_put (datacache,
+                            key,
+                            data_size,
+                            data,
+                            type,
+                            expiration,
+                            put_path_length,
+                            put_path);
+  if (GNUNET_OK == r)
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop ("# ITEMS stored in datacache"), 1,
+                              GNUNET_NO);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
-       GNUNET_h2s (key), data_size, r, put_path_length);
+       GNUNET_h2s (key),
+       data_size,
+       r,
+       put_path_length);
 }
 
+
 /**
- * List of peers in the get path. 
+ * List of peers in the get path.
  */
 struct GetPath
 {
@@ -94,16 +108,16 @@ struct GetPath
    * Pointer to next item in the list
    */
   struct GetPath *next;
-  
+
   /**
    * Pointer to previous item in the list
    */
   struct GetPath *prev;
-  
+
   /**
-   *  An element in the get path. 
+   *  An element in the get path.
    */
-  struct GNUNET_PeerIdentity peer;  
+  struct GNUNET_PeerIdentity peer;
 };
 
 
@@ -133,39 +147,40 @@ struct GetRequestContext
   size_t xquery_size;
 
   /**
-   * Mutator value for the reply_bf, see gnunet_block_lib.h
+   * Mutator value for the @e reply_bf, see gnunet_block_lib.h
    */
   uint32_t reply_bf_mutator;
-  
+
   /**
-   * Total number of peers in get path. 
+   * Total number of peers in get path.
    */
   unsigned int get_path_length;
-  
+
   /**
    * Return value to give back.
    */
   enum GNUNET_BLOCK_EvaluationResult eval;
-  
+
   /**
    * Peeer which has the data for the key.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
    * Next hop to forward the get result to.
    */
   struct GNUNET_PeerIdentity next_hop;
-  
+
   /**
    * Head of get path.
    */
   struct GetPath *head;
-  
+
   /**
    * Tail of get path.
    */
   struct GetPath *tail;
+
   /* get_path */
 };
 
@@ -173,36 +188,46 @@ struct GetRequestContext
 /**
  * Iterator for local get request results,
  *
- * @param cls closure for iterator, a DatacacheGetContext
- * @param exp when does this value expire?
- * @param key the key this data is stored under
+ * @param cls closure for iterator, a `struct GetRequestContext`
+ * @param key the key this @a data is stored under
  * @param size the size of the data identified by key
  * @param data the actual data
- * @param type the type of the data
- * @param put_path_length number of peers in 'put_path'
+ * @param type the type of the @a data
+ * @param exp when does this value expire?
+ * @param put_path_length number of peers in @a put_path
  * @param put_path path the reply took on put
- * @return GNUNET_OK to continue iteration, anything else
+ * @return #GNUNET_OK to continue iteration, anything else
  * to stop iteration.
  */
 static int
 datacache_get_iterator (void *cls,
-                        const struct GNUNET_HashCode * key, size_t size,
-                        const char *data, enum GNUNET_BLOCK_Type type,
-                       struct GNUNET_TIME_Absolute exp,
-                       unsigned int put_path_length,
-                       const struct GNUNET_PeerIdentity *put_path)
+                        const struct GNUNET_HashCode *key,
+                        size_t size,
+                        const char *data,
+                        enum GNUNET_BLOCK_Type type,
+                        struct GNUNET_TIME_Absolute exp,
+                        unsigned int put_path_length,
+                        const struct GNUNET_PeerIdentity *put_path)
 {
   struct GetRequestContext *ctx = cls;
   enum GNUNET_BLOCK_EvaluationResult eval;
 
   eval =
-      GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
-                             ctx->reply_bf_mutator, ctx->xquery,
-                             ctx->xquery_size, data, size);
+      GNUNET_BLOCK_evaluate (GDS_block_context,
+                             type,
+                             GNUNET_BLOCK_EO_NONE,
+                             key,
+                             ctx->reply_bf,
+                             ctx->reply_bf_mutator,
+                             ctx->xquery,
+                             ctx->xquery_size,
+                             data,
+                             size);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Found reply for query %s in datacache, evaluation result is %d\n",
        GNUNET_h2s (key), (int) eval);
   ctx->eval = eval;
+
   switch (eval)
   {
   case GNUNET_BLOCK_EVALUATION_OK_MORE:
@@ -211,26 +236,30 @@ datacache_get_iterator (void *cls,
     GNUNET_STATISTICS_update (GDS_stats,
                               gettext_noop
                               ("# Good RESULTS found in datacache"), 1,
-                              GNUNET_NO); 
+                              GNUNET_NO);
     struct GNUNET_PeerIdentity *get_path;
-    get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
+    get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
+                              ctx->get_path_length);
     struct GetPath *iterator;
     iterator = ctx->head;
     int i = 0;
     while (i < ctx->get_path_length)
     {
-      memcpy (&get_path[i], &(iterator->peer), sizeof (struct GNUNET_PeerIdentity));
+      get_path[i] = iterator->peer;
       i++;
       iterator = iterator->next;
     }
-    GDS_NEIGHBOURS_send_get_result (exp, key, put_path_length, put_path,
-                                    type, size, data, get_path, ctx->get_path_length,
-                                    &(ctx->next_hop),
-                                    &(ctx->source_peer));
-    
-    /* forward to other peers */
-    GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL,
-                         data, size);
+    GDS_NEIGHBOURS_send_get_result (key,type,
+                                    &ctx->next_hop,
+                                    &ctx->source_peer,
+                                    put_path_length,
+                                    put_path,
+                                    ctx->get_path_length,
+                                    get_path,
+                                    exp,
+                                    data,
+                                    size);
+    GNUNET_free_non_null (get_path);
     break;
   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
     GNUNET_STATISTICS_update (GDS_stats,
@@ -262,9 +291,11 @@ datacache_get_iterator (void *cls,
                               ("# Unsupported RESULTS found in datacache"), 1,
                               GNUNET_NO);
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Unsupported block type (%u) in local response!\n"), type);
+                _("Unsupported block type (%u) in local response!\n"),
+                type);
     break;
   }
+
   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
 }
 
@@ -275,16 +306,17 @@ datacache_get_iterator (void *cls,
  * @param key the query
  * @param type requested data type
  * @param xquery extended query
- * @param xquery_size number of bytes in xquery
+ * @param xquery_size number of bytes in @a xquery
  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
- * @param reply_bf_mutator mutation value for reply_bf
+ * @param reply_bf_mutator mutation value for @a reply_bf
  * @return evaluation result for the local replies
  * @get_path_length Total number of peers in get path
  * @get_path Peers in get path.
  */
 enum GNUNET_BLOCK_EvaluationResult
-GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
-                          enum GNUNET_BLOCK_Type type, const void *xquery,
+GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
+                          enum GNUNET_BLOCK_Type type,
+                          const void *xquery,
                           size_t xquery_size,
                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
                           uint32_t reply_bf_mutator,
@@ -308,28 +340,36 @@ GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
   ctx.reply_bf = reply_bf;
   ctx.reply_bf_mutator = reply_bf_mutator;
   ctx.get_path_length = get_path_length;
-  if (next_hop != NULL)
-    memcpy (&(ctx.next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
-  /* FIXME: add the get path into ctx and then call gds_neighbours_handle_get*/
-  int i = 0;
-  while (i < get_path_length)
+
+  if (NULL != next_hop)
+    ctx.next_hop = *next_hop;
+  unsigned int i = 0;
+
+  ctx.head = NULL;
+  ctx.tail = NULL;
+  if (NULL != get_path)
   {
-    struct GetPath *element;
-    element = GNUNET_malloc (sizeof (struct GetPath));
-    element->next = NULL;
-    element->prev = NULL;
-    
-    memcpy (&(element->peer), &get_path[i], sizeof(struct GNUNET_PeerIdentity));
-    GNUNET_CONTAINER_DLL_insert_tail(ctx.head, ctx.tail, element);
-    i++;
+    while (i < get_path_length)
+    {
+      struct GetPath *element;
+      element = GNUNET_new (struct GetPath);
+      element->next = NULL;
+      element->prev = NULL;
+      element->peer = get_path[i];
+      GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
+      i++;
+    }
   }
-  
-  r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
+
+  r = GNUNET_DATACACHE_get (datacache,
+                            key,
+                            type,
+                            &datacache_get_iterator,
                             &ctx);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "DATACACHE GET for key %s completed (%d). %u results found.\n",
-       GNUNET_h2s (key), ctx.eval, r);
+  DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",
+         GNUNET_h2s (key),
+         ctx.eval,
+         r);
   return ctx.eval;
 }
 
@@ -350,7 +390,7 @@ GDS_DATACACHE_init ()
 void
 GDS_DATACACHE_done ()
 {
-  if (datacache != NULL)
+  if (NULL != datacache)
   {
     GNUNET_DATACACHE_destroy (datacache);
     datacache = NULL;
@@ -358,4 +398,4 @@ GDS_DATACACHE_done ()
 }
 
 
-/* end of gnunet-service-dht_datacache.c */
+/* end of gnunet-service-xdht_datacache.c */