peerstore: fixes in watch functionality
authorOmar Tarabai <tarabai@devegypt.com>
Tue, 3 Jun 2014 13:50:37 +0000 (13:50 +0000)
committerOmar Tarabai <tarabai@devegypt.com>
Tue, 3 Jun 2014 13:50:37 +0000 (13:50 +0000)
src/include/gnunet_protocols.h
src/peerstore/gnunet-service-peerstore.c
src/peerstore/peerstore_api.c
src/peerstore/test_peerstore_api.c

index 5b64dfeaa41b522993aadeb8e7808eedffc1a5d9..bd54062f1c92078bebd8dac785a5f567a53f3b45 100644 (file)
@@ -2477,35 +2477,34 @@ extern "C"
 #define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE 820
 
 /**
- * Store result message
+ * Iteration request
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK 821
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE 821
 
 /**
- * Iteration request
+ * Iteration record message
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE 823
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD 822
 
 /**
- * Iteration response messages
+ * Iteration end message
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD 824
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END 825
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END 823
 
 /**
  * Watch request
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH 826
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH 824
 
 /**
  * Watch response
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD 827
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD 825
 
 /**
  * Watch cancel request
  */
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL 828
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL 826
 
 /*******************************************************************************
  * SOCIAL message types
index 706fcaaae39e8a8e7cea56130908fe7d7eede034..82961f685c721d4ded5cb38bbc5504cae720bbe3 100644 (file)
@@ -144,7 +144,7 @@ int record_iterator(void *cls,
     struct GNUNET_PEERSTORE_Record *record,
     char *emsg)
 {
-  struct GNUNET_SERVER_TransmitContext *tc = cls;
+  struct GNUNET_SERVER_Client *client = cls;
   struct StoreRecordMessage *srm;
 
   srm = PEERSTORE_create_record_message(record->sub_system,
@@ -154,7 +154,7 @@ int record_iterator(void *cls,
       record->value_size,
       record->expiry,
       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
-  GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
+  GNUNET_SERVER_notification_context_unicast(nc, client, (struct GNUNET_MessageHeader *)srm, GNUNET_NO);
   return GNUNET_YES;
 }
 
@@ -189,19 +189,17 @@ int watch_notifier_it(void *cls,
       record->expiry,
       GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
   GNUNET_SERVER_notification_context_unicast(nc, client,
-      (const struct GNUNET_MessageHeader *)srm, GNUNET_YES);
+      (const struct GNUNET_MessageHeader *)srm, GNUNET_NO);
   return GNUNET_YES;
 }
 
 /**
  * Given a new record, notifies watchers
  *
- * @cls closure, a 'struct GNUNET_PEERSTORE_Record *'
- * @tc unused
+ * @param record changed record to update watchers with
  */
-void watch_notifier (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+void watch_notifier (struct GNUNET_PEERSTORE_Record *record)
 {
-  struct GNUNET_PEERSTORE_Record *record = cls;
   struct GNUNET_HashCode keyhash;
 
   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sending update to any watchers.\n");
@@ -265,7 +263,7 @@ void handle_iterate (void *cls,
     const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_PEERSTORE_Record *record;
-  struct GNUNET_SERVER_TransmitContext *tc;
+  struct GNUNET_MessageHeader *endmsg;
 
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
   record = PEERSTORE_parse_record_message(message);
@@ -281,20 +279,21 @@ void handle_iterate (void *cls,
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
-  tc = GNUNET_SERVER_transmit_context_create (client);
+  GNUNET_SERVER_notification_context_add(nc, client);
   if(GNUNET_OK == db->iterate_records(db->cls,
       record->sub_system,
       record->peer,
       record->key,
       &record_iterator,
-      tc))
+      client))
   {
-    GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
-    GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
+    endmsg = GNUNET_new(struct GNUNET_MessageHeader);
+    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);
   }
   else
   {
-    GNUNET_free(tc);
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
   }
   GNUNET_free(record); /* FIXME: destroy record */
@@ -312,7 +311,6 @@ void handle_store (void *cls,
     const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_PEERSTORE_Record *record;
-  struct GNUNET_SERVER_TransmitContext *tc;
 
   record = PEERSTORE_parse_record_message(message);
   if(NULL == record)
@@ -348,10 +346,8 @@ void handle_store (void *cls,
     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
     return;
   }
-  tc = GNUNET_SERVER_transmit_context_create (client);
-  GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK);
-  GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
-  GNUNET_SCHEDULER_add_continuation(&watch_notifier, record, -1);
+  GNUNET_SERVER_receive_done(client, GNUNET_OK);
+  watch_notifier(record);
 }
 
 /**
index 8748625b74bb58d647c666cef6667c4d8a39046c..62b4c370527b9d6a8bfb49c006d700deb00dea0c 100644 (file)
@@ -117,12 +117,6 @@ struct GNUNET_PEERSTORE_StoreContext
    */
   void *cont_cls;
 
