WiP
[oweals/gnunet.git] / src / dht / gnunet-dht-get-peer.c
index 04959792b1d298512ee6af352349ab48e4762c45..8d318efc2ed17528403f3fade73241771e2c450f 100644 (file)
@@ -52,10 +52,6 @@ static int verbose;
  */
 static struct GNUNET_DHT_Handle *dht_handle;
 
-/**
- * Global handle of the scheduler
- */
-static struct GNUNET_SCHEDULER_Handle *sched;
 
 /**
  * Global handle of the configuration
@@ -78,22 +74,30 @@ static unsigned int result_count;
 static int ret;
 
 static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   if (dht_handle != NULL)
-    GNUNET_DHT_disconnect (dht_handle);
-
-  dht_handle = NULL;
+    {
+      GNUNET_DHT_disconnect (dht_handle);
+      dht_handle = NULL;
+    }
+  fprintf (stderr,
+          _("Found %u peers\n"),
+          result_count);
 }
 
+
 static void
-cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup_task (void *cls, 
+             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   if (find_peer_handle != NULL)
-    GNUNET_DHT_find_peer_stop (find_peer_handle, &shutdown_task, NULL);
-  else
-    GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
+    {
+      GNUNET_DHT_find_peer_stop (find_peer_handle);
+      find_peer_handle = NULL;
+    }
+  GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
 }
 
 /**
@@ -103,64 +107,38 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls closure (NULL)
  * @param hello the response message, a HELLO
  */
-void find_peer_processor (void *cls,
-                          const struct GNUNET_HELLO_Message *hello)
+static void 
+find_peer_processor (void *cls,
+                    const struct GNUNET_HELLO_Message *hello)
 {
   struct GNUNET_PeerIdentity peer;
+
   if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
     {
       result_count++;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "test_find_peer_processor called (peer `%s'), total results %d!\n", GNUNET_i2s(&peer), result_count);
-    }
-}
-
-
-/**
- * Signature of the main function of a task.
- *
- * @param cls closure
- * @param tc context information (why was this task triggered now)
- */
-void
-message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
-    {
       if (verbose)
-        fprintf (stderr,
-                 "Failed to send FIND PEER request to service, quitting.\n");
-      ret = 1;
-      GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
-    }
-  else
-    {
-      if (verbose)
-        fprintf (stderr, "FIND PEER request sent, awaiting results!\n");
-      GNUNET_SCHEDULER_add_delayed (sched,
-                                    GNUNET_TIME_absolute_get_remaining
-                                    (absolute_timeout), &cleanup_task, NULL);
+       fprintf (stderr,
+                _("Found peer `%s'\n"),
+                GNUNET_i2s (&peer));
     }
 }
 
+
 /**
  * Main function that will be run by the scheduler.
  *
  * @param cls closure
- * @param s the scheduler to use
  * @param args remaining command-line arguments
  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  * @param c configuration
  */
 static void
 run (void *cls,
-     struct GNUNET_SCHEDULER_Handle *s,
      char *const *args,
      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 {
   struct GNUNET_TIME_Relative timeout;
   GNUNET_HashCode key;
-  sched = s;
   cfg = c;
 
   if (query_key == NULL)
@@ -171,7 +149,7 @@ run (void *cls,
       return;
     }
 
-  dht_handle = GNUNET_DHT_connect (sched, cfg, 1);
+  dht_handle = GNUNET_DHT_connect (cfg, 1);
 
   if (dht_handle == NULL)
     {
@@ -194,13 +172,18 @@ run (void *cls,
 
   find_peer_handle = GNUNET_DHT_find_peer_start (dht_handle,
                                                  timeout,
-                                                 0,
                                                  &key,
+                                                GNUNET_DHT_RO_NONE,
                                                  &find_peer_processor,
-                                                 NULL,
-                                                 &message_sent_cont,
                                                  NULL);
-
+  if (NULL == find_peer_handle)
+    {
+      GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+      return;
+    }
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
+                               (absolute_timeout),
+                               &cleanup_task, NULL);
 }
 
 
@@ -234,8 +217,10 @@ main (int argc, char *const *argv)
   return (GNUNET_OK ==
           GNUNET_PROGRAM_run (argc,
                               argv,
-                              "gnunet-dht-get",
+                              "gnunet-dht-get-peer",
                               gettext_noop
-                              ("Issue a GET request to the GNUnet DHT, prints results."),
+                              ("Issue a GET PEER request to the GNUnet DHT, print results."),
                               options, &run, NULL)) ? ret : 1;
 }
+
+/* end of gnunet-dht-get-peer */