gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r8748 - in gnunet: . src/datastore


From: gnunet
Subject: [GNUnet-SVN] r8748 - in gnunet: . src/datastore
Date: Sat, 18 Jul 2009 16:24:00 -0600

Author: grothoff
Date: 2009-07-18 16:24:00 -0600 (Sat, 18 Jul 2009)
New Revision: 8748

Modified:
   gnunet/TODO
   gnunet/src/datastore/datastore_api.c
Log:
finished first draft of implementation of datastore API

Modified: gnunet/TODO
===================================================================
--- gnunet/TODO 2009-07-18 21:38:14 UTC (rev 8747)
+++ gnunet/TODO 2009-07-18 22:24:00 UTC (rev 8748)
@@ -58,7 +58,6 @@
 Datastores:
 * implement sqlite-based sqstore/datastore service
   + implement service (datastore + talks to plugin)
-  + implement library (talks to service)
   + get testcases to pass
 * implement sqlite-based dstore services
   + design API

Modified: gnunet/src/datastore/datastore_api.c
===================================================================
--- gnunet/src/datastore/datastore_api.c        2009-07-18 21:38:14 UTC (rev 
8747)
+++ gnunet/src/datastore/datastore_api.c        2009-07-18 22:24:00 UTC (rev 
8748)
@@ -23,7 +23,6 @@
  * @brief Management for the datastore for files stored on a GNUnet node
  * @author Christian Grothoff
  */
-
 #include "platform.h"
 #include "gnunet_datastore_service.h"
 #include "datastore.h"
@@ -72,7 +71,7 @@
    * this struct, 0 if we have no request pending.
    */
   size_t message_size;
-  
+
 };
 
 
