- moved timeout handling responsibility from for nat tests from caller to the library
[oweals/gnunet.git] / src / peerstore / peerstore_api.c
index f31a3b84842c6a1e00f13fe0ff5e3a8ce00046e0..b53bc2f1a2133742eea9161708679e4fa6a1d85b 100644 (file)
@@ -26,6 +26,7 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "peerstore.h"
+#include "peerstore_common.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__)
 
@@ -42,7 +43,7 @@ struct GNUNET_PEERSTORE_Handle
   /**
    * Our configuration.
    */
-    const struct GNUNET_CONFIGURATION_Handle *cfg;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
    * Connection to the service.
@@ -50,57 +51,88 @@ struct GNUNET_PEERSTORE_Handle
   struct GNUNET_CLIENT_Connection *client;
 
   /**
-   * Head of transmission queue.
+   * Message queue
    */
-  struct GNUNET_PEERSTORE_RequestContext *rc_head;
+  struct GNUNET_MQ_Handle *mq;
 
   /**
-   * Tail of transmission queue.
+   * Head of active STORE requests.
    */
-  struct GNUNET_PEERSTORE_RequestContext *rc_tail;
+  struct GNUNET_PEERSTORE_StoreContext *store_head;
 
   /**
-   * Handle for the current transmission request, or NULL if none is pending.
+   * Tail of active STORE requests.
    */
-  struct GNUNET_CLIENT_TransmitHandle *th;
+  struct GNUNET_PEERSTORE_StoreContext *store_tail;
 
   /**
-   * Head of store requests DLL.
+   * Head of active ITERATE requests.
    */
-  struct GNUNET_PEERSTORE_StoreContext *sc_head;
+  struct GNUNET_PEERSTORE_IterateContext *iterate_head;
 
   /**
-   * Tail of store requests DLL.
+   * Tail of active ITERATE requests.
    */
-  struct GNUNET_PEERSTORE_StoreContext *sc_tail;
+  struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
 
   /**
-   * ID for a reconnect task.
+   * Hashmap of watch requests
    */
-  GNUNET_SCHEDULER_TaskIdentifier r_task;
+  struct GNUNET_CONTAINER_MultiHashMap *watches;
+
+};
+
+/**
+ * Context for a store request
+ */
+struct GNUNET_PEERSTORE_StoreContext
+{
+  /**
+   * Kept in a DLL.
+   */
+  struct GNUNET_PEERSTORE_StoreContext *next;
 
   /**
-   * Are we now receiving?
+   * Kept in a DLL.
    */
-  int in_receive;
+  struct GNUNET_PEERSTORE_StoreContext *prev;
+
+  /**
+   * Handle to the PEERSTORE service.
+   */
+  struct GNUNET_PEERSTORE_Handle *h;
+
+  /**
+   * MQ Envelope with store request message
+   */
+  struct GNUNET_MQ_Envelope *ev;
+
+  /**
+   * Continuation called with service response
+   */
+  GNUNET_PEERSTORE_Continuation cont;
+
+  /**
+   * Closure for 'cont'
+   */
+  void *cont_cls;
 
 };
 
 /**
- * Entry in the transmission queue to PEERSTORE service.
- *
+ * Context for a iterate request
  */
-struct GNUNET_PEERSTORE_RequestContext
+struct GNUNET_PEERSTORE_IterateContext
 {
   /**
-   * This is a linked list.
+   * Kept in a DLL.
    */
-  struct GNUNET_PEERSTORE_RequestContext *next;
+  struct GNUNET_PEERSTORE_IterateContext *next;
 
   /**
-   * This is a linked list.
+   * Kept in a DLL.
    */
-  struct GNUNET_PEERSTORE_RequestContext *prev;
+  struct GNUNET_PEERSTORE_IterateContext *prev;
 
   /**
    * Handle to the PEERSTORE service.
@@ -108,37 +140,48 @@ struct GNUNET_PEERSTORE_RequestContext
   struct GNUNET_PEERSTORE_Handle *h;
 
   /**
-   * Function to call after request has been transmitted, or NULL.
+   * MQ Envelope with iterate request message
    */
-  GNUNET_PEERSTORE_Continuation cont;
+  struct GNUNET_MQ_Envelope *ev;
 
   /**
-   * Closure for 'cont'.
+   * Callback with each matching record
    */
-  void *cont_cls;
+  GNUNET_PEERSTORE_Processor callback;
 
   /**
-   * Number of bytes of the request message (follows after this struct).
+   * Closure for 'callback'
    */
-  size_t size;
+  void *callback_cls;
+
+  /**
+   * #GNUNET_YES / #GNUNET_NO
+   * if sent, cannot be canceled
+   */
+  int request_sent;
+
+  /**
+   * Task identifier for the function called
+   * on iterate request timeout
+   */
+  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
 
 };
 
 /**
- * Context for a store request
- *
+ * Context for a watch request
  */
