temporary hack for gnunet-arm. given enough time processes seem to go away, so give...
[oweals/gnunet.git] / src / peerinfo-tool / gnunet-peerinfo.c
index c056b390e13bf8b978e7fa43ea623e0e32f6b641..a3fa98fa9267269b8f905bdb5280750f5f1c44f4 100644 (file)
@@ -39,11 +39,44 @@ static int get_self;
 
 static struct GNUNET_SCHEDULER_Handle *sched;
 
+static struct GNUNET_PEERINFO_Handle *peerinfo;
+
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
-#define FIXME 0
+struct PrintContext
+{
+  struct GNUNET_PeerIdentity peer;
+  char **address_list;
+  unsigned int num_addresses;
+  uint32_t off;
+  uint32_t trust;
+};
+
+
+static void
+dump_pc (struct PrintContext *pc)
+{
+  struct GNUNET_CRYPTO_HashAsciiEncoded enc;
+  unsigned int i;
+
+  GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
+  printf (_("Peer `%s' with trust %8u\n"), 
+         (const char *) &enc,
+         pc->trust);
+  for (i=0;i<pc->num_addresses;i++)
+    {
+      printf ("\t%s\n",
+             pc->address_list[i]);
+      GNUNET_free (pc->address_list[i]);
+    }
+  printf ("\n");
+  GNUNET_array_grow (pc->address_list,
+                    pc->num_addresses,
+                    0);
+  GNUNET_free (pc);
+}
+
 
-#if FIXME
 /**
  * Function to call with a human-readable format of an address
  *
@@ -51,18 +84,45 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
  */
 static void
-print_resolved_address (void *cls,
-                       const char *address)
+process_resolved_address (void *cls,
+                         const char *address)
 {
-  /* FIXME: need to buffer output from all requests and print it at
-     once, otherwise we mix results... */
+  struct PrintContext *pc = cls;
+
   if (address == NULL)
     {
+      pc->off--;
+      if (pc->off == 0)
+       dump_pc (pc);
       return;
     }
-  fprintf (stderr, " %s\n", address);
+  GNUNET_array_append (pc->address_list,
+                      pc->num_addresses,
+                      GNUNET_strdup (address));
 }
-#endif
+
+
+/**
+ * Iterator callback to go over all addresses.
+ *
+ * @param cls closure
+ * @param tname name of the transport
+ * @param expiration expiration time
+ * @param addr the address
+ * @param addrlen length of the address
+ * @return GNUNET_OK to keep the address and continue
+ */
+static int
+count_address (void *cls,
+              const char *tname,
+              struct GNUNET_TIME_Absolute expiration,
+              const void *addr, uint16_t addrlen)
+{
+  struct PrintContext *pc = cls;
+  pc->off++;
+  return GNUNET_OK;
+}
+
 
 /**
  * Iterator callback to go over all addresses.
@@ -78,9 +138,9 @@ static int
 print_address (void *cls,
               const char *tname,
               struct GNUNET_TIME_Absolute expiration,
-              const void *addr, size_t addrlen)
+              const void *addr, uint16_t addrlen)
 {
-#if FIXME
+  struct PrintContext *pc = cls;
   GNUNET_TRANSPORT_address_lookup (sched,
                                   cfg,
                                   addr,
@@ -88,9 +148,8 @@ print_address (void *cls,
                                   no_resolve,
                                   tname,
                                   GNUNET_TIME_UNIT_SECONDS,
-                                  &print_resolved_address,
-                                  NULL);
-#endif
+                                  &process_resolved_address,
+                                  pc);
   return GNUNET_OK;
 }
 
@@ -106,19 +165,48 @@ print_peer_info (void *cls,
                  const struct GNUNET_HELLO_Message *hello, uint32_t trust)
 {
   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
+  struct PrintContext *pc;
 
   if (peer == NULL)    
-    return;    
-  GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
+    {
+      GNUNET_PEERINFO_disconnect (peerinfo);
+      switch (trust)
+       {
+       case 0:
+         break;
+       case 1:
+         fprintf (stderr,
+                  _("Timeout trying to interact with PEERINFO service\n"));
+         break;
+       case 2:
+         fprintf (stderr,
+                  _("Error in communication with PEERINFO service\n"));
+         break;
+       default:
+         GNUNET_break (0);
+         break;
+       }
+      return;    
+    }
   if (be_quiet)
     {
+      GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
       printf ("%s\n", (const char *) &enc);
       return;
     }
-  printf (_("Peer `%s' with trust %8u\n"), (const char *) &enc, trust);
-  GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, NULL);
+  pc = GNUNET_malloc (sizeof (struct PrintContext));
+  pc->peer = *peer;  
+  pc->trust = trust;
+  GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
+  if (0 == pc->off)
+    {
+      dump_pc (pc);
+      return;
+    }
+  GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
 }
 
+
 /**
  * Main function that will be run by the scheduler.
  *
@@ -140,13 +228,43 @@ run (void *cls,
   struct GNUNET_PeerIdentity pid;
   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
   char *fn;
+  int delta;
 
   sched = s;
   cfg = c;
+  delta = 0;
+  if ( (args[0] != NULL) &&
+       (args[1] != NULL) &&
+       (1 == sscanf(args[0], "%d", &delta)) &&
+       (GNUNET_OK == 
+       GNUNET_CRYPTO_hash_from_string (args[1],
+                                       &pid.hashPubKey)) )
+    {
+      peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
+      GNUNET_PEERINFO_iterate (peerinfo,
+                              &pid,
+                              delta,
+                              GNUNET_TIME_UNIT_SECONDS,
+                              &print_peer_info, NULL);                            
+      return;
+    }
+  else if (args[0] != NULL)
+    {
+      fprintf (stderr,
+              _("Invalid command line argument `%s'\n"),
+              args[0]);
+      return;    
+    }
   if (get_self != GNUNET_YES)
     {
-      (void) GNUNET_PEERINFO_iterate (cfg,
-                                     sched,
+      peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
+      if (peerinfo == NULL)
+       {
+         fprintf (stderr,
+                  _("Could not access PEERINFO service.  Exiting.\n"));
+         return;
+       }
+      (void) GNUNET_PEERINFO_iterate (peerinfo,
                                      NULL,
                                      0,
                                      GNUNET_TIME_relative_multiply