add typemap retransmission code
authorChristian Grothoff <christian@grothoff.org>
Sat, 22 Oct 2011 21:05:05 +0000 (21:05 +0000)
committerChristian Grothoff <christian@grothoff.org>
Sat, 22 Oct 2011 21:05:05 +0000 (21:05 +0000)
src/core/gnunet-service-core_sessions.c
src/core/gnunet-service-core_typemap.c

index 24aa2f9bc6fe80c251065a18ae3c469f8d412014..6076604a11a94ca7694b769b6a9fcdfdf331ba55 100644 (file)
 #include "gnunet-service-core_clients.h"
 #include "gnunet_constants.h"
 
+/**
+ * How often do we transmit our typemap?
+ */
+#define TYPEMAP_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
+
+
 /**
  * Message ready for encryption.  This struct is followed by the
  * actual content of the message.
@@ -120,6 +126,11 @@ struct Session
    */
   GNUNET_SCHEDULER_TaskIdentifier cork_task;
 
+  /**
+   * Task to transmit our type map.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier typemap_task;
+
   /**
    * Is the neighbour queue empty and thus ready for us
    * to transmit an encrypted message?  
@@ -181,6 +192,7 @@ GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
                                 car);
     GSC_CLIENTS_reject_request (car);
   }
+  GNUNET_SCHEDULER_cancel (session->typemap_task);
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multihashmap_remove (sessions,
                                                        &session->peer.hashPubKey, session));
@@ -197,6 +209,36 @@ GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
 }
 
 
+/**
+ * Transmit our current typemap message to the other peer.
+ * (Done periodically in case an update got lost).
+ *
+ * @param cls the 'struct Session*'
+ * @param tc unused
+ */ 
+static void
+transmit_typemap_task (void *cls,
+                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Session *session = cls;
+  struct GNUNET_MessageHeader *hdr;
+  struct GNUNET_TIME_Relative delay;
+
+  delay = TYPEMAP_FREQUENCY;
+  /* randomize a bit to avoid spont. sync */
+  delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                              1000);
+  session->typemap_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                       &transmit_typemap_task,
+                                                       session);
+  hdr = GSC_TYPEMAP_compute_type_map_message ();
+  GSC_KX_encrypt_and_transmit (session->kxinfo, 
+                              hdr,
+                              ntohs (hdr->size));
+  GNUNET_free (hdr);
+}
+
+
 /**
  * Create a session, a key exchange was just completed.
  *
@@ -207,7 +249,6 @@ void
 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
                     struct GSC_KeyExchangeInfo *kx)
 {
-  struct GNUNET_MessageHeader *hdr;
   struct Session *session;
 
 #if DEBUG_CORE
@@ -218,6 +259,8 @@ GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
   session->peer = *peer;
   session->kxinfo = kx;
   session->time_established = GNUNET_TIME_absolute_get ();
+  session->typemap_task = GNUNET_SCHEDULER_add_now (&transmit_typemap_task,
+                                                   session);
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multihashmap_put (sessions,
                                                     &peer->hashPubKey, session,
@@ -226,13 +269,6 @@ GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
                            gettext_noop ("# entries in session map"),
                            GNUNET_CONTAINER_multihashmap_size (sessions), 
                            GNUNET_NO);
-  /* FIXME: we should probably do this periodically (in case
-     type map message is lost...) */
-  hdr = GSC_TYPEMAP_compute_type_map_message ();
-  GSC_KX_encrypt_and_transmit (kx, 
-                              hdr,
-                              ntohs (hdr->size));
-  GNUNET_free (hdr);
 }
 
 
index e60f99ed61e0f7545c4f6919ecef99efa44f32ba..e4f75bd0034b09ffcefd6e66a9aa461fd29c362f 100644 (file)
@@ -107,6 +107,10 @@ GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
   switch (ntohs (msg->type))
   {
   case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
+    GNUNET_STATISTICS_update (GSC_stats, 
+                             gettext_noop ("# type maps received"),
+                             1,
+                             GNUNET_NO);
     if (size != sizeof (struct GSC_TypeMap))
     {
       GNUNET_break_op (0);
@@ -116,6 +120,10 @@ GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
     memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap));
     return ret;
   case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
+    GNUNET_STATISTICS_update (GSC_stats, 
+                             gettext_noop ("# type maps received"),
+                             1,
+                             GNUNET_NO);
     ret = GNUNET_malloc (sizeof (struct GSC_TypeMap));
     dlen = sizeof (struct GSC_TypeMap);
     if ( (Z_OK !=
@@ -144,6 +152,10 @@ broadcast_my_type_map ()
   struct GNUNET_MessageHeader *hdr;
 
   hdr = GSC_TYPEMAP_compute_type_map_message ();
+  GNUNET_STATISTICS_update (GSC_stats, 
+                           gettext_noop ("# updates to my type map"),
+                           1,
+                           GNUNET_NO);
   GSC_SESSIONS_broadcast (hdr);
   GNUNET_free (hdr);
 }