-struct GNUNET_PEERSTORE_StoreContext
+struct GNUNET_PEERSTORE_WatchContext
 {
   /**
    * Kept in a DLL.
    */
-  struct GNUNET_PEERSTORE_StoreContext *next;
+  struct GNUNET_PEERSTORE_WatchContext *next;
 
   /**
    * Kept in a DLL.
    */
-  struct GNUNET_PEERSTORE_StoreContext *prev;
+  struct GNUNET_PEERSTORE_WatchContext *prev;
 
   /**
    * Handle to the PEERSTORE service.
@@ -146,32 +189,53 @@ struct GNUNET_PEERSTORE_StoreContext
   struct GNUNET_PEERSTORE_Handle *h;
 
   /**
-   * Our entry in the transmission queue.
+   * MQ Envelope with watch request message
    */
-  struct GNUNET_PEERSTORE_RequestContext *rc;
+  struct GNUNET_MQ_Envelope *ev;
 
   /**
-   * Function to call with store operation result
+   * Callback with each record received
    */
-  GNUNET_PEERSTORE_Continuation cont;
+  GNUNET_PEERSTORE_Processor callback;
 
   /**
-   * Closure for 'cont'.
+   * Closure for 'callback'
    */
-  void *cont_cls;
+  void *callback_cls;
 
   /**
-   * Set to GNUNET_YES if we are currently receiving replies from the
-   * service.
+   * Hash of the combined key
    */
-  int request_transmitted;
+  struct GNUNET_HashCode keyhash;
+
+  /**
+   * #GNUNET_YES / #GNUNET_NO
+   * if sent, cannot be canceled
+   */
+  int request_sent;
 
 };
 
 /******************************************************************************/
-/***********************         DECLARATIONS         *************************/
+/*******************             DECLARATIONS             *********************/
 /******************************************************************************/
 
+/**
+ * When a response for iterate request is received
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
+ * @param msg message received, NULL on timeout or fatal error
+ */
+void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
+
+/**
+ * When a watch record is received
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
+ * @param msg message received, NULL on timeout or fatal error
+ */
+void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
+
 /**
  * Close the existing connection to PEERSTORE and reconnect.
  *
@@ -181,31 +245,133 @@ static void
 reconnect (struct GNUNET_PEERSTORE_Handle *h);
 
 /**
- * Check if we have a request pending in the transmission queue and are
- * able to transmit it right now.  If so, schedule transmission.
+ * Callback after MQ envelope is sent
  *
- * @param h handle to the service
+ * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
  */
