more code to get latency in ATSI working; not complete, also now generating a warning...
authorChristian Grothoff <christian@grothoff.org>
Mon, 7 Nov 2011 22:06:02 +0000 (22:06 +0000)
committerChristian Grothoff <christian@grothoff.org>
Mon, 7 Nov 2011 22:06:02 +0000 (22:06 +0000)
src/transport/gnunet-service-transport.c
src/transport/gnunet-service-transport_neighbours.c
src/transport/gnunet-service-transport_neighbours.h
src/transport/gnunet-service-transport_validation.c
src/transport/gnunet-service-transport_validation.h

index 30bcad8062bd427a6e6a8eff91291c9d930bca19..11a8e23ae2e7391b6d761f1d88e3bcc8ffffe682 100644 (file)
@@ -135,7 +135,7 @@ process_payload (const struct GNUNET_PeerIdentity *peer,
   size_t msg_size = ntohs (message->size);
   size_t size =
       sizeof (struct InboundMessage) + msg_size +
-      sizeof (struct GNUNET_ATS_Information) * ats_count;
+    sizeof (struct GNUNET_ATS_Information) * (ats_count + 1);
   char buf[size];
   struct GNUNET_ATS_Information *ap;
 
@@ -162,11 +162,13 @@ process_payload (const struct GNUNET_PeerIdentity *peer,
   im = (struct InboundMessage *) buf;
   im->header.size = htons (size);
   im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
-  im->ats_count = htonl (ats_count);
+  im->ats_count = htonl (ats_count + 1);
   im->peer = *peer;
   ap = (struct GNUNET_ATS_Information *) &im[1];
   memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
-  memcpy (&ap[ats_count], message, ntohs (message->size));
+  ap[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
+  ap[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
+  memcpy (&ap[ats_count + 1], message, ntohs (message->size));
 
   GST_clients_broadcast (&im->header, GNUNET_YES);
 
index 32ac7c05dd6843d8bf85645366797360105f56ed..e7d458e7ff5622eb5c93937fa5ceb7fa5cdbc860 100644 (file)
@@ -870,6 +870,12 @@ disconnect_neighbour (struct NeighbourMapEntry *n)
   if (is_disconnecting (n))
     return;
   change_state (n, S_DISCONNECT);
+  GST_validation_set_address_use (&n->id,
+                                 n->plugin_name,
+                                 n->session,
+                                 n->addr,
+                                 n->addrlen,
+                                 GNUNET_NO);
 
   if (n->plugin_name != NULL)
   {
@@ -1301,7 +1307,13 @@ GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
       return GNUNET_NO;
     }
   }
-
+  if (n->state == S_CONNECTED) 
+    GST_validation_set_address_use (&n->id,
+                                   n->plugin_name,
+                                   n->session,
+                                   n->addr,
+                                   n->addrlen,
+                                   GNUNET_NO);
   GNUNET_free_non_null (n->addr);
   n->addr = GNUNET_malloc (address_len);
   memcpy (n->addr, address, address_len);
@@ -1315,6 +1327,14 @@ GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
   n->timeout_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
                                     &neighbour_timeout_task, n);
+  if (n->state == S_CONNECTED)
+    GST_validation_set_address_use (&n->id,
+                                   n->plugin_name,
+                                   n->session,
+                                   n->addr,
+                                   n->addrlen,
+                                   GNUNET_YES);
+
 
   if (n->state == S_DISCONNECT)
   {
@@ -1399,6 +1419,29 @@ GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
 }
 
 
+/**
+ * Obtain current latency information for the given neighbour.
+ *
+ * @param peer 
+ * @return observed latency of the address, FOREVER if the address was
+ *         never successfully validated
+ */
+struct GNUNET_TIME_Relative
+GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
+{
+  struct NeighbourMapEntry *n;
+
+  n = lookup_neighbour (peer);
+  if (NULL == n)
+    return GNUNET_TIME_UNIT_FOREVER_REL;
+  return GST_validation_get_address_latency (peer,
+                                            n->plugin_name,
+                                            n->session,
+                                            n->addr,
+                                            n->addrlen);
+}
+
+
 /**
  * Create an entry in the neighbour map for the given peer
  *
@@ -2042,7 +2085,15 @@ GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
 
   was_connected = is_connected (n);
   if (!is_connected (n))
+  {
     change_state (n, S_CONNECTED);
+    GST_validation_set_address_use (&n->id,
+                                   n->plugin_name,
+                                   n->session,
+                                   n->addr,
+                                   n->addrlen,
+                                   GNUNET_YES);
+  }
 
   GNUNET_ATS_address_in_use (GST_ats, &n->id, n->plugin_name, n->addr,
                              n->addrlen, n->addr, GNUNET_YES);
@@ -2174,6 +2225,12 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
 
   if (!was_connected)
   {
+    GST_validation_set_address_use (&n->id,
+                                   n->plugin_name,
+                                   n->session,
+                                   n->addr,
+                                   n->addrlen,
+                                   GNUNET_YES);
     neighbours_connected++;
     GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
                               GNUNET_NO);
@@ -2333,19 +2390,20 @@ GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
   /* do blacklist check */
   bcc =
       GNUNET_malloc (sizeof (struct BlackListCheckContext) +
-                     sizeof (struct GNUNET_ATS_Information) * ats_count +
+                     sizeof (struct GNUNET_ATS_Information) * (ats_count + 1) +
                      sender_address_len + strlen (plugin_name) + 1);
 
   bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
 
-  bcc->ats_count = ats_count;
+  bcc->ats_count = ats_count + 1;
   bcc->sender_address_len = sender_address_len;
   bcc->session = session;
 
   bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
   memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
-
-  bcc->sender_address = (char *) &bcc->ats[ats_count];
+  bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
+  bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
+  bcc->sender_address = (char *) &bcc->ats[ats_count + 1];
   memcpy (bcc->sender_address, sender_address, sender_address_len);
 
   bcc->plugin_name = &bcc->sender_address[sender_address_len];
index 3aafc3d55c8307f1e06e3c379f2a70e77d42232e..48e54e656421377b7b7d9a9f9799a2d6e14c85ef 100644 (file)
@@ -265,6 +265,18 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
                            const struct GNUNET_ATS_Information *ats,
                            uint32_t ats_count);
 
+
+/**
+ * Obtain current latency information for the given neighbour.
+ *
+ * @param peer 
+ * @return observed latency of the address, FOREVER if the address was
+ *         never successfully validated
+ */
+struct GNUNET_TIME_Relative
+GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer);
+
+
 /**
  * We received a disconnect message from the given peer,
  * validate and process.
index ce2ef76a7fe3a1cf94367d839ce60326861cf301..afc35a3a67f8f462d4730e26944cc293c32d4642 100644 (file)
@@ -529,13 +529,6 @@ revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
   delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
-  if (delay.rel_value > 0)
-  {
-    /* should wait a bit longer */
-    ve->revalidation_task =
-        GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
-    return;
-  }
   /* How long until we can possibly permit the next PING? */
   canonical_delay = 
     (ve->in_use == GNUNET_YES) 
