log: add \n
[oweals/gnunet.git] / src / regex / gnunet-service-regex.c
index 697b04a36b25ffd5e2799d9f7fb32390e24975e7..294670be658c8e66175430860781e82085602336 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -69,7 +69,7 @@ struct ClientEntry
   /**
    * Task for re-announcing.
    */
-  GNUNET_SCHEDULER_TaskIdentifier refresh_task;
+  struct GNUNET_SCHEDULER_Task * refresh_task;
 
 };
 
@@ -99,23 +99,10 @@ static struct ClientEntry *client_tail;
  */
 static struct GNUNET_SERVER_NotificationContext *nc;
 
-
 /**
- * Task run during shutdown.
- *
- * @param cls unused
- * @param tc unused
+ * Private key for this peer.
  */
-static void
-cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_DHT_disconnect (dht);
-  dht = NULL;
-  GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
-  stats = NULL;
-  GNUNET_SERVER_notification_context_destroy (nc);
-  nc = NULL;
-}
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
 
 
 /**
@@ -125,7 +112,8 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param client identification of the client
  */
 static void
-handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
+handle_client_disconnect (void *cls,
+                          struct GNUNET_SERVER_Client *client)
 {
   struct ClientEntry *ce;
   struct ClientEntry *nx;
@@ -136,10 +124,10 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     nx = ce->next;
     if (ce->client == client)
     {
-      if (GNUNET_SCHEDULER_NO_TASK != ce->refresh_task)
+      if (NULL != ce->refresh_task)
       {
        GNUNET_SCHEDULER_cancel (ce->refresh_task);
-       ce->refresh_task = GNUNET_SCHEDULER_NO_TASK;
+       ce->refresh_task = NULL;
       }
       if (NULL != ce->ah)
       {
@@ -151,23 +139,47 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
        REGEX_INTERNAL_search_cancel (ce->sh);
        ce->sh = NULL;
       }
-      GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
+      GNUNET_CONTAINER_DLL_remove (client_head,
+                                   client_tail,
+                                   ce);
       GNUNET_free (ce);
     }
   }
 }
 
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ */
+static void
+cleanup_task (void *cls)
+{
+  struct ClientEntry *ce;
+
+  while (NULL != (ce = client_head))
+    handle_client_disconnect (NULL,
+                              ce->client);
+  GNUNET_DHT_disconnect (dht);
+  dht = NULL;
+  GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
+  stats = NULL;
+  GNUNET_SERVER_notification_context_destroy (nc);
+  nc = NULL;
+  GNUNET_free (my_private_key);
+  my_private_key = NULL;
+}
+
+
 /**
  * Periodic task to refresh our announcement of the regex.
  *
- * @param cls the 'struct ClientEntry' of the client that triggered the
+ * @param cls the `struct ClientEntry *` of the client that triggered the
  *        announcement
- * @param tc scheduler context
  */
 static void