-static void
-trigger_transmit (struct GNUNET_PEERSTORE_Handle *h);
+void watch_request_sent (void *cls);
+
+/**
+ * Callback after MQ envelope is sent
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
+ */
+void iterate_request_sent (void *cls);
+
+/**
+ * Callback after MQ envelope is sent
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
+ */
+void store_request_sent (void *cls);
+
+/**
+ * MQ message handlers
+ */
+static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
+    {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
+    {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
+    {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
+    GNUNET_MQ_HANDLERS_END
+};
 
 /******************************************************************************/
 /*******************         CONNECTION FUNCTIONS         *********************/
 /******************************************************************************/
 
+static void
+handle_client_error (void *cls, enum GNUNET_MQ_Error error)
+{
+  struct GNUNET_PEERSTORE_Handle *h = cls;
+
+  LOG(GNUNET_ERROR_TYPE_ERROR, "Received an error notification from MQ of type: %d\n", error);
+  reconnect(h);
+}
+
+/**
+ * Iterator over previous watches to resend them
+ */
+int rewatch_it(void *cls,
+    const struct GNUNET_HashCode *key,
+    void *value)
+{
+  struct GNUNET_PEERSTORE_Handle *h = cls;
+  struct GNUNET_PEERSTORE_WatchContext *wc = value;
+  struct StoreKeyHashMessage *hm;
+
+  if(GNUNET_YES == wc->request_sent)
+  { /* Envelope gone, create new one. */
+    wc->ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
+    hm->keyhash = wc->keyhash;
+    wc->request_sent = GNUNET_NO;
+  }
+  GNUNET_MQ_notify_sent(wc->ev, &watch_request_sent, wc);
+  GNUNET_MQ_send(h->mq, wc->ev);
+  return GNUNET_YES;
+}
+
 /**
- * Task scheduled to re-try connecting to the peerstore service.
+ * Close the existing connection to PEERSTORE and reconnect.
  *
- * @param cls the 'struct GNUNET_PEERSTORE_Handle'
- * @param tc scheduler context
+ * @param h handle to the service
  */
 static void
-reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect (struct GNUNET_PEERSTORE_Handle *h)
 {
-  struct GNUNET_PEERSTORE_Handle *h = cls;
+  struct GNUNET_PEERSTORE_IterateContext *ic;
+  GNUNET_PEERSTORE_Processor icb;
+  void *icb_cls;
+  struct GNUNET_PEERSTORE_StoreContext *sc;
 
-  h->r_task = GNUNET_SCHEDULER_NO_TASK;
-  reconnect (h);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
+  if (NULL != h->mq)
+  {
+    GNUNET_MQ_destroy(h->mq);
+    h->mq = NULL;
+  }
+  if (NULL != h->client)
+  {
+    GNUNET_CLIENT_disconnect (h->client);
+    h->client = NULL;
+  }
+  h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
+  GNUNET_assert(NULL != h->client);
+  h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
+      mq_handlers,
+      &handle_client_error,
+      h);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "Resending pending requests after reconnect.\n");
+  if (NULL != h->watches)
+  {
+    GNUNET_CONTAINER_multihashmap_iterate(h->watches,
+        &rewatch_it, h);
+  }
+  ic = h->iterate_head;
+  while (NULL != ic)
+  {
+    if (GNUNET_YES == ic->request_sent)
+    {
+      icb = ic->callback;
+      icb_cls = ic->callback_cls;
+      GNUNET_PEERSTORE_iterate_cancel(ic);
+      if(NULL != icb)
+        icb(icb_cls, NULL,_("Iteration canceled due to reconnection."));
+    }
+    else
+    {
+      GNUNET_MQ_notify_sent(ic->ev, &iterate_request_sent, ic);
+      GNUNET_MQ_send(h->mq, ic->ev);
+    }
+    ic = ic->next;
+  }
+  sc = h->store_head;
+  while (NULL != sc)
+  {
+    GNUNET_MQ_notify_sent(sc->ev, &store_request_sent, sc);
+    GNUNET_MQ_send(h->mq, sc->ev);
+    sc = sc->next;
+  }
 }
 
 /**
@@ -216,342 +382,435 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 struct GNUNET_PEERSTORE_Handle *
 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  struct GNUNET_CLIENT_Connection *client;
   struct GNUNET_PEERSTORE_Handle *h;
 
-  client = GNUNET_CLIENT_connect ("peerstore", cfg);
-  if(NULL == client)
-    return NULL;
   h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
-  h->client = client;
+  h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
+  if(NULL == h->client)
+  {
+    GNUNET_free(h);
+    return NULL;
+  }
   h->cfg = cfg;
+  h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
+      mq_handlers,
+      &handle_client_error,
+      h);
+  if(NULL == h->mq)
+  {
+    GNUNET_free(h);
+    return NULL;
+  }
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
   return h;
 }
 
 /**
  * Disconnect from the PEERSTORE service
+ * Do not call in case of pending requests
  *
  * @param h handle to disconnect
  */
 void
 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
 {
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
+  if(NULL != h->watches)
+  {
+    GNUNET_CONTAINER_multihashmap_destroy(h->watches);
+    h->watches = NULL;
+  }
+  if(NULL != h->mq)
+  {
+    GNUNET_MQ_destroy(h->mq);
+    h->mq = NULL;
+  }
   if (NULL != h->client)
   {
     GNUNET_CLIENT_disconnect (h->client);
     h->client = NULL;
   }
-  GNUNET_free (h);
+  GNUNET_free(h);
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
 }
 