@@ -194,6 +193,7 @@
        (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) ) 
     {
       GNUNET_break (0);
+      h->response_proc = NULL;
       GNUNET_CLIENT_disconnect (h->client);
       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
       cont (h->response_proc_cls, 
@@ -240,10 +240,10 @@
   GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
   uint16_t msize;
 
-  h->response_proc = NULL;
   if (buf == NULL)
     {
       h->message_size = 0;
+      h->response_proc = NULL;
       cont (h->response_proc_cls, 
            GNUNET_SYSERR,
            gettext_noop ("Error transmitting message to datastore 
service.\n"));
@@ -451,7 +451,168 @@
 }
 
 
+
+
 /**
+ * Type of a function to call when we receive a message
+ * from the service.  This specific function is used
+ * to handle messages of type "struct DataMessage".
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void 
+with_result_response_handler (void *cls,
+                             const struct
+                             GNUNET_MessageHeader * msg)
+{
+  static struct GNUNET_TIME_Absolute zero;
+  struct GNUNET_DATASTORE_Handle *h = cls;
+  GNUNET_DATASTORE_Iterator cont = h->response_proc;
+  const struct DataMessage *dm;
+  size_t msize;
+
+  if (msg == NULL)
+    {
+      h->response_proc = NULL;
+      GNUNET_CLIENT_disconnect (h->client);
+      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
+      cont (h->response_proc_cls, 
+           NULL, 0, NULL, 0, 0, 0, zero, 0);
+      return;
+    }
+  if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 
+    {
+      GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
+      h->response_proc = NULL;
+      cont (h->response_proc_cls, 
+           NULL, 0, NULL, 0, 0, 0, zero, 0);
+      return;
+    }
+  if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
+       (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ) 
+    {
+      GNUNET_break (0);
+      GNUNET_CLIENT_disconnect (h->client);
+      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
+      h->response_proc = NULL;
+      cont (h->response_proc_cls, 
+           NULL, 0, NULL, 0, 0, 0, zero, 0);
+      return;
+    }
+  dm = (const struct DataMessage*) msg;
+  msize = ntohl(dm->size);
+  if (ntohs(msg->size) != msize + sizeof(struct DataMessage))
+    {
+      GNUNET_break (0);
+      GNUNET_CLIENT_disconnect (h->client);
+      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
+      h->response_proc = NULL;
+      cont (h->response_proc_cls, 
+           NULL, 0, NULL, 0, 0, 0, zero, 0);
+      return;
+    }
+  cont (h->response_proc_cls, 
+       &dm->key,
+       msize,
+       &dm[1],
+       ntohl(dm->type),
+       ntohl(dm->priority),
+       ntohl(dm->anonymity),
+       GNUNET_TIME_absolute_ntoh(dm->expiration),      
+       GNUNET_ntohll(dm->uid));
+  GNUNET_CLIENT_receive (h->client,
+                        &with_result_response_handler,
+                        h,
+                        GNUNET_TIME_absolute_get_remaining (h->timeout));
+}
+
+
+/**
+ * Transmit message to datastore service and then
+ * read a result message.
+ *
+ * @param cls closure with handle to datastore
+ * @param size number of bytes we can transmit at most
+ * @param buf where to write transmission, NULL on
+ *        timeout
+ * @return number of bytes copied to buf
+ */
+static size_t
+transmit_get_result (void *cls,
+                    size_t size,
+                    void *buf)
+{
+  struct GNUNET_DATASTORE_Handle *h = cls;
+  GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
+  uint16_t msize;
+
+  h->response_proc = NULL;
+  if (buf == NULL)
+    {
+      h->message_size = 0;
+      cont (h->response_proc_cls, 
+           GNUNET_SYSERR,
+           gettext_noop ("Error transmitting message to datastore 
service.\n"));
+      return 0;
+    }
+  GNUNET_assert (h->message_size <= size);
+  memcpy (buf, &h[1], h->message_size);
+  h->message_size = 0;
+  GNUNET_CLIENT_receive (h->client,
+                        &with_result_response_handler,
+                        h,
+                        GNUNET_TIME_absolute_get_remaining (h->timeout));
+  return msize;
+}
+
+
+/**
+ * Helper function that will initiate the
+ * transmission of a message to the datastore
+ * service.  The message must already be prepared
+ * and stored in the buffer at the end of the
+ * handle.  The message must be of a type that
+ * expects a "DataMessage" in response.
+ *
+ * @param h handle to the service with prepared message
+ * @param cont function to call with result
+ * @param cont_cls closure
+ * @param timeout timeout for the operation
+ */
+static void
+transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
+                    GNUNET_DATASTORE_Iterator cont,
+                    void *cont_cls,
+                    struct GNUNET_TIME_Relative timeout)
+{
+  static struct GNUNET_TIME_Absolute zero;
+  const struct GNUNET_MessageHeader *hdr;
+  uint16_t msize;
+
+  hdr = (const struct GNUNET_MessageHeader*) &h[1];
+  msize = ntohs(hdr->size);
+  GNUNET_assert (h->response_proc == NULL);
+  h->response_proc = cont;
+  h->response_proc_cls = cont_cls;
+  h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
+  h->message_size = msize;
+  if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
+                                                  msize,
+                                                  timeout,
+                                                  &transmit_get_result,
+                                                  h))
+    {
+      GNUNET_break (0);
+      h->response_proc = NULL;
+      h->message_size = 0;
+      cont (h->response_proc_cls, 
+           NULL, 0, NULL, 0, 0, 0, zero, 0);
+    }
+}
+
+
+/**
  * Iterate over the results for a particular key
  * in the datastore.
  *
@@ -470,9 +631,21 @@
                       GNUNET_DATASTORE_Iterator iter, void *iter_cls,
                      struct GNUNET_TIME_Relative timeout)
 {
-  static struct GNUNET_TIME_Absolute zero;
-  iter (iter_cls,
-       NULL, 0, NULL, 0, 0, 0, zero, 0);
+  struct GetMessage *gm;
+
+  gm = (struct GetMessage*) &h[1];
+  gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
+  gm->type = htonl(type);
+  if (key != NULL)
+    {
+      gm->header.size = htons(sizeof (struct GetMessage));
+      gm->key = *key;
+    }
+  else
+    {
+      gm->header.size = htons(sizeof (struct GetMessage) - 
sizeof(GNUNET_HashCode));
+    }
+  transmit_for_result (h, iter, iter_cls, timeout);
 }
 
 
@@ -491,10 +664,12 @@
                              GNUNET_DATASTORE_Iterator iter, void *iter_cls,
                             struct GNUNET_TIME_Relative timeout)
 {
-  static struct GNUNET_TIME_Absolute zero;
-  
-  iter (iter_cls,
-       NULL, 0, NULL, 0, 0, 0, zero, 0);
+  struct GNUNET_MessageHeader *m;
+
+  m = (struct GNUNET_MessageHeader*) &h[1];
+  m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
+  m->size = htons(sizeof (struct GNUNET_MessageHeader));
+  transmit_for_result (h, iter, iter_cls, timeout);
 }
 
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]