-reannounce (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+reannounce (void *cls)
 {
   struct ClientEntry *ce = cls;
 
@@ -186,7 +198,7 @@ reannounce (void *cls,
  * @param message the actual message
  */
 static void
-handle_announce (void *cls, 
+handle_announce (void *cls,
                 struct GNUNET_SERVER_Client *client,
                 const struct GNUNET_MessageHeader *message)
 {
@@ -205,26 +217,33 @@ handle_announce (void *cls,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
+
   ce = GNUNET_new (struct ClientEntry);
   ce->client = client;
+  ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
+  ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
+                                                  &reannounce,
+                                                  ce);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting to announce regex `%s' every %s\n",
+             regex,
+             GNUNET_STRINGS_relative_time_to_string (ce->frequency,
+                                                     GNUNET_NO));
   ce->ah = REGEX_INTERNAL_announce (dht,
-                                 &am->pid,
-                                 regex,
-                                 ntohs (am->compression),
-                                 stats);
+                                   my_private_key,
+                                   regex,
+                                   ntohs (am->compression),
+                                   stats);
   if (NULL == ce->ah)
   {
     GNUNET_break (0);
+    GNUNET_SCHEDULER_cancel (ce->refresh_task);
     GNUNET_free (ce);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
-  ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
-                                                  &reannounce,
-                                                  ce);
   GNUNET_CONTAINER_DLL_insert (client_head,
-                              client_tail, 
+                              client_tail,
                               ce);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -236,11 +255,11 @@ handle_announce (void *cls,
  * @param cls the struct ClientEntry of the client searching
  * @param id Peer providing a regex that matches the string.
  * @param get_path Path of the get request.
- * @param get_path_length Lenght of get_path.
+ * @param get_path_length Lenght of @a get_path.
  * @param put_path Path of the put request.
- * @param put_path_length Length of the put_path.
+ * @param put_path_length Length of the @a put_path.
  */
-static void 
+static void
 handle_search_result (void *cls,
                      const struct GNUNET_PeerIdentity *id,
                      const struct GNUNET_PeerIdentity *get_path,
@@ -269,11 +288,11 @@ handle_search_result (void *cls,
   result->put_path_length = htons ((uint16_t) put_path_length);
   result->id = *id;
   gp = &result->id;
-  memcpy (&gp[1], 
+  GNUNET_memcpy (&gp[1],
          get_path,
          get_path_length * sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&gp[1 + get_path_length], 
-         put_path, 
+  GNUNET_memcpy (&gp[1 + get_path_length],
+         put_path,
          put_path_length * sizeof (struct GNUNET_PeerIdentity));
   GNUNET_SERVER_notification_context_unicast (nc,
                                              ce->client,
@@ -290,32 +309,35 @@ handle_search_result (void *cls,
  * @param message the actual message
  */
 static void
-handle_search (void *cls, 
+handle_search (void *cls,
               struct GNUNET_SERVER_Client *client,
               const struct GNUNET_MessageHeader *message)
 {
-  const struct SearchMessage *sm;
+  const struct RegexSearchMessage *sm;
   const char *string;
   struct ClientEntry *ce;
   uint16_t size;
 
   size = ntohs (message->size);
-  sm = (const struct SearchMessage *) message;
+  sm = (const struct RegexSearchMessage *) message;
   string = (const char *) &sm[1];
-  if ( (size <= sizeof (struct SearchMessage)) ||
-       ('\0' != string[size - sizeof (struct SearchMessage) - 1]) )
+  if ( (size <= sizeof (struct RegexSearchMessage)) ||
+       ('\0' != string[size - sizeof (struct RegexSearchMessage) - 1]) )
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting to search for `%s'\n",
+             string);
   ce = GNUNET_new (struct ClientEntry);
   ce->client = client;
   ce->sh = REGEX_INTERNAL_search (dht,
-                               string,
-                               &handle_search_result,
-                               ce,
-                               stats);
+                                  string,
+                                  &handle_search_result,
+                                  ce,
+                                  stats);
   if (NULL == ce->sh)
   {
     GNUNET_break (0);
@@ -324,7 +346,7 @@ handle_search (void *cls,
     return;
   }
   GNUNET_CONTAINER_DLL_insert (client_head,
-                              client_tail, 
+                              client_tail,
                               ce);
   GNUNET_SERVER_notification_context_add (nc, client);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -347,18 +369,29 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
     {NULL, NULL, 0, 0}
   };
+
+  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
+  if (NULL == my_private_key)
+  {
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
   dht = GNUNET_DHT_connect (cfg, 1024);
   if (NULL == dht)
   {
+    GNUNET_free (my_private_key);
+    my_private_key = NULL;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
+                                NULL);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
   stats = GNUNET_STATISTICS_create ("regex", cfg);
   GNUNET_SERVER_add_handlers (server, handlers);
-  GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
+  GNUNET_SERVER_disconnect_notify (server,
+                                   &handle_client_disconnect,
+                                   NULL);
 }