@@ -543,6 +536,19 @@ revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     : ( (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0)
        ? VALIDATED_PING_FREQUENCY
        : UNVALIDATED_PING_KEEPALIVE);   
+  if (delay.rel_value > canonical_delay.rel_value * 2)
+  {
+    /* situation changed, recalculate delay */
+    delay = canonical_delay;
+    ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
+  }
+  if (delay.rel_value > 0)
+  {
+    /* should wait a bit longer */
+    ve->revalidation_task =
+        GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
+    return;
+  }
   ve->revalidation_block =
     GNUNET_TIME_relative_to_absolute (canonical_delay);
 
@@ -1180,10 +1186,9 @@ GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
  * Based on this, the validation module will measure latency for the
  * address more or less often.
  *
- * @param sender peer sending the PING
- * @param hdr the PING
- * @param plugin_name name of plugin that received the PING
- * @param session session we got the PING from
+ * @param sender peer 
+ * @param plugin_name name of plugin 
+ * @param session session 
  * @param sender_address address of the sender as known to the plugin, NULL
  *                       if we did not initiate the connection
  * @param sender_address_len number of bytes in sender_address
@@ -1192,13 +1197,32 @@ GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
  */
 void
 GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
-                               const struct GNUNET_MessageHeader *hdr,
                                const char *plugin_name, struct Session *session,
                                const void *sender_address,
                                size_t sender_address_len,
                                int in_use)
 {
-  // FIXME: lookup address, update flag, re-schedule validation task
+  struct ValidationEntry *ve;
+
+  ve = find_validation_entry (NULL, sender, plugin_name, sender_address, sender_address_len);
+  if (NULL == ve)
+  {
+    /* FIXME: this can happen for inbound connections (sender_address_len == 0);
+       in that case, we should hack up some more code here to measure
+       latency for inbound connections! Also, in this case we'll need the session... 
+    */
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_break (ve->in_use != in_use); /* should be different... */
+  ve->in_use = in_use;
+  if (in_use == GNUNET_YES)
+  {
+    /* from now on, higher frequeny, so reschedule now */
+    GNUNET_SCHEDULER_cancel (ve->revalidation_task);
+    ve->revalidation_task =
+      GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
+  }
 }
 
 
@@ -1206,10 +1230,9 @@ GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
  * Query validation about the latest observed latency on a given
  * address.
  *
- * @param sender peer sending the PING
- * @param hdr the PING
- * @param plugin_name name of plugin that received the PING
- * @param session session we got the PING from
+ * @param sender peer
+ * @param plugin_name name of plugin 
+ * @param session session 
  * @param sender_address address of the sender as known to the plugin, NULL
  *                       if we did not initiate the connection
  * @param sender_address_len number of bytes in sender_address
@@ -1218,13 +1241,16 @@ GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
  */
 struct GNUNET_TIME_Relative
 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
-                                   const struct GNUNET_MessageHeader *hdr,
                                    const char *plugin_name, struct Session *session,
                                    const void *sender_address,
                                    size_t sender_address_len)
 {
-  // FIXME: lookup address, return latency field...
-  return GNUNET_TIME_UNIT_ZERO;
+  struct ValidationEntry *ve;
+
+  ve = find_validation_entry (NULL, sender, plugin_name, sender_address, sender_address_len);
+  if (NULL == ve)
+    return GNUNET_TIME_UNIT_FOREVER_REL;
+  return ve->latency;
 }
 
 
