types
[oweals/gnunet.git] / src / hostlist / gnunet-daemon-hostlist.c
index 0fb77ecbd737dbf7e26f22ba384446a957aa698b..7d075cf808df69ffd43b961fbbb171d76b916ae0 100644 (file)
  * @file hostlist/gnunet-daemon-hostlist.c
  * @brief code for bootstrapping via hostlist servers
  * @author Christian Grothoff
+ *
+ * TODO:
+ * - implement -a and -e switches (send P2P messages about our hostlist URL,
+ *   receive such messages and automatically update our hostlist URL config
+ *   value).
  */
 
 #include <stdlib.h>
 #include "platform.h"
+#include "hostlist-client.h"
+#include "hostlist-server.h"
+#include "gnunet_core_service.h"
 #include "gnunet_getopt_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_program_lib.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_strings_lib.h"
 #include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
 
 
+/**
+ * Set if we are allowed to advertise our hostlist to others.
+ */
+static int advertising;
+
 /**
  * Set if we are allowed to learn about peers by accessing
  * hostlist servers.
@@ -51,21 +65,61 @@ static int learning;
  */
 static int provide_hostlist;
 
+/**
+ * Statistics handle.
+ */
+static struct GNUNET_STATISTICS_Handle *stats;
 
 /**
  * gnunet-daemon-hostlist command line options.
  */
 static struct GNUNET_GETOPT_CommandLineOption options[] = {
-  { 'b', "bootstrap", NULL, gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
+  { 'a', "advertise", NULL, 
+    gettext_noop ("advertise our hostlist to other peers"),
+    GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
+  { 'b', "bootstrap", NULL, 
+    gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
-  { 'e', "enable-learning", NULL, gettext_noop ("enable learning about hostlist servers from other peers"),
+  { 'e', "enable-learning", NULL,
+    gettext_noop ("enable learning about hostlist servers from other peers"),
     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
-  { 'p', "provide-hostlist", NULL, gettext_noop ("provide a hostlist server"),
+  { 'p', "provide-hostlist", NULL, 
+    gettext_noop ("provide a hostlist server"),
     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
   GNUNET_GETOPT_OPTION_END
 };
 
 
+static void
+core_init (void *cls,
+          struct GNUNET_CORE_Handle * server,
+          const struct GNUNET_PeerIdentity *
+          my_identity,
+          const struct
+          GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
+          publicKey)
+{
+  if (advertising && (NULL != server))
+    {    
+      /* FIXME: provide "server" to 'hostlist' module */
+    }
+}
+
+
+/**
+ * Last task run during shutdown.  Disconnects us from
+ * the other services.
+ */
+static void
+cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (stats != NULL)
+    {
+      GNUNET_STATISTICS_destroy (stats);
+      stats = NULL;
+    }
+}
+
 
 /**
  * Main function that will be run.
@@ -81,8 +135,15 @@ run (void *cls,
      struct GNUNET_SCHEDULER_Handle * sched,
      char *const *args,
      const char *cfgfile,
-     struct GNUNET_CONFIGURATION_Handle * cfg)
+     const struct GNUNET_CONFIGURATION_Handle * cfg)
 {
+  GNUNET_CORE_ClientEventHandler ch = NULL;
+  GNUNET_CORE_ClientEventHandler dh = NULL;
+  struct GNUNET_CORE_MessageHandler handlers[] = 
+    {
+      { NULL, 0, 0 }
+    };
+
   if ( (! bootstrapping) &&
        (! learning) &&
        (! provide_hostlist) )
@@ -91,24 +152,35 @@ run (void *cls,
                  _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
       return;
     }
+  stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
   if (learning)
     {
-      // FIXME!
-      // (register handler with core for hostlist ads)
+      /* FIXME (register handler with core for hostlist ads) */
     }
   if (bootstrapping)
     {
-      // FIXME!
-      // (register handler with core to monitor number of active
-      //  connections; trigger hostlist download via CURL if
-      //  number is low)
+      GNUNET_HOSTLIST_client_start (cfg, sched, stats,
+                                   &ch, &dh);
     }
   if (provide_hostlist)
     {      
-      // FIXME!
-      // (initialize MHD server and run using scheduler;
-      //  use peerinfo to gather HELLOs)
+      GNUNET_HOSTLIST_server_start (cfg, sched, stats);
     }
+  GNUNET_CORE_connect (sched, cfg,
+                      GNUNET_TIME_UNIT_FOREVER_REL,
+                      NULL,
+                      &core_init,
+                      ch, dh,
+                      NULL,
+                      NULL, GNUNET_NO,
+                      NULL, GNUNET_NO,
+                      handlers);
+  GNUNET_SCHEDULER_add_delayed (sched,
+                                GNUNET_YES,
+                                GNUNET_SCHEDULER_PRIORITY_IDLE,
+                                GNUNET_SCHEDULER_NO_TASK,
+                                GNUNET_TIME_UNIT_FOREVER_REL,
+                                &cleaning_task, NULL);
 }