- changes
[oweals/gnunet.git] / src / peerinfo / peerinfo_api.c
index 4febf2eeb890209c33213989e51f61c322bf05e7..593ee8b3c28a94c896c7f99b99e5c8de979c32f6 100644 (file)
 #include "gnunet_time_lib.h"
 #include "peerinfo.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "nse-api",__VA_ARGS__)
+
 /**
  * Function to call after transmission has succeeded.
- * 
+ *
  * @param cls closure
  * @param success GNUNET_OK if transmission worked, GNUNET_SYSERR on error
  */
@@ -188,7 +190,8 @@ GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
  *
  * @param h handle to the service
  */
-static void trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
+static void
+trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
 
 
 /**
@@ -196,13 +199,14 @@ static void trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
  *
  * @param h handle to the service
  */
-static void reconnect (struct GNUNET_PEERINFO_Handle *h);
+static void
+reconnect (struct GNUNET_PEERINFO_Handle *h);
 
 /**
  * Task scheduled to re-try connecting to the peerinfo service.
  *
  * @param cls the 'struct GNUNET_PEERINFO_Handle'
- * @param ts scheduler context
+ * @param tc scheduler context
  */
 static void
 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -270,8 +274,8 @@ do_transmit (void *cls, size_t size, void *buf)
     return 0;
   if (buf == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
-                _("Failed to transmit message to `%s' service.\n"), "PEERINFO");
+    LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
+         _("Failed to transmit message to `%s' service.\n"), "PEERINFO");
     GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
     reconnect (h);
     if (tqe->cont != NULL)
@@ -282,11 +286,8 @@ do_transmit (void *cls, size_t size, void *buf)
   ret = tqe->size;
   GNUNET_assert (size >= ret);
   memcpy (buf, &tqe[1], ret);
-#if DEBUG_PEERINFO
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmitting request of size %u to `%s' service.\n", ret,
-              "PEERINFO");
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
   GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
   if (tqe->cont != NULL)
     tqe->cont (tqe->cont_cls, GNUNET_OK);
@@ -328,7 +329,7 @@ trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
 
 
 /**
- * Add a host to the persistent list.  This method operates in 
+ * Add a host to the persistent list.  This method operates in
  * semi-reliable mode: if the transmission is not completed by
  * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
  * aborted.  Furthermore, if a second HELLO is added for the
@@ -344,15 +345,12 @@ GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
 {
   uint16_t hs = GNUNET_HELLO_size (hello);
   struct TransmissionQueueEntry *tqe;
-
-#if DEBUG_PEERINFO
   struct GNUNET_PeerIdentity peer;
 
   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
-              GNUNET_i2s (&peer), hs, "HELLO");
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
+       GNUNET_i2s (&peer), hs, "HELLO");
   tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
   tqe->size = hs;
   tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
@@ -433,11 +431,8 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
   }
   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
   {
-#if DEBUG_PEERINFO
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received end of list of peers from `%s' service\n",
-                "PEERINFO");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Received end of list of peers from `%s' service\n", "PEERINFO");
     trigger_transmit (ic->h);
     if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
       GNUNET_SCHEDULER_cancel (ic->timeout_task);
@@ -456,7 +451,7 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
       GNUNET_SCHEDULER_cancel (ic->timeout_task);
     if (ic->callback != NULL)
       ic->callback (ic->callback_cls, NULL, NULL,
-                    _("Received invalid message from `PEERINFO' service.\n"));
+                    _("Received invalid message from `PEERINFO' service."));
     GNUNET_free (ic);
     return;
   }
@@ -474,17 +469,15 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
         GNUNET_SCHEDULER_cancel (ic->timeout_task);
       if (ic->callback != NULL)
         ic->callback (ic->callback_cls, NULL, NULL,
-                      _("Received invalid message from `PEERINFO' service.\n"));
+                      _("Received invalid message from `PEERINFO' service."));
       GNUNET_free (ic);
       return;
     }
   }
-#if DEBUG_PEERINFO
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
-              (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
-              "HELLO", GNUNET_i2s (&im->peer), "PEERINFO");
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
+       (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO",
+       GNUNET_i2s (&im->peer), "PEERINFO");
   ic->h->in_receive = GNUNET_YES;
   if (ic->callback != NULL)
     ic->callback (ic->callback_cls, &im->peer, hello, NULL);
@@ -516,14 +509,12 @@ iterator_start_receive (void *cls, int transmit_success)
     if (ic->callback != NULL)
       ic->callback (ic->callback_cls, NULL, NULL,
                     _
-                    ("Failed to transmit iteration request to `PEERINFO' service\n"));
+                    ("Failed to transmit iteration request to `PEERINFO' service"));
     GNUNET_free (ic);
     return;
   }
-#if DEBUG_PEERINFO
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Waiting for response from `%s' service.\n", "PEERINFO");
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
+       "PEERINFO");
   ic->h->in_receive = GNUNET_YES;
   ic->in_receive = GNUNET_YES;
   ic->tqe = NULL;
@@ -533,7 +524,7 @@ iterator_start_receive (void *cls, int transmit_success)
 
 
 /**
- * Peerinfo iteration request has timed out.  
+ * Peerinfo iteration request has timed out.
  *
  * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
  * @param tc scheduler context
@@ -550,7 +541,7 @@ signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     reconnect (ic->h);
   ic->callback (ic->callback_cls, NULL, NULL,
                 _
-                ("Timeout transmitting iteration request to `PEERINFO' service.\n"));
+                ("Timeout transmitting iteration request to `PEERINFO' service."));
   ic->callback = NULL;
   GNUNET_free_non_null (ic->tqe);
   GNUNET_free (ic);
@@ -565,7 +556,7 @@ signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * Instead of calling this function with 'peer == NULL' it is often
  * better to use 'GNUNET_PEERINFO_notify'.
- * 
+ *
  * @param h handle to the peerinfo service
  * @param peer restrict iteration to this peer only (can be NULL)
  * @param timeout how long to wait until timing out
@@ -586,10 +577,8 @@ GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
 
   if (peer == NULL)
   {
-#if DEBUG_PEERINFO
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Requesting list of peers from PEERINFO service\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Requesting list of peers from PEERINFO service\n");
     tqe =
         GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
                        sizeof (struct GNUNET_MessageHeader));
@@ -600,11 +589,9 @@ GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
   }
   else
   {
-#if DEBUG_PEERINFO
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Requesting information on peer `%4s' from PEERINFO service\n",
-                GNUNET_i2s (peer));
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Requesting information on peer `%4s' from PEERINFO service\n",
+         GNUNET_i2s (peer));
     tqe =
         GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
                        sizeof (struct ListPeerMessage));
@@ -622,10 +609,9 @@ GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
   ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
   ic->timeout_task =
       GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
-  tqe->timeout = ic->timeout;
+  tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
   tqe->cont = &iterator_start_receive;
   tqe->cont_cls = ic;
-  tqe->timeout = ic->timeout;
   GNUNET_CONTAINER_DLL_insert_after (h->tq_head, h->tq_tail, h->tq_tail, tqe);
   trigger_transmit (h);
   return ic;