+
+/******************************************************************************/
+/*******************            STORE FUNCTIONS           *********************/
+/******************************************************************************/
+
 /**
- * Close the existing connection to PEERSTORE and reconnect.
+ * Callback after MQ envelope is sent
  *
- * @param h handle to the service
+ * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
  */
-static void
-reconnect (struct GNUNET_PEERSTORE_Handle *h)
+void store_request_sent (void *cls)
+{
+  struct GNUNET_PEERSTORE_StoreContext *sc = cls;
+  GNUNET_PEERSTORE_Continuation cont;
+  void *cont_cls;
+
+  sc->ev = NULL;
+  cont = sc->cont;
+  cont_cls = sc->cont_cls;
+  GNUNET_PEERSTORE_store_cancel(sc);
+  if(NULL != cont)
+    cont(cont_cls, GNUNET_OK);
+}
+
+/**
+ * Cancel a store request
+ *
+ * @param sc Store request context
+ */
+void
+GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
+  if(NULL != sc->ev)
   {
-    GNUNET_SCHEDULER_cancel (h->r_task);
-    h->r_task = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_MQ_send_cancel(sc->ev);
+    sc->ev = NULL;
   }
-  if (NULL != h->th)
+  GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
+  GNUNET_free(sc);
+}
+
+/**
+ * Store a new entry in the PEERSTORE.
+ * Note that stored entries can be lost in some cases
+ * such as power failure.
+ *
+ * @param h Handle to the PEERSTORE service
+ * @param sub_system name of the sub system
+ * @param peer Peer Identity
+ * @param key entry key
+ * @param value entry value BLOB
+ * @param size size of 'value'
+ * @param expiry absolute time after which the entry is (possibly) deleted
+ * @param cont Continuation function after the store request is processed
+ * @param cont_cls Closure for 'cont'
+ */
+struct GNUNET_PEERSTORE_StoreContext *
+GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
+    const char *sub_system,
+    const struct GNUNET_PeerIdentity *peer,
+    const char *key,
+    const void *value,
+    size_t size,
+    struct GNUNET_TIME_Absolute expiry,
+    enum GNUNET_PEERSTORE_StoreOption options,
+    GNUNET_PEERSTORE_Continuation cont,
+    void *cont_cls)
+{
+  struct GNUNET_MQ_Envelope *ev;
+  struct GNUNET_PEERSTORE_StoreContext *sc;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+      "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
+      size, sub_system, GNUNET_i2s (peer), key);
+  ev = PEERSTORE_create_record_mq_envelope(sub_system,
+      peer,
+      key,
+      value,
+      size,
+      &expiry,
+      options,
+      GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
+  sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
+  sc->ev = ev;
+  sc->cont = cont;
+  sc->cont_cls = cont_cls;
+  sc->h = h;
+  GNUNET_CONTAINER_DLL_insert_tail(h->store_head, h->store_tail, sc);
+  GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
+  GNUNET_MQ_send(h->mq, ev);
+  return sc;
+
+}
+
+/******************************************************************************/
+/*******************           ITERATE FUNCTIONS          *********************/
+/******************************************************************************/
+
+/**
+ * When a response for iterate request is received
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
+ * @param msg message received, NULL on timeout or fatal error
+ */
+void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+  struct GNUNET_PEERSTORE_Handle *h = cls;
+  struct GNUNET_PEERSTORE_IterateContext *ic;
+  GNUNET_PEERSTORE_Processor callback;
+  void *callback_cls;
+  uint16_t msg_type;
+  struct GNUNET_PEERSTORE_Record *record;
+  int continue_iter;
+
+  ic = h->iterate_head;
+  if(NULL == ic)
   {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
+    LOG(GNUNET_ERROR_TYPE_ERROR, "Unexpected iteration response, this should not happen.\n");
+    reconnect(h);
+    return;
   }
