towards peerstore iterate
[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  * Function called by for each matching record.
84  *
85  * @param cls closure
86  * @param peer peer identity
87  * @param sub_system name of the GNUnet sub system responsible
88  * @param value stored value
89  * @param size size of stored value
90  */
91 int record_iterator(void *cls,
92     const char *sub_system,
93     const struct GNUNET_PeerIdentity *peer,
94     const char *key,
95     const void *value,
96     size_t size,
97     struct GNUNET_TIME_Absolute expiry)
98 {
99   struct GNUNET_SERVER_TransmitContext *tc = cls;
100   struct StoreRecordMessage *srm;
101
102   srm = PEERSTORE_create_record_message(sub_system,
103       peer,
104       key,
105       value,
106       size,
107       expiry,
108       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
109   GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
110   return GNUNET_YES;
111 }
112
113 /**
114  * Handle an iterate request from client
115  *
116  * @param cls unused
117  * @param client identification of the client
118  * @param message the actual message
119  */
120 void handle_iterate (void *cls,
121     struct GNUNET_SERVER_Client *client,
122     const struct GNUNET_MessageHeader *message)
123 {
124   struct GNUNET_PEERSTORE_Record *record;
125   struct GNUNET_SERVER_TransmitContext *tc;
126
127   record = PEERSTORE_parse_record_message(message);
128   if(NULL == record)
129   {
130     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
131     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
132     return;
133   }
134   if(NULL == record->sub_system)
135   {
136     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
137     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
138     return;
139   }
140   tc = GNUNET_SERVER_transmit_context_create (client);
141   if(GNUNET_OK == db->iterate_records(db->cls,
142       record->sub_system,
143       record->peer,
144       record->key,
145       &record_iterator,
146       tc))
147   {
148
149   }
150 }
151
152 /**
153  * Handle a store request from client
154  *
155  * @param cls unused
156  * @param client identification of the client
157  * @param message the actual message
158  */
159 void handle_store (void *cls,
160     struct GNUNET_SERVER_Client *client,
161     const struct GNUNET_MessageHeader *message)
162 {
163   struct GNUNET_PEERSTORE_Record *record;
164   uint16_t response_type;
165   struct GNUNET_SERVER_TransmitContext *tc;
166
167   record = PEERSTORE_parse_record_message(message);
168   if(NULL == record)
169   {
170     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
171     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
172     return;
173   }
174   if(NULL == record->sub_system
175       || NULL == record->peer
176       || NULL == record->key)
177   {
178     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
179     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
180     return;
181   }
182   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
183       record->value_size,
184       record->sub_system,
185       GNUNET_i2s (record->peer),
186       record->key);
187   if(GNUNET_OK == db->store_record(db->cls,
188       record->sub_system,
189       record->peer,
190       record->key,
191       record->value,
192       record->value_size,
193       record->expiry))
194   {
195     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
196   }
197   else
198   {
199     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
200     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
201   }
202
203   tc = GNUNET_SERVER_transmit_context_create (client);
204   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
205   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
206
207 }
208
209 /**
210  * Peerstore service runner.
211  *
212  * @param cls closure
213  * @param server the initialized server
214  * @param c configuration to use
215  */
216 static void
217 run (void *cls,
218      struct GNUNET_SERVER_Handle *server,
219      const struct GNUNET_CONFIGURATION_Handle *c)
220 {
221   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
222       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
223       {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
224       {NULL, NULL, 0, 0}
225   };
226   char *database;
227
228   cfg = c;
229   if (GNUNET_OK !=
230         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
231                                                &database))
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
233
234   else
235   {
236     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
237     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
238     GNUNET_free(database);
239   }
240   if(NULL == db)
241           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
242   else
243   {
244     GNUNET_SERVER_add_handlers (server, handlers);
245     GNUNET_SERVER_disconnect_notify (server,
246              &handle_client_disconnect,
247              NULL);
248   }
249   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
250                                 &shutdown_task,
251                                 NULL);
252 }
253
254
255 /**
256  * The main function for the peerstore service.
257  *
258  * @param argc number of arguments from the command line
259  * @param argv command line arguments
260  * @return 0 ok, 1 on error
261  */
262 int
263 main (int argc, char *const *argv)
264 {
265   return (GNUNET_OK ==
266           GNUNET_SERVICE_run (argc,
267                               argv,
268                               "peerstore",
269                               GNUNET_SERVICE_OPTION_NONE,
270                               &run, NULL)) ? 0 : 1;
271 }
272
273 /* end of gnunet-service-peerstore.c */