allow empty/NULL context message
[oweals/gnunet.git] / src / peerstore / gnunet-service-peerstore.c
index 8af15140004c284d89fbe748817be76fefa38321..af6438bb253c1496f58a14ea0884ad4f7ad734f0 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C)
+     Copyright (C) 2014, 2015 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.
 */
 
 /**
@@ -90,6 +90,11 @@ static struct ClientEntry *client_head;
  */
 static struct ClientEntry *client_tail;
 
+/**
+ * Task run to clean up expired records.
+ */
+static struct GNUNET_SCHEDULER_Task *expire_task;
+
 /**
  * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO
  */
@@ -117,6 +122,11 @@ do_shutdown ()
     GNUNET_CONTAINER_multihashmap_destroy (watchers);
     watchers = NULL;
   }
+  if (NULL != expire_task)
+  {
+    GNUNET_SCHEDULER_cancel (expire_task);
+    expire_task = NULL;
+  }
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -125,10 +135,9 @@ do_shutdown ()
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   in_shutdown = GNUNET_YES;
   if (NULL == client_head)      /* Only when no connected clients. */
@@ -145,23 +154,21 @@ expire_records_continuation (void *cls, int success);
  * Deletes any expired records from storage
  */
 static void
-cleanup_expired_records (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup_expired_records (void *cls)
 {
   int ret;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  expire_task = NULL;
   GNUNET_assert (NULL != db);
-  ret =
-      db->expire_records (db->cls, GNUNET_TIME_absolute_get (),
-                          expire_records_continuation, NULL);
+  ret = db->expire_records (db->cls, GNUNET_TIME_absolute_get (),
+                           &expire_records_continuation, NULL);
   if (GNUNET_OK != ret)
   {
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                  (GNUNET_TIME_UNIT_SECONDS,
-                                   EXPIRED_RECORDS_CLEANUP_INTERVAL),
-                                  &cleanup_expired_records, NULL);
+    GNUNET_assert (NULL == expire_task);
+    expire_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                               (GNUNET_TIME_UNIT_SECONDS,
+                                                EXPIRED_RECORDS_CLEANUP_INTERVAL),
+                                               &cleanup_expired_records, NULL);
   }
 }
 
@@ -173,14 +180,18 @@ cleanup_expired_records (void *cls,
  * @param success count of records deleted or #GNUNET_SYSERR
  */
 static void
-expire_records_continuation (void *cls, int success)
+expire_records_continuation (void *cls,
+                            int success)
 {
   if (success > 0)
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", success);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                (GNUNET_TIME_UNIT_SECONDS,
-                                 EXPIRED_RECORDS_CLEANUP_INTERVAL),
-                                &cleanup_expired_records, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "%d records expired.\n",
+               success);
+  GNUNET_assert (NULL == expire_task);
+  expire_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                             (GNUNET_TIME_UNIT_SECONDS,
+                                              EXPIRED_RECORDS_CLEANUP_INTERVAL),
+                                             &cleanup_expired_records, NULL);
 }
 
 
@@ -240,7 +251,7 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
  * @param emsg error message or NULL if no errors
  * @return #GNUNET_YES to continue iteration
  */
-static int
+static void
 record_iterator (void *cls, const struct GNUNET_PEERSTORE_Record *record,
                  const char *emsg)
 {
@@ -259,7 +270,7 @@ record_iterator (void *cls, const struct GNUNET_PEERSTORE_Record *record,
     GNUNET_SERVER_receive_done (cls_record->client,
                                 NULL == emsg ? GNUNET_OK : GNUNET_SYSERR);
     PEERSTORE_destroy_record (cls_record);
-    return GNUNET_NO;
+    return;
   }
 
   srm =
@@ -271,7 +282,6 @@ record_iterator (void *cls, const struct GNUNET_PEERSTORE_Record *record,
                                               (struct GNUNET_MessageHeader *)
                                               srm, GNUNET_NO);
   GNUNET_free (srm);
-  return GNUNET_YES;
 }
 
 
@@ -565,18 +575,20 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   if (NULL == db)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Could not load database backend `%s'\n"), db_lib_name);
+                _("Could not load database backend `%s'\n"),
+               db_lib_name);
     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
     return;
   }
   nc = GNUNET_SERVER_notification_context_create (server, 16);
   watchers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
-  GNUNET_SCHEDULER_add_now (&cleanup_expired_records, NULL);
+  expire_task = GNUNET_SCHEDULER_add_now (&cleanup_expired_records,
+                                         NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SERVER_connect_notify (server, &handle_client_connect, NULL);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 }