- replace deprecated INCLUDES with AM_CPPFLAGS
[oweals/gnunet.git] / src / dv / dv_api.c
index dab7c7ad34d6673addd9653ca1f7e0b980b47c9f..a24965e84b3a9e9f2373d6faff66d46b6e6223f8 100644 (file)
@@ -113,6 +113,11 @@ struct GNUNET_DV_ServiceHandle
    */
   GNUNET_DV_ConnectCallback connect_cb;
 
+  /**
+   * Function to call on distance change events.
+   */
+  GNUNET_DV_DistanceChangedCallback distance_cb;
+
   /**
    * Function to call on disconnect events.
    */
@@ -262,6 +267,9 @@ process_ack (void *cls,
 
   if (th->uid != ntohl (ctx->ack->uid))
     return GNUNET_OK;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Matchedk ACK for message to peer %s\n",
+       GNUNET_h2s (key));
   GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove (ctx->sh->send_callbacks,
                                                       key,
@@ -288,6 +296,7 @@ handle_message_receipt (void *cls,
 {
   struct GNUNET_DV_ServiceHandle *sh = cls;
   const struct GNUNET_DV_ConnectMessage *cm;
+  const struct GNUNET_DV_DistanceUpdateMessage *dum;
   const struct GNUNET_DV_DisconnectMessage *dm;
   const struct GNUNET_DV_ReceivedMessage *rm;
   const struct GNUNET_MessageHeader *payload;
@@ -300,6 +309,9 @@ handle_message_receipt (void *cls,
     reconnect (sh);
     return;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received message of type %u from DV service\n",
+       (unsigned int) msg->type);
   switch (ntohs (msg->type))
   {
   case GNUNET_MESSAGE_TYPE_DV_CONNECT:
@@ -314,6 +326,18 @@ handle_message_receipt (void *cls,
                    &cm->peer,
                    ntohl (cm->distance));
     break;
+  case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
+    if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DistanceUpdateMessage))
+    {
+      GNUNET_break (0);
+      reconnect (sh);
+      return;
+    }
+    dum = (const struct GNUNET_DV_DistanceUpdateMessage *) msg;
+    sh->distance_cb (sh->cls,
+                    &dum->peer,
+                    ntohl (dum->distance));
+    break;
   case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
     if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
     {
@@ -361,9 +385,6 @@ handle_message_receipt (void *cls,
                                                &process_ack,
                                                &ctx);
     break;
-  case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
-    GNUNET_break (0);
-    break;
   default:
     reconnect (sh);
     break;
@@ -457,6 +478,8 @@ reconnect (struct GNUNET_DV_ServiceHandle *sh)
   GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
                                         &cleanup_send_cb,
                                         sh);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Connecting to DV service\n");
   sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
   if (NULL == sh->client)
   {
@@ -478,6 +501,7 @@ reconnect (struct GNUNET_DV_ServiceHandle *sh)
  * @param cfg configuration
  * @param cls closure for callbacks
  * @param connect_cb function to call on connects
+ * @param distance_cb function to call if distances change
  * @param disconnect_cb function to call on disconnects
  * @param message_cb function to call if we receive messages
  * @return handle to access the service
@@ -486,15 +510,17 @@ struct GNUNET_DV_ServiceHandle *
 GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                           void *cls,
                           GNUNET_DV_ConnectCallback connect_cb,
+                          GNUNET_DV_DistanceChangedCallback distance_cb,
                           GNUNET_DV_DisconnectCallback disconnect_cb,
                           GNUNET_DV_MessageReceivedCallback message_cb)
 {
   struct GNUNET_DV_ServiceHandle *sh;
 
-  sh = GNUNET_malloc (sizeof (struct GNUNET_DV_ServiceHandle));
+  sh = GNUNET_new (struct GNUNET_DV_ServiceHandle);
   sh->cfg = cfg;
   sh->cls = cls;
   sh->connect_cb = connect_cb;
+  sh->distance_cb = distance_cb;
   sh->disconnect_cb = disconnect_cb;
   sh->message_cb = message_cb;
   sh->send_callbacks = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
@@ -565,6 +591,12 @@ GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
     GNUNET_break (0);
     return NULL;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Asked to send %u bytes of type %u to %s\n",
+       (unsigned int) msg->size,
+       (unsigned int) msg->type,
+       GNUNET_i2s (target));
+
   th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
                      sizeof (struct GNUNET_DV_SendMessage) +
                      ntohs (msg->size));