peerstore helper file
[oweals/gnunet.git] / src / peerstore / gnunet-service-peerstore.c
1 /*
2      This file is part of GNUnet.
3      (C) 
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file peerstore/gnunet-service-peerstore.c
23  * @brief peerstore service implementation
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "gnunet_peerstore_plugin.h"
30 #include "peerstore_common.h"
31
32 //TODO: GNUNET_SERVER_receive_done() ?
33 //TODO: implement value lifetime
34
35 /**
36  * Our configuration.
37  */
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39
40 /**
41  * Database plugin library name
42  */
43 char *db_lib_name;
44
45 /**
46  * Database handle
47  */
48 static struct GNUNET_PEERSTORE_PluginFunctions *db;
49
50 /**
51  * Task run during shutdown.
52  *
53  * @param cls unused
54  * @param tc unused
55  */
56 static void
57 shutdown_task (void *cls,
58                const struct GNUNET_SCHEDULER_TaskContext *tc)
59 {
60   if(NULL != db_lib_name)
61   {
62     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
63     GNUNET_free (db_lib_name);
64     db_lib_name = NULL;
65   }
66 }
67
68
69 /**
70  * A client disconnected.  Remove all of its data structure entries.
71  *
72  * @param cls closure, NULL
73  * @param client identification of the client
74  */
75 static void
76 handle_client_disconnect (void *cls,
77                           struct GNUNET_SERVER_Client
78                           * client)
79 {
80 }
81
82 /**
83  * Handle an iterate request from client
84  *
85  * @param cls unused
86  * @param client identification of the client
87  * @param message the actual message
88  */
89 void handle_iterate (void *cls,
90     struct GNUNET_SERVER_Client *client,
91     const struct GNUNET_MessageHeader *message)
92 {
93
94 }
95
96 /**
97  * Handle a store request from client
98  *
99  * @param cls unused
100  * @param client identification of the client
101  * @param message the actual message
102  */
103 void handle_store (void *cls,
104     struct GNUNET_SERVER_Client *client,
105     const struct GNUNET_MessageHeader *message)
106 {
107   struct GNUNET_PEERSTORE_Record *record;
108   uint16_t response_type;
109   struct GNUNET_SERVER_TransmitContext *tc;
110
111   record = PEERSTORE_parse_record_message(message);
112   if(NULL == record)
113   {
114     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
115     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
116     return;
117   }
118   if(NULL == record->sub_system)
119   {
120     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client store request\n");
121     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
122     return;
123   }
124   if(NULL == record->peer)
125   {
126     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Peer id not supplied in client store request\n");
127     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
128     return;
129   }
130   if(NULL == record->key)
131   {
132     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Key not supplied in client store request\n");
133     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
134     return;
135   }
136   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
137       record->value_size,
138       record->sub_system,
139       GNUNET_i2s (record->peer),
140       record->key);
141   if(GNUNET_OK == db->store_record(db->cls,
142       record->sub_system,
143       record->peer,
144       record->key,
145       record->value,
146       record->value_size))
147   {
148     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
149   }
150   else
151   {
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;
154   }
155
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);
159
160 }
161
162 /**
163  * Peerstore service runner.
164  *
165  * @param cls closure
166  * @param server the initialized server
167  * @param c configuration to use
168  */
169 static void
170 run (void *cls,
171      struct GNUNET_SERVER_Handle *server,
172      const struct GNUNET_CONFIGURATION_Handle *c)
173 {
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},
177       {NULL, NULL, 0, 0}
178   };
179   char *database;
180
181   cfg = c;
182   if (GNUNET_OK !=
183         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
184                                                &database))
185     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
186
187   else
188   {
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);
192   }
193   if(NULL == db)
194           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
195   else
196   {
197     GNUNET_SERVER_add_handlers (server, handlers);
198     GNUNET_SERVER_disconnect_notify (server,
199              &handle_client_disconnect,
200              NULL);
201   }
202   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
203                                 &shutdown_task,
204                                 NULL);
205 }
206
207
208 /**
209  * The main function for the peerstore service.
210  *
211  * @param argc number of arguments from the command line
212  * @param argv command line arguments
213  * @return 0 ok, 1 on error
214  */
215 int
216 main (int argc, char *const *argv)
217 {
218   return (GNUNET_OK ==
219           GNUNET_SERVICE_run (argc,
220                               argv,
221                               "peerstore",
222                               GNUNET_SERVICE_OPTION_NONE,
223                               &run, NULL)) ? 0 : 1;
224 }
225
226 /* end of gnunet-service-peerstore.c */