[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r33270 - in gnunet/src: include peerstore
From: |
gnunet |
Subject: |
[GNUnet-SVN] r33270 - in gnunet/src: include peerstore |
Date: |
Wed, 14 May 2014 00:08:22 +0200 |
Author: otarabai
Date: 2014-05-14 00:08:22 +0200 (Wed, 14 May 2014)
New Revision: 33270
Modified:
gnunet/src/include/gnunet_peerstore_plugin.h
gnunet/src/include/gnunet_peerstore_service.h
gnunet/src/include/gnunet_protocols.h
gnunet/src/peerstore/gnunet-service-peerstore.c
gnunet/src/peerstore/peerstore.h
gnunet/src/peerstore/peerstore_api.c
gnunet/src/peerstore/plugin_peerstore_sqlite.c
gnunet/src/peerstore/test_peerstore_api.c
Log:
PEERSTORE api overhaul
Modified: gnunet/src/include/gnunet_peerstore_plugin.h
===================================================================
--- gnunet/src/include/gnunet_peerstore_plugin.h 2014-05-13 21:54:23 UTC
(rev 33269)
+++ gnunet/src/include/gnunet_peerstore_plugin.h 2014-05-13 22:08:22 UTC
(rev 33270)
@@ -78,8 +78,9 @@
*/
int
(*store_record) (void *cls,
+ const char *sub_system,
const struct GNUNET_PeerIdentity *peer,
- const char *sub_system,
+ const char *key,
const void *value,
size_t size);
Modified: gnunet/src/include/gnunet_peerstore_service.h
===================================================================
--- gnunet/src/include/gnunet_peerstore_service.h 2014-05-13 21:54:23 UTC
(rev 33269)
+++ gnunet/src/include/gnunet_peerstore_service.h 2014-05-13 22:08:22 UTC
(rev 33270)
@@ -43,17 +43,17 @@
struct GNUNET_PEERSTORE_Handle;
/**
- * Context for add requests
+ * Context for a store request
*/
-struct GNUNET_PEERSTORE_AddContext;
+struct GNUNET_PEERSTORE_StoreContext;
/**
* Continuation called with a status result.
*
* @param cls closure
- * @param emsg error message, NULL on success
+ * @param success #GNUNET_OK or #GNUNET_SYSERR
*/
-typedef void (*GNUNET_PEERSTORE_Continuation)(void *cls, const char *emsg);
+typedef void (*GNUNET_PEERSTORE_Continuation)(void *cls, int success);
/**
* Connect to the PEERSTORE service.
@@ -75,8 +75,9 @@
* Store a new entry in the PEERSTORE
*
* @param h Handle to the PEERSTORE service
+ * @param sub_system name of the sub system
* @param peer Peer Identity
- * @param sub_system name of the sub system
+ * @param key entry key
* @param value entry value BLOB
* @param size size of 'value'
* @param lifetime relative time after which the entry is (possibly) deleted
@@ -85,8 +86,9 @@
*/
struct GNUNET_PEERSTORE_StoreContext *
GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
+ const char *sub_system,
const struct GNUNET_PeerIdentity *peer,
- const char *sub_system,
+ const char *key,
const void *value,
size_t size,
struct GNUNET_TIME_Relative lifetime,
Modified: gnunet/src/include/gnunet_protocols.h
===================================================================
--- gnunet/src/include/gnunet_protocols.h 2014-05-13 21:54:23 UTC (rev
33269)
+++ gnunet/src/include/gnunet_protocols.h 2014-05-13 22:08:22 UTC (rev
33270)
@@ -2467,9 +2467,10 @@
#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE 820
/**
- * Store result message
+ * Store result messages
*/
-#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT 821
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK 821
+#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL 822
/**
Modified: gnunet/src/peerstore/gnunet-service-peerstore.c
===================================================================
--- gnunet/src/peerstore/gnunet-service-peerstore.c 2014-05-13 21:54:23 UTC
(rev 33269)
+++ gnunet/src/peerstore/gnunet-service-peerstore.c 2014-05-13 22:08:22 UTC
(rev 33270)
@@ -90,65 +90,59 @@
const struct GNUNET_MessageHeader *message)
{
struct StoreRequestMessage *req;
- uint16_t msg_size;
- uint16_t sub_system_size;
+ uint16_t req_size;
+ uint16_t ss_size;
+ uint16_t key_size;
uint16_t value_size;
char *sub_system;
+ char *key;
void *value;
+ uint16_t response_type;
struct GNUNET_SERVER_TransmitContext *tc;
- struct StoreResponseMessage *res;
- char *emsg;
- size_t emsg_size = 0;
- char *emsg_dest;
- msg_size = ntohs(message->size);
- if(msg_size < sizeof(struct StoreRequestMessage))
+ req_size = ntohs(message->size);
+ if(req_size < sizeof(struct StoreRequestMessage))
{
GNUNET_break(0);
GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
return;
}
req = (struct StoreRequestMessage *)message;
- sub_system_size = ntohs(req->sub_system_size);
+ ss_size = ntohs(req->sub_system_size);
+ key_size = ntohs(req->key_size);
value_size = ntohs(req->value_size);
- if(sub_system_size + value_size + sizeof(struct StoreRequestMessage)
- != msg_size)
+ if(ss_size + key_size + value_size + sizeof(struct StoreRequestMessage)
+ != req_size)
{
GNUNET_break(0);
GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
return;
}
sub_system = (char *)&req[1];
- value = sub_system + sub_system_size;
- GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for
sub system `%s' and peer `%s'\n",
- msg_size,
+ key = sub_system + ss_size;
+ value = key + key_size;
+ GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for
sub system `%s', peer `%s', key `%s'\n",
+ value_size,
sub_system,
- GNUNET_i2s (&req->peer));
- if(GNUNET_OK != db->store_record(db->cls,
+ GNUNET_i2s (&req->peer),
+ key);
+ if(GNUNET_OK == db->store_record(db->cls,
+ sub_system,
&req->peer,
- sub_system,
- value,
- value_size))
+ key,
+ value,
+ value_size))
{
- GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value,
sqlite database error.");
- emsg = _("Failed to store requested value, sqlite database error.");
- emsg_size = strlen(emsg) + 1;
+ response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
}
- res = GNUNET_malloc(sizeof(struct StoreResponseMessage) + emsg_size);
- res->emsg_size = htons(emsg_size);
- res->header.size = htons(sizeof(struct StoreResponseMessage) + emsg_size);
- res->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT);
- if(emsg_size > 0)
+ else
{
- res->success = htons(GNUNET_NO);
- emsg_dest = (char *)&res[1];
- memcpy(emsg_dest, emsg, emsg_size);
+ GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value,
sqlite database error.");
+ response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
}
- else
- res->success = htons(GNUNET_YES);
+
tc = GNUNET_SERVER_transmit_context_create (client);
- GNUNET_SERVER_transmit_context_append_message(tc, (struct
GNUNET_MessageHeader *)res);
- GNUNET_free(res);
+ GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
}
Modified: gnunet/src/peerstore/peerstore.h
===================================================================
--- gnunet/src/peerstore/peerstore.h 2014-05-13 21:54:23 UTC (rev 33269)
+++ gnunet/src/peerstore/peerstore.h 2014-05-13 22:08:22 UTC (rev 33270)
@@ -40,19 +40,25 @@
struct GNUNET_MessageHeader header;
/**
+ * Size of the sub_system string
+ * Allocated at position 0 after this struct
+ */
+ size_t sub_system_size;
+
+ /**
* Peer Identity
*/
struct GNUNET_PeerIdentity peer;
/**
- * Size of the sub_system string
- * Allocated at position 0 after this struct
+ * Size of the key string
+ * Allocated at position 1 after this struct
*/
- size_t sub_system_size;
+ size_t key_size;
/**
* Size of value blob
- * Allocated at position 1 after this struct
+ * Allocated at position 2 after this struct
*/
size_t value_size;
@@ -63,25 +69,4 @@
};
-/**
- * Message carrying a PEERSTORE store response
- */
-struct StoreResponseMessage
-{
- /**
- * GNUnet message header
- */
- struct GNUNET_MessageHeader header;
-
- /**
- * Was the store operation successful (#GNUNET_YES / #GNUNET_NO)
- */
- uint16_t success GNUNET_PACKED;
-
- /**
- * Size of the following error message (0 if no error)
- */
- size_t emsg_size GNUNET_PACKED;
-};
-
GNUNET_NETWORK_STRUCT_END
Modified: gnunet/src/peerstore/peerstore_api.c
===================================================================
--- gnunet/src/peerstore/peerstore_api.c 2014-05-13 21:54:23 UTC (rev
33269)
+++ gnunet/src/peerstore/peerstore_api.c 2014-05-13 22:08:22 UTC (rev
33270)
@@ -42,174 +42,38 @@
/**
* Our configuration.
*/
- const struct GNUNET_CONFIGURATION_Handle *cfg;
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Connection to the service.
*/
struct GNUNET_CLIENT_Connection *client;
- /**
- * Head of transmission queue.
- */
- struct GNUNET_PEERSTORE_RequestContext *rc_head;
-
- /**
- * Tail of transmission queue.
- */
- struct GNUNET_PEERSTORE_RequestContext *rc_tail;
-
- /**
- * Handle for the current transmission request, or NULL if none is pending.
- */
- struct GNUNET_CLIENT_TransmitHandle *th;
-
- /**
- * Head of store requests DLL.
- */
- struct GNUNET_PEERSTORE_StoreContext *sc_head;
-
- /**
- * Tail of store requests DLL.
- */
- struct GNUNET_PEERSTORE_StoreContext *sc_tail;
-
- /**
- * ID for a reconnect task.
- */
- GNUNET_SCHEDULER_TaskIdentifier r_task;
-
- /**
- * Are we now receiving?
- */
- int in_receive;
-
};
/**
- * Entry in the transmission queue to PEERSTORE service.
- *
- */
-struct GNUNET_PEERSTORE_RequestContext
-{
- /**
- * This is a linked list.
- */
- struct GNUNET_PEERSTORE_RequestContext *next;
-
- /**
- * This is a linked list.
- */
- struct GNUNET_PEERSTORE_RequestContext *prev;
-
- /**
- * Handle to the PEERSTORE service.
- */
- struct GNUNET_PEERSTORE_Handle *h;
-
- /**
- * Function to call after request has been transmitted, or NULL.
- */
- GNUNET_PEERSTORE_Continuation cont;
-
- /**
- * Closure for 'cont'.
- */
- void *cont_cls;
-
- /**
- * Number of bytes of the request message (follows after this struct).
- */
- size_t size;
-
-};
-
-/**
* Context for a store request
- *
*/
struct GNUNET_PEERSTORE_StoreContext
{
- /**
- * Kept in a DLL.
- */
- struct GNUNET_PEERSTORE_StoreContext *next;
/**
- * Kept in a DLL.
+ * Continuation called with service response
*/
- struct GNUNET_PEERSTORE_StoreContext *prev;
-
- /**
- * Handle to the PEERSTORE service.
- */
- struct GNUNET_PEERSTORE_Handle *h;
-
- /**
- * Our entry in the transmission queue.
- */
- struct GNUNET_PEERSTORE_RequestContext *rc;
-
- /**
- * Function to call with store operation result
- */
GNUNET_PEERSTORE_Continuation cont;
/**
- * Closure for 'cont'.
+ * Closure for 'cont'
*/
void *cont_cls;
- /**
- * Set to GNUNET_YES if we are currently receiving replies from the
- * service.
- */
- int request_transmitted;
-
};
/******************************************************************************/
-/*********************** DECLARATIONS
*************************/
-/******************************************************************************/
-
-/**
- * Close the existing connection to PEERSTORE and reconnect.
- *
- * @param h handle to the service
- */
-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.
- *
- * @param h handle to the service
- */
-static void
-trigger_transmit (struct GNUNET_PEERSTORE_Handle *h);
-
-/******************************************************************************/
/******************* CONNECTION FUNCTIONS
*********************/
/******************************************************************************/
/**
- * Task scheduled to re-try connecting to the peerstore service.
- *
- * @param cls the 'struct GNUNET_PEERSTORE_Handle'
- * @param tc scheduler context
- */
-static void
-reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct GNUNET_PEERSTORE_Handle *h = cls;
-
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnect task executed\n");
- h->r_task = GNUNET_SCHEDULER_NO_TASK;
- reconnect (h);
-}
-
-/**
* Connect to the PEERSTORE service.
*
* @return NULL on error
@@ -239,324 +103,69 @@
void
GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
{
- struct GNUNET_PEERSTORE_StoreContext *sc;
- struct GNUNET_PEERSTORE_RequestContext *rc;
-
- while (NULL != (sc = h->sc_head))
- {
- GNUNET_break (GNUNET_YES == sc->request_transmitted);
- sc->request_transmitted = GNUNET_NO;
- GNUNET_PEERSTORE_store_cancel(sc);
- }
- while (NULL != (rc = h->rc_head))
- {
- GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc);
- if (NULL != rc->cont)
- rc->cont (rc->cont_cls, _("aborted due to explicit disconnect request"));
- GNUNET_free (rc);
- }
- if (NULL != h->th)
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
- h->th = NULL;
- }
if (NULL != h->client)
{
GNUNET_CLIENT_disconnect (h->client);
h->client = NULL;
}
- if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
- {
- GNUNET_SCHEDULER_cancel (h->r_task);
- h->r_task = GNUNET_SCHEDULER_NO_TASK;
- }
- GNUNET_free (h);
+ GNUNET_free(h);
LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
}
-/**
- * Close the existing connection to PEERSTORE and reconnect.
- *
- * @param h handle to the service
- */
-static void
-reconnect (struct GNUNET_PEERSTORE_Handle *h)
-{
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
- if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
- {
- GNUNET_SCHEDULER_cancel (h->r_task);
- h->r_task = GNUNET_SCHEDULER_NO_TASK;
- }
- if (NULL != h->th)
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
- h->th = NULL;
- }
- if (NULL != h->client)
- {
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
- }
- h->in_receive = GNUNET_NO;
- h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
- if (NULL == h->client)
- {
- h->r_task =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
&reconnect_task,
- h);
- return;
- }
- trigger_transmit (h);
-}
-/**
- * Transmit the request at the head of the transmission queue
- * and trigger continuation (if any).
- *
- * @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
- */
-static size_t
-do_transmit (void *cls, size_t size, void *buf)
-{
- struct GNUNET_PEERSTORE_Handle *h = cls;
- struct GNUNET_PEERSTORE_RequestContext *rc = h->rc_head;
- size_t ret;
-
- h->th = NULL;
- if (NULL == rc)
- return 0; /* request was canceled in the meantime */
- if (NULL == buf)
- {
- /* 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;
- }
- ret = rc->size;
- if (size < ret)
- {
- /* change in head of queue (i.e. cancel + add), try again */
- trigger_transmit (h);
- return 0;
- }
- 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;
-}
-
-/**
- * Check if we have a request pending in the transmission queue and are
- * able to transmit it right now. If so, schedule transmission.
- *
- * @param h handle to the service
- */
-static void
-trigger_transmit (struct GNUNET_PEERSTORE_Handle *h)
-{
- 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);
-}
-
/******************************************************************************/
/******************* ADD FUNCTIONS
*********************/
/******************************************************************************/
/**
- * Cancel a store request
+ * When a response for store request is received
*
- * @param sc Store request context
- */
-void
-GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
-{
- struct GNUNET_PEERSTORE_Handle *h;
-
- 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);
-}
-
-/**
- * Function called with server response message
- * after a store operation is requested
- *
- * @param cls a 'struct GNUNET_PEERSTORE_Handle'
+ * @param cls unused
* @param msg message received, NULL on timeout or fatal error
*/
-static void
-store_receive(void *cls, const struct GNUNET_MessageHeader *msg)
+void store_response_receiver (void *cls, const struct GNUNET_MessageHeader
*msg)
{
- struct GNUNET_PEERSTORE_Handle *h = cls;
- struct GNUNET_PEERSTORE_StoreContext *sc = h->sc_head;
- GNUNET_PEERSTORE_Continuation cont;
- void *cont_cls;
- uint16_t response_type;
- uint16_t response_size;
- struct StoreResponseMessage *srm;
- int malformed = GNUNET_NO;
- char *emsg;
+ struct GNUNET_PEERSTORE_StoreContext *sc = cls;
+ uint16_t msg_type;
- h->in_receive = GNUNET_NO;
- if (NULL == sc)
- {
- /* didn't expect a response, reconnect */
- reconnect (h);
+ if(NULL == sc->cont)
return;
- }
- cont = sc->cont;
- cont_cls = sc->cont_cls;
- sc->request_transmitted = GNUNET_NO;
- //cancel the request since we only need one response
- GNUNET_PEERSTORE_store_cancel(sc);
if(NULL == msg)
{
- LOG(GNUNET_ERROR_TYPE_ERROR, "`PEERSTORE' service died\n");
- reconnect (h);
- if (NULL != cont)
- cont (cont_cls,
- _("Failed to receive response from `PEERSTORE' service."));
+ sc->cont(sc->cont_cls, GNUNET_SYSERR);
return;
}
- response_type = ntohs(msg->type);
- response_size = ntohs(msg->size);
- if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT != response_type)
- {
- LOG(GNUNET_ERROR_TYPE_ERROR, "Received an unexpected response type: %lu to
store request\n", response_type);
- reconnect(h);
- if (NULL != cont)
- cont (cont_cls,
- _("Received an unexpected response from `PEERSTORE' service."));
- return;
- }
- if(response_size < sizeof(struct StoreResponseMessage))
- {
- malformed = GNUNET_YES;
- }
+ msg_type = ntohs(msg->type);
+ if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK == msg_type)
+ sc->cont(sc->cont_cls, GNUNET_OK);
+ else if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL == msg_type)
+ sc->cont(sc->cont_cls, GNUNET_SYSERR);
else
{
- srm = (struct StoreResponseMessage *)msg;
- if(sizeof(struct StoreResponseMessage) + ntohs(srm->emsg_size) !=
response_size)
- malformed = GNUNET_YES;
+ LOG(GNUNET_ERROR_TYPE_ERROR, "Invalid response from `PEERSTORE'
service.\n");
+ sc->cont(sc->cont_cls, GNUNET_SYSERR);
}
- if(GNUNET_YES == malformed)
- {
- LOG(GNUNET_ERROR_TYPE_ERROR, "Received a malformed response from
`PEERSTORE' service.\n");
- reconnect(h);
- if (NULL != cont)
- cont (cont_cls,
- _("Received a malformed response from `PEERSTORE' service."));
- return;
- }
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a response of type %lu from
server\n", response_type);
- trigger_transmit(h);
- if ( (GNUNET_NO == h->in_receive) && (NULL != h->sc_head) )
- {
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- "A store request was sent but response not received, receiving
now.\n");
- h->in_receive = GNUNET_YES;
- GNUNET_CLIENT_receive (h->client,
- &store_receive,
- h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- }
- if(NULL != cont)
- {
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Calling continuation of store request\n");
- srm = (struct StoreResponseMessage *)msg;
- emsg = NULL;
- if(GNUNET_NO == ntohs(srm->success))
- {
- emsg = GNUNET_malloc(ntohs(srm->emsg_size));
- memcpy(emsg, &srm[1], ntohs(srm->emsg_size));
- }
- cont(cont_cls, emsg);
- }
+
}
/**
- * Called after store request is sent
- * Waits for response from service
+ * Cancel a store request
*
- * @param cls a 'struct GNUNET_PEERSTORE_StoreContext'
- * @parma emsg error message (or NULL)
+ * @param sc Store request context
*/
-void store_trigger_receive(void *cls, const char *emsg)
+void
+GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
{
- struct GNUNET_PEERSTORE_StoreContext *sc = cls;
- struct GNUNET_PEERSTORE_Handle *h = sc->h;
- GNUNET_PEERSTORE_Continuation cont;
- void *cont_cls;
-
- sc->rc = NULL;
- if(NULL != emsg)
- {
- cont = sc->cont;
- cont_cls = sc->cont_cls;
- GNUNET_PEERSTORE_store_cancel (sc);
- reconnect (h);
- if (NULL != cont)
- cont (cont_cls, emsg);
- return;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
- "PEERSTORE");
- sc->request_transmitted = GNUNET_YES;
- if (GNUNET_NO == h->in_receive)
- {
- h->in_receive = GNUNET_YES;
- GNUNET_CLIENT_receive (h->client,
- &store_receive,
- h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- }
+ sc->cont = NULL;
}
/**
* Store a new entry in the PEERSTORE
*
* @param h Handle to the PEERSTORE service
+ * @param sub_system name of the sub system
* @param peer Peer Identity
- * @param sub_system name of the sub system
+ * @param key entry key
* @param value entry value BLOB
* @param size size of 'value'
* @param lifetime relative time after which the entry is (possibly) deleted
@@ -565,52 +174,54 @@
*/
struct GNUNET_PEERSTORE_StoreContext *
GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
+ const char *sub_system,
const struct GNUNET_PeerIdentity *peer,
- const char *sub_system,
+ const char *key,
const void *value,
size_t size,
struct GNUNET_TIME_Relative lifetime,
GNUNET_PEERSTORE_Continuation cont,
void *cont_cls)
{
- struct GNUNET_PEERSTORE_RequestContext *rc;
- struct StoreRequestMessage *entry;
struct GNUNET_PEERSTORE_StoreContext *sc;
- char *ss;
- void *val;
- size_t sub_system_size;
+ struct StoreRequestMessage *srm;
+ size_t ss_size;
+ size_t key_size;
size_t request_size;
+ void *dummy;
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) + 1;
- 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);
+ "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
+ size, sub_system, GNUNET_i2s (peer), key);
sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
sc->cont = cont;
sc->cont_cls = cont_cls;
- sc->h = h;
- sc->rc = rc;
- sc->request_transmitted = GNUNET_NO;
- rc->cont = &store_trigger_receive;
- 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);
+ ss_size = strlen(sub_system) + 1;
+ key_size = strlen(key) + 1;
+ request_size = sizeof(struct StoreRequestMessage) +
+ ss_size +
+ key_size +
+ size;
+ srm = GNUNET_malloc(request_size);
+ srm->header.size = htons(request_size);
+ srm->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
+ srm->key_size = htons(key_size);
+ srm->lifetime = lifetime;
+ srm->peer = *peer;
+ srm->sub_system_size = htons(ss_size);
+ srm->value_size = htons(size);
+ dummy = &srm[1];
+ memcpy(dummy, sub_system, ss_size);
+ dummy += ss_size;
+ memcpy(dummy, key, key_size);
+ dummy += key_size;
+ memcpy(dummy, value, size);
+ GNUNET_CLIENT_transmit_and_get_response(h->client,
+ (const struct GNUNET_MessageHeader *)srm,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES,
+ &store_response_receiver,
+ sc);
return sc;
}
Modified: gnunet/src/peerstore/plugin_peerstore_sqlite.c
===================================================================
--- gnunet/src/peerstore/plugin_peerstore_sqlite.c 2014-05-13 21:54:23 UTC
(rev 33269)
+++ gnunet/src/peerstore/plugin_peerstore_sqlite.c 2014-05-13 22:08:22 UTC
(rev 33270)
@@ -213,9 +213,10 @@
* @return #GNUNET_OK on success, else #GNUNET_SYSERR
*/
static int
-peerstore_sqlite_store_record (void *cls,
+peerstore_sqlite_store_record(void *cls,
+ const char *sub_system,
const struct GNUNET_PeerIdentity *peer,
- const char *sub_system,
+ const char *key,
const void *value,
size_t size)
{
@@ -224,9 +225,10 @@
//FIXME: check if value exists with the same key first
- if(SQLITE_OK != sqlite3_bind_blob(stmt, 1, peer, sizeof(struct
GNUNET_PeerIdentity), SQLITE_STATIC)
- || SQLITE_OK != sqlite3_bind_text(stmt, 2, sub_system,
strlen(sub_system) + 1, SQLITE_STATIC)
- || SQLITE_OK != sqlite3_bind_blob(stmt, 3, value, size, SQLITE_STATIC))
+ if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) +
1, SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct
GNUNET_PeerIdentity), SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1,
SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_blob(stmt, 4, value, size, SQLITE_STATIC))
LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"sqlite3_bind");
else if (SQLITE_DONE != sqlite3_step (stmt))
@@ -347,16 +349,17 @@
/* Create tables */
sql_exec (plugin->dbh,
- "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
- " peer_id BLOB NOT NULL,\n"
- " sub_system TEXT NOT NULL,\n"
- " value BLOB NULL"
- ");");
+ "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
+ " sub_system TEXT NOT NULL,\n"
+ " peer_id BLOB NOT NULL,\n"
+ " key TEXT NOT NULL,\n"
+ " value BLOB NULL"
+ ");");
/* Prepare statements */
sql_prepare (plugin->dbh,
- "INSERT INTO peerstoredata (peer_id, sub_system, value) VALUES (?,?,?);",
+ "INSERT INTO peerstoredata (sub_system, peer_id, key, value) VALUES
(?,?,?,?);",
&plugin->insert_peerstoredata);
sql_prepare(plugin->dbh,
"SELECT peer_id, sub_system, value FROM peerstoredata",
Modified: gnunet/src/peerstore/test_peerstore_api.c
===================================================================
--- gnunet/src/peerstore/test_peerstore_api.c 2014-05-13 21:54:23 UTC (rev
33269)
+++ gnunet/src/peerstore/test_peerstore_api.c 2014-05-13 22:08:22 UTC (rev
33270)
@@ -30,8 +30,12 @@
struct GNUNET_PEERSTORE_Handle *h;
-void store_cont(void *cls, const char *emsg)
+void store_cont(void *cls, int success)
{
+ if(GNUNET_OK == success)
+ ok = 0;
+ else
+ ok = 1;
GNUNET_PEERSTORE_disconnect(h);
}
@@ -49,8 +53,9 @@
h = GNUNET_PEERSTORE_connect(cfg);
GNUNET_assert(NULL != h);
GNUNET_PEERSTORE_store(h,
+ "peerstore-test",
&pid,
- "peerstore-test",
+ "peerstore-test-key",
val,
val_size,
GNUNET_TIME_UNIT_FOREVER_REL,
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r33270 - in gnunet/src: include peerstore,
gnunet <=