add units to time, use configuration time api where appropriate, fixing Mantis #1875
[oweals/gnunet.git] / src / util / client.c
index e568562296274a61722d4c572c50032dd643a4f6..e71ccdf3948ac44d87b18111a9fb2d5ac342c54f 100644 (file)
@@ -34,7 +34,7 @@
 #include "gnunet_server_lib.h"
 #include "gnunet_scheduler_lib.h"
 
-#define DEBUG_CLIENT GNUNET_NO
+#define DEBUG_CLIENT GNUNET_EXTRA_LOGGING
 
 /**
  * How often do we re-try tranmsitting requests before giving up?
@@ -43,6 +43,8 @@
  */
 #define MAX_ATTEMPTS 50
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
+
 /**
  * Handle for a transmission request.
  */
@@ -239,11 +241,6 @@ struct GNUNET_CLIENT_Connection
    */
   int in_receive;
 
-  /**
-   * Are we ignoring shutdown signals?
-   */
-  int ignore_shutdown;
-
   /**
    * How often have we tried to connect?
    */
@@ -281,8 +278,8 @@ do_connect (const char *service_name,
       if (sock != NULL)
       {
 #if DEBUG_CLIENT
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
-                    unixpath);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
+             unixpath);
 #endif
         GNUNET_free (unixpath);
         return sock;
@@ -299,18 +296,17 @@ do_connect (const char *service_name,
        GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
                                               &hostname)))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _
-                ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
-                service_name);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         _
+         ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
+         service_name);
     return NULL;
   }
   if (0 == strlen (hostname))
   {
     GNUNET_free (hostname);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Need a non-empty hostname for service `%s'.\n"),
-                service_name);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         _("Need a non-empty hostname for service `%s'.\n"), service_name);
     return NULL;
   }
   if (port == 0)
@@ -338,9 +334,9 @@ do_connect (const char *service_name,
     }
 #endif
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
-                service_name);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
+         service_name);
 #endif
     GNUNET_free (hostname);
     return NULL;
@@ -377,22 +373,6 @@ GNUNET_CLIENT_connect (const char *service_name,
 }
 
 
-/**
- * Configure this connection to ignore shutdown signals.
- *
- * @param h client handle
- * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
- */
-void
-GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
-                               int do_ignore)
-{
-  h->ignore_shutdown = do_ignore;
-  if (h->sock != NULL)
-    GNUNET_CONNECTION_ignore_shutdown (h->sock, do_ignore);
-}
-
-
 /**
  * Destroy connection with the service.  This will automatically
  * cancel any pending "receive" request (however, the handler will
@@ -485,10 +465,10 @@ receive_helper (void *cls, const void *buf, size_t available,
   {
     /* signal timeout! */
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
-                (unsigned int) available,
-                conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
+         (unsigned int) available, conn->sock == NULL ? "NULL" : "non-NULL",
+         STRERROR (errCode));
 #endif
     if (NULL != (receive_handler = conn->receiver_handler))
     {
@@ -544,9 +524,8 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
 
 #if DEBUG_CLIENT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received message of type %u and size %u\n", ntohs (cmsg->type),
-              msize);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
+       ntohs (cmsg->type), msize);
 #endif
   sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (GNUNET_YES == sock->msg_complete);
@@ -597,7 +576,7 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
     GNUNET_assert (sock->in_receive == GNUNET_NO);
     sock->in_receive = GNUNET_YES;
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
 #endif
     GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
                                timeout, &receive_helper, sock);
@@ -633,8 +612,8 @@ confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
   if (msg != NULL)
   {
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received confirmation that service is running.\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Received confirmation that service is running.\n");
 #endif
     GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
@@ -665,15 +644,14 @@ write_test (void *cls, size_t size, void *buf)
   if (size < sizeof (struct GNUNET_MessageHeader))
   {
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _("Failure to transmit TEST request.\n"));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
 #endif
     service_test_error (conn->test_cb, conn->test_cb_cls);
     GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
     return 0;                   /* client disconnected */
   }
 #if DEBUG_CLIENT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
 #endif
   msg = (struct GNUNET_MessageHeader *) buf;
   msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
