- debug
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_dht.c
index a194bd0b73b8efb00e15761f7326a630358787a9..63f98a4d743678ee9b7230472a9dabe143f97d4c 100644 (file)
 #include "gnunet_util_lib.h"
 
 #include "gnunet_dht_service.h"
+#include "gnunet_statistics_service.h"
 
+#include "block_mesh.h"
 #include "mesh_path.h"
 #include "gnunet-service-mesh_dht.h"
 #include "gnunet-service-mesh_peer.h"
 
-#define MESH_DEBUG_DHT          GNUNET_NO
+#define LOG(level, ...) GNUNET_log_from (level,"mesh-dht",__VA_ARGS__)
 
-#if MESH_DEBUG_DHT
-#define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
-#else
-#define DEBUG_DHT(...)
-#endif
-
-#define LOG (level, ...) GNUNET_log_from ("mesh-dht", level, __VA_ARGS__)
-
-
-
-/**
- * Callback called on each path found over the DHT.
- *
- * @param cls Closure.
- * @param path An unchecked, unoptimized path to the target node.
- *             After callback will no longer be valid!
- */
-typedef void (*GMD_search_callback) (void *cls,
-                                     const struct MeshPeerPath *path);
 
 /******************************************************************************/
 /********************************   STRUCTS  **********************************/
@@ -75,39 +58,39 @@ struct GMD_search_handle
 /******************************************************************************/
 
 /**
- * Handle to use DHT.
+ * Global handle to the statistics service.
  */
-static struct GNUNET_DHT_Handle *dht_handle;
+extern struct GNUNET_STATISTICS_Handle *stats;
 
 /**
- * How often to PUT own ID in the DHT.
+ * Own ID (short value).
  */
-static struct GNUNET_TIME_Relative id_announce_time;
+extern GNUNET_PEER_Id myid;
 
 /**
- * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
+ * Own ID (full value).
  */
-static unsigned long long dht_replication_level;
+extern struct GNUNET_PeerIdentity my_full_id;
 
 /**
- * Task to periodically announce itself in the network.
+ * Handle to use DHT.
  */
-static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
+static struct GNUNET_DHT_Handle *dht_handle;
 
 /**
- * Own ID (short value).
+ * How often to PUT own ID in the DHT.
  */
-static GNUNET_PEER_Id short_id;
+static struct GNUNET_TIME_Relative id_announce_time;
 
 /**
- * Own ID (full value).
+ * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
  */
-static struct GNUNET_PeerIdentity *full_id;
+static unsigned long long dht_replication_level;
 
 /**
- * Own private key.
+ * Task to periodically announce itself in the network.
  */
-static struct GNUNET_CRYPTO_EccPrivateKey *private_key;
+static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
 
 
 /******************************************************************************/
@@ -135,18 +118,18 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
   p->peers[0] = myid;
   GNUNET_PEER_change_rc (myid, 1);
   i = get_path_length;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   GET has %d hops.\n", i);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "   GET has %d hops.\n", i);
   for (i--; i >= 0; i--)
   {
     id = GNUNET_PEER_intern (&get_path[i]);
     if (p->length > 0 && id == p->peers[p->length - 1])
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
       GNUNET_PEER_change_rc (id, -1);
     }
     else
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from GET: %s.\n",
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "   Adding from GET: %s.\n",
                   GNUNET_i2s (&get_path[i]));
       p->length++;
       p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
@@ -154,7 +137,7 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
     }
   }
   i = put_path_length;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   PUT has %d hops.\n", i);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "   PUT has %d hops.\n", i);
   for (i--; i >= 0; i--)
   {
     id = GNUNET_PEER_intern (&put_path[i]);
@@ -168,12 +151,12 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
     }
     if (p->length > 0 && id == p->peers[p->length - 1])
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
       GNUNET_PEER_change_rc (id, -1);
     }
     else
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from PUT: %s.\n",
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "   Adding from PUT: %s.\n",
                   GNUNET_i2s (&put_path[i]));
       p->length++;
       p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
@@ -182,19 +165,19 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
   }
 #if MESH_DEBUG
   if (get_path_length > 0)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of GET: %s)\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "   (first of GET: %s)\n",
                 GNUNET_i2s (&get_path[0]));
   if (put_path_length > 0)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of PUT: %s)\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "   (first of PUT: %s)\n",
                 GNUNET_i2s (&put_path[0]));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   In total: %d hops\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "   In total: %d hops\n",
               p->length);
   for (i = 0; i < p->length; i++)
   {
     struct GNUNET_PeerIdentity peer_id;
 
     GNUNET_PEER_resolve (p->peers[i], &peer_id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "       %u: %s\n", p->peers[i],
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "       %u: %s\n", p->peers[i],
                 GNUNET_i2s (&peer_id));
   }
 #endif
@@ -256,14 +239,13 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
     return;
   }
+
   /* TODO
    * - Set data expiration in function of X
    * - Adapt X to churn
    */
-  DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (id));
-
-  block.id = *full_id;
-  GNUNET_CRYPTO_hash (full_id, sizeof (struct GNUNET_PeerIdentity), &phash);
+  block.id = my_full_id;
+  GNUNET_CRYPTO_hash (&my_full_id, sizeof (struct GNUNET_PeerIdentity), &phash);
   GNUNET_DHT_put (dht_handle,   /* DHT handle */
                   &phash,       /* Key to use */
                   dht_replication_level,     /* Replication level */
@@ -291,10 +273,9 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param peer_id Local peer ID (must remain valid during all execution time).
  */
 void
-GMD_init (const struct GNUNET_CONFIGURATION_Handle *c,
-          struct GNUNET_PeerIdentity *peer_id)
+GMD_init (const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  full_id = peer_id;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
                                              &dht_replication_level))
@@ -353,6 +334,7 @@ GMD_search (const struct GNUNET_PeerIdentity *peer_id,
        "  Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id));
   GNUNET_CRYPTO_hash (peer_id, sizeof (struct GNUNET_PeerIdentity), &phash);
   h = GNUNET_new (struct GMD_search_handle);
+  h->callback = callback;
   h->cls = cls;
   h->dhtget = GNUNET_DHT_get_start (dht_handle,    /* handle */
                                     GNUNET_BLOCK_TYPE_MESH_PEER, /* type */