fixing compile errors
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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 fs/gnunet-service-fs.c
23  * @brief program that provides the file-sharing service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_util_lib.h"
31 #include "fs.h"
32
33 static struct GNUNET_DATASTORE_Handle *dsh;
34
35 /**
36  * Handle INDEX_START-message.
37  *
38  * @param cls closure
39  * @param client identification of the client
40  * @param message the actual message
41  */
42 static void
43 handle_index_start (void *cls,
44                     struct GNUNET_SERVER_Client *client,
45                     const struct GNUNET_MessageHeader *message)
46 {
47   const struct IndexStartMessage *ism;
48   const char *fn;
49   uint16_t msize;
50
51   msize = ntohs(message->size);
52   if ( (msize <= sizeof (struct IndexStartMessage)) ||
53        ( ((const char *)message)[msize-1] != '\0') )
54     {
55       GNUNET_break (0);
56       GNUNET_SERVER_receive_done (client,
57                                   GNUNET_SYSERR);
58       return;
59     }
60   ism = (const struct IndexStartMessage*) message;
61   fn = (const char*) &ism[1];
62   // FIXME: store fn, hash, check, respond to client, etc.
63 }
64
65
66 /**
67  * Handle INDEX_LIST_GET-message.
68  *
69  * @param cls closure
70  * @param client identification of the client
71  * @param message the actual message
72  */
73 static void
74 handle_index_list_get (void *cls,
75                        struct GNUNET_SERVER_Client *client,
76                        const struct GNUNET_MessageHeader *message)
77 {
78   struct GNUNET_SERVER_TransmitContext *tc;
79   struct IndexInfoMessage *iim;
80   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
81   size_t slen;
82   char *fn;
83   struct GNUNET_MessageHeader *msg;
84
85   tc = GNUNET_SERVER_transmit_context_create (client);
86   iim = (struct IndexInfoMessage*) buf;
87   msg = &iim->header;
88   while (0)
89     {
90       iim->reserved = 0;
91       // FIXME: read actual list of indexed files...
92       // iim->file_id = id;
93       fn = "FIXME";
94       slen = strlen (fn) + 1;
95       if (slen + sizeof (struct IndexInfoMessage) > 
96           GNUNET_SERVER_MAX_MESSAGE_SIZE)
97         {
98           GNUNET_break (0);
99           break;
100         }
101       memcpy (&iim[1], fn, slen);
102       GNUNET_SERVER_transmit_context_append
103         (tc,
104          &msg[1],
105          sizeof (struct IndexInfoMessage) 
106          - sizeof (struct GNUNET_MessageHeader) + slen,
107          GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
108     }
109   GNUNET_SERVER_transmit_context_append (tc,
110                                          NULL, 0,
111                                          GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
112   GNUNET_SERVER_transmit_context_run (tc,
113                                       GNUNET_TIME_UNIT_MINUTES);
114 }
115
116
117 /**
118  * Handle UNINDEX-message.
119  *
120  * @param cls closure
121  * @param client identification of the client
122  * @param message the actual message
123  */
124 static void
125 handle_unindex (void *cls,
126                 struct GNUNET_SERVER_Client *client,
127                 const struct GNUNET_MessageHeader *message)
128 {
129   const struct UnindexMessage *um;
130   struct GNUNET_SERVER_TransmitContext *tc;
131   
132   um = (const struct UnindexMessage*) message;
133   // fixme: process!
134   tc = GNUNET_SERVER_transmit_context_create (client);
135   GNUNET_SERVER_transmit_context_append (tc,
136                                          NULL, 0,
137                                          GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
138   GNUNET_SERVER_transmit_context_run (tc,
139                                       GNUNET_TIME_UNIT_MINUTES);
140 }
141
142
143 /**
144  * FIXME
145  *
146  * @param cls closure
147  * @param ok GNUNET_OK if DS is ready, GNUNET_SYSERR on timeout
148  */
149 typedef void (*RequestFunction)(void *cls,
150                                 int ok);
151
152
153 /**
154  * Run the next DS request in our
155  * queue, we're done with the current one.
156  */
157 static void
158 next_ds_request ()
159 {
160 }
161
162
163 /**
164  * FIXME.
165  */
166 static void
167 queue_ds_request (struct GNUNET_TIME_Relative deadline,
168                   RequestFunction fun,
169                   void *fun_cls)
170 {
171 }
172
173
174
175 /**
176  * Closure for processing START_SEARCH
177  * messages from a client.
178  */
179 struct LocalGetContext
180 {
181   /**
182    * Client that initiated the search.
183    */
184   struct GNUNET_SERVER_Client *client;
185
186
187   
188 };
189
190
191 static void 
192 transmit_local_get (void *cls,
193                     int ok)
194 {
195   struct LocalGetContext *lgc = cls;
196   // FIXME: search locally
197
198   GNUNET_assert (GNUNET_OK == ok);
199   GNUNET_SERVER_receive_done (lgc->client,
200                               GNUNET_OK);
201
202   // once we're done processing the DS reply, do:
203   next_ds_request ();
204   // FIXME: if not found, initiate P2P search  
205
206   // FIXME: once done with "client" handle:
207   GNUNET_SERVER_client_drop (lgc->client); 
208 }
209
210
211 /**
212  * Handle START_SEARCH-message.
213  *
214  * @param cls closure
215  * @param client identification of the client
216  * @param message the actual message
217  */
218 static void
219 handle_start_search (void *cls,
220                      struct GNUNET_SERVER_Client *client,
221                      const struct GNUNET_MessageHeader *message)
222 {
223   const struct SearchMessage *sm;
224   struct LocalGetContext *lgc;
225
226   sm = (const struct SearchMessage*) message;
227   GNUNET_SERVER_client_keep (client);
228   lgc = GNUNET_malloc (sizeof (struct LocalGetContext));
229   lgc->client = client;
230   // lgc->x = y;
231   queue_ds_request (GNUNET_TIME_UNIT_FOREVER_REL,
232                     &transmit_local_get,
233                     lgc);
234 }
235
236
237 /**
238  * List of handlers for the messages understood by this
239  * service.
240  */
241 static struct GNUNET_SERVER_MessageHandler handlers[] = {
242   {&handle_index_start, NULL, 
243    GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
244   {&handle_index_list_get, NULL, 
245    GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
246   {&handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX, 
247    sizeof (struct UnindexMessage) },
248   {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH, 
249    sizeof (struct SearchMessage) },
250   {NULL, NULL, 0, 0}
251 };
252
253
254 /**
255  * Task run during shutdown.
256  *
257  * @param cls unused
258  * @param tc unused
259  */
260 static void
261 shutdown_task (void *cls,
262                const struct GNUNET_SCHEDULER_TaskContext *tc)
263 {
264   GNUNET_DATASTORE_disconnect (dsh,
265                                GNUNET_NO);
266   dsh = NULL;
267 }
268
269
270 /**
271  * Process fs requests.
272  *
273  * @param cls closure
274  * @param sched scheduler to use
275  * @param server the initialized server
276  * @param cfg configuration to use
277  */
278 static void
279 run (void *cls,
280      struct GNUNET_SCHEDULER_Handle *sched,
281      struct GNUNET_SERVER_Handle *server,
282      const struct GNUNET_CONFIGURATION_Handle *cfg)
283 {
284   dsh = GNUNET_DATASTORE_connect (cfg,
285                                   sched);
286   if (NULL == dsh)
287     {
288       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289                   _("Failed to connect to datastore service.\n"));
290       return;
291     }
292   GNUNET_SERVER_add_handlers (server, handlers);
293   // FIXME: also handle P2P messages!
294
295   GNUNET_SCHEDULER_add_delayed (sched,
296                                 GNUNET_YES,
297                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
298                                 GNUNET_SCHEDULER_NO_TASK,
299                                 GNUNET_TIME_UNIT_FOREVER_REL,
300                                 &shutdown_task,
301                                 NULL);
302 }
303
304
305 /**
306  * The main function for the fs service.
307  *
308  * @param argc number of arguments from the command line
309  * @param argv command line arguments
310  * @return 0 ok, 1 on error
311  */
312 int
313 main (int argc, char *const *argv)
314 {
315   return (GNUNET_OK ==
316           GNUNET_SERVICE_run (argc,
317                               argv,
318                               "fs", &run, NULL, NULL, NULL)) ? 0 : 1;
319 }
320
321 /* end of gnunet-service-fs.c */