From: Omar Tarabai Date: Mon, 4 Aug 2014 11:06:52 +0000 (+0000) Subject: peerstore: indentation X-Git-Tag: initial-import-from-subversion-38251~3344 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b;p=oweals%2Fgnunet.git peerstore: indentation --- diff --git a/src/peerstore/gnunet-peerstore.c b/src/peerstore/gnunet-peerstore.c index f99c42819..bdd045d09 100644 --- a/src/peerstore/gnunet-peerstore.c +++ b/src/peerstore/gnunet-peerstore.c @@ -41,16 +41,16 @@ struct GNUNET_PEERSTORE_Handle *peerstore_handle; * @param tc scheduler context */ static void -shutdown_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - if(NULL != peerstore_handle) + if (NULL != peerstore_handle) { - GNUNET_PEERSTORE_disconnect(peerstore_handle, GNUNET_YES); + GNUNET_PEERSTORE_disconnect (peerstore_handle, GNUNET_YES); peerstore_handle = NULL; } } + /** * Main function that will be run by the scheduler. * @@ -60,22 +60,20 @@ shutdown_task (void *cls, * @param cfg configuration */ static void -run (void *cls, - char *const *args, - const char *cfgfile, +run (void *cls, char *const *args, const char *cfgfile, 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); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, + NULL); + peerstore_handle = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != peerstore_handle); ret = 0; } + /** * The main function to peerstore. * @@ -90,12 +88,9 @@ main (int argc, char *const *argv) GNUNET_GETOPT_OPTION_END }; return (GNUNET_OK == - GNUNET_PROGRAM_run (argc, - argv, - "gnunet-peerstore [options [value]]", - gettext_noop - ("peerstore"), - options, &run, NULL)) ? ret : 1; + GNUNET_PROGRAM_run (argc, argv, "gnunet-peerstore [options [value]]", + gettext_noop ("peerstore"), options, &run, + NULL)) ? ret : 1; } /* end of gnunet-peerstore.c */ diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c index c93d2cc95..c674d3c11 100644 --- a/src/peerstore/gnunet-service-peerstore.c +++ b/src/peerstore/gnunet-service-peerstore.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) + (C) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -53,7 +53,7 @@ struct ClientEntry /** * Interval for expired records cleanup (in seconds) */ -#define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */ +#define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */ /** * Our configuration. @@ -98,27 +98,29 @@ int in_shutdown; /** * Perform the actual shutdown operations */ -void do_shutdown () +void +do_shutdown () { - if(NULL != db_lib_name) + if (NULL != db_lib_name) { GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db)); GNUNET_free (db_lib_name); db_lib_name = NULL; } - if(NULL != nc) + if (NULL != nc) { - GNUNET_SERVER_notification_context_destroy(nc); + GNUNET_SERVER_notification_context_destroy (nc); nc = NULL; } - if(NULL != watchers) + if (NULL != watchers) { - GNUNET_CONTAINER_multihashmap_destroy(watchers); + GNUNET_CONTAINER_multihashmap_destroy (watchers); watchers = NULL; } - GNUNET_SCHEDULER_shutdown(); + GNUNET_SCHEDULER_shutdown (); } + /** * Task run during shutdown. * @@ -126,34 +128,36 @@ void do_shutdown () * @param tc unused */ static void -shutdown_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { in_shutdown = GNUNET_YES; - if (NULL == client_head) /* Only when no connected clients. */ + if (NULL == client_head) /* Only when no connected clients. */ do_shutdown (); } + /** * Deletes any expired records from storage */ static void -cleanup_expired_records(void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +cleanup_expired_records (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { int deleted; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) return; - GNUNET_assert(NULL != db); - deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get()); + GNUNET_assert (NULL != db); + deleted = db->expire_records (db->cls, GNUNET_TIME_absolute_get ()); if (deleted > 0) - GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted); - GNUNET_SCHEDULER_add_delayed( - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, EXPIRED_RECORDS_CLEANUP_INTERVAL), - &cleanup_expired_records, NULL); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, + EXPIRED_RECORDS_CLEANUP_INTERVAL), + &cleanup_expired_records, NULL); } + /** * Search for a disconnected client and remove it * @@ -162,15 +166,15 @@ cleanup_expired_records(void *cls, * @param value the watcher client, a 'struct GNUNET_SERVER_Client *' * @return #GNUNET_OK to continue iterating */ -static int client_disconnect_it(void *cls, - const struct GNUNET_HashCode *key, - void *value) +static int +client_disconnect_it (void *cls, const struct GNUNET_HashCode *key, void *value) { - if(cls == value) - GNUNET_CONTAINER_multihashmap_remove(watchers, key, value); + if (cls == value) + GNUNET_CONTAINER_multihashmap_remove (watchers, key, value); return GNUNET_OK; } + /** * A client disconnected. Remove all of its data structure entries. * @@ -178,16 +182,14 @@ static int client_disconnect_it(void *cls, * @param client identification of the client */ static void -handle_client_disconnect (void *cls, - struct GNUNET_SERVER_Client - * client) +handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) { struct ClientEntry *ce; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n"); - if(NULL != watchers) - GNUNET_CONTAINER_multihashmap_iterate(watchers, - &client_disconnect_it, client); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n"); + if (NULL != watchers) + GNUNET_CONTAINER_multihashmap_iterate (watchers, &client_disconnect_it, + client); ce = client_head; while (ce != NULL) { @@ -203,6 +205,7 @@ handle_client_disconnect (void *cls, do_shutdown (); } + /** * Function called by for each matching record. * @@ -212,28 +215,24 @@ handle_client_disconnect (void *cls, * @return #GNUNET_YES to continue iteration */ static int -record_iterator (void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +record_iterator (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { struct GNUNET_SERVER_Client *client = cls; struct StoreRecordMessage *srm; srm = - PEERSTORE_create_record_message(record->sub_system, - record->peer, - record->key, - record->value, - record->value_size, - record->expiry, - GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD); + PEERSTORE_create_record_message (record->sub_system, record->peer, + record->key, record->value, + record->value_size, record->expiry, + GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD); GNUNET_SERVER_notification_context_unicast (nc, client, - (struct GNUNET_MessageHeader *)srm, - GNUNET_NO); - GNUNET_free(srm); + (struct GNUNET_MessageHeader *) + srm, GNUNET_NO); + GNUNET_free (srm); return GNUNET_YES; } + /** * Iterator over all watcher clients * to notify them of a new record @@ -243,44 +242,43 @@ record_iterator (void *cls, * @param value the watcher client, a 'struct GNUNET_SERVER_Client *' * @return #GNUNET_YES to continue iterating */ -static int watch_notifier_it(void *cls, - const struct GNUNET_HashCode *key, - void *value) +static int +watch_notifier_it (void *cls, const struct GNUNET_HashCode *key, void *value) { struct GNUNET_PEERSTORE_Record *record = cls; struct GNUNET_SERVER_Client *client = value; struct StoreRecordMessage *srm; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n"); - srm = PEERSTORE_create_record_message(record->sub_system, - record->peer, - record->key, - record->value, - record->value_size, - record->expiry, - GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD); - GNUNET_SERVER_notification_context_unicast(nc, client, - (const struct GNUNET_MessageHeader *)srm, GNUNET_NO); - GNUNET_free(srm); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n"); + srm = + PEERSTORE_create_record_message (record->sub_system, record->peer, + record->key, record->value, + record->value_size, record->expiry, + GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD); + GNUNET_SERVER_notification_context_unicast (nc, client, + (const struct GNUNET_MessageHeader + *) srm, GNUNET_NO); + GNUNET_free (srm); return GNUNET_YES; } + /** * Given a new record, notifies watchers * * @param record changed record to update watchers with */ -static void watch_notifier (struct GNUNET_PEERSTORE_Record *record) +static void +watch_notifier (struct GNUNET_PEERSTORE_Record *record) { struct GNUNET_HashCode keyhash; - PEERSTORE_hash_key(record->sub_system, - record->peer, - record->key, - &keyhash); - GNUNET_CONTAINER_multihashmap_get_multiple(watchers, &keyhash, &watch_notifier_it, record); + PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash); + GNUNET_CONTAINER_multihashmap_get_multiple (watchers, &keyhash, + &watch_notifier_it, record); } + /** * Handle a watch cancel request from client * @@ -288,18 +286,19 @@ static void watch_notifier (struct GNUNET_PEERSTORE_Record *record) * @param client identification of the client * @param message the actual message */ -static void handle_watch_cancel (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_watch_cancel (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct StoreKeyHashMessage *hm; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n"); hm = (struct StoreKeyHashMessage *) message; - GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client); - GNUNET_SERVER_receive_done(client, GNUNET_OK); + GNUNET_CONTAINER_multihashmap_remove (watchers, &hm->keyhash, client); + GNUNET_SERVER_receive_done (client, GNUNET_OK); } + /** * Handle a watch request from client * @@ -307,21 +306,22 @@ static void handle_watch_cancel (void *cls, * @param client identification of the client * @param message the actual message */ -static void handle_watch (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_watch (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct StoreKeyHashMessage *hm; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n"); hm = (struct StoreKeyHashMessage *) message; - GNUNET_SERVER_client_mark_monitor(client); - GNUNET_SERVER_notification_context_add(nc, client); - GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash, - client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); - GNUNET_SERVER_receive_done(client, GNUNET_OK); + GNUNET_SERVER_client_mark_monitor (client); + GNUNET_SERVER_notification_context_add (nc, client); + GNUNET_CONTAINER_multihashmap_put (watchers, &hm->keyhash, client, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + GNUNET_SERVER_receive_done (client, GNUNET_OK); } + /** * Handle an iterate request from client * @@ -329,55 +329,54 @@ static void handle_watch (void *cls, * @param client identification of the client * @param message the actual message */ -static void handle_iterate (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_iterate (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_PEERSTORE_Record *record; struct GNUNET_MessageHeader *endmsg; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request.\n"); - record = PEERSTORE_parse_record_message(message); - if(NULL == record) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request.\n"); + record = PEERSTORE_parse_record_message (message); + if (NULL == record) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request.\n")); - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request.\n")); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - if(NULL == record->sub_system) + if (NULL == record->sub_system) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("Sub system not supplied in client iterate request.\n")); - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Sub system not supplied in client iterate request.\n")); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Iterate request: ss `%s', peer `%s', key `%s'\n", - record->sub_system, - (NULL == record->peer) ? "NULL" : GNUNET_i2s(record->peer), - (NULL == record->key) ? "NULL" : record->key); - GNUNET_SERVER_notification_context_add(nc, client); - if(GNUNET_OK == db->iterate_records(db->cls, - record->sub_system, - record->peer, - record->key, - &record_iterator, - client)) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Iterate request: ss `%s', peer `%s', key `%s'\n", + record->sub_system, + (NULL == record->peer) ? "NULL" : GNUNET_i2s (record->peer), + (NULL == record->key) ? "NULL" : record->key); + GNUNET_SERVER_notification_context_add (nc, client); + if (GNUNET_OK == + db->iterate_records (db->cls, record->sub_system, record->peer, + record->key, &record_iterator, client)) { - 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); - GNUNET_free(endmsg); - GNUNET_SERVER_receive_done(client, GNUNET_OK); + 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); + GNUNET_free (endmsg); + GNUNET_SERVER_receive_done (client, GNUNET_OK); } else { - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); } - PEERSTORE_destroy_record(record); + PEERSTORE_destroy_record (record); } + /** * Handle a store request from client * @@ -385,54 +384,51 @@ static void handle_iterate (void *cls, * @param client identification of the client * @param message the actual message */ -static void handle_store (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_store (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_PEERSTORE_Record *record; struct StoreRecordMessage *srm; - record = PEERSTORE_parse_record_message(message); - if(NULL == record) + record = PEERSTORE_parse_record_message (message); + if (NULL == record) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed store request from client\n")); - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Malformed store request from client\n")); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - srm = (struct StoreRecordMessage *)message; - if(NULL == record->sub_system - || NULL == record->peer - || NULL == record->key) + srm = (struct StoreRecordMessage *) message; + if (NULL == record->sub_system || NULL == record->peer || NULL == record->key) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Full key not supplied in client store request\n")); - PEERSTORE_destroy_record(record); - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Full key not supplied in client store request\n")); + PEERSTORE_destroy_record (record); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n", - record->value_size, - record->sub_system, - GNUNET_i2s (record->peer), - record->key); - if(GNUNET_OK != db->store_record(db->cls, - record->sub_system, - record->peer, - record->key, - record->value, - record->value_size, - *record->expiry, - srm->options)) + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n", + record->value_size, record->sub_system, GNUNET_i2s (record->peer), + record->key); + if (GNUNET_OK != + db->store_record (db->cls, record->sub_system, record->peer, record->key, + record->value, record->value_size, *record->expiry, + srm->options)) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to store requested value, sqlite database error.")); - PEERSTORE_destroy_record(record); - GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to store requested value, sqlite database error.")); + PEERSTORE_destroy_record (record); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - GNUNET_SERVER_receive_done(client, GNUNET_OK); - watch_notifier(record); - PEERSTORE_destroy_record(record); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + watch_notifier (record); + PEERSTORE_destroy_record (record); } + /** * Creates an entry for a new client or returns it if it already exists. * @@ -462,6 +458,7 @@ make_client_entry (struct GNUNET_SERVER_Client *client) return ce; } + /** * Callback on a new client connection * @@ -475,6 +472,7 @@ handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client) make_client_entry (client); } + /** * Peerstore service runner. * @@ -483,47 +481,48 @@ handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client) * @param c configuration to use */ static void -run (void *cls, - struct GNUNET_SERVER_Handle *server, +run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *c) { static const struct GNUNET_SERVER_MessageHandler handlers[] = { - {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, - {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, - {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)}, - {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)}, - {NULL, NULL, 0, 0} + {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, + {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, + {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, + sizeof (struct StoreKeyHashMessage)}, + {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, + sizeof (struct StoreKeyHashMessage)}, + {NULL, NULL, 0, 0} }; char *database; in_shutdown = GNUNET_NO; cfg = c; if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE", - &database)) + GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE", + &database)) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No database backend configured\n")); else { GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database); - db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg); - GNUNET_free(database); + db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg); + GNUNET_free (database); } - if(NULL == db) + if (NULL == db) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Could not load database backend `%s'\n"), db_lib_name); - GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); - return; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not load database backend `%s'\n"), db_lib_name); + GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); + return; } nc = GNUNET_SERVER_notification_context_create (server, 16); - watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); - GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL); + watchers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); + GNUNET_SCHEDULER_add_now (&cleanup_expired_records, NULL); GNUNET_SERVER_add_handlers (server, handlers); GNUNET_SERVER_connect_notify (server, &handle_client_connect, NULL); GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, - &shutdown_task, - NULL); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, + NULL); } @@ -538,11 +537,9 @@ int main (int argc, char *const *argv) { return (GNUNET_OK == - GNUNET_SERVICE_run (argc, - argv, - "peerstore", - GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, - &run, NULL)) ? 0 : 1; + GNUNET_SERVICE_run (argc, argv, "peerstore", + GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, + NULL)) ? 0 : 1; } /* end of gnunet-service-peerstore.c */ diff --git a/src/peerstore/peerstore.h b/src/peerstore/peerstore.h index 5757f784e..94970d906 100644 --- a/src/peerstore/peerstore.h +++ b/src/peerstore/peerstore.h @@ -30,11 +30,10 @@ GNUNET_NETWORK_STRUCT_BEGIN - /** * Message carrying a PEERSTORE record message */ -struct StoreRecordMessage + struct StoreRecordMessage { /** @@ -102,5 +101,4 @@ struct StoreKeyHashMessage }; GNUNET_NETWORK_STRUCT_END - #endif diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index 3c67846e6..bebf9678e 100644 --- a/src/peerstore/peerstore_api.c +++ b/src/peerstore/peerstore_api.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) + (C) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -256,30 +256,34 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h); * * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' */ -static void watch_request_sent (void *cls); +static void +watch_request_sent (void *cls); /** * Callback after MQ envelope is sent * * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' */ -static void iterate_request_sent (void *cls); +static void +iterate_request_sent (void *cls); /** * Callback after MQ envelope is sent * * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' */ -static void store_request_sent (void *cls); +static 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 + {&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 }; /******************************************************************************/ @@ -291,32 +295,34 @@ 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); + 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 */ -static int rewatch_it(void *cls, - const struct GNUNET_HashCode *key, - void *value) +static 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); + 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); + GNUNET_MQ_notify_sent (wc->ev, &watch_request_sent, wc); + GNUNET_MQ_send (h->mq, wc->ev); return GNUNET_YES; } + /** * Close the existing connection to PEERSTORE and reconnect. * @@ -330,10 +336,10 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h) void *icb_cls; struct GNUNET_PEERSTORE_StoreContext *sc; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); if (NULL != h->mq) { - GNUNET_MQ_destroy(h->mq); + GNUNET_MQ_destroy (h->mq); h->mq = NULL; } if (NULL != h->client) @@ -342,17 +348,15 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h) 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"); + 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); + GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h); } ic = h->iterate_head; while (NULL != ic) @@ -361,26 +365,27 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h) { 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.")); + 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); + 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); + GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc); + GNUNET_MQ_send (h->mq, sc->ev); sc = sc->next; } } + /** * Iterator over watch requests to cancel them. * @@ -398,6 +403,7 @@ destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value) return GNUNET_YES; } + /** * Kill the connection to the service. This can be delayed in case of pending * STORE requests and the user explicitly asked to sync first. Otherwise it is @@ -405,11 +411,12 @@ destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value) * * @param h Handle to the service. */ -static void do_disconnect (struct GNUNET_PEERSTORE_Handle *h) +static void +do_disconnect (struct GNUNET_PEERSTORE_Handle *h) { - if(NULL != h->mq) + if (NULL != h->mq) { - GNUNET_MQ_destroy(h->mq); + GNUNET_MQ_destroy (h->mq); h->mq = NULL; } if (NULL != h->client) @@ -417,9 +424,10 @@ static void do_disconnect (struct GNUNET_PEERSTORE_Handle *h) GNUNET_CLIENT_disconnect (h->client); h->client = NULL; } - GNUNET_free(h); + GNUNET_free (h); } + /** * Connect to the PEERSTORE service. * @@ -431,27 +439,28 @@ GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) struct GNUNET_PEERSTORE_Handle *h; h = GNUNET_new (struct GNUNET_PEERSTORE_Handle); + h->client = GNUNET_CLIENT_connect ("peerstore", cfg); - if(NULL == h->client) + if (NULL == h->client) { - GNUNET_free(h); + GNUNET_free (h); return NULL; } h->cfg = cfg; h->disconnecting = GNUNET_NO; - h->mq = GNUNET_MQ_queue_for_connection_client(h->client, - mq_handlers, - &handle_client_error, - h); - if(NULL == h->mq) + h->mq = + GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers, + &handle_client_error, h); + if (NULL == h->mq) { - GNUNET_free(h); + GNUNET_free (h); return NULL; } - LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); return h; } + /** * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests * will be canceled. @@ -468,11 +477,11 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) struct GNUNET_PEERSTORE_StoreContext *sc; struct GNUNET_PEERSTORE_StoreContext *sc_iter; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n"); - if(NULL != h->watches) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n"); + if (NULL != h->watches) { GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL); - GNUNET_CONTAINER_multihashmap_destroy(h->watches); + GNUNET_CONTAINER_multihashmap_destroy (h->watches); h->watches = NULL; } ic_iter = h->iterate_head; @@ -487,7 +496,7 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) if (GNUNET_YES == sync_first) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "Delaying disconnection due to pending store requests.\n"); + "Delaying disconnection due to pending store requests.\n"); h->disconnecting = GNUNET_YES; return; } @@ -512,7 +521,8 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) * * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' */ -static void store_request_sent (void *cls) +static void +store_request_sent (void *cls) { struct GNUNET_PEERSTORE_StoreContext *sc = cls; GNUNET_PEERSTORE_Continuation cont; @@ -521,11 +531,12 @@ static void store_request_sent (void *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); + GNUNET_PEERSTORE_store_cancel (sc); + if (NULL != cont) + cont (cont_cls, GNUNET_OK); } + /** * Cancel a store request * @@ -536,17 +547,18 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) { struct GNUNET_PEERSTORE_Handle *h = sc->h; - if(NULL != sc->ev) + if (NULL != sc->ev) { - GNUNET_MQ_send_cancel(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); + GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc); + GNUNET_free (sc); if (GNUNET_YES == h->disconnecting && NULL == h->store_head) do_disconnect (h); } + /** * Store a new entry in the PEERSTORE. * Note that stored entries can be lost in some cases @@ -566,41 +578,35 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) 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, + 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) + 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); + "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); + 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 *********************/ /******************************************************************************/ @@ -623,62 +629,67 @@ handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) int continue_iter; ic = h->iterate_head; - if(NULL == ic) + if (NULL == ic) { - LOG(GNUNET_ERROR_TYPE_ERROR, _("Unexpected iteration response, this should not happen.\n")); - reconnect(h); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Unexpected iteration response, this should not happen.\n")); + reconnect (h); return; } callback = ic->callback; callback_cls = ic->callback_cls; - if(NULL == msg) /* Connection error */ + if (NULL == msg) /* Connection error */ { - if(NULL != callback) - callback(callback_cls, NULL, - _("Error communicating with `PEERSTORE' service.")); - reconnect(h); + if (NULL != callback) + callback (callback_cls, NULL, + _("Error communicating with `PEERSTORE' service.")); + reconnect (h); return; } - msg_type = ntohs(msg->type); - if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) + msg_type = ntohs (msg->type); + if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) { ic->request_sent = GNUNET_NO; - GNUNET_PEERSTORE_iterate_cancel(ic); - if(NULL != callback) - callback(callback_cls, NULL, NULL); + GNUNET_PEERSTORE_iterate_cancel (ic); + if (NULL != callback) + callback (callback_cls, NULL, NULL); return; } - if(NULL != callback) + if (NULL != callback) { - record = PEERSTORE_parse_record_message(msg); - if(NULL == record) - continue_iter = callback(callback_cls, NULL, _("Received a malformed response from service.")); + 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); + continue_iter = callback (callback_cls, record, NULL); + PEERSTORE_destroy_record (record); } - if(GNUNET_NO == continue_iter) + if (GNUNET_NO == continue_iter) ic->callback = NULL; } - } + /** * Callback after MQ envelope is sent * * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' */ -static void iterate_request_sent (void *cls) +static void +iterate_request_sent (void *cls) { struct GNUNET_PEERSTORE_IterateContext *ic = cls; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n"); + 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 * @@ -691,9 +702,10 @@ 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); + GNUNET_PEERSTORE_iterate_cancel (ic); } + /** * Cancel an iterate request * Please do not call after the iterate request is done @@ -703,25 +715,26 @@ iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) void GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) { - if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task) + if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task) { - GNUNET_SCHEDULER_cancel(ic->timeout_task); + GNUNET_SCHEDULER_cancel (ic->timeout_task); ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; } - if(GNUNET_NO == ic->request_sent) + if (GNUNET_NO == ic->request_sent) { - if(NULL != ic->ev) + if (NULL != ic->ev) { - GNUNET_MQ_send_cancel(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); + GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic); + GNUNET_free (ic); } else ic->callback = NULL; } + /** * Iterate over records matching supplied key information * @@ -736,38 +749,36 @@ GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) */ 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) + 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_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); + 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); + 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; } + /******************************************************************************/ /******************* WATCH FUNCTIONS *********************/ /******************************************************************************/ @@ -786,36 +797,38 @@ handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg) struct GNUNET_HashCode keyhash; struct GNUNET_PEERSTORE_WatchContext *wc; - if(NULL == msg) + if (NULL == msg) { - LOG(GNUNET_ERROR_TYPE_ERROR, - _("Problem receiving a watch response, no way to determine which request.\n")); - reconnect(h); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Problem receiving a watch response, no way to determine which request.\n")); + reconnect (h); return; } - 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) + 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) { - LOG(GNUNET_ERROR_TYPE_ERROR, - _("Received a watch result for a non existing watch.\n")); - reconnect(h); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Received a watch result for a non existing watch.\n")); + reconnect (h); return; } - if(NULL != wc->callback) - wc->callback(wc->callback_cls, record, NULL); - PEERSTORE_destroy_record(record); + if (NULL != wc->callback) + wc->callback (wc->callback_cls, record, NULL); + PEERSTORE_destroy_record (record); } + /** * Callback after MQ envelope is sent * * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' */ -static void watch_request_sent (void *cls) +static void +watch_request_sent (void *cls) { struct GNUNET_PEERSTORE_WatchContext *wc = cls; @@ -823,37 +836,39 @@ static void watch_request_sent (void *cls) wc->ev = NULL; } + /** * Cancel a watch request * * @param wc handle to the watch request */ void -GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc) +GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc) { struct GNUNET_PEERSTORE_Handle *h = wc->h; struct GNUNET_MQ_Envelope *ev; struct StoreKeyHashMessage *hm; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n"); - if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n"); + if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */ { - ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL); + ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL); hm->keyhash = wc->keyhash; - GNUNET_MQ_send(h->mq, ev); + GNUNET_MQ_send (h->mq, ev); wc->callback = NULL; wc->callback_cls = NULL; } - if(NULL != wc->ev) + if (NULL != wc->ev) { - GNUNET_MQ_send_cancel(wc->ev); + GNUNET_MQ_send_cancel (wc->ev); wc->ev = NULL; } - GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc); - GNUNET_free(wc); + GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc); + GNUNET_free (wc); } + /** * Request watching a given key * User will be notified with any new values added to key @@ -868,36 +883,36 @@ GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc) */ struct GNUNET_PEERSTORE_WatchContext * GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, - const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - GNUNET_PEERSTORE_Processor callback, void *callback_cls) + const char *sub_system, + const struct GNUNET_PeerIdentity *peer, const char *key, + GNUNET_PEERSTORE_Processor callback, void *callback_cls) { struct GNUNET_MQ_Envelope *ev; struct StoreKeyHashMessage *hm; struct GNUNET_PEERSTORE_WatchContext *wc; - GNUNET_assert(NULL != sub_system); - GNUNET_assert(NULL != peer); - GNUNET_assert(NULL != key); - 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); + GNUNET_assert (NULL != sub_system); + GNUNET_assert (NULL != peer); + GNUNET_assert (NULL != key); + 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); + 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; } diff --git a/src/peerstore/peerstore_common.c b/src/peerstore/peerstore_common.c index 0cb801cc4..1f86fe442 100644 --- a/src/peerstore/peerstore_common.c +++ b/src/peerstore/peerstore_common.c @@ -30,10 +30,9 @@ * */ void -PEERSTORE_hash_key(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - struct GNUNET_HashCode *ret) +PEERSTORE_hash_key (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, const char *key, + struct GNUNET_HashCode *ret) { size_t sssize; size_t psize; @@ -42,21 +41,22 @@ PEERSTORE_hash_key(const char *sub_system, void *block; void *blockptr; - sssize = strlen(sub_system) + 1; - psize = sizeof(struct GNUNET_PeerIdentity); - ksize = strlen(key) + 1; + sssize = strlen (sub_system) + 1; + psize = sizeof (struct GNUNET_PeerIdentity); + ksize = strlen (key) + 1; totalsize = sssize + psize + ksize; - block = GNUNET_malloc(totalsize); + block = GNUNET_malloc (totalsize); blockptr = block; - memcpy(blockptr, sub_system, sssize); + memcpy (blockptr, sub_system, sssize); blockptr += sssize; - memcpy(blockptr, peer, psize); + memcpy (blockptr, peer, psize); blockptr += psize; - memcpy(blockptr, key, ksize); - GNUNET_CRYPTO_hash(block, totalsize, ret); - GNUNET_free(block); + memcpy (blockptr, key, ksize); + GNUNET_CRYPTO_hash (block, totalsize, ret); + GNUNET_free (block); } + /** * Creates a record message ready to be sent * @@ -70,13 +70,12 @@ PEERSTORE_hash_key(const char *sub_system, * @return pointer to record message struct */ struct StoreRecordMessage * -PEERSTORE_create_record_message(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - const void *value, - size_t value_size, - struct GNUNET_TIME_Absolute *expiry, - uint16_t msg_type) +PEERSTORE_create_record_message (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, const void *value, + size_t value_size, + struct GNUNET_TIME_Absolute *expiry, + uint16_t msg_type) { struct StoreRecordMessage *srm; size_t ss_size; @@ -84,40 +83,38 @@ PEERSTORE_create_record_message(const char *sub_system, size_t request_size; void *dummy; - ss_size = strlen(sub_system) + 1; - if(NULL == key) + ss_size = strlen (sub_system) + 1; + if (NULL == key) key_size = 0; else - key_size = strlen(key) + 1; - request_size = sizeof(struct StoreRecordMessage) + - ss_size + - key_size + - value_size; - srm = GNUNET_malloc(request_size); - srm->header.size = htons(request_size); - srm->header.type = htons(msg_type); - srm->key_size = htons(key_size); - if(NULL != expiry) + key_size = strlen (key) + 1; + request_size = + sizeof (struct StoreRecordMessage) + ss_size + key_size + value_size; + srm = GNUNET_malloc (request_size); + srm->header.size = htons (request_size); + srm->header.type = htons (msg_type); + srm->key_size = htons (key_size); + if (NULL != expiry) srm->expiry = *expiry; - if(NULL == peer) - srm->peer_set = htons(GNUNET_NO); + if (NULL == peer) + srm->peer_set = htons (GNUNET_NO); else { - srm->peer_set = htons(GNUNET_YES); + srm->peer_set = htons (GNUNET_YES); srm->peer = *peer; } - srm->sub_system_size = htons(ss_size); - srm->value_size = htons(value_size); + srm->sub_system_size = htons (ss_size); + srm->value_size = htons (value_size); dummy = &srm[1]; - memcpy(dummy, sub_system, ss_size); + memcpy (dummy, sub_system, ss_size); dummy += ss_size; - memcpy(dummy, key, key_size); + memcpy (dummy, key, key_size); dummy += key_size; - memcpy(dummy, value, value_size); + memcpy (dummy, value, value_size); return srm; - } + /** * Creates a MQ envelope for a single record * @@ -132,14 +129,13 @@ PEERSTORE_create_record_message(const char *sub_system, * @return pointer to record message struct */ struct GNUNET_MQ_Envelope * -PEERSTORE_create_record_mq_envelope(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - const void *value, - size_t value_size, - struct GNUNET_TIME_Absolute *expiry, - enum GNUNET_PEERSTORE_StoreOption options, - uint16_t msg_type) +PEERSTORE_create_record_mq_envelope (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, const void *value, + size_t value_size, + struct GNUNET_TIME_Absolute *expiry, + enum GNUNET_PEERSTORE_StoreOption options, + uint16_t msg_type) { struct StoreRecordMessage *srm; struct GNUNET_MQ_Envelope *ev; @@ -148,39 +144,37 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, size_t msg_size; void *dummy; - GNUNET_assert(NULL != sub_system); - ss_size = strlen(sub_system) + 1; - if(NULL == key) + GNUNET_assert (NULL != sub_system); + ss_size = strlen (sub_system) + 1; + if (NULL == key) key_size = 0; else - key_size = strlen(key) + 1; - msg_size = ss_size + - key_size + - value_size; - ev = GNUNET_MQ_msg_extra(srm, msg_size, msg_type); - srm->key_size = htons(key_size); - if(NULL != expiry) + key_size = strlen (key) + 1; + msg_size = ss_size + key_size + value_size; + ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type); + srm->key_size = htons (key_size); + if (NULL != expiry) srm->expiry = *expiry; - if(NULL == peer) - srm->peer_set = htons(GNUNET_NO); + if (NULL == peer) + srm->peer_set = htons (GNUNET_NO); else { - srm->peer_set = htons(GNUNET_YES); + srm->peer_set = htons (GNUNET_YES); srm->peer = *peer; } - srm->sub_system_size = htons(ss_size); - srm->value_size = htons(value_size); + srm->sub_system_size = htons (ss_size); + srm->value_size = htons (value_size); srm->options = options; dummy = &srm[1]; - memcpy(dummy, sub_system, ss_size); + memcpy (dummy, sub_system, ss_size); dummy += ss_size; - memcpy(dummy, key, key_size); + memcpy (dummy, key, key_size); dummy += key_size; - memcpy(dummy, value, value_size); - + memcpy (dummy, value, value_size); return ev; } + /** * Parses a message carrying a record * @@ -188,7 +182,7 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, * @return Pointer to record or NULL if error */ struct GNUNET_PEERSTORE_Record * -PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message) +PEERSTORE_parse_record_message (const struct GNUNET_MessageHeader *message) { struct StoreRecordMessage *srm; struct GNUNET_PEERSTORE_Record *record; @@ -198,64 +192,67 @@ PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message) uint16_t value_size; char *dummy; - req_size = ntohs(message->size); - if(req_size < sizeof(struct StoreRecordMessage)) + req_size = ntohs (message->size); + if (req_size < sizeof (struct StoreRecordMessage)) return NULL; - srm = (struct StoreRecordMessage *)message; - ss_size = ntohs(srm->sub_system_size); - key_size = ntohs(srm->key_size); - value_size = ntohs(srm->value_size); - if(ss_size + key_size + value_size + sizeof(struct StoreRecordMessage) - != req_size) + srm = (struct StoreRecordMessage *) message; + ss_size = ntohs (srm->sub_system_size); + key_size = ntohs (srm->key_size); + value_size = ntohs (srm->value_size); + if (ss_size + key_size + value_size + sizeof (struct StoreRecordMessage) != + req_size) return NULL; - record = GNUNET_new(struct GNUNET_PEERSTORE_Record); - if(GNUNET_YES == ntohs(srm->peer_set)) + record = GNUNET_new (struct GNUNET_PEERSTORE_Record); + if (GNUNET_YES == ntohs (srm->peer_set)) { - record->peer = GNUNET_new(struct GNUNET_PeerIdentity); - memcpy(record->peer, &srm->peer, sizeof(struct GNUNET_PeerIdentity)); + record->peer = GNUNET_new (struct GNUNET_PeerIdentity); + + memcpy (record->peer, &srm->peer, sizeof (struct GNUNET_PeerIdentity)); } - record->expiry = GNUNET_new(struct GNUNET_TIME_Absolute); + record->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); + *(record->expiry) = srm->expiry; - dummy = (char *)&srm[1]; - if(ss_size > 0) + dummy = (char *) &srm[1]; + if (ss_size > 0) { - record->sub_system = GNUNET_strdup(dummy); + record->sub_system = GNUNET_strdup (dummy); dummy += ss_size; } - if(key_size > 0) + if (key_size > 0) { - record->key = GNUNET_strdup(dummy); + record->key = GNUNET_strdup (dummy); dummy += key_size; } - if(value_size > 0) + if (value_size > 0) { - record->value = GNUNET_malloc(value_size); - memcpy(record->value, dummy, value_size); + record->value = GNUNET_malloc (value_size); + memcpy (record->value, dummy, value_size); } record->value_size = value_size; - return record; } + /** * Free any memory allocated for this record * * @param record */ -void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record) +void +PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record) { - if(NULL != record->sub_system) - GNUNET_free(record->sub_system); - if(NULL != record->peer) - GNUNET_free(record->peer); - if(NULL != record->key) - GNUNET_free(record->key); - if(NULL != record->value) + if (NULL != record->sub_system) + GNUNET_free (record->sub_system); + if (NULL != record->peer) + GNUNET_free (record->peer); + if (NULL != record->key) + GNUNET_free (record->key); + if (NULL != record->value) { - GNUNET_free(record->value); + GNUNET_free (record->value); record->value = 0; } - if(NULL != record->expiry) - GNUNET_free(record->expiry); - GNUNET_free(record); + if (NULL != record->expiry) + GNUNET_free (record->expiry); + GNUNET_free (record); } diff --git a/src/peerstore/peerstore_common.h b/src/peerstore/peerstore_common.h index 50ff4a2f9..fb1318146 100644 --- a/src/peerstore/peerstore_common.h +++ b/src/peerstore/peerstore_common.h @@ -31,10 +31,9 @@ * */ void -PEERSTORE_hash_key(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - struct GNUNET_HashCode *ret); +PEERSTORE_hash_key (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, const char *key, + struct GNUNET_HashCode *ret); /** * Creates a record message ready to be sent @@ -49,13 +48,12 @@ PEERSTORE_hash_key(const char *sub_system, * @return pointer to record message struct */ struct StoreRecordMessage * -PEERSTORE_create_record_message(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - const void *value, - size_t value_size, - struct GNUNET_TIME_Absolute *expiry, - uint16_t msg_type); +PEERSTORE_create_record_message (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, const void *value, + size_t value_size, + struct GNUNET_TIME_Absolute *expiry, + uint16_t msg_type); /** * Creates a MQ envelope for a single record @@ -71,14 +69,13 @@ PEERSTORE_create_record_message(const char *sub_system, * @return pointer to record message struct */ struct GNUNET_MQ_Envelope * -PEERSTORE_create_record_mq_envelope(const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - const void *value, - size_t value_size, - struct GNUNET_TIME_Absolute *expiry, - enum GNUNET_PEERSTORE_StoreOption options, - uint16_t msg_type); +PEERSTORE_create_record_mq_envelope (const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, const void *value, + size_t value_size, + struct GNUNET_TIME_Absolute *expiry, + enum GNUNET_PEERSTORE_StoreOption options, + uint16_t msg_type); /** * Parses a message carrying a record @@ -87,11 +84,12 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, * @return Pointer to record or NULL if error */ struct GNUNET_PEERSTORE_Record * -PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message); +PEERSTORE_parse_record_message (const struct GNUNET_MessageHeader *message); /** * Free any memory allocated for this record * * @param record */ -void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record); +void +PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record); diff --git a/src/peerstore/perf_peerstore_store.c b/src/peerstore/perf_peerstore_store.c index bdf70272c..e97699fe2 100644 --- a/src/peerstore/perf_peerstore_store.c +++ b/src/peerstore/perf_peerstore_store.c @@ -40,78 +40,68 @@ static char *v = "test_peerstore_stress_val"; int count = 0; void -disconnect() +disconnect () { - if(NULL != h) - GNUNET_PEERSTORE_disconnect(h, GNUNET_YES); - GNUNET_SCHEDULER_shutdown(); + if (NULL != h) + GNUNET_PEERSTORE_disconnect (h, GNUNET_YES); + GNUNET_SCHEDULER_shutdown (); } + void -store() +store () { - GNUNET_PEERSTORE_store(h, - ss, - &p, - k, - v, - strlen(v) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - (count == 0) ? GNUNET_PEERSTORE_STOREOPTION_REPLACE : GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, - NULL, - NULL); + GNUNET_PEERSTORE_store (h, ss, &p, k, v, strlen (v) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + (count == + 0) ? GNUNET_PEERSTORE_STOREOPTION_REPLACE : + GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL); count++; } + static int -watch_cb(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - GNUNET_assert(NULL == emsg); - if(STORES == count) + GNUNET_assert (NULL == emsg); + if (STORES == count) { ok = 0; - disconnect(); + disconnect (); } else - store(); + store (); return GNUNET_YES; } + static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { memset (&p, 5, sizeof (p)); - h = GNUNET_PEERSTORE_connect(cfg); - GNUNET_assert(NULL != h); - GNUNET_PEERSTORE_watch(h, - ss, - &p, - k, - &watch_cb, - NULL); - store(); + h = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != h); + GNUNET_PEERSTORE_watch (h, ss, &p, k, &watch_cb, NULL); + store (); } + int main (int argc, char *argv[]) { struct GNUNET_TIME_Absolute start; struct GNUNET_TIME_Relative diff; - start = GNUNET_TIME_absolute_get(); - if (0 != GNUNET_TESTING_service_run ("perf-peerstore-store", - "peerstore", - "test_peerstore_api_data.conf", - &run, NULL)) + start = GNUNET_TIME_absolute_get (); + if (0 != + GNUNET_TESTING_service_run ("perf-peerstore-store", "peerstore", + "test_peerstore_api_data.conf", &run, NULL)) return 1; - diff = GNUNET_TIME_absolute_get_duration(start); - fprintf(stderr, "Stored and retrieved %d records in %s (%s).\n", - STORES, GNUNET_STRINGS_relative_time_to_string(diff, GNUNET_YES), - GNUNET_STRINGS_relative_time_to_string(diff, GNUNET_NO)); + diff = GNUNET_TIME_absolute_get_duration (start); + fprintf (stderr, "Stored and retrieved %d records in %s (%s).\n", STORES, + GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_YES), + GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_NO)); return ok; } diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index 2726eef3d..588fae73c 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c @@ -121,19 +121,24 @@ struct Plugin * @return number of deleted records */ static int -peerstore_sqlite_delete_records(void *cls, - const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key) +peerstore_sqlite_delete_records (void *cls, const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key) { struct Plugin *plugin = cls; sqlite3_stmt *stmt = plugin->delete_peerstoredata; - 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))) + 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))) { - LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind"); + LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "sqlite3_bind"); } else if (SQLITE_DONE != sqlite3_step (stmt)) { @@ -146,9 +151,10 @@ peerstore_sqlite_delete_records(void *cls, "sqlite3_reset"); return 0; } - return sqlite3_changes(plugin->dbh); + return sqlite3_changes (plugin->dbh); } + /** * Delete expired records (expiry < now) * @@ -157,15 +163,16 @@ peerstore_sqlite_delete_records(void *cls, * @return number of records deleted */ static int -peerstore_sqlite_expire_records(void *cls, - struct GNUNET_TIME_Absolute now) +peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now) { struct Plugin *plugin = cls; sqlite3_stmt *stmt = plugin->expire_peerstoredata; - if(SQLITE_OK != sqlite3_bind_int64(stmt, 1, (sqlite3_uint64)now.abs_value_us)) + if (SQLITE_OK != + sqlite3_bind_int64 (stmt, 1, (sqlite3_uint64) now.abs_value_us)) { - LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind"); + LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "sqlite3_bind"); } else if (SQLITE_DONE != sqlite3_step (stmt)) { @@ -178,10 +185,10 @@ peerstore_sqlite_expire_records(void *cls, "sqlite3_reset"); return 0; } - return sqlite3_changes(plugin->dbh); - + return sqlite3_changes (plugin->dbh); } + /** * Iterate over the records given an optional peer id * and/or key. @@ -195,11 +202,11 @@ peerstore_sqlite_expire_records(void *cls, * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ static int -peerstore_sqlite_iterate_records (void *cls, - const char *sub_system, - const struct GNUNET_PeerIdentity *peer, - const char *key, - GNUNET_PEERSTORE_Processor iter, void *iter_cls) +peerstore_sqlite_iterate_records (void *cls, const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, + GNUNET_PEERSTORE_Processor iter, + void *iter_cls) { struct Plugin *plugin = cls; sqlite3_stmt *stmt; @@ -207,59 +214,76 @@ peerstore_sqlite_iterate_records (void *cls, int sret; struct GNUNET_PEERSTORE_Record *ret; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n"); - if(NULL == peer && NULL == key) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n"); + if (NULL == peer && NULL == key) { stmt = plugin->select_peerstoredata; - err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)); + err = + (SQLITE_OK != + sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, + SQLITE_STATIC)); } - else if(NULL == key) + else if (NULL == key) { stmt = plugin->select_peerstoredata_by_pid; - err = (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)); + err = + (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)); } - else if(NULL == peer) + else if (NULL == peer) { stmt = plugin->select_peerstoredata_by_key; - err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) - || (SQLITE_OK != sqlite3_bind_text(stmt, 2, key, strlen(key) + 1, SQLITE_STATIC)); + err = + (SQLITE_OK != + sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, + SQLITE_STATIC)) || + (SQLITE_OK != + sqlite3_bind_text (stmt, 2, key, strlen (key) + 1, SQLITE_STATIC)); } else { stmt = plugin->select_peerstoredata_by_all; - err = (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)); + err = + (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)); } if (err) { LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "sqlite3_bind_XXXX"); + "sqlite3_bind_XXXX"); if (SQLITE_OK != sqlite3_reset (stmt)) - LOG_SQLITE (plugin, - GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "sqlite3_reset"); + LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "sqlite3_reset"); return GNUNET_SYSERR; } while (SQLITE_ROW == (sret = sqlite3_step (stmt))) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n"); - ret = GNUNET_new(struct GNUNET_PEERSTORE_Record); - ret->sub_system = (char *)sqlite3_column_text(stmt, 0); - ret->peer = (struct GNUNET_PeerIdentity *)sqlite3_column_blob(stmt, 1); - ret->key = (char *)sqlite3_column_text(stmt, 2); - ret->value = (void *)sqlite3_column_blob(stmt, 3); - ret->value_size = sqlite3_column_bytes(stmt, 3); - ret->expiry = GNUNET_new(struct GNUNET_TIME_Absolute); - ret->expiry->abs_value_us = (uint64_t)sqlite3_column_int64(stmt, 4); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n"); + ret = GNUNET_new (struct GNUNET_PEERSTORE_Record); + + ret->sub_system = (char *) sqlite3_column_text (stmt, 0); + ret->peer = (struct GNUNET_PeerIdentity *) sqlite3_column_blob (stmt, 1); + ret->key = (char *) sqlite3_column_text (stmt, 2); + ret->value = (void *) sqlite3_column_blob (stmt, 3); + ret->value_size = sqlite3_column_bytes (stmt, 3); + ret->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); + + ret->expiry->abs_value_us = (uint64_t) sqlite3_column_int64 (stmt, 4); if (NULL != iter) - iter (iter_cls, - ret, - NULL); - GNUNET_free(ret->expiry); - GNUNET_free(ret); + iter (iter_cls, ret, NULL); + GNUNET_free (ret->expiry); + GNUNET_free (ret); } if (SQLITE_DONE != sret) { @@ -268,16 +292,16 @@ peerstore_sqlite_iterate_records (void *cls, } if (SQLITE_OK != sqlite3_reset (stmt)) { - LOG_SQLITE (plugin, - GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "sqlite3_reset"); + LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + "sqlite3_reset"); err = 1; } - if(err) + if (err) return GNUNET_SYSERR; return GNUNET_OK; } + /** * Store a record in the peerstore. * Key is the combination of sub system and peer identity. @@ -294,29 +318,32 @@ peerstore_sqlite_iterate_records (void *cls, * @return #GNUNET_OK on success, else #GNUNET_SYSERR */ static int -peerstore_sqlite_store_record (void *cls, - const char *sub_system, +peerstore_sqlite_store_record (void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, - const char *key, - const void *value, - size_t size, + const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options) { struct Plugin *plugin = cls; sqlite3_stmt *stmt = plugin->insert_peerstoredata; - if(GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) + if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) { - peerstore_sqlite_delete_records(cls, sub_system, peer, key); + peerstore_sqlite_delete_records (cls, sub_system, peer, key); } - 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) - || SQLITE_OK != sqlite3_bind_int64(stmt, 5, (sqlite3_uint64)expiry.abs_value_us)) + 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) || + SQLITE_OK != sqlite3_bind_int64 (stmt, 5, + (sqlite3_uint64) expiry.abs_value_us)) LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "sqlite3_bind"); + "sqlite3_bind"); else if (SQLITE_DONE != sqlite3_step (stmt)) { LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, @@ -328,7 +355,6 @@ peerstore_sqlite_store_record (void *cls, "sqlite3_reset"); return GNUNET_SYSERR; } - return GNUNET_OK; } @@ -341,20 +367,19 @@ peerstore_sqlite_store_record (void *cls, * @return 0 on success */ static int -sql_exec (sqlite3 *dbh, const char *sql) +sql_exec (sqlite3 * dbh, const char *sql) { int result; result = sqlite3_exec (dbh, sql, NULL, NULL, NULL); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Executed `%s' / %d\n", sql, result); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Executed `%s' / %d\n", sql, result); if (result != SQLITE_OK) - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Error executing SQL query: %s\n %s\n"), - sqlite3_errmsg (dbh), sql); + LOG (GNUNET_ERROR_TYPE_ERROR, _("Error executing SQL query: %s\n %s\n"), + sqlite3_errmsg (dbh), sql); return result; } + /** * @brief Prepare a SQL statement * @@ -364,37 +389,37 @@ sql_exec (sqlite3 *dbh, const char *sql) * @return 0 on success */ static int -sql_prepare (sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt) +sql_prepare (sqlite3 * dbh, const char *sql, sqlite3_stmt ** stmt) { char *tail; int result; - result = sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt, - (const char **) &tail); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Prepared `%s' / %p: %d\n", sql, *stmt, result); + result = + sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt, (const char **) &tail); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Prepared `%s' / %p: %d\n", sql, *stmt, result); if (result != SQLITE_OK) - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Error preparing SQL query: %s\n %s\n"), - sqlite3_errmsg (dbh), sql); + LOG (GNUNET_ERROR_TYPE_ERROR, _("Error preparing SQL query: %s\n %s\n"), + sqlite3_errmsg (dbh), sql); return result; } + /** * sqlite3 custom function for comparison of uint64_t values * since it is not supported by default */ -void sqlite3_lessthan(sqlite3_context* ctx, int dummy, - sqlite3_value** values) +void +sqlite3_lessthan (sqlite3_context * ctx, int dummy, sqlite3_value ** values) { uint64_t v1; uint64_t v2; - v1 = (uint64_t)sqlite3_value_int64(values[0]); - v2 = (uint64_t)sqlite3_value_int64(values[1]); - sqlite3_result_int(ctx, v1 < v2); + v1 = (uint64_t) sqlite3_value_int64 (values[0]); + v2 = (uint64_t) sqlite3_value_int64 (values[1]); + sqlite3_result_int (ctx, v1 < v2); } + /** * Initialize the database connections and associated * data structures (create tables and indices @@ -412,8 +437,8 @@ database_setup (struct Plugin *plugin) GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-sqlite", "FILENAME", &filename)) { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "peerstore-sqlite", "FILENAME"); + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "peerstore-sqlite", + "FILENAME"); return GNUNET_SYSERR; } if (GNUNET_OK != GNUNET_DISK_file_test (filename)) @@ -427,16 +452,13 @@ database_setup (struct Plugin *plugin) } /* filename should be UTF-8-encoded. If it isn't, it's a bug */ plugin->fn = filename; - /* Open database and precompile statements */ if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh)) { - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Unable to initialize SQLite: %s.\n"), - sqlite3_errmsg (plugin->dbh)); + LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to initialize SQLite: %s.\n"), + sqlite3_errmsg (plugin->dbh)); return GNUNET_SYSERR; } - sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY"); sql_exec (plugin->dbh, "PRAGMA synchronous=OFF"); sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF"); @@ -444,73 +466,54 @@ database_setup (struct Plugin *plugin) sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\""); sql_exec (plugin->dbh, "PRAGMA count_changes=OFF"); sql_exec (plugin->dbh, "PRAGMA page_size=4096"); - sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS); - /* Create tables */ - sql_exec (plugin->dbh, - "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,\n" - " expiry sqlite3_uint64 NOT NULL" - ");"); - - sqlite3_create_function(plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, &sqlite3_lessthan, NULL, 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,\n" + " expiry sqlite3_uint64 NOT NULL" ");"); + sqlite3_create_function (plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, + &sqlite3_lessthan, NULL, NULL); /* Create Indices */ if (SQLITE_OK != - sqlite3_exec(plugin->dbh, - "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", - NULL, NULL, NULL)) + sqlite3_exec (plugin->dbh, + "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", + NULL, NULL, NULL)) { - LOG (GNUNET_ERROR_TYPE_ERROR, - _("Unable to create indices: %s.\n"), - sqlite3_errmsg (plugin->dbh)); - return GNUNET_SYSERR; + LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to create indices: %s.\n"), + sqlite3_errmsg (plugin->dbh)); + return GNUNET_SYSERR; } - /* Prepare statements */ sql_prepare (plugin->dbh, - "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);", - &plugin->insert_peerstoredata); - sql_prepare(plugin->dbh, - "SELECT * FROM peerstoredata" - " WHERE sub_system = ?", - &plugin->select_peerstoredata); - sql_prepare(plugin->dbh, - "SELECT * FROM peerstoredata" - " WHERE sub_system = ?" - " AND peer_id = ?", - &plugin->select_peerstoredata_by_pid); - sql_prepare(plugin->dbh, - "SELECT * FROM peerstoredata" - " WHERE sub_system = ?" - " AND key = ?", - &plugin->select_peerstoredata_by_key); - sql_prepare(plugin->dbh, - "SELECT * FROM peerstoredata" - " WHERE sub_system = ?" - " AND peer_id = ?" - " AND key = ?", - &plugin->select_peerstoredata_by_all); - sql_prepare(plugin->dbh, - "DELETE FROM peerstoredata" - " WHERE UINT64_LT(expiry, ?)", - &plugin->expire_peerstoredata); - sql_prepare(plugin->dbh, - "DELETE FROM peerstoredata" - " WHERE sub_system = ?" - " AND peer_id = ?" - " AND key = ?", - &plugin->delete_peerstoredata); - + "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);", + &plugin->insert_peerstoredata); + sql_prepare (plugin->dbh, + "SELECT * FROM peerstoredata" " WHERE sub_system = ?", + &plugin->select_peerstoredata); + sql_prepare (plugin->dbh, + "SELECT * FROM peerstoredata" " WHERE sub_system = ?" + " AND peer_id = ?", &plugin->select_peerstoredata_by_pid); + sql_prepare (plugin->dbh, + "SELECT * FROM peerstoredata" " WHERE sub_system = ?" + " AND key = ?", &plugin->select_peerstoredata_by_key); + sql_prepare (plugin->dbh, + "SELECT * FROM peerstoredata" " WHERE sub_system = ?" + " AND peer_id = ?" " AND key = ?", + &plugin->select_peerstoredata_by_all); + sql_prepare (plugin->dbh, + "DELETE FROM peerstoredata" " WHERE UINT64_LT(expiry, ?)", + &plugin->expire_peerstoredata); + sql_prepare (plugin->dbh, + "DELETE FROM peerstoredata" " WHERE sub_system = ?" + " AND peer_id = ?" " AND key = ?", + &plugin->delete_peerstoredata); return GNUNET_OK; } + /** * Shutdown database connection and associate data * structures. @@ -521,19 +524,20 @@ database_shutdown (struct Plugin *plugin) { int result; sqlite3_stmt *stmt; + while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL))) { result = sqlite3_finalize (stmt); if (SQLITE_OK != result) - LOG (GNUNET_ERROR_TYPE_WARNING, - "Failed to close statement %p: %d\n", stmt, result); + LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to close statement %p: %d\n", + stmt, result); } if (SQLITE_OK != sqlite3_close (plugin->dbh)) LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); - GNUNET_free_non_null (plugin->fn); } + /** * Entry point for the plugin. * @@ -561,10 +565,11 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls) api->store_record = &peerstore_sqlite_store_record; api->iterate_records = &peerstore_sqlite_iterate_records; api->expire_records = &peerstore_sqlite_expire_records; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); return api; } + /** * Exit point from the plugin. * @@ -582,7 +587,6 @@ libgnunet_plugin_peerstore_sqlite_done (void *cls) GNUNET_free (api); LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n"); return NULL; - } /* end of plugin_peerstore_sqlite.c */ diff --git a/src/peerstore/test_peerstore_api_iterate.c b/src/peerstore/test_peerstore_api_iterate.c index 0b864aea5..626a020de 100644 --- a/src/peerstore/test_peerstore_api_iterate.c +++ b/src/peerstore/test_peerstore_api_iterate.c @@ -40,127 +40,87 @@ static char *val = "test_peerstore_api_iterate_val"; static int count = 0; static int -iter3_cb(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +iter3_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { count++; return GNUNET_YES; } - GNUNET_assert(count == 3); + GNUNET_assert (count == 3); ok = 0; - GNUNET_PEERSTORE_disconnect(h, GNUNET_NO); - GNUNET_SCHEDULER_shutdown(); + GNUNET_PEERSTORE_disconnect (h, GNUNET_NO); + GNUNET_SCHEDULER_shutdown (); return GNUNET_YES; } + static int -iter2_cb(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +iter2_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { count++; return GNUNET_YES; } - GNUNET_assert(count == 2); + GNUNET_assert (count == 2); count = 0; - GNUNET_PEERSTORE_iterate(h, - ss, - NULL, - NULL, - GNUNET_TIME_UNIT_FOREVER_REL, - iter3_cb, - NULL); + GNUNET_PEERSTORE_iterate (h, ss, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL, + iter3_cb, NULL); return GNUNET_YES; } + static int -iter1_cb(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +iter1_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { count++; return GNUNET_YES; } - GNUNET_assert(count == 1); + GNUNET_assert (count == 1); count = 0; - GNUNET_PEERSTORE_iterate(h, - ss, - &p1, - NULL, - GNUNET_TIME_UNIT_FOREVER_REL, - iter2_cb, - NULL); + GNUNET_PEERSTORE_iterate (h, ss, &p1, NULL, GNUNET_TIME_UNIT_FOREVER_REL, + iter2_cb, NULL); return GNUNET_YES; } + static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { - h = GNUNET_PEERSTORE_connect(cfg); - GNUNET_assert(NULL != h); + h = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != h); memset (&p1, 1, sizeof (p1)); memset (&p2, 2, sizeof (p2)); - GNUNET_PEERSTORE_store(h, - ss, - &p1, - k1, - val, - strlen(val) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - NULL, - NULL); - GNUNET_PEERSTORE_store(h, - ss, - &p1, - k2, - val, - strlen(val) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - NULL, - NULL); - GNUNET_PEERSTORE_store(h, - ss, - &p2, - k3, - val, - strlen(val) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - NULL, - NULL); - GNUNET_PEERSTORE_iterate(h, - ss, - &p1, - k1, - GNUNET_TIME_UNIT_FOREVER_REL, - iter1_cb, - NULL); + GNUNET_PEERSTORE_store (h, ss, &p1, k1, val, strlen (val) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); + GNUNET_PEERSTORE_store (h, ss, &p1, k2, val, strlen (val) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); + GNUNET_PEERSTORE_store (h, ss, &p2, k3, val, strlen (val) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); + GNUNET_PEERSTORE_iterate (h, ss, &p1, k1, GNUNET_TIME_UNIT_FOREVER_REL, + iter1_cb, NULL); } + int main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore", - "peerstore", - "test_peerstore_api_data.conf", - &run, NULL)) + if (0 != + GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", + "test_peerstore_api_data.conf", &run, NULL)) return 1; return ok; } diff --git a/src/peerstore/test_peerstore_api_store.c b/src/peerstore/test_peerstore_api_store.c index 53ace3d4a..06ad34756 100644 --- a/src/peerstore/test_peerstore_api_store.c +++ b/src/peerstore/test_peerstore_api_store.c @@ -39,182 +39,155 @@ static char *val3 = "test_peerstore_api_store_val3--"; int count = 0; -int test3_cont2(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +int +test3_cont2 (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { - GNUNET_assert((strlen(val3) + 1) == record->value_size); - GNUNET_assert(0 == strcmp((char *)val3, (char *)record->value)); + GNUNET_assert ((strlen (val3) + 1) == record->value_size); + GNUNET_assert (0 == strcmp ((char *) val3, (char *) record->value)); count++; return GNUNET_YES; } - GNUNET_assert(count == 1); + GNUNET_assert (count == 1); ok = 0; - GNUNET_PEERSTORE_disconnect(h, GNUNET_YES); - GNUNET_SCHEDULER_shutdown(); + GNUNET_PEERSTORE_disconnect (h, GNUNET_YES); + GNUNET_SCHEDULER_shutdown (); return GNUNET_YES; } + static void -test3_cont(void *cls, int success) +test3_cont (void *cls, int success) { - if(GNUNET_YES != success) + if (GNUNET_YES != success) return; count = 0; - GNUNET_PEERSTORE_iterate(h, - subsystem, - &pid, - key, - GNUNET_TIME_UNIT_SECONDS, - &test3_cont2, - NULL); + GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS, + &test3_cont2, NULL); } + /** * Replace the previous 2 records */ static void -test3() +test3 () { - GNUNET_PEERSTORE_store(h, - subsystem, - &pid, - key, - val3, - strlen(val3) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - &test3_cont, - NULL); + GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val3, strlen (val3) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test3_cont, + NULL); } -int test2_cont2(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) + +int +test2_cont2 (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { - GNUNET_assert(((strlen(val1) + 1) == record->value_size) - || ((strlen(val2) + 1) == record->value_size)); - GNUNET_assert((0 == strcmp((char *)val1, (char *)record->value)) - || (0 == strcmp((char *)val2, (char *)record->value))); + GNUNET_assert (((strlen (val1) + 1) == record->value_size) || + ((strlen (val2) + 1) == record->value_size)); + GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) || + (0 == strcmp ((char *) val2, (char *) record->value))); count++; return GNUNET_YES; } - GNUNET_assert(count == 2); + GNUNET_assert (count == 2); count = 0; - test3(); + test3 (); return GNUNET_YES; } + static void -test2_cont(void *cls, int success) +test2_cont (void *cls, int success) { - if(GNUNET_YES != success) - return; + if (GNUNET_YES != success) + return; count = 0; - GNUNET_PEERSTORE_iterate(h, - subsystem, - &pid, - key, - GNUNET_TIME_UNIT_SECONDS, - &test2_cont2, - NULL); + GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS, + &test2_cont2, NULL); } + /** * Test storing a second value with the same key */ -void test2() +void +test2 () { - GNUNET_PEERSTORE_store(h, - subsystem, - &pid, - key, - val2, - strlen(val2) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, - &test2_cont, - NULL); + GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val2, strlen (val2) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, &test2_cont, + NULL); } -int test1_cont2(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) + +int +test1_cont2 (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - if(NULL != emsg) + if (NULL != emsg) return GNUNET_NO; - if(NULL != record) + if (NULL != record) { - GNUNET_assert((strlen(val1) + 1) == record->value_size); - GNUNET_assert(0 == strcmp((char *)val1, (char *)record->value)); + GNUNET_assert ((strlen (val1) + 1) == record->value_size); + GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value)); count++; return GNUNET_YES; } - GNUNET_assert(count == 1); + GNUNET_assert (count == 1); count = 0; - test2(); + test2 (); return GNUNET_YES; } + static void -test1_cont(void *cls, int success) +test1_cont (void *cls, int success) { - if(GNUNET_YES != success) + if (GNUNET_YES != success) return; count = 0; - GNUNET_PEERSTORE_iterate(h, - subsystem, - &pid, - key, - GNUNET_TIME_UNIT_SECONDS, - &test1_cont2, - NULL); + GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS, + &test1_cont2, NULL); } + /** * Store a single record */ static void -test1() +test1 () { - GNUNET_PEERSTORE_store(h, - subsystem, - &pid, - key, - val1, - strlen(val1) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - &test1_cont, - NULL); + GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val1, strlen (val1) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test1_cont, + NULL); } + static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { - h = GNUNET_PEERSTORE_connect(cfg); - GNUNET_assert(NULL != h); + h = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != h); memset (&pid, 1, sizeof (pid)); - test1(); + test1 (); } + int main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore", - "peerstore", - "test_peerstore_api_data.conf", - &run, NULL)) + if (0 != + GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", + "test_peerstore_api_data.conf", &run, NULL)) return 1; return ok; } diff --git a/src/peerstore/test_peerstore_api_sync.c b/src/peerstore/test_peerstore_api_sync.c index 634f62c26..3803d72c1 100644 --- a/src/peerstore/test_peerstore_api_sync.c +++ b/src/peerstore/test_peerstore_api_sync.c @@ -37,7 +37,8 @@ static struct GNUNET_PeerIdentity pid; static char *key = "test_peerstore_api_store_key"; static char *val = "test_peerstore_api_store_val"; -int iterate_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) +int +iterate_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { const char *rec_val; @@ -45,46 +46,47 @@ int iterate_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) if (NULL == record) { GNUNET_PEERSTORE_disconnect (h, GNUNET_NO); - GNUNET_SCHEDULER_shutdown(); + GNUNET_SCHEDULER_shutdown (); return GNUNET_YES; } rec_val = record->value; - GNUNET_break (0 == strcmp(rec_val, val)); + GNUNET_break (0 == strcmp (rec_val, val)); ok = 0; return GNUNET_YES; } + static void -test1() +test1 () { - GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val, strlen(val) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_PEERSTORE_STOREOPTION_REPLACE, - NULL, NULL); + GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val, strlen (val) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); GNUNET_PEERSTORE_disconnect (h, GNUNET_YES); h = GNUNET_PEERSTORE_connect (cfg); GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, - GNUNET_TIME_UNIT_FOREVER_REL, &iterate_cb, NULL); + GNUNET_TIME_UNIT_FOREVER_REL, &iterate_cb, NULL); } + static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *c, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, + struct GNUNET_TESTING_Peer *peer) { cfg = c; - h = GNUNET_PEERSTORE_connect(cfg); - GNUNET_assert(NULL != h); + h = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != h); memset (&pid, 1, sizeof (pid)); - test1(); + test1 (); } + int main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore", - "peerstore", - "test_peerstore_api_data.conf", - &run, NULL)) + if (0 != + GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", + "test_peerstore_api_data.conf", &run, NULL)) return 1; return ok; } diff --git a/src/peerstore/test_peerstore_api_watch.c b/src/peerstore/test_peerstore_api_watch.c index fb6cdc540..3820f68f6 100644 --- a/src/peerstore/test_peerstore_api_watch.c +++ b/src/peerstore/test_peerstore_api_watch.c @@ -36,51 +36,37 @@ static char *k = "test_peerstore_api_watch_key"; static char *val = "test_peerstore_api_watch_val"; static int -watch_cb(void *cls, - struct GNUNET_PEERSTORE_Record *record, - char *emsg) +watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) { - GNUNET_assert(NULL == emsg); - GNUNET_assert(0 == strcmp(val, (char *)record->value)); + GNUNET_assert (NULL == emsg); + GNUNET_assert (0 == strcmp (val, (char *) record->value)); ok = 0; - GNUNET_PEERSTORE_disconnect(h, GNUNET_NO); - GNUNET_SCHEDULER_shutdown(); + GNUNET_PEERSTORE_disconnect (h, GNUNET_NO); + GNUNET_SCHEDULER_shutdown (); return GNUNET_YES; } + static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { - h = GNUNET_PEERSTORE_connect(cfg); - GNUNET_assert(NULL != h); + h = GNUNET_PEERSTORE_connect (cfg); + GNUNET_assert (NULL != h); memset (&p, 4, sizeof (p)); - GNUNET_PEERSTORE_watch(h, - ss, - &p, - k, - &watch_cb, - NULL); - GNUNET_PEERSTORE_store(h, - ss, - &p, - k, - val, - strlen(val) + 1, - GNUNET_TIME_UNIT_FOREVER_ABS, - GNUNET_PEERSTORE_STOREOPTION_REPLACE, - NULL, - NULL); + GNUNET_PEERSTORE_watch (h, ss, &p, k, &watch_cb, NULL); + GNUNET_PEERSTORE_store (h, ss, &p, k, val, strlen (val) + 1, + GNUNET_TIME_UNIT_FOREVER_ABS, + GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); } + int main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore", - "peerstore", - "test_peerstore_api_data.conf", - &run, NULL)) + if (0 != + GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", + "test_peerstore_api_data.conf", &run, NULL)) return 1; return ok; }