@@ -710,8 +688,8 @@ GNUNET_CLIENT_service_test (const char *service,
   struct GNUNET_CLIENT_Connection *conn;
 
 #if DEBUG_CLIENT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
-              service);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
+       service);
 #endif
 #ifdef AF_UNIX
   {
@@ -725,9 +703,9 @@ GNUNET_CLIENT_service_test (const char *service,
     {
       if (strlen (unixpath) >= sizeof (s_un.sun_path))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                    _("UNIXPATH `%s' too long, maximum length is %llu\n"),
-                    unixpath, sizeof (s_un.sun_path));
+        LOG (GNUNET_ERROR_TYPE_WARNING,
+             _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
+             sizeof (s_un.sun_path));
       }
       else
       {
@@ -867,9 +845,9 @@ GNUNET_CLIENT_service_test (const char *service,
   conn = GNUNET_CLIENT_connect (service, cfg);
   if (conn == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _("Could not connect to service `%s', must not be running.\n"),
-                service);
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         _("Could not connect to service `%s', must not be running.\n"),
+         service);
     service_test_error (task, task_cls);
     return;
   }
@@ -883,8 +861,8 @@ GNUNET_CLIENT_service_test (const char *service,
                                            timeout, GNUNET_YES, &write_test,
                                            conn))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Failure to transmit request to service `%s'\n"), service);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         _("Failure to transmit request to service `%s'\n"), service);
     service_test_error (task, task_cls);
     GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
     return;
@@ -923,8 +901,7 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Transmission failed due to shutdown.\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
 #endif
     th->sock->th = NULL;
     th->notify (th->notify_cls, 0, NULL);
@@ -944,16 +921,15 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                   (th->sock->back_off, 2),
                                   GNUNET_TIME_UNIT_SECONDS);
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Transmission failed %u times, trying again in %llums.\n",
-                MAX_ATTEMPTS - th->attempts_left,
-                (unsigned long long) delay.rel_value);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Transmission failed %u times, trying again in %llums.\n",
+         MAX_ATTEMPTS - th->attempts_left,
+         (unsigned long long) delay.rel_value);
 #endif
     th->reconnect_task =
         GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
     return;
   }
-  GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, th->sock->ignore_shutdown);
   th->th =
       GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
                                                GNUNET_TIME_absolute_get_remaining
@@ -998,9 +974,9 @@ client_notify (void *cls, size_t size, void *buf)
         (delay.rel_value < 1))
     {
 #if DEBUG_CLIENT
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Transmission failed %u times, giving up.\n",
-                  MAX_ATTEMPTS - th->attempts_left);
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Transmission failed %u times, giving up.\n",
+           MAX_ATTEMPTS - th->attempts_left);
 #endif
       GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
       GNUNET_free (th);
@@ -1008,9 +984,9 @@ client_notify (void *cls, size_t size, void *buf)
     }
     /* auto-retry */
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Failed to connect to `%s', automatically trying again.\n",
-                th->sock->service_name);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Failed to connect to `%s', automatically trying again.\n",
+         th->sock->service_name);
 #endif
     GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
     th->sock->sock = NULL;
@@ -1020,10 +996,10 @@ client_notify (void *cls, size_t size, void *buf)
                                   (th->sock->back_off, 2),
                                   GNUNET_TIME_UNIT_SECONDS);
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Transmission failed %u times, trying again in %llums.\n",
-                MAX_ATTEMPTS - th->attempts_left,
-                (unsigned long long) delay.rel_value);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Transmission failed %u times, trying again in %llums.\n",
+         MAX_ATTEMPTS - th->attempts_left,
+         (unsigned long long) delay.rel_value);
 #endif
     th->sock->th = th;
     th->reconnect_task =
@@ -1153,9 +1129,8 @@ transmit_for_response (void *cls, size_t size, void *buf)
   if (NULL == buf)
   {
 #if DEBUG_CLIENT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _
-                ("Could not submit request, not expecting to receive a response.\n"));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         _("Could not submit request, not expecting to receive a response.\n"));
 #endif
     if (NULL != tc->rn)
       tc->rn (tc->rn_cls, NULL);