2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file statistics/gnunet-service-fs.c
23 * @brief program that provides the file-sharing service
24 * @author Christian Grothoff
27 #include "gnunet_protocols.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_util_lib.h"
33 static struct GNUNET_DATASTORE_Handle *dsh;
36 * Handle INDEX_START-message.
39 * @param client identification of the client
40 * @param message the actual message
43 handle_index_start (void *cls,
44 struct GNUNET_SERVER_Client *client,
45 const struct GNUNET_MessageHeader *message)
47 const struct IndexStartMessage *ism;
51 msize = ntohs(message->size);
52 if ( (msize <= sizeof (struct IndexStartMessage)) ||
53 ( ((const char *)message)[msize-1] != '\0') )
56 GNUNET_SERVER_receive_done (client,
60 ism = (const struct IndexStartMessage*) message;
61 fn = (const char*) &ism[1];
62 // FIXME: store fn, hash, check, respond to client, etc.
67 * Handle INDEX_LIST_GET-message.
70 * @param client identification of the client
71 * @param message the actual message
74 handle_index_list_get (void *cls,
75 struct GNUNET_SERVER_Client *client,
76 const struct GNUNET_MessageHeader *message)
78 struct GNUNET_SERVER_TransmitContext *tc;
79 struct IndexInfoMessage *iim;
80 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
83 struct GNUNET_MessageHeader *msg;
85 tc = GNUNET_SERVER_transmit_context_create (client);
86 iim = (struct IndexInfoMessage*) buf;
91 // FIXME: read actual list of indexed files...
94 slen = strlen (fn) + 1;
95 if (slen + sizeof (struct IndexInfoMessage) >
96 GNUNET_SERVER_MAX_MESSAGE_SIZE)
101 memcpy (&iim[1], fn, slen);
102 GNUNET_SERVER_transmit_context_append
105 sizeof (struct IndexInfoMessage)
106 - sizeof (struct GNUNET_MessageHeader) + slen,
107 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
109 GNUNET_SERVER_transmit_context_append (tc,
111 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
112 GNUNET_SERVER_transmit_context_run (tc,
113 GNUNET_TIME_UNIT_MINUTES);
118 * Handle UNINDEX-message.
121 * @param client identification of the client
122 * @param message the actual message
125 handle_unindex (void *cls,
126 struct GNUNET_SERVER_Client *client,
127 const struct GNUNET_MessageHeader *message)
129 const struct UnindexMessage *um;
130 struct GNUNET_SERVER_TransmitContext *tc;
132 um = (const struct UnindexMessage*) message;
134 tc = GNUNET_SERVER_transmit_context_create (client);
135 GNUNET_SERVER_transmit_context_append (tc,
137 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
138 GNUNET_SERVER_transmit_context_run (tc,
139 GNUNET_TIME_UNIT_MINUTES);
147 * @param ok GNUNET_OK if DS is ready, GNUNET_SYSERR on timeout
149 typedef void (*RequestFunction)(void *cls,
154 * Run the next DS request in our
155 * queue, we're done with the current one.
167 queue_ds_request (struct GNUNET_TIME_Relative deadline,
176 * Closure for processing START_SEARCH
177 * messages from a client.
179 struct LocalGetContext
182 * Client that initiated the search.
184 struct GNUNET_SERVER_Client *client;
190 * Handle START_SEARCH-message.
193 * @param client identification of the client
194 * @param message the actual message
197 handle_start_search (void *cls,
198 struct GNUNET_SERVER_Client *client,
199 const struct GNUNET_MessageHeader *message)
201 const struct SearchMessage *sm;
202 struct LocalGetContext *lgc;
204 sm = (const struct SearchMessage*) message;
205 GNUNET_SERVER_client_keep (client);
206 lgc = GNUNET_malloc (sizeof (struct LocalGetContext));
207 lgc->client = client;
209 queue_ds_request (&transmit_local_get,
215 transmit_local_get (void *cls,
218 struct LocalGetContext *lgc = cls;
219 // FIXME: search locally
221 GNUNET_assert (GNUNET_OK == ok);
222 GNUNET_SERVER_receive_done (lgc->client,
225 // FIXME: if not found, initiate P2P search
227 // FIXME: once done with "client" handle:
228 GNUNET_SERVER_client_drop (lgc->client);
233 * List of handlers for the messages understood by this
236 static struct GNUNET_SERVER_MessageHandler handlers[] = {
237 {&handle_index_start, NULL,
238 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
239 {&handle_index_list_get, NULL,
240 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
241 {&handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
242 sizeof (struct UnindexMessage) },
243 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
244 sizeof (struct SearchMessage) },
250 * Task run during shutdown.
256 shutdown_task (void *cls,
257 const struct GNUNET_SCHEDULER_TaskContext *tc)
259 GNUNET_DATASTORE_disconnect (dsh,
266 * Process fs requests.
269 * @param sched scheduler to use
270 * @param server the initialized server
271 * @param cfg configuration to use
275 struct GNUNET_SCHEDULER_Handle *sched,
276 struct GNUNET_SERVER_Handle *server,
277 const struct GNUNET_CONFIGURATION_Handle *cfg)
279 dsh = GNUNET_DATASTORE_connect (cfg,
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 _("Failed to connect to datastore service.\n"));
287 GNUNET_SERVER_add_handlers (server, handlers);
288 // FIXME: also handle P2P messages!
290 GNUNET_SCHEDULER_add_delayed (sched,
292 GNUNET_SCHEDULER_PRIORITY_IDLE,
293 GNUNET_SCHEDULER_NO_TASK,
294 GNUNET_TIME_UNIT_FOREVER_REL,
301 * The main function for the fs service.
303 * @param argc number of arguments from the command line
304 * @param argv command line arguments
305 * @return 0 ok, 1 on error
308 main (int argc, char *const *argv)
311 GNUNET_SERVICE_run (argc,
313 "fs", &run, NULL, NULL, NULL)) ? 0 : 1;
316 /* end of gnunet-service-fs.c */