undo 17963, better solution ahead
[oweals/gnunet.git] / src / transport / plugin_transport_http_client.c
index ebce5659f1e24e4b08e7345ce0d0f497c988eb97..a2024953c9498afe33eed9cf6651f5bd62eab87c 100644 (file)
@@ -52,9 +52,11 @@ client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
       text[size + 1] = '\0';
     }
 #if BUILD_HTTPS
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-https", "Client: %X - %s", cls, text);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+        "transport-https", "Client: %X - %s", cls, text);
 #else
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-http", "Client: %X - %s", cls, text);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+        "transport-http", "Client: %X - %s", cls, text);
 #endif
   }
   return 0;
@@ -76,7 +78,7 @@ client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
  */
 static int
-client_schedule (struct Plugin *plugin)
+client_schedule (struct Plugin *plugin, int now)
 {
   fd_set rs;
   fd_set ws;
@@ -109,9 +111,12 @@ client_schedule (struct Plugin *plugin)
   }
   mret = curl_multi_timeout (plugin->client_mh, &to);
   if (to == -1)
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
+    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
   else
     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
+  if (now == GNUNET_YES)
+    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1);
+
   if (mret != CURLM_OK)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
@@ -147,13 +152,14 @@ client_send (struct Session *s, struct HTTP_Message *msg)
   if ((s != NULL) && (s->client_put_paused == GNUNET_YES))
   {
 #if VERBOSE_CLIENT
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name, "Client: %X was suspended, unpausing\n", s->client_put);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
+        "Client: %X was suspended, unpausing\n", s->client_put);
 #endif
     s->client_put_paused = GNUNET_NO;
     curl_easy_pause(s->client_put, CURLPAUSE_CONT);
   }
 
-  client_schedule (s->plugin);
+  client_schedule (s->plugin, GNUNET_YES);
 
   return GNUNET_OK;
 }
@@ -190,7 +196,18 @@ client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
        CURL *easy_h  = msg->easy_handle;
        struct Session *s =  NULL;
        char * d = (char *) s;
-       GNUNET_assert (easy_h != NULL);
+
+
+       //GNUNET_assert (easy_h != NULL);
+       if (easy_h == NULL)
+       {
+         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   "Client: connection to ended with reason %i: `%s', %i handles running\n",
+                   msg->data.result,
+                   curl_easy_strerror(msg->data.result),
+                   running);
+         continue;
+       }
 
        GNUNET_assert (CURLE_OK == curl_easy_getinfo(easy_h, CURLINFO_PRIVATE, &d));
        s = (struct Session *) d;
@@ -198,10 +215,13 @@ client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
        if (msg->msg == CURLMSG_DONE)
        {
-#if DEBUG_HTTP
          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Client: %X connection to '%s'  %s ended\n", msg->easy_handle, GNUNET_i2s(&s->target), GNUNET_a2s (s->addr, s->addrlen));
-#endif
+                   "Client: %X connection to '%s'  %s ended with reason %i: `%s'\n",
+                   msg->easy_handle, GNUNET_i2s(&s->target),
+                   http_plugin_address_to_string (NULL, s->addr, s->addrlen),
+                   msg->data.result,
+                   curl_easy_strerror(msg->data.result));
+
          client_disconnect(s);
          //GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,"Notifying about ended session to peer `%s' `%s'\n", GNUNET_i2s (&s->target), http_plugin_address_to_string (plugin, s->addr, s->addrlen));
          notify_session_end (plugin, &s->target, s);
@@ -209,7 +229,7 @@ client_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     }
   }
   while (mret == CURLM_CALL_MULTI_PERFORM);
-  client_schedule (plugin);
+  client_schedule (plugin, GNUNET_NO);
 }
 
 int
@@ -289,7 +309,7 @@ client_disconnect (struct Session *s)
     plugin->client_perform_task = GNUNET_SCHEDULER_NO_TASK;
   }
 
-  plugin->client_perform_task = GNUNET_SCHEDULER_add_now(client_run, plugin);
+  client_schedule (plugin, GNUNET_YES);
 
   return res;
 }
@@ -299,20 +319,17 @@ client_receive_mst_cb (void *cls, void *client,
                      const struct GNUNET_MessageHeader *message)
 {
   struct Session *s = cls;
-  struct Plugin *plugin = s->plugin;
   struct GNUNET_TIME_Relative delay;
 
   delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "CLIENT: CLIENT DELAY %llu ms\n",
-              delay.rel_value);
-
-
   s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
 
   if (GNUNET_TIME_absolute_get().abs_value < s->next_receive.abs_value)
   {
 #if VERBOSE_CLIENT
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
+    struct Plugin *plugin = s->plugin;
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+        "Client: peer `%s' address `%s' next read delayed for %llu ms\n",
                 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
 #endif
   }
@@ -355,7 +372,8 @@ client_receive (void *stream, size_t size, size_t nmemb, void *cls)
 
 #if VERBOSE_CLIENT
   struct Plugin *plugin = s->plugin;
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: Received %Zu bytes from peer `%s'\n",
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+      "Client: Received %Zu bytes from peer `%s'\n",
                    len,
                    GNUNET_i2s (&s->target));
 #endif
@@ -387,7 +405,6 @@ client_receive (void *stream, size_t size, size_t nmemb, void *cls)
                              GNUNET_NO);
 
   return len;
-  client_wake_up(NULL,NULL);
 }
 
 /**
@@ -414,7 +431,8 @@ client_send_cb (void *stream, size_t size, size_t nmemb, void *cls)
   if (msg == NULL)
   {
 #if VERBOSE_CLIENT
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Client: %X Nothing to send! Suspending PUT handle!\n", s->client_put);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+        "Client: %X Nothing to send! Suspending PUT handle!\n", s->client_put);
 #endif
     s->client_put_paused = GNUNET_YES;
     return CURL_READFUNC_PAUSE;
@@ -481,7 +499,9 @@ client_connect (struct Session *s)
 
   plugin->last_tag++;
   /* create url */
-  GNUNET_asprintf (&url, "%s%s;%u", http_plugin_address_to_string (plugin, s->addr, s->addrlen), GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),plugin->last_tag);
+  GNUNET_asprintf (&url, "%s%s;%u",
+      http_plugin_address_to_string (plugin, s->addr, s->addrlen),
+      GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey),plugin->last_tag);
 #if 0
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "URL `%s'\n",
@@ -507,12 +527,10 @@ client_connect (struct Session *s)
   curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, client_receive);
   curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, s);
   curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT_MS,
-      30000);
-                    //(long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+                    (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
   curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, s);
   curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT_MS,
-      30000);
-                    //(long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
+                    (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
   curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
 #if CURL_TCP_NODELAY
@@ -540,12 +558,10 @@ client_connect (struct Session *s)
   curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, client_receive);
   curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, s);
   curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT_MS,
-      300000);
-                    //(long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+                    (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
   curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, s);
   curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT_MS,
-      300000);
-                    //(long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
+                    (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
   curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
                     2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
 #if CURL_TCP_NODELAY