gnunet

Main GNUnet Logic
Log | Files | Refs | Submodules | README | LICENSE

commit d6dd23d50411a11226a8dcd88e51aa0a332e499d
parent 603d264417198385513b09844ddf4557dcc44952
Author: Omar Tarabai <tarabai@devegypt.com>
Date:   Wed,  7 May 2014 18:11:18 +0000

PEERSTORE api update


Diffstat:
Msrc/peerstore/gnunet-peerstore.c | 43+++++++++++++++++++++++++++++++++++++++++++
Msrc/peerstore/gnunet-service-peerstore.c | 42+++++++++++++++++++++++++++++++++++++++++-
Msrc/peerstore/peerstore.h | 9+++++++--
Msrc/peerstore/peerstore_api.c | 13++++++++++---
4 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/src/peerstore/gnunet-peerstore.c b/src/peerstore/gnunet-peerstore.c @@ -30,6 +30,16 @@ static int ret; /** + * option '-t' + */ +static int test; + +/* + * Handle to PEERSTORE service + */ +struct GNUNET_PEERSTORE_Handle *peerstore_handle; + +/** * Run on shutdown * * @param cls unused @@ -39,6 +49,20 @@ static void shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { + if(NULL != peerstore_handle) + { + GNUNET_PEERSTORE_disconnect(peerstore_handle); + peerstore_handle = NULL; + } +} + +void test_cont(void *cls, const char *emsg) +{ + printf("Received a response\n"); + if(NULL != emsg) + { + printf("Response: %s\n", emsg); + } } /** @@ -56,9 +80,25 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) { + peerstore_handle = NULL; GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL); + peerstore_handle = GNUNET_PEERSTORE_connect(cfg); + GNUNET_assert(NULL != peerstore_handle); + if(GNUNET_YES == test) + { + struct GNUNET_PeerIdentity pid; + memset (&pid, 32, sizeof (pid)); + GNUNET_PEERSTORE_store(peerstore_handle, + &pid, + "subsub", + "value", + 5, + GNUNET_TIME_UNIT_FOREVER_REL, + &test_cont, + NULL); + } ret = 0; } @@ -74,6 +114,9 @@ int main (int argc, char *const *argv) { static const struct GNUNET_GETOPT_CommandLineOption options[] = { + {'t', "test", NULL, + gettext_noop("TESTING"), + 0, &GNUNET_GETOPT_set_one, &test}, GNUNET_GETOPT_OPTION_END }; return (GNUNET_OK == diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c @@ -75,6 +75,45 @@ handle_client_disconnect (void *cls, } /** + * Handle a store request from client + * + * @param cls unused + * @param client identification of the client + * @param message the actual message + */ +void handle_store (void *cls, + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct StoreRequestMessage *sreqm; + struct GNUNET_SERVER_TransmitContext *tc; + struct StoreResponseMessage *sresm; + uint16_t msg_size; + char *sub_system; + + msg_size = ntohs(message->size); + GNUNET_break_op(msg_size > sizeof(struct GNUNET_MessageHeader) + sizeof(struct StoreRequestMessage)); + sreqm = (struct StoreRequestMessage *)&message[1]; + sub_system = (char *)&sreqm[1]; + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s' and peer `%s'\n", + msg_size, + sub_system, + GNUNET_i2s (&sreqm->peer)); + //TODO: do the actual storage + //create a fake response for testing + char *response = "This is a response"; + tc = GNUNET_SERVER_transmit_context_create (client); + sresm = malloc(sizeof(struct StoreResponseMessage) + strlen(response)); + sresm->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT); + sresm->header.size = htons(sizeof(struct StoreResponseMessage) + strlen(response)); + sresm->success = htons(GNUNET_NO); + sresm->emsg_size = htons(strlen(response)); + memcpy(&sresm[1], response, strlen(response)); + GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)sresm); + GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); +} + +/** * Peerstore service runner. * * @param cls closure @@ -87,7 +126,8 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c) { static const struct GNUNET_SERVER_MessageHandler handlers[] = { - {NULL, NULL, 0, 0} + {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, + {NULL, NULL, 0, 0} }; char *database; diff --git a/src/peerstore/peerstore.h b/src/peerstore/peerstore.h @@ -35,7 +35,7 @@ struct StoreRequestMessage { /** - * Type will be GNUNET_MESSAGE_TYPE_PEERINFO_GET + * GNUnet message header */ struct GNUNET_MessageHeader header; @@ -69,9 +69,14 @@ struct StoreRequestMessage struct StoreResponseMessage { /** + * GNUnet message header + */ + struct GNUNET_MessageHeader header; + + /** * Was the store operation successful (#GNUNET_YES / #GNUNET_NO) */ - int success; + uint16_t success; /** * Size of the error message (0 if no error) diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c @@ -375,6 +375,8 @@ peerstore_handler (void *cls, const struct GNUNET_MessageHeader *msg) uint16_t response_type; uint16_t response_size; char *emsg; + GNUNET_PEERSTORE_Continuation cont; + void *cont_cls; h->in_receive = GNUNET_NO; if(NULL == msg) @@ -384,6 +386,7 @@ peerstore_handler (void *cls, const struct GNUNET_MessageHeader *msg) } 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: @@ -394,6 +397,8 @@ peerstore_handler (void *cls, const struct GNUNET_MessageHeader *msg) 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) @@ -404,16 +409,18 @@ peerstore_handler (void *cls, const struct GNUNET_MessageHeader *msg) h, GNUNET_TIME_UNIT_FOREVER_REL); } - if(NULL != sc->cont) + 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)); } - sc->cont(sc->cont_cls, emsg); + LOG(GNUNET_ERROR_TYPE_DEBUG, "Calling user callback without a message\n"); + cont(cont_cls, emsg); } break; } @@ -514,7 +521,7 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, size_t request_size; LOG (GNUNET_ERROR_TYPE_DEBUG, - "Storing value (size: %lu) for subsytem `%s' and peer `%s'", + "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;