- verboser log, faster start
[oweals/gnunet.git] / src / transport / transport_api.c
index 768ed36dadb34dc00dbefe494631f8bd9eb0fad4..7f0bc92d2597c3f74477351ee29380a9b1e85061 100644 (file)
@@ -180,6 +180,11 @@ struct GNUNET_TRANSPORT_GetHelloHandle
    */
   GNUNET_TRANSPORT_HelloUpdateCallback rec;
 
+  /**
+   * Task for calling the HelloUpdateCallback when we already have a HELLO
+   */
+  GNUNET_SCHEDULER_TaskIdentifier notify_task;
+
   /**
    * Closure for rec.
    */
@@ -1084,6 +1089,7 @@ send_try_connect (void *cls, size_t size, void *buf)
  * @param target who we should try to connect to
  * @param cb callback to be called when request was transmitted to transport
  *         service
+ * @param cb_cls closure for the callback
  * @return a GNUNET_TRANSPORT_TryConnectHandle handle or
  *         NULL on failure (cb will not be called)
  */
@@ -1173,6 +1179,92 @@ send_hello (void *cls, size_t size, void *buf)
 }
 
 
+/**
+ * Send traffic metric message to the service.
+ *
+ * @param cls the message to send
+ * @param size number of bytes available in buf
+ * @param buf where to copy the message
+ * @return number of bytes copied to buf
+ */
+static size_t
+send_metric (void *cls, size_t size, void *buf)
+{
+       struct TrafficMetricMessage *msg = cls;
+  uint16_t ssize;
+  if (buf == NULL)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Timeout while trying to transmit `%s' request.\n", "TRAFFIC_METRIC");
+    GNUNET_free (msg);
+    return 0;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TRAFFIC_METRIC");
+  ssize = ntohs (msg->header.size);
+  GNUNET_assert (size >= ssize);
+  memcpy (buf, msg, ssize);
+  GNUNET_free (msg);
+  return ssize;
+}
+
+
+/**
+ * Set transport metrics for a peer and a direction
+ *
+ * @param handle transport handle
+ * @param peer the peer to set the metric for
+ * @param direction can be: TM_SEND, TM_RECV, TM_BOTH
+ * @param ats the metric as ATS information
+ * @param ats_count the number of metrics
+ *
+ * Supported ATS values:
+ * GNUNET_ATS_QUALITY_NET_DELAY  (value in ms)
+ * GNUNET_ATS_QUALITY_NET_DISTANCE (value in #hops)
+ *
+ * Example
+ * To enforce a delay of 10 ms for peer p1 in sending direction use:
+ *
+ * struct GNUNET_ATS_Information ats;
+ * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
+ * ats.value = ntohl (10);
+ * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
+ *
+ * Note:
+ * Delay restrictions in receiving direction will be enforced with
+ * 1 message delay.
+ */
+void
+GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
+                                                                                                                                               const struct GNUNET_PeerIdentity *peer,
+                                                                                                                                               int direction,
+                                                                                                                                               const struct GNUNET_ATS_Information *ats,
+                                                                                                                                               size_t ats_count)
+{
+  struct TrafficMetricMessage *msg;
+
+  GNUNET_assert (NULL != handle);
+  GNUNET_assert (NULL != peer);
+  GNUNET_assert (direction >= TM_SEND);
+  GNUNET_assert (direction <= TM_BOTH);
+
+  if (0 == ats_count)
+       return;
+
+  size_t len = sizeof (struct TrafficMetricMessage) +
+                                                ats_count * sizeof (struct GNUNET_ATS_Information);
+
+  msg = GNUNET_malloc (len);
+  msg->header.size = htons (len);
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
+  msg->direction = htons (direction);
+  msg->ats_count = htons (ats_count);
+  msg->peer = (*peer);
+  memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
+  schedule_control_transmit (handle, len, &send_metric, msg);
+}
+
+
+
 /**
  * Offer the transport service the HELLO of another peer.  Note that
  * the transport service may just ignore this message if the HELLO is
@@ -1248,10 +1340,43 @@ GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *o
   GNUNET_free (ohh);
 }
 
+int
+GNUNET_TRANSPORT_check_neighbour_connected (struct GNUNET_TRANSPORT_Handle *handle,
+                                                               const struct GNUNET_PeerIdentity *peer)
+{
+       GNUNET_assert (NULL != handle);
+       GNUNET_assert (NULL != peer);
+
+       if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(handle->neighbours, &peer->hashPubKey))
+                       return GNUNET_YES;
+       else
+               return GNUNET_NO;
+}
+
+
+/**
+ * Task to call the HelloUpdateCallback of the GetHelloHandle
+ *
+ * @param cls the GetHelloHandle
+ * @param tc the scheduler task context
+ */
+static void
+call_hello_update_cb_async (void *cls,
+                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
+
+  GNUNET_assert (NULL != ghh->handle->my_hello);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task);
+  ghh->notify_task = GNUNET_SCHEDULER_NO_TASK;
+  ghh->rec (ghh->rec_cls, 
+            (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
+}
 
 
 /**
- * Obtain the HELLO message for this peer.
+ * Obtain the HELLO message for this peer.  The callback given in this function
+ * is never called synchronously.
  *
  * @param handle connection to transport service
  * @param rec function to call with the HELLO, sender will be our peer
@@ -1274,7 +1399,8 @@ GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
   hwl->handle = handle;
   GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
   if (handle->my_hello != NULL)
-    rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
+    hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
+                                                 hwl);
   return hwl;
 }
 
@@ -1289,6 +1415,8 @@ GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
 {
   struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
 
+  if (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task)
+    GNUNET_SCHEDULER_cancel (ghh->notify_task);
   GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
   GNUNET_free (ghh);
 }