-  if (NULL != h->client)
+  callback = ic->callback;
+  callback_cls = ic->callback_cls;
+  if(NULL == msg) /* Connection error */
   {
-    GNUNET_CLIENT_disconnect (h->client);
-    h->client = NULL;
+
+    if(NULL != callback)
+      callback(callback_cls, NULL,
+          _("Error communicating with `PEERSTORE' service."));
+    reconnect(h);
+    return;
   }
-  h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
-  if (NULL == h->client)
+  msg_type = ntohs(msg->type);
+  if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
   {
-    h->r_task =
-        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
-                                      h);
+    ic->request_sent = GNUNET_NO;
+    GNUNET_PEERSTORE_iterate_cancel(ic);
+    if(NULL != callback)
+      callback(callback_cls, NULL, NULL);
     return;
   }
-  trigger_transmit (h);
+  if(NULL != callback)
+  {
+    record = PEERSTORE_parse_record_message(msg);
+    if(NULL == record)
+      continue_iter = callback(callback_cls, NULL, _("Received a malformed response from service."));
+    else
+    {
+      continue_iter = callback(callback_cls, record, NULL);
+      PEERSTORE_destroy_record(record);
+    }
+    if(GNUNET_NO == continue_iter)
+      ic->callback = NULL;
+  }
+
 }
 
 /**
- * Transmit the request at the head of the transmission queue
- * and trigger continuation (if any).
+ * Callback after MQ envelope is sent
  *
- * @param cls the 'struct GNUNET_PEERSTORE_Handle' (with the queue)
- * @param size size of the buffer (0 on error)
- * @param buf where to copy the message
- * @return number of bytes copied to buf
+ * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
  */
