- moved timeout handling responsibility from for nat tests from caller to the library
[oweals/gnunet.git] / src / peerstore / gnunet-service-peerstore.c
index 82961f685c721d4ded5cb38bbc5504cae720bbe3..140db80d8795714b5dbbfa534c696be9bf575066 100644 (file)
 #include "gnunet_peerstore_plugin.h"
 #include "peerstore_common.h"
 
-/**
- * Context of a PEERSTORE watch
- */
-struct WatchContext
-{
-
-  /**
-   * Hash of key of watched record
-   */
-  struct GNUNET_HashCode keyhash;
-
-  /**
-   * Client requested the watch
-   */
-  struct GNUNET_SERVER_Client *client;
-
-};
-
 /**
  * Interval for expired records cleanup (in seconds)
  */
-#define CLEANUP_INTERVAL 300 /* 5mins */
+#define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
 
 /**
  * Our configuration.
@@ -95,6 +77,7 @@ shutdown_task (void *cls,
   }
   GNUNET_SERVER_notification_context_destroy(nc);
   GNUNET_CONTAINER_multihashmap_destroy(watchers);
+  watchers = NULL;
   GNUNET_SCHEDULER_shutdown();
 }
 
@@ -113,10 +96,26 @@ cleanup_expired_records(void *cls,
   deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
   GNUNET_SCHEDULER_add_delayed(
-      GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
+      GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, EXPIRED_RECORDS_CLEANUP_INTERVAL),
       &cleanup_expired_records, NULL);
 }
 
+/**
+ * Search for a disconnected client and remove it
+ *
+ * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
+ * @param key hash of record key
+ * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
+ * @return #GNUNET_OK to continue iterating
+ */
+int client_disconnect_it(void *cls,
+    const struct GNUNET_HashCode *key,
+    void *value)
+{
+  if(cls == value)
+    GNUNET_CONTAINER_multihashmap_remove(watchers, key, value);
+  return GNUNET_OK;
+}
 
 /**
  * A client disconnected.  Remove all of its data structure entries.
@@ -129,6 +128,10 @@ handle_client_disconnect (void *cls,
                          struct GNUNET_SERVER_Client
                          * client)
 {
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client was disconnected, cleaning up.\n");
+  if(NULL != watchers)
+    GNUNET_CONTAINER_multihashmap_iterate(watchers,
+        &client_disconnect_it, client);
 }
 
 /**
@@ -155,6 +158,7 @@ int record_iterator(void *cls,
       record->expiry,
       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
   GNUNET_SERVER_notification_context_unicast(nc, client, (struct GNUNET_MessageHeader *)srm, GNUNET_NO);
+  GNUNET_free(srm);
   return GNUNET_YES;
 }
 
@@ -176,11 +180,6 @@ int watch_notifier_it(void *cls,
   struct StoreRecordMessage *srm;
 
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
-  if(NULL == value)
-  {
-    GNUNET_CONTAINER_multihashmap_remove(watchers, key, value);
-    return GNUNET_YES;
-  }
   srm = PEERSTORE_create_record_message(record->sub_system,
       record->peer,
       record->key,
@@ -190,6 +189,7 @@ int watch_notifier_it(void *cls,
       GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
   GNUNET_SERVER_notification_context_unicast(nc, client,
       (const struct GNUNET_MessageHeader *)srm, GNUNET_NO);
+  GNUNET_free(srm);
   return GNUNET_YES;
 }
 
@@ -202,7 +202,6 @@ void watch_notifier (struct GNUNET_PEERSTORE_Record *record)
 {
   struct GNUNET_HashCode keyhash;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sending update to any watchers.\n");
   PEERSTORE_hash_key(record->sub_system,
       record->peer,
       record->key,
@@ -279,6 +278,10 @@ void handle_iterate (void *cls,
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterate request: ss `%s', peer `%s', key `%s'\n",
+      record->sub_system,
+      (NULL == record->peer) ? "NULL" : GNUNET_i2s(record->peer),
+      (NULL == record->key) ? "NULL" : record->key);
   GNUNET_SERVER_notification_context_add(nc, client);
   if(GNUNET_OK == db->iterate_records(db->cls,
       record->sub_system,
@@ -291,12 +294,14 @@ void handle_iterate (void *cls,
     endmsg->size = htons(sizeof(struct GNUNET_MessageHeader));
     endmsg->type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
     GNUNET_SERVER_notification_context_unicast(nc, client, endmsg, GNUNET_NO);
+    GNUNET_free(endmsg);
+    GNUNET_SERVER_receive_done(client, GNUNET_OK);
   }
   else
   {
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
   }
-  GNUNET_free(record); /* FIXME: destroy record */
+  PEERSTORE_destroy_record(record);
 }
 
 /**
@@ -311,6 +316,7 @@ void handle_store (void *cls,
     const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_PEERSTORE_Record *record;
+  struct StoreRecordMessage *srm;
 
   record = PEERSTORE_parse_record_message(message);
   if(NULL == record)
@@ -319,12 +325,13 @@ void handle_store (void *cls,
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
+  srm = (struct StoreRecordMessage *)message;
   if(NULL == record->sub_system
       || NULL == record->peer
       || NULL == record->key)
   {
-    /* FIXME: Destroy record */
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
+    PEERSTORE_destroy_record(record);
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
@@ -339,15 +346,17 @@ void handle_store (void *cls,
       record->key,
       record->value,
       record->value_size,
-      *record->expiry))
+      *record->expiry,
+      srm->options))
   {
-    /* FIXME: Destroy record */
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
+    PEERSTORE_destroy_record(record);
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
   GNUNET_SERVER_receive_done(client, GNUNET_OK);
   watch_notifier(record);
+  PEERSTORE_destroy_record(record);
 }
 
 /**