peerstore: towards watch functionality
[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 /**
33  * Context of a PEERSTORE watch
34  */
35 struct WatchContext
36 {
37
38   /**
39    * Hash of key of watched record
40    */
41   struct GNUNET_HashCode keyhash;
42
43   /**
44    * Client requested the watch
45    */
46   struct GNUNET_SERVER_Client *client;
47
48 };
49
50 /**
51  * Interval for expired records cleanup (in seconds)
52  */
53 #define CLEANUP_INTERVAL 300 /* 5mins */
54
55 /**
56  * Our configuration.
57  */
58 static const struct GNUNET_CONFIGURATION_Handle *cfg;
59
60 /**
61  * Database plugin library name
62  */
63 char *db_lib_name;
64
65 /**
66  * Database handle
67  */
68 static struct GNUNET_PEERSTORE_PluginFunctions *db;
69
70 /**
71  * Hashmap with all watch requests
72  */
73 static struct GNUNET_CONTAINER_MultiHashMap *watchers;
74
75 /**
76  * Task run during shutdown.
77  *
78  * @param cls unused
79  * @param tc unused
80  */
81 static void
82 shutdown_task (void *cls,
83                const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   if(NULL != db_lib_name)
86   {
87     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
88     GNUNET_free (db_lib_name);
89     db_lib_name = NULL;
90   }
91   if(NULL != watchers)
92     GNUNET_CONTAINER_multihashmap_destroy(watchers);
93   GNUNET_SCHEDULER_shutdown();
94 }
95
96 /**
97  * Deletes any expired records from storage
98  */
99 static void
100 cleanup_expired_records(void *cls,
101     const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   int deleted;
104
105   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
106     return;
107   GNUNET_assert(NULL != db);
108   deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
109   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
110   GNUNET_SCHEDULER_add_delayed(
111       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
112       &cleanup_expired_records, NULL);
113 }
114
115
116 /**
117  * A client disconnected.  Remove all of its data structure entries.
118  *
119  * @param cls closure, NULL
120  * @param client identification of the client
121  */
122 static void
123 handle_client_disconnect (void *cls,
124                           struct GNUNET_SERVER_Client
125                           * client)
126 {
127 }
128
129 /**
130  * Function called by for each matching record.
131  *
132  * @param cls closure
133  * @param peer peer identity
134  * @param sub_system name of the GNUnet sub system responsible
135  * @param value stored value
136  * @param size size of stored value
137  */
138 int record_iterator(void *cls,
139     struct GNUNET_PEERSTORE_Record *record,
140     char *emsg)
141 {
142   struct GNUNET_SERVER_TransmitContext *tc = cls;
143   struct StoreRecordMessage *srm;
144
145   srm = PEERSTORE_create_record_message(record->sub_system,
146       record->peer,
147       record->key,
148       record->value,
149       record->value_size,
150       record->expiry,
151       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
152   GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
153   return GNUNET_YES;
154 }
155
156 /**
157  * Handle a watch cancel request from client
158  *
159  * @param cls unused
160  * @param client identification of the client
161  * @param message the actual message
162  */
163 void handle_watch_cancel (void *cls,
164     struct GNUNET_SERVER_Client *client,
165     const struct GNUNET_MessageHeader *message)
166 {
167   struct StoreKeyHashMessage *hm;
168
169   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request from client.\n");
170   if(NULL == watchers)
171   {
172     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
173         "Received a watch cancel request when we don't have any watchers.\n");
174     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
175     return;
176   }
177   hm = (struct StoreKeyHashMessage *) message;
178   GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client);
179   GNUNET_SERVER_receive_done(client, GNUNET_OK);
180 }
181
182 /**
183  * Handle a watch request from client
184  *
185  * @param cls unused
186  * @param client identification of the client
187  * @param message the actual message
188  */
189 void handle_watch (void *cls,
190     struct GNUNET_SERVER_Client *client,
191     const struct GNUNET_MessageHeader *message)
192 {
193   struct StoreKeyHashMessage *hm;
194
195   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request from client.\n");
196   hm = (struct StoreKeyHashMessage *) message;
197   GNUNET_SERVER_client_mark_monitor(client);
198   if(NULL == watchers)
199     watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
200   GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash,
201      client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
202   GNUNET_SERVER_receive_done(client, GNUNET_OK);
203 }
204
205 /**
206  * Handle an iterate request from client
207  *
208  * @param cls unused
209  * @param client identification of the client
210  * @param message the actual message
211  */
212 void handle_iterate (void *cls,
213     struct GNUNET_SERVER_Client *client,
214     const struct GNUNET_MessageHeader *message)
215 {
216   struct GNUNET_PEERSTORE_Record *record;
217   struct GNUNET_SERVER_TransmitContext *tc;
218
219   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
220   record = PEERSTORE_parse_record_message(message);
221   if(NULL == record)
222   {
223     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
224     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
225     return;
226   }
227   if(NULL == record->sub_system)
228   {
229     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
230     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
231     return;
232   }
233   tc = GNUNET_SERVER_transmit_context_create (client);
234   if(GNUNET_OK == db->iterate_records(db->cls,
235       record->sub_system,
236       record->peer,
237       record->key,
238       &record_iterator,
239       tc))
240   {
241     GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
242     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
243   }
244   else
245   {
246     GNUNET_free(tc);
247     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
248   }
249   GNUNET_free(record);
250 }
251
252 /**
253  * Handle a store request from client
254  *
255  * @param cls unused
256  * @param client identification of the client
257  * @param message the actual message
258  */
259 void handle_store (void *cls,
260     struct GNUNET_SERVER_Client *client,
261     const struct GNUNET_MessageHeader *message)
262 {
263   struct GNUNET_PEERSTORE_Record *record;
264   uint16_t response_type;
265   struct GNUNET_SERVER_TransmitContext *tc;
266
267   record = PEERSTORE_parse_record_message(message);
268   if(NULL == record)
269   {
270     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
271     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
272     return;
273   }
274   if(NULL == record->sub_system
275       || NULL == record->peer
276       || NULL == record->key)
277   {
278     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
279     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
280     return;
281   }
282   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
283       record->value_size,
284       record->sub_system,
285       GNUNET_i2s (record->peer),
286       record->key);
287   if(GNUNET_OK == db->store_record(db->cls,
288       record->sub_system,
289       record->peer,
290       record->key,
291       record->value,
292       record->value_size,
293       *record->expiry))
294   {
295     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
296   }
297   else
298   {
299     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
300     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
301   }
302
303   tc = GNUNET_SERVER_transmit_context_create (client);
304   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
305   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
306   //TODO: notify watchers, if a client is disconnected, remove its watch entry
307 }
308
309 /**
310  * Peerstore service runner.
311  *
312  * @param cls closure
313  * @param server the initialized server
314  * @param c configuration to use
315  */
316 static void
317 run (void *cls,
318      struct GNUNET_SERVER_Handle *server,
319      const struct GNUNET_CONFIGURATION_Handle *c)
320 {
321   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
322       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
323       {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
324       {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)},
325       {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)},
326       {NULL, NULL, 0, 0}
327   };
328   char *database;
329
330   cfg = c;
331   if (GNUNET_OK !=
332         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
333                                                &database))
334     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
335
336   else
337   {
338     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
339     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
340     GNUNET_free(database);
341   }
342   if(NULL == db)
343           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
344   else
345   {
346     GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
347     GNUNET_SERVER_add_handlers (server, handlers);
348     GNUNET_SERVER_disconnect_notify (server,
349              &handle_client_disconnect,
350              NULL);
351   }
352   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
353                                 &shutdown_task,
354                                 NULL);
355 }
356
357
358 /**
359  * The main function for the peerstore service.
360  *
361  * @param argc number of arguments from the command line
362  * @param argv command line arguments
363  * @return 0 ok, 1 on error
364  */
365 int
366 main (int argc, char *const *argv)
367 {
368   return (GNUNET_OK ==
369           GNUNET_SERVICE_run (argc,
370                               argv,
371                               "peerstore",
372                               GNUNET_SERVICE_OPTION_NONE,
373                               &run, NULL)) ? 0 : 1;
374 }
375
376 /* end of gnunet-service-peerstore.c */