-static size_t
-do_transmit (void *cls, size_t size, void *buf)
+void iterate_request_sent (void *cls)
 {
-  struct GNUNET_PEERSTORE_Handle *h = cls;
-  struct GNUNET_PEERSTORE_RequestContext *rc = h->rc_head;
-  size_t ret;
+  struct GNUNET_PEERSTORE_IterateContext *ic = cls;
 
-  h->th = NULL;
-  if (NULL == rc)
-    return 0; /* request was canceled in the meantime */
-  if (NULL == buf)
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
+  ic->request_sent = GNUNET_YES;
+  ic->ev = NULL;
+}
+
+/**
+ * Called when the iterate request is timedout
+ *
+ * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
+ */
+void iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_PEERSTORE_IterateContext *ic = cls;
+
+  ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_PEERSTORE_iterate_cancel(ic);
+}
+
+/**
+ * Cancel an iterate request
+ * Please do not call after the iterate request is done
+ *
+ * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
+ */
+void
+GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
+{
+  if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
   {
-    /* peerstore service died */
-    LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
-         "Failed to transmit message to `%s' service.\n", "PEERSTORE");
-    GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc);
-    reconnect (h);
-    if (NULL != rc->cont)
-      rc->cont (rc->cont_cls, _("failed to transmit request (service down?)"));
-    GNUNET_free (rc);
-    return 0;
+    GNUNET_SCHEDULER_cancel(ic->timeout_task);
+    ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  ret = rc->size;
-  if (size < ret)
+  if(GNUNET_NO == ic->request_sent)
   {
-    /* change in head of queue (i.e. cancel + add), try again */
-    trigger_transmit (h);
-    return 0;
+    if(NULL != ic->ev)
+    {
+      GNUNET_MQ_send_cancel(ic->ev);
+      ic->ev = NULL;
+    }
+    GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic);
+    GNUNET_free(ic);
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Transmitting request of size %u to `%s' service.\n", ret, "PEERSTORE");
-  memcpy (buf, &rc[1], ret);
-  GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc);
-  trigger_transmit (h);
-  if (NULL != rc->cont)
-    rc->cont (rc->cont_cls, NULL);
-  GNUNET_free (rc);
-  return ret;
+  else
+    ic->callback = NULL;
 }
 
 /**
- * Check if we have a request pending in the transmission queue and are
- * able to transmit it right now.  If so, schedule transmission.
+ * Iterate over records matching supplied key information
  *
- * @param h handle to the service
+ * @param h handle to the PEERSTORE service
+ * @param sub_system name of sub system
+ * @param peer Peer identity (can be NULL)
+ * @param key entry key string (can be NULL)
+ * @param timeout time after which the iterate request is canceled
+ * @param callback function called with each matching record, all NULL's on end
+ * @param callback_cls closure for @a callback
+ * @return Handle to iteration request
  */
-static void
-trigger_transmit (struct GNUNET_PEERSTORE_Handle *h)
+struct GNUNET_PEERSTORE_IterateContext *
+GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
+    const char *sub_system,
+    const struct GNUNET_PeerIdentity *peer,
+    const char *key,
+    struct GNUNET_TIME_Relative timeout,
+    GNUNET_PEERSTORE_Processor callback, void *callback_cls)
 {
-  struct GNUNET_PEERSTORE_RequestContext *rc;
-
-  if (NULL == (rc = h->rc_head))
-    return; /* no requests queued */
-  if (NULL != h->th)
-    return; /* request already pending */
-  if (NULL == h->client)
-  {
-    /* disconnected, try to reconnect */
-    reconnect (h);
-    return;
-  }
-  h->th =
-    GNUNET_CLIENT_notify_transmit_ready (h->client, rc->size,
-           GNUNET_TIME_UNIT_FOREVER_REL,
-           GNUNET_YES,
-           &do_transmit, h);
+  struct GNUNET_MQ_Envelope *ev;
+  struct GNUNET_PEERSTORE_IterateContext *ic;
+
+  ev = PEERSTORE_create_record_mq_envelope(sub_system,
+      peer,
+      key,
+      NULL,
+      0,
+      NULL,
+      0,
+      GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
+  ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
+  ic->callback = callback;
+  ic->callback_cls = callback_cls;
+  ic->ev = ev;
+  ic->h = h;
+  ic->request_sent = GNUNET_NO;
+  GNUNET_CONTAINER_DLL_insert_tail(h->iterate_head, h->iterate_tail, ic);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+        "Sending an iterate request for sub system `%s'\n", sub_system);
+  GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);
+  GNUNET_MQ_send(h->mq, ev);
+  ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic);
+  return ic;
 }
 
 /******************************************************************************/
-/*******************           GENERAL FUNCTIONS          *********************/
+/*******************            WATCH FUNCTIONS           *********************/
 /******************************************************************************/
 
 /**
- * Function called with server response message
- * after a store operation is request
+ * When a watch record is received
  *
- * @param cls a 'struct GNUNET_PEERSTORE_StoreContext'
+ * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
  * @param msg message received, NULL on timeout or fatal error
  */
-static void
-peerstore_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_PEERSTORE_Handle *h = cls;
-  struct GNUNET_PEERSTORE_StoreContext *sc;
-  struct StoreResponseMessage *srm;
-  uint16_t response_type;
-  uint16_t response_size;
-  char *emsg;
-  GNUNET_PEERSTORE_Continuation cont;
-  void *cont_cls;
+  struct GNUNET_PEERSTORE_Record *record;
+  struct GNUNET_HashCode keyhash;
+  struct GNUNET_PEERSTORE_WatchContext *wc;
 
-  h->in_receive = GNUNET_NO;
   if(NULL == msg)
   {
+    LOG(GNUNET_ERROR_TYPE_ERROR,
+        "Problem receiving a watch response, no way to determine which request.\n");
     reconnect(h);
     return;
   }
