* @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.
*
* @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.
*
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 */
/*
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
/**
* Interval for expired records cleanup (in seconds)
*/
-#define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
+#define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
/**
* Our configuration.
/**
* 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.
*
* @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
*
* @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.
*
* @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)
{
do_shutdown ();
}
+
/**
* Function called by for each matching record.
*
* @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
* @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
*
* @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
*
* @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
*
* @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
*
* @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.
*
return ce;
}
+
/**
* Callback on a new client connection
*
make_client_entry (client);
}
+
/**
* Peerstore service runner.
*
* @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);
}
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 */
GNUNET_NETWORK_STRUCT_BEGIN
-
/**
* Message carrying a PEERSTORE record message
*/
-struct StoreRecordMessage
+ struct StoreRecordMessage
{
/**
};
GNUNET_NETWORK_STRUCT_END
-
#endif
/*
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
*
* @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
};
/******************************************************************************/
{
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.
*
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)
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)
{
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.
*
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
*
* @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)
GNUNET_CLIENT_disconnect (h->client);
h->client = NULL;
}
- GNUNET_free(h);
+ GNUNET_free (h);
}
+
/**
* Connect to the PEERSTORE service.
*
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.
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;
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;
}
*
* @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;
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
*
{
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
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 *********************/
/******************************************************************************/
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
*
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
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
*
*/
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 *********************/
/******************************************************************************/
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;
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
*/
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;
}
*
*/
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;
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
*
* @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;
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
*
* @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;
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
*
* @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;
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);
}
*
*/
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
* @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
* @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
* @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);
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;
}
* @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))
{
"sqlite3_reset");
return 0;
}
- return sqlite3_changes(plugin->dbh);
+ return sqlite3_changes (plugin->dbh);
}
+
/**
* Delete expired records (expiry < now)
*
* @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))
{
"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.
* @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;
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)
{
}
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.
* @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,
"sqlite3_reset");
return GNUNET_SYSERR;
}
-
return GNUNET_OK;
}
* @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
*
* @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
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))
}
/* 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");
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.
{
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.
*
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.
*
GNUNET_free (api);
LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n");
return NULL;
-
}
/* end of plugin_peerstore_sqlite.c */
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;
}
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;
}
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;
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;
}
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;
}