index 0e3c5e96d512ee4c4f2feba48961921c8d03f553..2d93e508ebc6c7fbd1e07f467f8a9b47972c9409 100644 (file)
@@ -50,10 +50,9 @@ GST_validation_stop (void);
  * Based on this, the validation module will measure latency for the
  * address more or less often.
  *
- * @param sender peer sending the PING
- * @param hdr the PING
- * @param plugin_name name of plugin that received the PING
- * @param session session we got the PING from
+ * @param sender peer 
+ * @param plugin_name name of plugin
+ * @param session session
  * @param sender_address address of the sender as known to the plugin, NULL
  *                       if we did not initiate the connection
  * @param sender_address_len number of bytes in sender_address
@@ -62,7 +61,6 @@ GST_validation_stop (void);
  */
 void
 GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
-                               const struct GNUNET_MessageHeader *hdr,
                                const char *plugin_name, struct Session *session,
                                const void *sender_address,
                                size_t sender_address_len,
@@ -73,10 +71,9 @@ GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
  * Query validation about the latest observed latency on a given
  * address.
  *
- * @param sender peer sending the PING
- * @param hdr the PING
- * @param plugin_name name of plugin that received the PING
- * @param session session we got the PING from
+ * @param sender peer 
+ * @param plugin_name name of plugin
+ * @param session session
  * @param sender_address address of the sender as known to the plugin, NULL
  *                       if we did not initiate the connection
  * @param sender_address_len number of bytes in sender_address
@@ -85,7 +82,6 @@ GST_validation_set_address_use (const struct GNUNET_PeerIdentity *sender,
  */
 struct GNUNET_TIME_Relative
 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
-                                   const struct GNUNET_MessageHeader *hdr,
                                    const char *plugin_name, struct Session *session,
                                    const void *sender_address,
                                    size_t sender_address_len);