-  response_type = ntohs(msg->type);
-  response_size = ntohs(msg->size);
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a response of type %lu from server\n", response_type);
-  switch(response_type)
-  {
-  case GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT:
-    GNUNET_assert(response_size >= sizeof(struct GNUNET_MessageHeader) + sizeof(struct StoreResponseMessage));
-    sc = h->sc_head;
-    if(NULL == sc)
-    {
-      LOG(GNUNET_ERROR_TYPE_ERROR, "Received a response to a non-existent store request\n");
-      return;
-    }
-    cont = sc->cont;
-    cont_cls = sc->cont_cls;
-    GNUNET_PEERSTORE_store_cancel(sc);
-    trigger_transmit (h);
-    if (NULL != h->sc_head)
-    {
-      h->in_receive = GNUNET_YES;
-      GNUNET_CLIENT_receive (h->client,
-          &peerstore_handler,
-          h,
-          GNUNET_TIME_UNIT_FOREVER_REL);
-    }
-    if(NULL != cont)
-    {
-      srm = (struct StoreResponseMessage *)&msg[1];
-      emsg = NULL;
-      if(GNUNET_NO == ntohs(srm->success))
-      {
-        LOG(GNUNET_ERROR_TYPE_DEBUG, "Calling user callback with message: %s\n", emsg);
-        emsg = GNUNET_malloc(ntohs(srm->emsg_size));
-        memcpy(emsg, &srm[1], ntohs(srm->emsg_size));
-      }
-      LOG(GNUNET_ERROR_TYPE_DEBUG, "Calling user callback without a message\n");
-      cont(cont_cls, emsg);
-    }
-    break;
-  }
-
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
+  record = PEERSTORE_parse_record_message(msg);
+  PEERSTORE_hash_key(record->sub_system,
+      record->peer, record->key, &keyhash);
+  wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash);
+  if(NULL != wc->callback)
+    wc->callback(wc->callback_cls, record, NULL);
+  PEERSTORE_destroy_record(record);
 }
 
-/******************************************************************************/
-/*******************             ADD FUNCTIONS            *********************/
-/******************************************************************************/
-
 /**
- * Cancel a store request
+ * Callback after MQ envelope is sent
  *
- * @param sc Store request context
+ * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
  */
-void
-GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
+void watch_request_sent (void *cls)
 {
-  struct GNUNET_PEERSTORE_Handle *h;
+  struct GNUNET_PEERSTORE_WatchContext *wc = cls;
 
-  h = sc->h;
-  sc->cont = NULL;
-  if (GNUNET_YES == sc->request_transmitted)
-    return;                     /* need to finish processing */
-  GNUNET_CONTAINER_DLL_remove (h->sc_head,
-             h->sc_tail,
-             sc);
-  if (NULL != sc->rc)
-  {
-    GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, sc->rc);
-    GNUNET_free (sc->rc);
-  }
-  GNUNET_free (sc);
+  wc->request_sent = GNUNET_YES;
+  wc->ev = NULL;
 }
 
 /**
- * Called after store request is sent
- * Waits for response from service
+ * Cancel a watch request
  *
- * @param cls a 'struct GNUNET_PEERSTORE_StoreContext'
- * @parma emsg error message (or NULL)
+ * @wc handle to the watch request
  */
-void store_receive_result(void *cls, const char *emsg)
+void
+GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
 {
-  struct GNUNET_PEERSTORE_StoreContext *sc = cls;
-  struct GNUNET_PEERSTORE_Handle *h = sc->h;
+  struct GNUNET_PEERSTORE_Handle *h = wc->h;
+  struct GNUNET_MQ_Envelope *ev;
+  struct StoreKeyHashMessage *hm;
 
-  sc->rc = NULL;
-  if(NULL != emsg)
+  LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
+  if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
   {
-    GNUNET_PEERSTORE_store_cancel (sc);
-    reconnect (h);
-    if (NULL != sc->cont)
-      sc->cont (sc->cont_cls, emsg);
-    return;
+    ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
+    GNUNET_MQ_send(h->mq, ev);
+    wc->callback = NULL;
+    wc->callback_cls = NULL;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
-         "PEERSTORE");
-  sc->request_transmitted = GNUNET_YES;
-  if (GNUNET_NO == h->in_receive)
+  if(NULL != wc->ev)
   {
-    h->in_receive = GNUNET_YES;
-    GNUNET_CLIENT_receive (h->client,
-        &peerstore_handler,
-        h,
-        GNUNET_TIME_UNIT_FOREVER_REL);
+    GNUNET_MQ_send_cancel(wc->ev);
+    wc->ev = NULL;
   }
+  GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc);
+  GNUNET_free(wc);
+
 }
 
 /**
- * Store a new entry in the PEERSTORE
+ * Request watching a given key
+ * User will be notified with any new values added to key
  *
- * @param h Handle to the PEERSTORE service
- * @param peer Peer Identity
- * @param sub_system name of the sub system
- * @param value entry value BLOB
- * @param size size of 'value'
- * @param lifetime relative time after which the entry is (possibly) deleted
- * @param cont Continuation function after the store request is processed
- * @param cont_cls Closure for 'cont'
+ * @param h handle to the PEERSTORE service
+ * @param sub_system name of sub system
+ * @param peer Peer identity
+ * @param key entry key string
+ * @param callback function called with each new value
+ * @param callback_cls closure for @a callback
+ * @return Handle to watch request
  */
