more stats
[oweals/gnunet.git] / src / core / gnunet-service-core.c
index 1dd7e1d8bd7bcd65fd93772ee6b8199b2a21e195..8bf6f0798914d92a6ab7a2b5abd7028ef514f344 100644 (file)
@@ -37,6 +37,7 @@
 #include "gnunet_peerinfo_service.h"
 #include "gnunet_protocols.h"
 #include "gnunet_signatures.h"
+#include "gnunet_statistics_service.h"
 #include "gnunet_transport_service.h"
 #include "core.h"
 
@@ -689,6 +690,11 @@ static struct GNUNET_SERVER_NotificationContext *notifier;
  */
 static struct Neighbour *neighbours;
 
+/**
+ * For creating statistics.
+ */
+static struct GNUNET_STATISTICS_Handle *stats;
+
 /**
  * Sum of all preferences among all neighbours.
  */
@@ -737,6 +743,7 @@ update_preference_sum (unsigned long long inc)
       preference_sum += n->current_preference;
       n = n->next;
     }    
+  GNUNET_STATISTICS_set (stats, gettext_noop ("# total peer preference"), preference_sum, GNUNET_NO);
 }
 
 
@@ -805,7 +812,14 @@ send_to_all_clients (const struct GNUNET_MessageHeader *msg,
   while (c != NULL)
     {
       if (0 != (c->options & options))
-       send_to_client (c, msg, can_drop);
+       {
+#if DEBUG_CORE_CLIENT
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Sending message of type %u to client.\n",
+                     ntohs (msg->type));
+#endif
+         send_to_client (c, msg, can_drop);
+       }
       c = c->next;
     }
 }
@@ -885,7 +899,7 @@ handle_client_init (void *cls,
               "Sending `%s' message to client.\n", "INIT_REPLY");
 #endif
   send_to_client (c, &irm.header, GNUNET_NO);
-  if (c->options & GNUNET_CORE_OPTION_SEND_CONNECT)
+  if (0 != (c->options & GNUNET_CORE_OPTION_SEND_CONNECT))
     {
       /* notify new client about existing neighbours */
       cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
@@ -1087,8 +1101,10 @@ free_neighbour (struct Neighbour *n)
     GNUNET_SCHEDULER_cancel (sched, n->quota_update_task);
   if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
-  if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
+  if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)    
+      GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
+  if (n->status == PEER_STATE_KEY_CONFIRMED)
+    GNUNET_STATISTICS_update (stats, gettext_noop ("# peers connected"), -1, GNUNET_NO);
   GNUNET_free_non_null (n->public_key);
   GNUNET_free_non_null (n->pending_ping);
   GNUNET_free_non_null (n->pending_pong);
@@ -1134,6 +1150,7 @@ do_encrypt (struct Neighbour *n,
                                             (const struct
                                              GNUNET_CRYPTO_AesInitializationVector
                                              *) iv, out));
+  GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes encrypted"), size, GNUNET_NO);
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Encrypted %u bytes for `%4s' using key %u\n", size,
@@ -1279,6 +1296,7 @@ consider_free_neighbour (struct Neighbour *n)
     prev->next = n->next;
   GNUNET_assert (neighbour_count > 0);
   neighbour_count--;
+  GNUNET_STATISTICS_set (stats, gettext_noop ("# active neighbours"), neighbour_count, GNUNET_NO);
   free_neighbour (n);
 }
 
@@ -1437,6 +1455,7 @@ do_decrypt (struct Neighbour *n,
       GNUNET_break (0);
       return GNUNET_SYSERR;
     }
+  GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes decrypted"), size, GNUNET_NO);
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Decrypted %u bytes from `%4s' using key %u\n",
@@ -2043,6 +2062,7 @@ create_neighbour (const struct GNUNET_PeerIdentity *pid)
   n->next = neighbours;
   neighbours = n;
   neighbour_count++;
+  GNUNET_STATISTICS_set (stats, gettext_noop ("# active neighbours"), neighbour_count, GNUNET_NO);
   n->peer = *pid;
   GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
   now = GNUNET_TIME_absolute_get ();
