-improve UDP logging
[oweals/gnunet.git] / src / core / gnunet-service-core_sessions.c
index 79a609566d4ada32c2a1d9497be2a0ff04c35927..289d9468fe993dc8626b1e5414b4f2a0ea1b3414 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -59,9 +59,9 @@ struct SessionMessageEntry
   struct GNUNET_TIME_Absolute deadline;
 
   /**
-   * How long is the message? (number of bytes following the "struct
-   * MessageEntry", but not including the size of "struct
-   * MessageEntry" itself!)
+   * How long is the message? (number of bytes following the `struct
+   * MessageEntry`, but not including the size of `struct
+   * MessageEntry` itself!)
    */
   size_t size;
 
@@ -118,12 +118,12 @@ struct Session
   /**
    * Task to transmit corked messages with a delay.
    */
-  GNUNET_SCHEDULER_TaskIdentifier cork_task;
+  struct GNUNET_SCHEDULER_Task * cork_task;
 
   /**
    * Task to transmit our type map.
    */
-  GNUNET_SCHEDULER_TaskIdentifier typemap_task;
+  struct GNUNET_SCHEDULER_Task * typemap_task;
 
   /**
    * Retransmission delay we currently use for the typemap
@@ -213,10 +213,10 @@ GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Destroying session for peer `%4s'\n",
               GNUNET_i2s (&session->peer));
-  if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
+  if (NULL != session->cork_task)
   {
     GNUNET_SCHEDULER_cancel (session->cork_task);
-    session->cork_task = GNUNET_SCHEDULER_NO_TASK;
+    session->cork_task = NULL;
   }
   while (NULL != (car = session->active_client_request_head))
   {
@@ -231,10 +231,10 @@ GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
                                  sme);
     GNUNET_free (sme);
   }
-  if (GNUNET_SCHEDULER_NO_TASK != session->typemap_task)
+  if (NULL != session->typemap_task)
   {
     GNUNET_SCHEDULER_cancel (session->typemap_task);
-    session->typemap_task = GNUNET_SCHEDULER_NO_TASK;
+    session->typemap_task = NULL;
   }
   GSC_CLIENTS_notify_clients_about_neighbour (&session->peer,
                                               session->tmap, NULL);
@@ -291,7 +291,7 @@ transmit_typemap_task (void *cls,
 static void
 start_typemap_task (struct Session *session)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != session->typemap_task)
+  if (NULL != session->typemap_task)
     GNUNET_SCHEDULER_cancel (session->typemap_task);
   session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
   session->typemap_task =
@@ -329,7 +329,8 @@ GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
                          GNUNET_CONTAINER_multipeermap_size (sessions),
                          GNUNET_NO);
   GSC_CLIENTS_notify_clients_about_neighbour (peer,
-                                              NULL, session->tmap);
+                                              NULL,
+                                              session->tmap);
   start_typemap_task (session);
 }
 
@@ -394,10 +395,10 @@ GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
                               1, GNUNET_NO);
     return;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != session->typemap_task)
+  if (NULL != session->typemap_task)
   {
     GNUNET_SCHEDULER_cancel (session->typemap_task);
-    session->typemap_task = GNUNET_SCHEDULER_NO_TASK;
+    session->typemap_task = NULL;
   }
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop
@@ -422,7 +423,8 @@ notify_client_about_session (void *cls,
   struct GSC_Client *client = cls;
   struct Session *session = value;
 
-  GSC_CLIENTS_notify_client_about_neighbour (client, &session->peer,
+  GSC_CLIENTS_notify_client_about_neighbour (client,
+                                             &session->peer,
                                              NULL,      /* old TMAP: none */
                                              session->tmap);
   return GNUNET_OK;
@@ -533,15 +535,16 @@ discard_expired_requests (struct Session *session)
   {
     pos = nxt;
     nxt = pos->next;
-    if ((pos->deadline.abs_value_us < now.abs_value_us) &&
-        (GNUNET_YES != pos->was_solicited))
+    if ( (pos->deadline.abs_value_us < now.abs_value_us) &&
+         (GNUNET_YES != pos->was_solicited) )
     {
       GNUNET_STATISTICS_update (GSC_stats,
                                 gettext_noop
                                 ("# messages discarded (expired prior to transmission)"),
                                 1, GNUNET_NO);
       GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
-                                   session->active_client_request_tail, pos);
+                                   session->active_client_request_tail,
+                                   pos);
       GSC_CLIENTS_reject_request (pos);
     }
   }
@@ -603,7 +606,7 @@ pop_cork_task (void *cls,
 {
   struct Session *session = cls;
 
-  session->cork_task = GNUNET_SCHEDULER_NO_TASK;
+  session->cork_task = NULL;
   try_transmission (session);
 }
 
@@ -646,9 +649,17 @@ try_transmission (struct Session *session)
     GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
     msize += pos->size;
     maxp = GNUNET_MAX (maxp, pos->priority);
-    min_deadline = GNUNET_TIME_absolute_min (min_deadline, pos->deadline);
+    min_deadline = GNUNET_TIME_absolute_min (min_deadline,
+                                             pos->deadline);
     pos = pos->next;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
+              maxp,
+              (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
+              GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
+                                                      GNUNET_YES));
+
   if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
   {
     /* if highest already solicited priority from clients is not critical,
@@ -668,6 +679,10 @@ try_transmission (struct Session *session)
       /* we have messages waiting for solicitation that have a higher
          priority than those that we already accepted; solicit the
          high-priority messages first */
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Soliciting messages based on priority (%u > %u)\n",
+                  maxpc,
+                  maxp);
       solicit_messages (session, 0);
       return;
     }
@@ -676,6 +691,7 @@ try_transmission (struct Session *session)
   {
     /* never solicit more, we have critical messages to process */
     excess = GNUNET_NO;
+    maxpc = GNUNET_CORE_PRIO_BACKGROUND;
   }
   now = GNUNET_TIME_absolute_get ();
   if ( ( (GNUNET_YES == excess) ||
@@ -687,21 +703,34 @@ try_transmission (struct Session *session)
     /* not enough ready yet (tiny message & cork possible), or no messages at all,
        and either excess bandwidth or best-effort or higher message waiting at
        client; in this case, we try to solicit more */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
+                excess,
+                maxpc,
+                msize,
+                GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
+                                                        GNUNET_YES));
     solicit_messages (session,
                       msize);
     if (msize > 0)
     {
       /* if there is data to send, just not yet, make sure we do transmit
        * it once the deadline is reached */
-      if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Corking until %s\n",
+                  GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
+                                                          GNUNET_YES));
+      if (NULL != session->cork_task)
         GNUNET_SCHEDULER_cancel (session->cork_task);
       session->cork_task =
-          GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                        (min_deadline), &pop_cork_task,
+          GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
+                                        &pop_cork_task,
                                         session);
     }
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Building combined plaintext buffer to transmit message!\n");
   /* create plaintext buffer of all messages (that fit), encrypt and
      transmit */
   {
@@ -843,7 +872,7 @@ GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
   pos = session->sme_head;
   while ( (NULL != pos) &&
-          (pos->priority > sme->priority) )
+          (pos->priority >= sme->priority) )
     pos = pos->next;
   if (NULL == pos)
     GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,