2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file psycstore/gnunet-service-psycstore.c
23 * @brief PSYCstore service
24 * @author Gabor X Toth
25 * @author Christian Grothoff
27 * The purpose of this service is to manage private keys that
28 * represent the various egos/pseudonyms/identities of a GNUnet user.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_constants.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_psycstore_service.h"
37 #include "gnunet_psycstore_plugin.h"
38 #include "psycstore.h"
42 * Handle to our current configuration.
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * Handle to the statistics service.
49 static struct GNUNET_STATISTICS_Handle *stats;
52 * Notification context, simplifies client broadcasts.
54 static struct GNUNET_SERVER_NotificationContext *nc;
59 static struct GNUNET_PSYCSTORE_PluginFunctions *db;
62 * Name of the database plugin
64 static char *db_lib_name;
68 * Task run during shutdown.
74 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 GNUNET_SERVER_notification_context_destroy (nc);
83 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
86 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
87 GNUNET_free (db_lib_name);
93 * Send a result code back to the client.
95 * @param client client that should receive the result code
96 * @param result_code code to transmit
97 * @param emsg error message to include (or NULL for none)
100 send_result_code (struct GNUNET_SERVER_Client *client,
101 uint32_t result_code,
104 struct GNUNET_PSYCSTORE_ResultCodeMessage *rcm;
110 elen = strlen (emsg) + 1;
111 rcm = GNUNET_malloc (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) + elen);
112 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
113 rcm->header.size = htons (sizeof (struct GNUNET_PSYCSTORE_ResultCodeMessage) + elen);
114 rcm->result_code = htonl (result_code);
115 memcpy (&rcm[1], emsg, elen);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Sending result %d (%s) to client\n",
120 GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_NO);
126 * Handle PSYCstore clients.
129 * @param server the initialized server
130 * @param c configuration to use
134 struct GNUNET_SERVER_Handle *server,
135 const struct GNUNET_CONFIGURATION_Handle *c)
137 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
143 /* Loading database plugin */
146 GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
153 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
154 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
155 GNUNET_free (database);
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Could not load database backend `%s'\n",
162 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
166 stats = GNUNET_STATISTICS_create ("psycstore", cfg);
167 GNUNET_SERVER_add_handlers (server, handlers);
168 nc = GNUNET_SERVER_notification_context_create (server, 1);
169 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
175 * The main function for the network size estimation service.
177 * @param argc number of arguments from the command line
178 * @param argv command line arguments
179 * @return 0 ok, 1 on error
182 main (int argc, char *const *argv)
185 GNUNET_SERVICE_run (argc, argv, "psycstore",
186 GNUNET_SERVICE_OPTION_NONE,
187 &run, NULL)) ? 0 : 1;
191 /* end of gnunet-service-psycstore.c */