ad4d68335a87b01c2ef395639260f22e71b10654
[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       || NULL == record->peer
120       || NULL == record->key)
121   {
122     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
123     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
124     return;
125   }
126   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
127       record->value_size,
128       record->sub_system,
129       GNUNET_i2s (record->peer),
130       record->key);
131   if(GNUNET_OK == db->store_record(db->cls,
132       record->sub_system,
133       record->peer,
134       record->key,
135       record->value,
136       record->value_size,
137       GNUNET_TIME_relative_to_absolute(record->lifetime)))
138   {
139     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
140   }
141   else
142   {
143     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
144     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
145   }
146
147   tc = GNUNET_SERVER_transmit_context_create (client);
148   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
149   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
150
151 }
152
153 /**
154  * Peerstore service runner.
155  *
156  * @param cls closure
157  * @param server the initialized server
158  * @param c configuration to use
159  */
160 static void
161 run (void *cls,
162      struct GNUNET_SERVER_Handle *server,
163      const struct GNUNET_CONFIGURATION_Handle *c)
164 {
165   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
166       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
167       {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
168       {NULL, NULL, 0, 0}
169   };
170   char *database;
171
172   cfg = c;
173   if (GNUNET_OK !=
174         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
175                                                &database))
176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
177
178   else
179   {
180     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
181     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
182     GNUNET_free(database);
183   }
184   if(NULL == db)
185           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
186   else
187   {
188     GNUNET_SERVER_add_handlers (server, handlers);
189     GNUNET_SERVER_disconnect_notify (server,
190              &handle_client_disconnect,
191              NULL);
192   }
193   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
194                                 &shutdown_task,
195                                 NULL);
196 }
197
198
199 /**
200  * The main function for the peerstore service.
201  *
202  * @param argc number of arguments from the command line
203  * @param argv command line arguments
204  * @return 0 ok, 1 on error
205  */
206 int
207 main (int argc, char *const *argv)
208 {
209   return (GNUNET_OK ==
210           GNUNET_SERVICE_run (argc,
211                               argv,
212                               "peerstore",
213                               GNUNET_SERVICE_OPTION_NONE,
214                               &run, NULL)) ? 0 : 1;
215 }
216
217 /* end of gnunet-service-peerstore.c */