-struct GNUNET_PEERSTORE_StoreContext *
-GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
-    const struct GNUNET_PeerIdentity *peer,
+struct GNUNET_PEERSTORE_WatchContext *
+GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
     const char *sub_system,
-    const void *value,
-    size_t size,
-    struct GNUNET_TIME_Relative lifetime,
-    GNUNET_PEERSTORE_Continuation cont,
-    void *cont_cls)
+    const struct GNUNET_PeerIdentity *peer,
+    const char *key,
+    GNUNET_PEERSTORE_Processor callback, void *callback_cls)
 {
-  struct GNUNET_PEERSTORE_RequestContext *rc;
-  struct StoreRequestMessage *entry;
-  struct GNUNET_PEERSTORE_StoreContext *sc;
-  char *ss;
-  void *val;
-  size_t sub_system_size;
-  size_t request_size;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-      "Storing value (size: %lu) for subsytem `%s' and peer `%s'\n",
-      size, sub_system, GNUNET_i2s (peer));
-  sub_system_size = strlen(sub_system);
-  request_size = sizeof(struct StoreRequestMessage) + sub_system_size + size;
-  rc = GNUNET_malloc(sizeof(struct GNUNET_PEERSTORE_RequestContext) + request_size);
-  rc->h = h;
-  rc->size = request_size;
-  entry = (struct StoreRequestMessage *)&rc[1];
-  entry->header.size = htons(request_size);
-  entry->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
-  entry->peer = *peer;
-  entry->sub_system_size = htons(sub_system_size);
-  entry->value_size = htons(size);
-  entry->lifetime = lifetime;
-  ss = (char *)&entry[1];
-  memcpy(ss, sub_system, sub_system_size);
-  val = ss + sub_system_size;
-  memcpy(val, value, size);
-  sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
-  sc->cont = cont;
-  sc->cont_cls = cont_cls;
-  sc->h = h;
-  sc->rc = rc;
-  rc->cont = &store_receive_result;
-  rc->cont_cls = sc;
-  GNUNET_CONTAINER_DLL_insert_tail(h->rc_head, h->rc_tail, rc);
-  GNUNET_CONTAINER_DLL_insert_tail(h->sc_head, h->sc_tail, sc);
-  trigger_transmit (h);
-  return sc;
-
+  struct GNUNET_MQ_Envelope *ev;
+  struct StoreKeyHashMessage *hm;
+  struct GNUNET_PEERSTORE_WatchContext *wc;
+
+  ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
+  PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash);
+  wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
+  wc->callback = callback;
+  wc->callback_cls = callback_cls;
+  wc->ev = ev;
+  wc->h = h;
+  wc->request_sent = GNUNET_NO;
+  wc->keyhash = hm->keyhash;
+  if(NULL == h->watches)
+    h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO);
+  GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash,
+      wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+  LOG(GNUNET_ERROR_TYPE_DEBUG,
+      "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
+      sub_system, GNUNET_i2s(peer), key);
+  GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
+  GNUNET_MQ_send(h->mq, ev);
+  return wc;
 }
 
-
 /* end of peerstore_api.c */