adding basic declarations for datastore implementation
[oweals/gnunet.git] / src / datastore / gnunet-service-datastore.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2005, 2006, 2007, 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 datastore/gnunet-service-datastore.c
23  * @brief Management for the datastore for files stored on a GNUnet node
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "plugin_datastore.h"
31 #include "datastore.h"
32
33
34 /**
35  * Our datastore plugin.
36  */
37 struct DatastorePlugin
38 {
39
40   /**
41    * API of the transport as returned by the plugin's
42    * initialization function.
43    */
44   struct GNUNET_DATSTORE_PluginFunctions *api;
45
46   /**
47    * Short name for the plugin (i.e. "sqlite").
48    */
49   char *short_name;
50
51   /**
52    * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
53    */
54   char *lib_name;
55
56   /**
57    * Environment this transport service is using
58    * for this plugin.
59    */
60   struct GNUNET_DATASTORE_PluginEnvironment env;
61
62 };
63
64
65 /**
66  * Our datastore plugin (NULL if not available).
67  */
68 static struct DatastorePlugin *plugin;
69
70
71 /**
72  * List of handlers for the messages understood by this
73  * service.
74  */
75 static struct GNUNET_SERVER_MessageHandler handlers[] = {
76   /*  {&handle_xxx, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_XXX, 0}, */
77   {NULL, NULL, 0, 0}
78 };
79
80
81
82 /**
83  * Load the datastore plugin.
84  */
85 static struct DatastorePlugin *
86 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
87              struct GNUNET_SCHEDULER_Handle *sched)
88 {
89   struct DatastorePlugin *ret;
90   char *libname;
91   char *name;
92
93   if (GNUNET_OK !=
94       GNUNET_CONFIGURATION_get_value_string (cfg,
95                                              "DATASTORE", "DATABASE", &name))
96     {
97       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98                   _("No `%s' specified for `%s' in configuration!\n"),
99                   "DATABASE",
100                   "DATASTORE");
101       return NULL;
102     }
103   ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
104   ret->env.cfg = cfg;
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)
113     {
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);
118       GNUNET_free (ret);
119       return NULL;
120     }
121   return ret;
122 }
123
124
125 /**
126  * Function called when the service shuts
127  * down.  Unloads our datastore plugin.
128  *
129  * @param cls closure
130  * @param cfg configuration to use
131  */
132 static void
133 unload_plugin (struct DatastorePlugin *plug)
134 {
135 #if DEBUG_DATASTORE
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137               "Datastore service is unloading plugin...\n");
138 #endif
139   GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
140   GNUNET_free (plug->lib_name);
141   GNUNET_free (plug->short_name);
142   GNUNET_free (plug);
143 }
144
145
146 /**
147  * Last task run during shutdown.  Disconnects us from
148  * the transport and core.
149  */
150 static void
151 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 {
153   unload_plugin (plugin);
154   plugin = NULL;
155 }
156
157
158 /**
159  * Process datastore requests.
160  *
161  * @param cls closure
162  * @param sched scheduler to use
163  * @param server the initialized server
164  * @param cfg configuration to use
165  */
166 static void
167 run (void *cls,
168      struct GNUNET_SCHEDULER_Handle *sched,
169      struct GNUNET_SERVER_Handle *server,
170      struct GNUNET_CONFIGURATION_Handle *cfg)
171 {
172   plugin = load_plugin (cfg, sched);
173   if (NULL == plugin)
174     return;
175   GNUNET_SERVER_add_handlers (server, handlers);
176   GNUNET_SCHEDULER_add_delayed (sched,
177                                 GNUNET_YES,
178                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
179                                 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
180                                 GNUNET_TIME_UNIT_FOREVER_REL,
181                                 &cleaning_task, NULL);
182 }
183
184
185 /**
186  * The main function for the datastore service.
187  *
188  * @param argc number of arguments from the command line
189  * @param argv command line arguments
190  * @return 0 ok, 1 on error
191  */
192 int
193 main (int argc, char *const *argv)
194 {
195   int ret;
196
197   ret = (GNUNET_OK ==
198          GNUNET_SERVICE_run (argc,
199                              argv,
200                              "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
201   return ret;
202 }
203
204
205 /* end of gnunet-service-datastore.c */