-fixing includex for xdht
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_datacache.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 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 dht/gnunet-service-xdht_datacache.c
23  * @brief GNUnet DHT service's datacache integration
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27 #include "platform.h"
28 #include "gnunet_datacache_lib.h"
29 #include "gnunet-service-xdht_clients.h"
30 #include "gnunet-service-xdht_datacache.h"
31 #include "gnunet-service-xdht_routing.h"
32 #include "gnunet-service-xdht.h"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
35
36
37 /**
38  * Handle to the datacache service (for inserting/retrieving data)
39  */
40 static struct GNUNET_DATACACHE_Handle *datacache;
41
42
43 /**
44  * Handle a datum we've received from another peer.  Cache if
45  * possible.
46  *
47  * @param expiration when will the reply expire
48  * @param key the query this reply is for
49  * @param put_path_length number of peers in 'put_path'
50  * @param put_path path the reply took on put
51  * @param type type of the reply
52  * @param data_size number of bytes in 'data'
53  * @param data application payload data
54  */
55 void
56 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
57                           const struct GNUNET_HashCode * key,
58                           unsigned int put_path_length,
59                           const struct GNUNET_PeerIdentity *put_path,
60                           enum GNUNET_BLOCK_Type type, size_t data_size,
61                           const void *data)
62 {
63   int r;
64
65   if (NULL == datacache)
66   {
67     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
68                 _("%s request received, but have no datacache!\n"), "PUT");
69     return;
70   }
71   if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
72   {
73     GNUNET_break (0);
74     return;
75   }
76   /* Put size is actual data size plus struct overhead plus path length (if any) */
77   GNUNET_STATISTICS_update (GDS_stats,
78                             gettext_noop ("# ITEMS stored in datacache"), 1,
79                             GNUNET_NO);
80   r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
81                             put_path_length, put_path);
82   LOG (GNUNET_ERROR_TYPE_DEBUG,
83        "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
84        GNUNET_h2s (key), data_size, r, put_path_length);
85 }
86
87
88 /**
89  * Context containing information about a GET request.
90  */
91 struct GetRequestContext
92 {
93   /**
94    * extended query (see gnunet_block_lib.h).
95    */
96   const void *xquery;
97
98   /**
99    * Bloomfilter to filter out duplicate replies (updated)
100    */
101   struct GNUNET_CONTAINER_BloomFilter **reply_bf;
102
103   /**
104    * The key this request was about
105    */
106   struct GNUNET_HashCode key;
107
108   /**
109    * Number of bytes in xquery.
110    */
111   size_t xquery_size;
112
113   /**
114    * Mutator value for the reply_bf, see gnunet_block_lib.h
115    */
116   uint32_t reply_bf_mutator;
117
118   /**
119    * Return value to give back.
120    */
121   enum GNUNET_BLOCK_EvaluationResult eval;
122 };
123
124
125 /**
126  * Iterator for local get request results,
127  *
128  * @param cls closure for iterator, a DatacacheGetContext
129  * @param exp when does this value expire?
130  * @param key the key this data is stored under
131  * @param size the size of the data identified by key
132  * @param data the actual data
133  * @param type the type of the data
134  * @param put_path_length number of peers in 'put_path'
135  * @param put_path path the reply took on put
136  * @return GNUNET_OK to continue iteration, anything else
137  * to stop iteration.
138  */
139 static int
140 datacache_get_iterator (void *cls,
141                         const struct GNUNET_HashCode * key, size_t size,
142                         const char *data, enum GNUNET_BLOCK_Type type,
143                         struct GNUNET_TIME_Absolute exp,
144                         unsigned int put_path_length,
145                         const struct GNUNET_PeerIdentity *put_path)
146 {
147   struct GetRequestContext *ctx = cls;
148   enum GNUNET_BLOCK_EvaluationResult eval;
149
150   eval =
151       GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
152                              ctx->reply_bf_mutator, ctx->xquery,
153                              ctx->xquery_size, data, size);
154   LOG (GNUNET_ERROR_TYPE_DEBUG,
155        "Found reply for query %s in datacache, evaluation result is %d\n",
156        GNUNET_h2s (key), (int) eval);
157   ctx->eval = eval;
158   switch (eval)
159   {
160   case GNUNET_BLOCK_EVALUATION_OK_MORE:
161   case GNUNET_BLOCK_EVALUATION_OK_LAST:
162     /* forward to local clients */
163     GNUNET_STATISTICS_update (GDS_stats,
164                               gettext_noop
165                               ("# Good RESULTS found in datacache"), 1,
166                               GNUNET_NO);
167     GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path,
168                               type, size, data);
169     /* forward to other peers */
170     GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL,
171                          data, size);
172     break;
173   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
174     GNUNET_STATISTICS_update (GDS_stats,
175                               gettext_noop
176                               ("# Duplicate RESULTS found in datacache"), 1,
177                               GNUNET_NO);
178     break;
179   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
180     GNUNET_STATISTICS_update (GDS_stats,
181                               gettext_noop
182                               ("# Invalid RESULTS found in datacache"), 1,
183                               GNUNET_NO);
184     break;
185   case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
186     GNUNET_STATISTICS_update (GDS_stats,
187                               gettext_noop
188                               ("# Irrelevant RESULTS found in datacache"), 1,
189                               GNUNET_NO);
190     break;
191   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
192     GNUNET_break (0);
193     break;
194   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
195     GNUNET_break_op (0);
196     return GNUNET_SYSERR;
197   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
198     GNUNET_STATISTICS_update (GDS_stats,
199                               gettext_noop
200                               ("# Unsupported RESULTS found in datacache"), 1,
201                               GNUNET_NO);
202     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
203                 _("Unsupported block type (%u) in local response!\n"), type);
204     break;
205   }
206   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
207 }
208
209
210 /**
211  * Handle a GET request we've received from another peer.
212  *
213  * @param key the query
214  * @param type requested data type
215  * @param xquery extended query
216  * @param xquery_size number of bytes in xquery
217  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
218  * @param reply_bf_mutator mutation value for reply_bf
219  * @return evaluation result for the local replies
220  */
221 enum GNUNET_BLOCK_EvaluationResult
222 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
223                           enum GNUNET_BLOCK_Type type, const void *xquery,
224                           size_t xquery_size,
225                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
226                           uint32_t reply_bf_mutator)
227 {
228   struct GetRequestContext ctx;
229   unsigned int r;
230
231   if (datacache == NULL)
232     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
233   GNUNET_STATISTICS_update (GDS_stats,
234                             gettext_noop ("# GET requests given to datacache"),
235                             1, GNUNET_NO);
236   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
237   ctx.key = *key;
238   ctx.xquery = xquery;
239   ctx.xquery_size = xquery_size;
240   ctx.reply_bf = reply_bf;
241   ctx.reply_bf_mutator = reply_bf_mutator;
242   r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
243                             &ctx);
244   LOG (GNUNET_ERROR_TYPE_DEBUG,
245        "DATACACHE GET for key %s completed (%d). %u results found.\n",
246        GNUNET_h2s (key), ctx.eval, r);
247   return ctx.eval;
248 }
249
250
251 /**
252  * Initialize datacache subsystem.
253  */
254 void
255 GDS_DATACACHE_init ()
256 {
257   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
258 }
259
260
261 /**
262  * Shutdown datacache subsystem.
263  */
264 void
265 GDS_DATACACHE_done ()
266 {
267   if (datacache != NULL)
268   {
269     GNUNET_DATACACHE_destroy (datacache);
270     datacache = NULL;
271   }
272 }
273
274
275 /* end of gnunet-service-dht_datacache.c */