diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-06-16 20:32:40 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-06-16 20:32:40 +0000 |
commit | adb7f45adb8eff27515519f05042fc6ec616b38e (patch) | |
tree | 46e25c1b0659d125a2efae82fdcd5404c70ce2ef /src/datastore/gnunet-service-datastore.c | |
parent | dc6e9887c24209d0e3dfb6cfb30fbde6ae3b47e9 (diff) | |
download | gnunet-adb7f45adb8eff27515519f05042fc6ec616b38e.tar.gz gnunet-adb7f45adb8eff27515519f05042fc6ec616b38e.zip |
improving datastore API
Diffstat (limited to 'src/datastore/gnunet-service-datastore.c')
-rw-r--r-- | src/datastore/gnunet-service-datastore.c | 136 |
1 files changed, 107 insertions, 29 deletions
diff --git a/src/datastore/gnunet-service-datastore.c b/src/datastore/gnunet-service-datastore.c index fe7987632..db3524557 100644 --- a/src/datastore/gnunet-service-datastore.c +++ b/src/datastore/gnunet-service-datastore.c | |||
@@ -92,18 +92,28 @@ transmit (struct GNUNET_SERVER_Client *client, | |||
92 | 92 | ||
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Transmit the size of the current datastore to the client. | 95 | * Transmit a status code to the client. |
96 | * | ||
97 | * @param client receiver of the response | ||
98 | * @param code status code | ||
99 | * @param msg optional error message (can be NULL) | ||
96 | */ | 100 | */ |
97 | static void | 101 | static void |
98 | transmit_size (struct GNUNET_SERVER_Client *client) | 102 | transmit_status (struct GNUNET_SERVER_Client *client, |
103 | int code, | ||
104 | const char *msg) | ||
99 | { | 105 | { |
100 | struct SizeMessage sm; | 106 | struct StatusMessage *sm; |
101 | 107 | size_t slen; | |
102 | sm.header.size = htons(sizeof(struct SizeMessage)); | 108 | |
103 | sm.header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_SIZE); | 109 | slen = (msg == NULL) ? 0 : strlen(msg) + 1; |
104 | sm.reserved = htonl(0); | 110 | sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen); |
105 | sm.size = GNUNET_htonll(plugin->api->get_size (plugin->api->cls)); | 111 | sm->header.size = htons(sizeof(struct StatusMessage) + slen); |
106 | transmit (client, &sm.header); | 112 | sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS); |
113 | sm->status = htonl(code); | ||
114 | memcpy (&sm[1], msg, slen); | ||
115 | transmit (client, &sm->header); | ||
116 | GNUNET_free (sm); | ||
107 | } | 117 | } |
108 | 118 | ||
109 | 119 | ||
@@ -148,16 +158,16 @@ transmit_item (void *cls, | |||
148 | transmit (client, &end); | 158 | transmit (client, &end); |
149 | return GNUNET_OK; | 159 | return GNUNET_OK; |
150 | } | 160 | } |
151 | /* FIXME: make use of 'uid' for efficient priority/expiration update! */ | ||
152 | dm = GNUNET_malloc (sizeof(struct DataMessage) + size); | 161 | dm = GNUNET_malloc (sizeof(struct DataMessage) + size); |
153 | dm->header.size = htons(sizeof(struct DataMessage) + size); | 162 | dm->header.size = htons(sizeof(struct DataMessage) + size); |
154 | dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA); | 163 | dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA); |
155 | dm->reserved = htonl(0); | 164 | dm->rid = htonl(0); |
156 | dm->size = htonl(size); | 165 | dm->size = htonl(size); |
157 | dm->type = htonl(type); | 166 | dm->type = htonl(type); |
158 | dm->priority = htonl(priority); | 167 | dm->priority = htonl(priority); |
159 | dm->anonymity = htonl(anonymity); | 168 | dm->anonymity = htonl(anonymity); |
160 | dm->expiration = GNUNET_TIME_absolute_hton(expiration); | 169 | dm->expiration = GNUNET_TIME_absolute_hton(expiration); |
170 | dm->uid = GNUNET_htonll(uid); | ||
161 | dm->key = *key; | 171 | dm->key = *key; |
162 | memcpy (&dm[1], data, size); | 172 | memcpy (&dm[1], data, size); |
163 | transmit (client, &dm->header); | 173 | transmit (client, &dm->header); |
@@ -167,18 +177,35 @@ transmit_item (void *cls, | |||
167 | 177 | ||
168 | 178 | ||
169 | /** | 179 | /** |
170 | * Handle INIT-message. | 180 | * Handle RESERVE-message. |
171 | * | 181 | * |
172 | * @param cls closure | 182 | * @param cls closure |
173 | * @param client identification of the client | 183 | * @param client identification of the client |
174 | * @param message the actual message | 184 | * @param message the actual message |
175 | */ | 185 | */ |
176 | static void | 186 | static void |
177 | handle_init (void *cls, | 187 | handle_reserve (void *cls, |
178 | struct GNUNET_SERVER_Client *client, | 188 | struct GNUNET_SERVER_Client *client, |
179 | const struct GNUNET_MessageHeader *message) | 189 | const struct GNUNET_MessageHeader *message) |
180 | { | 190 | { |
181 | transmit_size (client); | 191 | transmit_status (client, GNUNET_SYSERR, "not implemented"); |
192 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Handle RELEASE_RESERVE-message. | ||
198 | * | ||
199 | * @param cls closure | ||
200 | * @param client identification of the client | ||
201 | * @param message the actual message | ||
202 | */ | ||
203 | static void | ||
204 | handle_release_reserve (void *cls, | ||
205 | struct GNUNET_SERVER_Client *client, | ||
206 | const struct GNUNET_MessageHeader *message) | ||
207 | { | ||
208 | transmit_status (client, GNUNET_SYSERR, "not implemented"); | ||
182 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 209 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
183 | } | 210 | } |
184 | 211 | ||
@@ -208,8 +235,7 @@ check_data (const struct GNUNET_MessageHeader *message) | |||
208 | GNUNET_break (0); | 235 | GNUNET_break (0); |
209 | return NULL; | 236 | return NULL; |
210 | } | 237 | } |
211 | if ( (ntohl(dm->type) == 0) || | 238 | if (ntohl(dm->type) == 0) |
212 | (ntohl(dm->reserved) != 0) ) | ||
213 | { | 239 | { |
214 | GNUNET_break (0); | 240 | GNUNET_break (0); |
215 | return NULL; | 241 | return NULL; |
@@ -231,21 +257,33 @@ handle_put (void *cls, | |||
231 | const struct GNUNET_MessageHeader *message) | 257 | const struct GNUNET_MessageHeader *message) |
232 | { | 258 | { |
233 | const struct DataMessage *dm = check_data (message); | 259 | const struct DataMessage *dm = check_data (message); |
260 | char *msg; | ||
261 | int ret; | ||
262 | int rid; | ||
263 | |||
234 | if (dm == NULL) | 264 | if (dm == NULL) |
235 | { | 265 | { |
236 | GNUNET_break (0); | 266 | GNUNET_break (0); |
237 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 267 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
238 | return; | 268 | return; |
239 | } | 269 | } |
240 | plugin->api->put (plugin->api->cls, | 270 | rid = ntohl(dm->rid); |
241 | &dm->key, | 271 | if (rid > 0) |
242 | ntohl(dm->size), | 272 | { |
243 | &dm[1], | 273 | /* FIXME: find reservation, update remaining! */ |
244 | ntohl(dm->type), | 274 | } |
245 | ntohl(dm->priority), | 275 | msg = NULL; |
246 | ntohl(dm->anonymity), | 276 | ret = plugin->api->put (plugin->api->cls, |
247 | GNUNET_TIME_absolute_ntoh(dm->expiration)); | 277 | &dm->key, |
248 | transmit_size (client); | 278 | ntohl(dm->size), |
279 | &dm[1], | ||
280 | ntohl(dm->type), | ||
281 | ntohl(dm->priority), | ||
282 | ntohl(dm->anonymity), | ||
283 | GNUNET_TIME_absolute_ntoh(dm->expiration), | ||
284 | &msg); | ||
285 | transmit_status (client, ret, msg); | ||
286 | GNUNET_free_non_null (msg); | ||
249 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 287 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
250 | } | 288 | } |
251 | 289 | ||
@@ -285,6 +323,35 @@ handle_get (void *cls, | |||
285 | 323 | ||
286 | 324 | ||
287 | /** | 325 | /** |
326 | * Handle UPDATE-message. | ||
327 | * | ||
328 | * @param cls closure | ||
329 | * @param client identification of the client | ||
330 | * @param message the actual message | ||
331 | */ | ||
332 | static void | ||
333 | handle_update (void *cls, | ||
334 | struct GNUNET_SERVER_Client *client, | ||
335 | const struct GNUNET_MessageHeader *message) | ||
336 | { | ||
337 | const struct UpdateMessage *msg; | ||
338 | int ret; | ||
339 | char *emsg; | ||
340 | |||
341 | msg = (const struct UpdateMessage*) message; | ||
342 | emsg = NULL; | ||
343 | ret = plugin->api->update (plugin->api->cls, | ||
344 | GNUNET_ntohll(msg->uid), | ||
345 | (int32_t) ntohl(msg->priority), | ||
346 | GNUNET_TIME_absolute_ntoh(msg->expiration), | ||
347 | &emsg); | ||
348 | transmit_status (client, ret, emsg); | ||
349 | GNUNET_free_non_null (emsg); | ||
350 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
288 | * Handle GET_RANDOM-message. | 355 | * Handle GET_RANDOM-message. |
289 | * | 356 | * |
290 | * @param cls closure | 357 | * @param cls closure |
@@ -319,6 +386,8 @@ remove_callback (void *cls, | |||
319 | struct GNUNET_TIME_Absolute | 386 | struct GNUNET_TIME_Absolute |
320 | expiration, unsigned long long uid) | 387 | expiration, unsigned long long uid) |
321 | { | 388 | { |
389 | int *found = cls; | ||
390 | *found = GNUNET_YES; | ||
322 | return GNUNET_NO; | 391 | return GNUNET_NO; |
323 | } | 392 | } |
324 | 393 | ||
@@ -337,6 +406,7 @@ handle_remove (void *cls, | |||
337 | { | 406 | { |
338 | const struct DataMessage *dm = check_data (message); | 407 | const struct DataMessage *dm = check_data (message); |
339 | GNUNET_HashCode vhash; | 408 | GNUNET_HashCode vhash; |
409 | int found; | ||
340 | 410 | ||
341 | if (dm == NULL) | 411 | if (dm == NULL) |
342 | { | 412 | { |
@@ -344,6 +414,7 @@ handle_remove (void *cls, | |||
344 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 414 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
345 | return; | 415 | return; |
346 | } | 416 | } |
417 | found = GNUNET_NO; | ||
347 | GNUNET_CRYPTO_hash (&dm[1], | 418 | GNUNET_CRYPTO_hash (&dm[1], |
348 | ntohl(dm->size), | 419 | ntohl(dm->size), |
349 | &vhash); | 420 | &vhash); |
@@ -352,8 +423,11 @@ handle_remove (void *cls, | |||
352 | &vhash, | 423 | &vhash, |
353 | ntohl(dm->type), | 424 | ntohl(dm->type), |
354 | &remove_callback, | 425 | &remove_callback, |
355 | NULL); | 426 | &found); |
356 | transmit_size (client); | 427 | if (GNUNET_YES == found) |
428 | transmit_status (client, GNUNET_OK, NULL); | ||
429 | else | ||
430 | transmit_status (client, GNUNET_SYSERR, _("Content not found")); | ||
357 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | 431 | GNUNET_SERVER_receive_done (client, GNUNET_OK); |
358 | } | 432 | } |
359 | 433 | ||
@@ -380,9 +454,13 @@ handle_drop (void *cls, | |||
380 | * service. | 454 | * service. |
381 | */ | 455 | */ |
382 | static struct GNUNET_SERVER_MessageHandler handlers[] = { | 456 | static struct GNUNET_SERVER_MessageHandler handlers[] = { |
383 | {&handle_init, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_INIT, | 457 | {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE, |
384 | sizeof(struct GNUNET_MessageHeader) }, | 458 | sizeof(struct ReserveMessage) }, |
459 | {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE, | ||
460 | sizeof(struct ReleaseReserveMessage) }, | ||
385 | {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 }, | 461 | {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 }, |
462 | {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE, | ||
463 | sizeof (struct UpdateMessage) }, | ||
386 | {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 }, | 464 | {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 }, |
387 | {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM, | 465 | {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM, |
388 | sizeof(struct GNUNET_MessageHeader) }, | 466 | sizeof(struct GNUNET_MessageHeader) }, |