-  /**
-   * #GNUNET_YES / #GNUNET_NO
-   * if sent, cannot be canceled
-   */
-  int request_sent;
-
 };
 
 /**
@@ -226,14 +220,6 @@ struct GNUNET_PEERSTORE_WatchContext
 /*******************             DECLARATIONS             *********************/
 /******************************************************************************/
 
-/**
- * When a response for store request is received
- *
- * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
- * @param msg message received, NULL on timeout or fatal error
- */
-void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg);
-
 /**
  * When a response for iterate request is received
  *
@@ -262,7 +248,6 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h);
  * MQ message handlers
  */
 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
-    {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK, sizeof(struct GNUNET_MessageHeader)},
     {&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},
@@ -375,42 +360,6 @@ GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
 /*******************            STORE FUNCTIONS           *********************/
 /******************************************************************************/
 
-/**
- * When a response for store request is received
- *
- * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
- * @param msg message received, NULL on timeout or fatal error
- */
-void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct GNUNET_PEERSTORE_Handle *h = cls;
-  struct GNUNET_PEERSTORE_StoreContext *sc;
-  GNUNET_PEERSTORE_Continuation cont;
-  void *cont_cls;
-
-  sc = h->store_head;
-  if(NULL == sc)
-  {
-    LOG(GNUNET_ERROR_TYPE_ERROR, "Unexpected store response, this should not happen.\n");
-    reconnect(h);
-    return;
-  }
-  cont = sc->cont;
-  cont_cls = sc->cont_cls;
-  GNUNET_CONTAINER_DLL_remove(h->store_head, h->store_tail, sc);
-  GNUNET_free(sc);
-  if(NULL == msg) /* Connection error */
-  {
-    if(NULL != cont)
-      cont(cont_cls, GNUNET_SYSERR);
-    reconnect(h);
-    return;
-  }
-  if(NULL != cont) /* Run continuation */
-    cont(cont_cls, GNUNET_OK);
-
-}
-
 /**
  * Callback after MQ envelope is sent
  *
@@ -419,9 +368,15 @@ void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg)
 void store_request_sent (void *cls)
 {
   struct GNUNET_PEERSTORE_StoreContext *sc = cls;
+  GNUNET_PEERSTORE_Continuation cont;
+  void *cont_cls;
 
-  sc->request_sent = GNUNET_YES;
   sc->ev = NULL;
+  cont = sc->cont;
+  cont_cls = sc->cont_cls;
+  GNUNET_PEERSTORE_store_cancel(sc);
+  if(NULL != cont)
+    cont(cont_cls, GNUNET_OK);
 }
 
 /**
@@ -434,21 +389,13 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
 {
   LOG(GNUNET_ERROR_TYPE_DEBUG,
           "Canceling store request.\n");
-  if(GNUNET_NO == sc->request_sent)
+  if(NULL != sc->ev)
   {
-    if(NULL != sc->ev)
-    {
-      GNUNET_MQ_send_cancel(sc->ev);
-      sc->ev = NULL;
-    }
-    GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
-    GNUNET_free(sc);
-  }
-  else
-  { /* request already sent, will have to wait for response */
-    sc->cont = NULL;
+    GNUNET_MQ_send_cancel(sc->ev);
+    sc->ev = NULL;
   }
-
+  GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
+  GNUNET_free(sc);
 }
 
 /**
@@ -493,7 +440,6 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
   sc->cont = cont;
   sc->cont_cls = cont_cls;
   sc->h = h;
-  sc->request_sent = GNUNET_NO;
   GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc);
   GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
   GNUNET_MQ_send(h->mq, ev);
index 02c8815c5cd1554a22ba689a87bd4ed8796a0a1b..968467231cc0590c7a36af71d5c7ea94d3f08d90 100644 (file)
@@ -129,17 +129,6 @@ run (void *cls,
       NULL);
 }
 
-int iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
-{
-  struct GNUNET_CONTAINER_MultiHashMap *map = cls;
-  uint32_t *x = value;
-
-  printf("Received value: %d\n", *x);
-  if(*x == 2)
-    GNUNET_CONTAINER_multihashmap_remove(map, key, value);
-  return GNUNET_YES;
-}
-
 int
 main (int argc, char *argv[])
 {