static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
- * Handle to the statistics service.
+ * Service handle.
*/
-static struct GNUNET_STATISTICS_Handle *stats;
+struct GNUNET_SERVICE_Handle *service;
/**
- * Notification context, simplifies client broadcasts.
+ * Handle to the statistics service.
*/
-static struct GNUNET_SERVER_NotificationContext *nc;
+static struct GNUNET_STATISTICS_Handle *stats;
/**
* Database handle
static void
shutdown_task (void *cls)
{
- if (NULL != nc)
- {
- GNUNET_SERVER_notification_context_destroy (nc);
- nc = NULL;
- }
if (NULL != stats)
{
GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
* Error message to include (or NULL for none).
*/
static void
-send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
- int64_t result_code, const char *err_msg)
+send_result_code (struct GNUNET_SERVICE_Client *client,
+ uint64_t op_id,
+ int64_t result_code,
+ const char *err_msg)
{
struct OperationResult *res;
size_t err_size = 0;
if (NULL != err_msg)
err_size = strnlen (err_msg,
GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*res) - 1) + 1;
- res = GNUNET_malloc (sizeof (struct OperationResult) + err_size);
- res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
- res->header.size = htons (sizeof (struct OperationResult) + err_size);
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg_extra (res, err_size,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
res->result_code = GNUNET_htonll (result_code - INT64_MIN);
res->op_id = op_id;
if (0 < err_size)
GNUNET_memcpy (&res[1], err_msg, err_size);
((char *) &res[1])[err_size - 1] = '\0';
}
+
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sending result to client: %" PRId64 " (%s)\n",
result_code, err_msg);
- GNUNET_SERVER_notification_context_add (nc, client);
- GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
- GNUNET_NO);
- GNUNET_free (res);
+ GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
}
struct SendClosure
{
- struct GNUNET_SERVER_Client *client;
+ struct GNUNET_SERVICE_Client *client;
/**
* Channel's public key.
size_t msg_size = ntohs (msg->header.size);
- res = GNUNET_malloc (sizeof (struct FragmentResult) + msg_size);
- res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT);
- res->header.size = htons (sizeof (struct FragmentResult) + msg_size);
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg_extra (res, msg_size,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT);
res->op_id = sc->op_id;
res->psycstore_flags = htonl (flags);
GNUNET_memcpy (&res[1], msg, msg_size);
"Sending fragment %ld to client\n",
GNUNET_ntohll (msg->fragment_id));
GNUNET_free (msg);
- GNUNET_SERVER_notification_context_add (nc, sc->client);
- GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
- GNUNET_NO);
- GNUNET_free (res);
+
+ GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (sc->client), env);
return GNUNET_YES;
}
/** @todo FIXME: split up value into 64k chunks */
- res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size);
- res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
- res->header.size = htons (sizeof (struct StateResult) + name_size + value_size);
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg_extra (res, name_size + value_size,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
res->op_id = sc->op_id;
res->name_size = htons (name_size);
GNUNET_memcpy (&res[1], name, name_size);
GNUNET_memcpy ((char *) &res[1] + name_size, value, value_size);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sending state variable %s to client\n", name);
- GNUNET_SERVER_notification_context_add (nc, sc->client);
- GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
- GNUNET_NO);
- GNUNET_free (res);
+
+ GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (sc->client), env);
return GNUNET_OK;
}
static void
-handle_membership_store (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_membership_store (void *cls,
+ const struct MembershipStoreRequest *req)
{
- const struct MembershipStoreRequest *req =
- (const struct MembershipStoreRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
req->did_join,
_("Failed to store membership information!\n"));
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_membership_test (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_membership_test (void *cls,
+ const struct MembershipTestRequest *req)
{
- const struct MembershipTestRequest *req =
- (const struct MembershipTestRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = db->membership_test (db->cls, &req->channel_key, &req->slave_key,
GNUNET_ntohll (req->message_id));
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_fragment_store (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_fragment_store (void *cls,
+ const struct FragmentStoreRequest *req)
{
- const struct FragmentStoreRequest *req =
- (const struct FragmentStoreRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = db->fragment_store (db->cls, &req->channel_key,
(const struct GNUNET_MULTICAST_MessageHeader *)
_("Failed to store fragment!\n"));
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_fragment_get (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_fragment_get (void *cls,
+ const struct FragmentGetRequest *req)
{
- const struct FragmentGetRequest *
- req = (const struct FragmentGetRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
+
struct SendClosure
sc = { .op_id = req->op_id,
.client = client,
_("Failed to get fragment!\n"));
}
send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_message_get (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_message_get (void *cls,
+ const struct MessageGetRequest *req)
{
- const struct MessageGetRequest *
- req = (const struct MessageGetRequest *) msg;
- uint16_t size = ntohs (msg->size);
+ struct GNUNET_SERVICE_Client *client = cls;
+
+ uint16_t size = ntohs (req->header.size);
const char *method_prefix = (const char *) &req[1];
if (size < sizeof (*req) + 1
size,
(unsigned int) (sizeof (*req) + 1));
GNUNET_break (0);
- GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ GNUNET_SERVICE_client_drop (client);
return;
}
}
send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_message_get_fragment (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_message_get_fragment (void *cls,
+ const struct MessageGetFragmentRequest *req)
{
- const struct MessageGetFragmentRequest *
- req = (const struct MessageGetFragmentRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
+
struct SendClosure
sc = { .op_id = req->op_id, .client = client,
.channel_key = req->channel_key, .slave_key = req->slave_key,
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_counters_get (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_counters_get (void *cls,
+ const struct OperationRequest *req)
{
- const struct OperationRequest *req = (const struct OperationRequest *) msg;
- struct CountersResult res = { {0} };
+ struct GNUNET_SERVICE_Client *client = cls;
+
+ struct CountersResult *res;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg (res, GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
int ret = db->counters_message_get (db->cls, &req->channel_key,
- &res.max_fragment_id, &res.max_message_id,
- &res.max_group_generation);
+ &res->max_fragment_id, &res->max_message_id,
+ &res->max_group_generation);
switch (ret)
{
case GNUNET_OK:
ret = db->counters_state_get (db->cls, &req->channel_key,
- &res.max_state_message_id);
+ &res->max_state_message_id);
case GNUNET_NO:
break;
default:
_("Failed to get master counters!\n"));
}
- res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
- res.header.size = htons (sizeof (res));
- res.result_code = htonl (ret);
- res.op_id = req->op_id;
- res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
- res.max_message_id = GNUNET_htonll (res.max_message_id);
- res.max_group_generation = GNUNET_htonll (res.max_group_generation);
- res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
+ res->result_code = htonl (ret);
+ res->op_id = req->op_id;
+ res->max_fragment_id = GNUNET_htonll (res->max_fragment_id);
+ res->max_message_id = GNUNET_htonll (res->max_message_id);
+ res->max_group_generation = GNUNET_htonll (res->max_group_generation);
+ res->max_state_message_id = GNUNET_htonll (res->max_state_message_id);
- GNUNET_SERVER_notification_context_add (nc, client);
- GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
- GNUNET_NO);
-
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_state_modify (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_modify (void *cls,
+ const struct StateModifyRequest *req)
{
- const struct StateModifyRequest *req
- = (const struct StateModifyRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
uint64_t message_id = GNUNET_ntohll (req->message_id);
uint64_t state_delta = GNUNET_ntohll (req->state_delta);
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
+}
+
+
+static int
+check_client_state_sync (void *cls,
+ const struct StateSyncRequest *req)
+{
+ return GNUNET_OK;
}
/** @todo FIXME: stop processing further state sync messages after an error */
static void
-handle_state_sync (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_sync (void *cls,
+ const struct StateSyncRequest *req)
{
- const struct StateSyncRequest *req
- = (const struct StateSyncRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = GNUNET_SYSERR;
const char *name = (const char *) &req[1];
}
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_state_reset (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_reset (void *cls,
+ const struct OperationRequest *req)
{
- const struct OperationRequest *req =
- (const struct OperationRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = db->state_reset (db->cls, &req->channel_key);
_("Failed to reset state!\n"));
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
static void
-handle_state_hash_update (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_hash_update (void *cls,
+ const struct StateHashUpdateRequest *req)
{
- const struct OperationRequest *req =
- (const struct OperationRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
int ret = db->state_reset (db->cls, &req->channel_key);
-
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to reset state!\n"));
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
+}
+
+
+static int
+check_client_state_get (void *cls,
+ const struct OperationRequest *req)
+{
+ return GNUNET_OK;
}
static void
-handle_state_get (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_get (void *cls,
+ const struct OperationRequest *req)
{
- const struct OperationRequest *req =
- (const struct OperationRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure sc = { .op_id = req->op_id, .client = client };
int64_t ret = GNUNET_SYSERR;
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
+}
+
+
+static int
+check_client_state_get_prefix (void *cls,
+ const struct OperationRequest *req)
+{
+ return GNUNET_OK;
}
static void
-handle_state_get_prefix (void *cls,
- struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *msg)
+handle_client_state_get_prefix (void *cls,
+ const struct OperationRequest *req)
{
- const struct OperationRequest *req =
- (const struct OperationRequest *) msg;
+ struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure sc = { .op_id = req->op_id, .client = client };
int64_t ret = GNUNET_SYSERR;
}
send_result_code (client, req->op_id, ret, NULL);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_SERVICE_client_continue (client);
}
/**
- * Initialize the PSYCstore service.
+ * A new client connected.
*
- * @param cls Closure.
- * @param server The initialized server.
- * @param c Configuration to use.
+ * @param cls NULL
+ * @param client client to add
+ * @param mq message queue for @a client
+ * @return @a client
*/
-static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
- const struct GNUNET_CONFIGURATION_Handle *c)
+static void *
+client_notify_connect (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ struct GNUNET_MQ_Handle *mq)
{
- static const struct GNUNET_SERVER_MessageHandler handlers[] = {
- { &handle_membership_store, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
- sizeof (struct MembershipStoreRequest) },
-
- { &handle_membership_test, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
- sizeof (struct MembershipTestRequest) },
-
- { &handle_fragment_store, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE, 0, },
-
- { &handle_fragment_get, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
- sizeof (struct FragmentGetRequest) },
-
- { &handle_message_get, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET, 0 },
-
- { &handle_message_get_fragment, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
- sizeof (struct MessageGetFragmentRequest) },
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
- { &handle_counters_get, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
- sizeof (struct OperationRequest) },
-
- { &handle_state_modify, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY, 0 },
-
- { &handle_state_sync, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC, 0 },
-
- { &handle_state_reset, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
- sizeof (struct OperationRequest) },
-
- { &handle_state_hash_update, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
- sizeof (struct StateHashUpdateRequest) },
+ return client;
+}
- { &handle_state_get, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET, 0 },
- { &handle_state_get_prefix, NULL,
- GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX, 0 },
+/**
+ * Called whenever a client is disconnected.
+ * Frees our resources associated with that client.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param app_ctx must match @a client
+ */
+static void
+client_notify_disconnect (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ void *app_ctx)
+{
+}
- { NULL, NULL, 0, 0 }
- };
+/**
+ * Initialize the PSYCstore service.
+ *
+ * @param cls Closure.
+ * @param server The initialized server.
+ * @param c Configuration to use.
+ */
+static void
+run (void *cls,
+ const struct GNUNET_CONFIGURATION_Handle *c,
+ struct GNUNET_SERVICE_Handle *svc)
+{
cfg = c;
+ service = svc;
/* Loading database plugin */
char *database;
}
stats = GNUNET_STATISTICS_create ("psycstore", cfg);
- GNUNET_SERVER_add_handlers (server, handlers);
- nc = GNUNET_SERVER_notification_context_create (server, 1);
- GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+ GNUNET_SCHEDULER_add_shutdown (shutdown_task,
NULL);
}
-
/**
- * The main function for the service.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * Define "main" method using service macro.
*/
-int
-main (int argc, char *const *argv)
-{
- return (GNUNET_OK ==
- GNUNET_SERVICE_run (argc, argv, "psycstore",
- GNUNET_SERVICE_OPTION_NONE,
- &run, NULL)) ? 0 : 1;
-}
-
+GNUNET_SERVICE_MAIN
+("psycstore",
+ GNUNET_SERVICE_OPTION_NONE,
+ run,
+ client_notify_connect,
+ client_notify_disconnect,
+ NULL,
+ GNUNET_MQ_hd_fixed_size (client_membership_store,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
+ struct MembershipStoreRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_membership_test,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
+ struct MembershipTestRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_fragment_store,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE,
+ struct FragmentStoreRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_fragment_get,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
+ struct FragmentGetRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_message_get,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET,
+ struct MessageGetRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_message_get_fragment,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
+ struct MessageGetFragmentRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_counters_get,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
+ struct OperationRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_state_modify,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY,
+ struct StateModifyRequest,
+ NULL),
+ GNUNET_MQ_hd_var_size (client_state_sync,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC,
+ struct StateSyncRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_state_reset,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
+ struct OperationRequest,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (client_state_hash_update,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
+ struct StateHashUpdateRequest,
+ NULL),
+ GNUNET_MQ_hd_var_size (client_state_get,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET,
+ struct OperationRequest,
+ NULL),
+ GNUNET_MQ_hd_var_size (client_state_get_prefix,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX,
+ struct OperationRequest,
+ NULL));
/* end of gnunet-service-psycstore.c */