- pausing, unpausing
[oweals/gnunet.git] / src / transport / transport_api.c
index 3a1068cd1b47f06197309395e0d7a57b6bede0df..2d4890db9aa3d8ad5103ba6afafacc2d44e3b70b 100644 (file)
@@ -477,6 +477,9 @@ demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
       break;
     }
     n = neighbour_add (h, &cim->id);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
+         "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
+    GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
     if (h->nc_cb != NULL)
       h->nc_cb (h->cls, &n->id, ats, ats_count);
     break;
@@ -563,6 +566,8 @@ demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
     n = neighbour_find (h, &qm->peer);
     if (n == NULL)
       break;
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
+         "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
     break;
   default:
@@ -983,9 +988,13 @@ GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
                               const struct GNUNET_PeerIdentity *target)
 {
   struct GNUNET_PeerIdentity *pid;
-
   if (NULL == handle->client)
-    return;
+  {
+      /* FIXME: handle->client can be NULL when transport api is reconnecting */
+      GNUNET_break (0);
+      return;
+  }
+
   pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
   *pid = *target;
   schedule_control_transmit (handle,
@@ -994,6 +1003,15 @@ GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
 }
 
 
+struct SendHelloContext
+{
+       GNUNET_SCHEDULER_Task cont;
+
+       void *cls;
+
+       struct GNUNET_MessageHeader *msg;
+};
+
 /**
  * Send HELLO message to the service.
  *
@@ -1005,14 +1023,22 @@ GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
 static size_t
 send_hello (void *cls, size_t size, void *buf)
 {
-  struct GNUNET_MessageHeader *msg = cls;
+  struct SendHelloContext *shc = cls;
+  struct GNUNET_MessageHeader *msg = shc->msg;
   uint16_t ssize;
+  struct GNUNET_SCHEDULER_TaskContext tc;
+  tc.read_ready = NULL;
+  tc.write_ready = NULL;
+  tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
 
   if (buf == NULL)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Timeout while trying to transmit `%s' request.\n", "HELLO");
+    if (NULL != shc->cont)
+      shc->cont (shc->cls, &tc);
     GNUNET_free (msg);
+    GNUNET_free (shc);
     return 0;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
@@ -1020,6 +1046,10 @@ send_hello (void *cls, size_t size, void *buf)
   GNUNET_assert (size >= ssize);
   memcpy (buf, msg, ssize);
   GNUNET_free (msg);
+  tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
+  if (NULL != shc->cont)
+    shc->cont (shc->cls, &tc);
+  GNUNET_free (shc);
   return ssize;
 }
 
@@ -1031,7 +1061,9 @@ send_hello (void *cls, size_t size, void *buf)
  *
  * @param handle connection to transport service
  * @param hello the hello message
- * @param cont continuation to call when HELLO has been sent
+ * @param cont continuation to call when HELLO has been sent,
+ *     tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
+ *     tc reasong GNUNET_SCHEDULER_REASON_READY for success
  * @param cls closure for continuation
  *
  */
@@ -1043,9 +1075,19 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
   uint16_t size;
   struct GNUNET_PeerIdentity peer;
   struct GNUNET_MessageHeader *msg;
+  struct SendHelloContext * shc;
+  struct GNUNET_SCHEDULER_TaskContext tc;
+
+  tc.read_ready = NULL;
+  tc.write_ready = NULL;
+  tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
 
   if (NULL == handle->client)
+  {
+    if (NULL != cont)
+      cont (cls, &tc);
     return;
+  }
   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
   size = ntohs (hello->size);
   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
@@ -1053,6 +1095,9 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
   {
     GNUNET_break (0);
+    if (NULL != cont)
+      if (NULL != cont)
+        cont (cls, &tc);
     return;
   }
   msg = GNUNET_malloc (size);
@@ -1060,7 +1105,11 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
        GNUNET_i2s (&peer));
-  schedule_control_transmit (handle, size, &send_hello, msg);
+  shc = GNUNET_malloc (sizeof (struct SendHelloContext));
+  shc->msg = msg;
+  shc->cont = cont;
+  shc->cls = cls;
+  schedule_control_transmit (handle, size, &send_hello, shc);
 }
 
 
@@ -1145,7 +1194,14 @@ GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
       GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE);
   ret->ready_heap =
       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
+  ret->client = GNUNET_CLIENT_connect ("transport", cfg);
+  if (ret->client == NULL)
+  {
+    GNUNET_free (ret);
+    return NULL;
+  }
+  schedule_control_transmit (ret, sizeof (struct StartMessage), &send_start, ret);
   return ret;
 }