2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 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 datastore/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "plugin_datastore.h"
31 #include "datastore.h"
35 * Our datastore plugin.
37 struct DatastorePlugin
41 * API of the transport as returned by the plugin's
42 * initialization function.
44 struct GNUNET_DATSTORE_PluginFunctions *api;
47 * Short name for the plugin (i.e. "sqlite").
52 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
57 * Environment this transport service is using
60 struct GNUNET_DATASTORE_PluginEnvironment env;
66 * Our datastore plugin (NULL if not available).
68 static struct DatastorePlugin *plugin;
72 * List of handlers for the messages understood by this
75 static struct GNUNET_SERVER_MessageHandler handlers[] = {
76 /* {&handle_xxx, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_XXX, 0}, */
83 * Load the datastore plugin.
85 static struct DatastorePlugin *
86 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
87 struct GNUNET_SCHEDULER_Handle *sched)
89 struct DatastorePlugin *ret;
94 GNUNET_CONFIGURATION_get_value_string (cfg,
95 "DATASTORE", "DATABASE", &name))
97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 _("No `%s' specified for `%s' in configuration!\n"),
103 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
105 ret->env.sched = sched;
106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
107 _("Loading `%s' datastore plugin\n"), name);
108 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
109 ret->short_name = GNUNET_strdup (name);
110 ret->lib_name = libname;
111 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
112 if (ret->api == NULL)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 _("Failed to load datastore plugin for `%s'\n"), name);
116 GNUNET_free (ret->short_name);
117 GNUNET_free (libname);
126 * Function called when the service shuts
127 * down. Unloads our datastore plugin.
130 * @param cfg configuration to use
133 unload_plugin (struct DatastorePlugin *plug)
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Datastore service is unloading plugin...\n");
139 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
140 GNUNET_free (plug->lib_name);
141 GNUNET_free (plug->short_name);
147 * Last task run during shutdown. Disconnects us from
148 * the transport and core.
151 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 unload_plugin (plugin);
159 * Process datastore requests.
162 * @param sched scheduler to use
163 * @param server the initialized server
164 * @param cfg configuration to use
168 struct GNUNET_SCHEDULER_Handle *sched,
169 struct GNUNET_SERVER_Handle *server,
170 struct GNUNET_CONFIGURATION_Handle *cfg)
172 plugin = load_plugin (cfg, sched);
175 GNUNET_SERVER_add_handlers (server, handlers);
176 GNUNET_SCHEDULER_add_delayed (sched,
178 GNUNET_SCHEDULER_PRIORITY_IDLE,
179 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
180 GNUNET_TIME_UNIT_FOREVER_REL,
181 &cleaning_task, NULL);
186 * The main function for the datastore service.
188 * @param argc number of arguments from the command line
189 * @param argv command line arguments
190 * @return 0 ok, 1 on error
193 main (int argc, char *const *argv)
198 GNUNET_SERVICE_run (argc,
200 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
205 /* end of gnunet-service-datastore.c */