2 This file is part of GNUnet.
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 peerstore/gnunet-service-peerstore.c
23 * @brief peerstore service implementation
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "gnunet_peerstore_plugin.h"
30 #include "peerstore_common.h"
32 //TODO: GNUNET_SERVER_receive_done() ?
33 //TODO: implement value lifetime
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 * Database plugin library name
48 static struct GNUNET_PEERSTORE_PluginFunctions *db;
51 * Task run during shutdown.
57 shutdown_task (void *cls,
58 const struct GNUNET_SCHEDULER_TaskContext *tc)
60 if(NULL != db_lib_name)
62 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
63 GNUNET_free (db_lib_name);
70 * A client disconnected. Remove all of its data structure entries.
72 * @param cls closure, NULL
73 * @param client identification of the client
76 handle_client_disconnect (void *cls,
77 struct GNUNET_SERVER_Client
83 * Handle an iterate request from client
86 * @param client identification of the client
87 * @param message the actual message
89 void handle_iterate (void *cls,
90 struct GNUNET_SERVER_Client *client,
91 const struct GNUNET_MessageHeader *message)
97 * Handle a store request from client
100 * @param client identification of the client
101 * @param message the actual message
103 void handle_store (void *cls,
104 struct GNUNET_SERVER_Client *client,
105 const struct GNUNET_MessageHeader *message)
107 struct GNUNET_PEERSTORE_Record *record;
108 uint16_t response_type;
109 struct GNUNET_SERVER_TransmitContext *tc;
111 record = PEERSTORE_parse_record_message(message);
114 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
115 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
118 if(NULL == record->sub_system)
120 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client store request\n");
121 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
124 if(NULL == record->peer)
126 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Peer id not supplied in client store request\n");
127 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
130 if(NULL == record->key)
132 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Key not supplied in client store request\n");
133 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
136 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
139 GNUNET_i2s (record->peer),
141 if(GNUNET_OK == db->store_record(db->cls,
148 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
152 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
153 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
156 tc = GNUNET_SERVER_transmit_context_create (client);
157 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
158 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
163 * Peerstore service runner.
166 * @param server the initialized server
167 * @param c configuration to use
171 struct GNUNET_SERVER_Handle *server,
172 const struct GNUNET_CONFIGURATION_Handle *c)
174 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
175 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
176 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
183 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
189 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
190 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
191 GNUNET_free(database);
194 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
197 GNUNET_SERVER_add_handlers (server, handlers);
198 GNUNET_SERVER_disconnect_notify (server,
199 &handle_client_disconnect,
202 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
209 * The main function for the peerstore service.
211 * @param argc number of arguments from the command line
212 * @param argv command line arguments
213 * @return 0 ok, 1 on error
216 main (int argc, char *const *argv)
219 GNUNET_SERVICE_run (argc,
222 GNUNET_SERVICE_OPTION_NONE,
223 &run, NULL)) ? 0 : 1;
226 /* end of gnunet-service-peerstore.c */