(no commit message)
authorMatthias Wachs <wachs@net.in.tum.de>
Thu, 6 May 2010 13:05:24 +0000 (13:05 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Thu, 6 May 2010 13:05:24 +0000 (13:05 +0000)
src/transport/plugin_transport_http.c
src/transport/test_plugin_transport_http.c

index 7569c65c7a2f37468597d3cd584873cccd0ff9bf..bff92dd936a867ec72257ae0db64fe6df016580a 100644 (file)
@@ -163,7 +163,11 @@ static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
  */
 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
 
-static char * hd_src ;
+/**
+ * ID of the task downloading the hostlist
+ */
+static GNUNET_SCHEDULER_TaskIdentifier ti_download;
+
 
 
 /**
@@ -182,6 +186,20 @@ static size_t curl_read_function( void *ptr, size_t size, size_t nmemb, void *st
   return 0;
 }
 
+/**
+ * Task that is run when we are ready to receive more data from the hostlist
+ * server.
+ *
+ * @param cls closure, unused
+ * @param tc task context, unused
+ */
+static void
+task_download (void *cls,
+             const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Download!!!");
+}
+
 /**
  * Function that can be used by the transport service to transmit
  * a message using the plugin.
@@ -224,6 +242,7 @@ http_plugin_send (void *cls,
 {
   char * peer_url = get_url( target );
   CURL *curl;
+  CURLMcode mret;
   CURLcode ret;
 
   int bytes_sent = 0;
@@ -271,7 +290,91 @@ http_plugin_send (void *cls,
                     CURLOPT_TIMEOUT,
                     60L);
 
+  curl_multi = curl_multi_init ();
+  if (curl_multi == NULL)
+    {
+      GNUNET_break (0);
+      /* clean_up (); */
+      return 0;
+    }
+  mret = curl_multi_add_handle (curl_multi, curl);
+  if (mret != CURLM_OK)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  _("%s failed at %s:%d: `%s'\n"),
+                  "curl_multi_add_handle", __FILE__, __LINE__,
+                  curl_multi_strerror (mret));
+      mret = curl_multi_cleanup (curl_multi);
+      if (mret != CURLM_OK)
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    _("%s failed at %s:%d: `%s'\n"),
+                    "curl_multi_cleanup", __FILE__, __LINE__,
+                    curl_multi_strerror (mret));
+      curl_multi = NULL;
+      /* clean_up (); */
+      return 0;
+    }
+
+
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  struct GNUNET_NETWORK_FDSet *grs;
+  struct GNUNET_NETWORK_FDSet *gws;
+  struct GNUNET_TIME_Relative rtime;
+  long timeout_curl;
+  max = -1;
+  FD_ZERO (&rs);
+  FD_ZERO (&ws);
+  FD_ZERO (&es);
+  mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
+  if (mret != CURLM_OK)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  _("%s failed at %s:%d: `%s'\n"),
+                  "curl_multi_fdset", __FILE__, __LINE__,
+                  curl_multi_strerror (mret));
+      /* clean_up (); */
+      return 0;
+    }
+  mret = curl_multi_timeout (curl_multi, &timeout_curl);
+  if (mret != CURLM_OK)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  _("%s failed at %s:%d: `%s'\n"),
+                  "curl_multi_timeout", __FILE__, __LINE__,
+                  curl_multi_strerror (mret));
+      /* clean_up (); */
+      return 0;
+    }
+  /*rtime = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
+                                    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                                                   timeout));*/
+  grs = GNUNET_NETWORK_fdset_create ();
+  gws = GNUNET_NETWORK_fdset_create ();
+  GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
+  GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
+#if DEBUG_HOSTLIST_CLIENT
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Scheduling task for hostlist download using cURL\n");
+#endif
+
+  ti_download = GNUNET_SCHEDULER_add_select (plugin->env->sched,
+                                   GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                                   GNUNET_SCHEDULER_NO_TASK,
+                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                   grs,
+                                   gws,
+                                   &task_download,
+                                   curl_multi);
+  GNUNET_NETWORK_fdset_destroy (gws);
+  GNUNET_NETWORK_fdset_destroy (grs);
+
   GNUNET_free(peer_url);
+  /* FIXME: */
+  bytes_sent = msgbuf_size;
+
   return bytes_sent;
 }
 
@@ -498,6 +601,12 @@ libgnunet_plugin_transport_http_done (void *cls)
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
 
+  if ( ti_download != GNUNET_SCHEDULER_NO_TASK)
+  {
+    GNUNET_SCHEDULER_cancel(plugin->env->sched, ti_download);
+    http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
+  }
+
   if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
index eb27f3ee05977a3db68947170f75753e9aa4e510..f9304be6e92bedc16682a11e09af2885b59feacc 100644 (file)
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
+/**
+ * How long until we give up on transmitting the message?
+ */
+#define STAT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
 
 /**
  * Our public key.
@@ -100,6 +104,13 @@ static int fail;
 
 pid_t pid;
 
+/**
+ * ID of the task controlling the locking between two hostlist tests
+ */
+static GNUNET_SCHEDULER_TaskIdentifier ti_check_stat;
+
+static unsigned int timeout_count;
+
 /**
  * Initialize Environment for this plugin
  */
@@ -140,6 +151,8 @@ shutdown_clean ()
                                        api));
   if (my_private_key != NULL)
     GNUNET_CRYPTO_rsa_key_free (my_private_key);
+  if (ti_check_stat != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel(sched, ti_check_stat);
   GNUNET_SCHEDULER_shutdown(sched);
   return;
 }
@@ -171,6 +184,31 @@ process_stat (void *cls,
 }
 
 
+/**
+ * Task that checks if we should try to download a hostlist.
+ * If so, we initiate the download, otherwise we schedule
+ * this task again for a later time.
+ */
+static void
+task_check_stat (void *cls,
+            const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check...%u \n",  timeout_count);
+  ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
+
+  if ( timeout_count > 3 )
+  {
+    shutdown_clean();
+    return;
+  }
+  timeout_count++;
+
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  ti_check_stat = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_check_stat, NULL);
+}
+
 /**
  * Runs the test.
  *
@@ -265,11 +303,18 @@ run (void *cls,
   fail = GNUNET_NO;
 
   char * test_message  = "Hello World!";
+  size_t bs = 0;
   size_t size = strlen(test_message) +1;
 
   /* Testing to send */
-  api->send(NULL, &my_identity,test_message,size,0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, NULL, NULL);
-  shutdown_clean ();
+  bs = api->send(NULL, &my_identity,test_message,size,0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, NULL, NULL);
+  GNUNET_assert ( bs == size);
+
+  /* check statistics */
+  ti_check_stat = GNUNET_SCHEDULER_add_now(sched, &task_check_stat, NULL);
+  //GNUNET_STATISTICS_get(stats, "http-transport", )
+
+  //ps shutdown_clean ();
   return;
 }