cleanup debugging
[oweals/gnunet.git] / src / dht / test_dht_api.c
index dd940a6e45c2d19ae429dadc0b3eaac3c93471be..1e1a01e6e983a9cbd27d803a84c28d02c14e3b2a 100644 (file)
@@ -51,8 +51,8 @@ struct PeerContext
   struct GNUNET_CONFIGURATION_Handle *cfg;
   struct GNUNET_DHT_Handle *dht_handle;
   struct GNUNET_PeerIdentity id;
-  struct GNUNET_DHT_RouteHandle *get_handle;
-  struct GNUNET_DHT_RouteHandle *find_peer_handle;
+  struct GNUNET_DHT_GetHandle *get_handle;
+  struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
 
 #if START_ARM
   pid_t arm_pid;
@@ -75,8 +75,7 @@ GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 
 static void
-end (void *cls,
-    const struct GNUNET_SCHEDULER_TaskContext * tc)
+end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   /* do work here */
   GNUNET_SCHEDULER_cancel (sched, die_task);
@@ -86,15 +85,17 @@ end (void *cls,
   die_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning FAIL!\n");
-    ok = 365;
-  }
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "DHT disconnected, returning FAIL!\n");
+      ok = 365;
+    }
   else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning success!\n");
-    ok = 0;
-  }
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "DHT disconnected, returning success!\n");
+      ok = 0;
+    }
 }
 
 static void
@@ -114,7 +115,7 @@ end_badly ()
 {
   /* do work here */
 #if VERBOSE
-  fprintf(stderr, "Ending on an unhappy note.\n");
+  fprintf (stderr, "Ending on an unhappy note.\n");
 #endif
 
   GNUNET_DHT_disconnect (p1.dht_handle);
@@ -123,82 +124,161 @@ end_badly ()
   return;
 }
 
-
 /**
  * Signature of the main function of a task.
  *
  * @param cls closure
  * @param tc context information (why was this task triggered now)
  */
-void test_put (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext * tc)
+void
+test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerContext *peer = cls;
-  GNUNET_HashCode hash;
-  char *data;
-  size_t data_size = 42;
-  memset(&hash, 42, sizeof(GNUNET_HashCode));
-  data = GNUNET_malloc(data_size);
-  memset(data, 43, data_size);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
+  if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
+    GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
+
   GNUNET_assert (peer->dht_handle != NULL);
 
-  GNUNET_DHT_put(peer->dht_handle, &hash, 0, data_size, data, GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360)) ,GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360), &end, NULL);
+  GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
 
-  //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
+  //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
 
-  //GNUNET_SCHEDULER_add_now(sched, &end, NULL);
 }
 
+
+/**
+ * Iterator called on each result obtained from a find peer
+ * operation
+ *
+ * @param cls closure (NULL)
+ * @param peer the peer we learned about
+ * @param reply response
+ */
+void test_find_peer_processor (void *cls,
+                          const struct GNUNET_PeerIdentity *peer,
+                          const struct GNUNET_MessageHeader *reply)
+{
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer));
+
+  GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
+                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+}
+
+
 /**
  * Signature of the main function of a task.
  *
  * @param cls closure
  * @param tc context information (why was this task triggered now)
  */
-void test_get_stop (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext * tc)
+void
+test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerContext *peer = cls;
   GNUNET_HashCode hash;
-  memset(&hash, 42, sizeof(GNUNET_HashCode));
+  memset (&hash, 42, sizeof (GNUNET_HashCode));
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
+  GNUNET_assert (peer->dht_handle != NULL);
+
+  peer->find_peer_handle =
+    GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash,
+                                &test_find_peer_processor, NULL, NULL, NULL);
+
+  if (peer->find_peer_handle == NULL)
+    GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
+}
+
+/**
+ * Signature of the main function of a task.
+ *
+ * @param cls closure
+ * @param tc context information (why was this task triggered now)
+ */
+void
+test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct PeerContext *peer = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
-    GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
+    GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
 
   GNUNET_assert (peer->dht_handle != NULL);
 
-  GNUNET_DHT_get_stop(peer->get_handle);
+  GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
 
   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
-  GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
 
 }
 
+void
+test_get_iterator (void *cls,
+                   struct GNUNET_TIME_Absolute exp,
+                   const GNUNET_HashCode * key,
+                   uint32_t type, uint32_t size, const void *data)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "test_get_iterator called (we got a result), stopping get request!\n");
+
+  GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
+                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+}
+
 /**
  * Signature of the main function of a task.
  *
  * @param cls closure
  * @param tc context information (why was this task triggered now)
  */
-void test_get (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext * tc)
+void
+test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerContext *peer = cls;
   GNUNET_HashCode hash;
-  memset(&hash, 42, sizeof(GNUNET_HashCode));
+  memset (&hash, 42, sizeof (GNUNET_HashCode));
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
-  peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
+
   GNUNET_assert (peer->dht_handle != NULL);
 
-  peer->get_handle = GNUNET_DHT_get_start(peer->dht_handle, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 100), 42, &hash, NULL, NULL, &test_get_stop, &p1);
+  peer->get_handle =
+    GNUNET_DHT_get_start (peer->dht_handle, TIMEOUT, 42, &hash,
+                          &test_get_iterator, NULL, NULL, NULL);
 
   if (peer->get_handle == NULL)
-    GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
+    GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
+}
+
+/**
+ * Signature of the main function of a task.
+ *
+ * @param cls closure
+ * @param tc context information (why was this task triggered now)
+ */
+void
+test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct PeerContext *peer = cls;
+  GNUNET_HashCode hash;
+  char *data;
+  size_t data_size = 42;
+  memset (&hash, 42, sizeof (GNUNET_HashCode));
+  data = GNUNET_malloc (data_size);
+  memset (data, 43, data_size);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
+  peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
+
+  GNUNET_assert (peer->dht_handle != NULL);
+
+  GNUNET_DHT_put (peer->dht_handle, &hash, 42, data_size, data,
+                  GNUNET_TIME_relative_to_absolute (TIMEOUT), TIMEOUT,
+                  &test_get, &p1);
 
-  //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get_stop, &p1);
 }
 
 static void
@@ -228,11 +308,16 @@ run (void *cls,
   sched = s;
 
   die_task = GNUNET_SCHEDULER_add_delayed (sched,
-      GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
+                                           GNUNET_TIME_relative_multiply
+                                           (GNUNET_TIME_UNIT_MINUTES, 1),
+                                           &end_badly, NULL);
 
   setup_peer (&p1, "test_dht_api_peer1.conf");
 
-  GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get, &p1);
+  GNUNET_SCHEDULER_add_delayed (sched,
+                                GNUNET_TIME_relative_multiply
+                                (GNUNET_TIME_UNIT_SECONDS, 3), &test_put,
+                                &p1);
 }
 
 static int
@@ -254,8 +339,7 @@ check ()
 
   ok = 1;
   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                      argv, "test-dht-api", "nohelp",
-                      options, &run, &ok);
+                      argv, "test-dht-api", "nohelp", options, &run, &ok);
   stop_arm (&p1);
   return ok;
 }
@@ -278,7 +362,7 @@ main (int argc, char *argv[])
                     NULL);
   ret = check ();
 
-  //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
+  GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
 
   return ret;
 }