Don't pass NULL to destroy_route
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_dht.c
index fbd8b95f1c2a8fd70606cc2bc68deba695c4b633..849562f23797b0122350b47f92aac1370c6c6571 100644 (file)
 #include "gnunet-service-cadet-new_peer.h"
 #include "gnunet-service-cadet-new_paths.h"
 
+/**
+ * How long do we wait before first announcing our presence to the DHT.
+ * Used to wait for our HELLO to be available.  Note that we also get
+ * notifications when our HELLO is ready, so this is just the maximum
+ * we wait for the first notification.
+ */
+#define STARTUP_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
+
+/**
+ * How long do we wait after we get an updated HELLO before publishing?
+ * Allows for the HELLO to be updated again quickly, for example in
+ * case multiple addresses changed and we got a partial update.
+ */
+#define CHANGE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
+
+
 #define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__)
 
 
@@ -47,16 +63,6 @@ struct GCD_search_handle
    */
   struct GNUNET_DHT_GetHandle *dhtget;
 
-  /**
-   * Provided callback to call when a path is found.
-   */
-  GCD_search_callback callback;
-
-  /**
-   * Provided closure.
-   */
-  void *cls;
-
 };
 
 
@@ -86,7 +92,6 @@ static struct GNUNET_SCHEDULER_Task *announce_id_task;
 static struct GNUNET_TIME_Relative announce_delay;
 
 
-
 /**
  * Function to process paths received for a new peer addition. The recorded
  * paths form the initial tunnel, which can be optimized later.
@@ -114,18 +119,13 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
                     size_t size,
                     const void *data)
 {
-  struct GCD_search_handle *h = cls;
   const struct GNUNET_HELLO_Message *hello = data;
-  struct CadetPeerPath *p;
   struct CadetPeer *peer;
 
-  p = GCPP_path_from_dht (get_path,
+  GCPP_try_path_from_dht (get_path,
                           get_path_length,
                           put_path,
                           put_path_length);
-  h->callback (h->cls, p);
-  GCPP_path_destroy (p);
-
   if ( (size >= sizeof (struct GNUNET_HELLO_Message)) &&
        (ntohs (hello->header.size) == size) &&
        (size == GNUNET_HELLO_size (hello)) )
@@ -210,6 +210,23 @@ announce_id (void *cls)
 }
 
 
+/**
+ * Function called by the HELLO subsystem whenever OUR hello
+ * changes. Re-triggers the DHT PUT immediately.
+ */
+void
+GCD_hello_update ()
+{
+  if (NULL == announce_id_task)
+    return; /* too early */
+  GNUNET_SCHEDULER_cancel (announce_id_task);
+  announce_id_task
+    = GNUNET_SCHEDULER_add_delayed (CHANGE_DELAY,
+                                    &announce_id,
+                                    NULL);
+}
+
+
 /**
  * Initialize the DHT subsystem.
  *
@@ -218,8 +235,6 @@ announce_id (void *cls)
 void
 GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "init\n");
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (c,
                                              "CADET",
@@ -251,8 +266,9 @@ GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
                                    64);
   GNUNET_break (NULL != dht_handle);
   announce_delay = GNUNET_TIME_UNIT_SECONDS;
-  announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id,
-                                               NULL);
+  announce_id_task = GNUNET_SCHEDULER_add_delayed (STARTUP_DELAY,
+                                                   &announce_id,
+                                                   NULL);
 }
 
 
@@ -279,21 +295,14 @@ GCD_shutdown (void)
  * Search DHT for paths to @a peeR_id
  *
  * @param peer_id peer to search for
- * @param callback function to call with results
- * @param callback_cls closure for @a callback
  * @return handle to abort search
  */
 struct GCD_search_handle *
-GCD_search (const struct GNUNET_PeerIdentity *peer_id,
-            GCD_search_callback callback,
-            void *callback_cls)
+GCD_search (const struct GNUNET_PeerIdentity *peer_id)
 {
   struct GNUNET_HashCode phash;
   struct GCD_search_handle *h;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Starting DHT GET for peer %s\n",
-       GNUNET_i2s (peer_id));
   GNUNET_STATISTICS_update (stats,
                             "# DHT search",
                             1,
@@ -306,8 +315,6 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id,
                  sizeof (*peer_id));
 
   h = GNUNET_new (struct GCD_search_handle);
-  h->callback = callback;
-  h->cls = callback_cls;
   h->dhtget = GNUNET_DHT_get_start (dht_handle,    /* handle */
                                     GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
                                     &phash,     /* key to search */
@@ -318,6 +325,10 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id,
                                     0,     /* xquery bits */
                                     &dht_get_id_handler,
                                    h);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Starting DHT GET for peer %s (%p)\n",
+       GNUNET_i2s (peer_id),
+       h);
   return h;
 }
 
@@ -330,6 +341,9 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id,
 void
 GCD_search_stop (struct GCD_search_handle *h)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Stopping DHT GET %p\n",
+       h);
   GNUNET_DHT_get_stop (h->dhtget);
   GNUNET_free (h);
 }