@@ -2243,6 +2263,7 @@ handle_client_request_connect (void *cls,
   if ( (n->is_connected) ||
        (n->th != NULL) )
     return; /* already connected, or at least trying */
+  GNUNET_STATISTICS_update (stats, gettext_noop ("# connection requests received"), 1, GNUNET_NO);
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Core received `%s' request for `%4s', will try to establish connection\n",
@@ -2307,10 +2328,18 @@ process_hello_retry_send_key (void *cls,
       n->pitr = NULL;
       if (n->public_key != NULL)
        {
+         GNUNET_STATISTICS_update (stats,
+                                   gettext_noop ("# SETKEY messages deferred (need public key)"), 
+                                   -1, 
+                                   GNUNET_NO);
          send_key (n);
        }
       else
        {
+         GNUNET_STATISTICS_update (stats,
+                                   gettext_noop ("# Delayed connecting due to lack of public key"),
+                                   1,
+                                   GNUNET_NO);      
          if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
            n->retry_set_key_task
              = GNUNET_SCHEDULER_add_delayed (sched,
@@ -2345,6 +2374,10 @@ process_hello_retry_send_key (void *cls,
     GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
   if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
     {
+      GNUNET_STATISTICS_update (stats,
+                               gettext_noop ("# Error extracting public key from HELLO"),
+                               1,
+                               GNUNET_NO);      
       GNUNET_free (n->public_key);
       n->public_key = NULL;
 #if DEBUG_CORE
@@ -2593,6 +2626,7 @@ handle_ping (struct Neighbour *n, const struct PingMessage *m)
               "Target of `%s' request is `%4s'.\n",
               "PING", GNUNET_i2s (&t.target));
 #endif
+  GNUNET_STATISTICS_update (stats, gettext_noop ("# ping messages decrypted"), 1, GNUNET_NO);
   if (0 != memcmp (&t.target,
                    &my_identity, sizeof (struct GNUNET_PeerIdentity)))
     {
@@ -2714,7 +2748,7 @@ handle_pong (struct Neighbour *n,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Confirmed key via `%s' message for peer `%4s'\n",
                   "PONG", GNUNET_i2s (&n->peer));
-#endif
+#endif      
       if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
         {
           GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
@@ -2793,6 +2827,7 @@ handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m)
                                         0,
                                         GNUNET_TIME_UNIT_MINUTES,
                                         &process_hello_retry_handle_set_key, n);
+      GNUNET_STATISTICS_update (stats, gettext_noop ("# SETKEY messages deferred (need public key)"), 1, GNUNET_NO);
       return;
     }
   if (0 != memcmp (&m->target,
@@ -3260,6 +3295,7 @@ handle_transport_receive (void *cls,
           GNUNET_break_op (0);
           return;
         }
+      GNUNET_STATISTICS_update (stats, gettext_noop ("# session keys received"), 1, GNUNET_NO);
       handle_set_key (n, (const struct SetKeyMessage *) message);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
@@ -3273,6 +3309,13 @@ handle_transport_receive (void *cls,
           (n->status != PEER_STATE_KEY_CONFIRMED))
         {
           GNUNET_break_op (0);
+         /* blacklist briefly (?); might help recover (?) */
+         GNUNET_TRANSPORT_blacklist (sched, cfg,
+                                     &n->peer, 
+                                     GNUNET_TIME_UNIT_SECONDS,
+                                     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                                                    5),
+                                     NULL, NULL);
           return;
         }
       handle_encrypted_message (n, (const struct EncryptedMessage *) message);
@@ -3283,6 +3326,7 @@ handle_transport_receive (void *cls,
           GNUNET_break_op (0);
           return;
         }
+      GNUNET_STATISTICS_update (stats, gettext_noop ("# ping messages received"), 1, GNUNET_NO);
       if ((n->status != PEER_STATE_KEY_RECEIVED) &&
           (n->status != PEER_STATE_KEY_CONFIRMED))
         {
@@ -3304,6 +3348,7 @@ handle_transport_receive (void *cls,
           GNUNET_break_op (0);
           return;
         }
+      GNUNET_STATISTICS_update (stats, gettext_noop ("# pong messages received"), 1, GNUNET_NO);
       if ( (n->status != PEER_STATE_KEY_RECEIVED) &&
           (n->status != PEER_STATE_KEY_CONFIRMED) )
         {
@@ -3329,7 +3374,10 @@ handle_transport_receive (void *cls,
       now = GNUNET_TIME_absolute_get ();
       n->last_activity = now;
       if (!up)
-        n->time_established = now;
+       {
+         GNUNET_STATISTICS_update (stats, gettext_noop ("# peers connected"), 1, GNUNET_NO);
+         n->time_established = now;
+       }
       if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
        GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
       n->keep_alive_task 
@@ -3538,12 +3586,15 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       neighbour_count--;
       free_neighbour (n);
     }
+  GNUNET_STATISTICS_set (stats, gettext_noop ("# active neighbours"), neighbour_count, GNUNET_NO);
   GNUNET_SERVER_notification_context_destroy (notifier);
   notifier = NULL;
   while (NULL != (c = clients))
     handle_client_disconnect (NULL, c->client_handle);
   if (my_private_key != NULL)
     GNUNET_CRYPTO_rsa_key_free (my_private_key);
+  if (stats != NULL)
+    GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
 }
 
 
@@ -3613,6 +3664,7 @@ run (void *cls,
                                         &handle_transport_notify_connect,
                                         &handle_transport_notify_disconnect);
   GNUNET_assert (NULL != transport);
+  stats = GNUNET_STATISTICS_create (sched, "core", cfg);
   GNUNET_SCHEDULER_add_delayed (sched,
                                 GNUNET_TIME_UNIT_FOREVER_REL,
                                 &cleaning_task, NULL);