aboutsummaryrefslogtreecommitdiff
path: root/src/psycstore/gnunet-service-psycstore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/psycstore/gnunet-service-psycstore.c')
-rw-r--r--src/psycstore/gnunet-service-psycstore.c97
1 files changed, 61 insertions, 36 deletions
diff --git a/src/psycstore/gnunet-service-psycstore.c b/src/psycstore/gnunet-service-psycstore.c
index 7d27ea29b..87a2c87ab 100644
--- a/src/psycstore/gnunet-service-psycstore.c
+++ b/src/psycstore/gnunet-service-psycstore.c
@@ -25,6 +25,8 @@
25 * @author Christian Grothoff 25 * @author Christian Grothoff
26 */ 26 */
27 27
28#include <inttypes.h>
29
28#include "platform.h" 30#include "platform.h"
29#include "gnunet_util_lib.h" 31#include "gnunet_util_lib.h"
30#include "gnunet_constants.h" 32#include "gnunet_constants.h"
@@ -89,39 +91,41 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89/** 91/**
90 * Send a result code back to the client. 92 * Send a result code back to the client.
91 * 93 *
92 * @param client Client that should receive the result code. 94 * @param client
93 * @param result_code Code to transmit. 95 * Client that should receive the result code.
94 * @param op_id Operation ID. 96 * @param result_code
95 * @param err_msg Error message to include (or NULL for none). 97 * Code to transmit.
98 * @param op_id
99 * Operation ID in network byte order.
100 * @param err_msg
101 * Error message to include (or NULL for none).
96 */ 102 */
97static void 103static void
98send_result_code (struct GNUNET_SERVER_Client *client, uint32_t result_code, 104send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
99 uint32_t op_id, const char *err_msg) 105 int64_t result_code, const char *err_msg)
100{ 106{
101 struct OperationResult *res; 107 struct OperationResult *res;
102 size_t err_len; 108 size_t err_len = 0; // FIXME: maximum length
103 109
104 if (NULL == err_msg) 110 if (NULL != err_msg)
105 err_len = 0;
106 else
107 err_len = strlen (err_msg) + 1; 111 err_len = strlen (err_msg) + 1;
108 res = GNUNET_malloc (sizeof (struct OperationResult) + err_len); 112 res = GNUNET_malloc (sizeof (struct OperationResult) + err_len);
109 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE); 113 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
110 res->header.size = htons (sizeof (struct OperationResult) + err_len); 114 res->header.size = htons (sizeof (struct OperationResult) + err_len);
111 res->result_code = htonl (result_code); 115 res->result_code = GNUNET_htonll (result_code - INT64_MIN);
112 res->op_id = op_id; 116 res->op_id = op_id;
113 if (0 < err_len) 117 if (0 < err_len)
114 memcpy (&res[1], err_msg, err_len); 118 memcpy (&res[1], err_msg, err_len);
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Sending result %d (%s) to client\n", 120 "Sending result to client: %" PRId64 " (%s)\n",
117 (int) result_code, 121 result_code, err_msg);
118 err_msg);
119 GNUNET_SERVER_notification_context_add (nc, client); 122 GNUNET_SERVER_notification_context_add (nc, client);
120 GNUNET_SERVER_notification_context_unicast (nc, client, &res->header, 123 GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
121 GNUNET_NO); 124 GNUNET_NO);
122 GNUNET_free (res); 125 GNUNET_free (res);
123} 126}
124 127
128
125enum 129enum
126{ 130{
127 MEMBERSHIP_TEST_NOT_NEEDED = 0, 131 MEMBERSHIP_TEST_NOT_NEEDED = 0,
@@ -129,6 +133,7 @@ enum
129 MEMBERSHIP_TEST_DONE = 2, 133 MEMBERSHIP_TEST_DONE = 2,
130} MessageMembershipTest; 134} MessageMembershipTest;
131 135
136
132struct SendClosure 137struct SendClosure
133{ 138{
134 struct GNUNET_SERVER_Client *client; 139 struct GNUNET_SERVER_Client *client;
@@ -158,7 +163,6 @@ struct SendClosure
158 * @see enum MessageMembershipTest 163 * @see enum MessageMembershipTest
159 */ 164 */
160 uint8_t membership_test; 165 uint8_t membership_test;
161
162}; 166};
163 167
164 168
@@ -214,6 +218,8 @@ send_state_var (void *cls, const char *name,
214 struct StateResult *res; 218 struct StateResult *res;
215 size_t name_size = strlen (name) + 1; 219 size_t name_size = strlen (name) + 1;
216 220
221 /* FIXME: split up value into 64k chunks */
222
217 res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size); 223 res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size);
218 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE); 224 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
219 res->header.size = htons (sizeof (struct StateResult) + name_size + value_size); 225 res->header.size = htons (sizeof (struct StateResult) + name_size + value_size);
@@ -249,7 +255,7 @@ handle_membership_store (void *cls,
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Failed to store membership information!\n")); 256 _("Failed to store membership information!\n"));
251 257
252 send_result_code (client, ret, req->op_id, NULL); 258 send_result_code (client, req->op_id, ret, NULL);
253 GNUNET_SERVER_receive_done (client, GNUNET_OK); 259 GNUNET_SERVER_receive_done (client, GNUNET_OK);
254} 260}
255 261
@@ -274,7 +280,7 @@ handle_membership_test (void *cls,
274 _("Failed to test membership!\n")); 280 _("Failed to test membership!\n"));
275 } 281 }
276 282
277 send_result_code (client, ret, req->op_id, NULL); 283 send_result_code (client, req->op_id, ret, NULL);
278 GNUNET_SERVER_receive_done (client, GNUNET_OK); 284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
279} 285}
280 286
@@ -295,7 +301,7 @@ handle_fragment_store (void *cls,
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 _("Failed to store fragment!\n")); 302 _("Failed to store fragment!\n"));
297 303
298 send_result_code (client, ret, req->op_id, NULL); 304 send_result_code (client, req->op_id, ret, NULL);
299 GNUNET_SERVER_receive_done (client, GNUNET_OK); 305 GNUNET_SERVER_receive_done (client, GNUNET_OK);
300} 306}
301 307
@@ -312,9 +318,20 @@ handle_fragment_get (void *cls,
312 .channel_key = req->channel_key, .slave_key = req->slave_key, 318 .channel_key = req->channel_key, .slave_key = req->slave_key,
313 .membership_test = req->do_membership_test }; 319 .membership_test = req->do_membership_test };
314 320
315 int ret = db->fragment_get (db->cls, &req->channel_key, 321 int64_t ret;
316 GNUNET_ntohll (req->fragment_id), 322 uint64_t ret_frags = 0;
317 &send_fragment, &sc); 323 uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
324 uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
325 uint64_t limit = GNUNET_ntohll (req->fragment_limit);
326
327 if (0 == limit)
328 ret = db->fragment_get (db->cls, &req->channel_key,
329 first_fragment_id, last_fragment_id,
330 &ret_frags, &send_fragment, &sc);
331 else
332 ret = db->fragment_get_latest (db->cls, &req->channel_key, limit,
333 &ret_frags, &send_fragment, &sc);
334
318 switch (ret) 335 switch (ret)
319 { 336 {
320 case GNUNET_YES: 337 case GNUNET_YES:
@@ -340,8 +357,7 @@ handle_fragment_get (void *cls,
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 _("Failed to get fragment!\n")); 358 _("Failed to get fragment!\n"));
342 } 359 }
343 360 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
344 send_result_code (client, ret, req->op_id, NULL);
345 GNUNET_SERVER_receive_done (client, GNUNET_OK); 361 GNUNET_SERVER_receive_done (client, GNUNET_OK);
346} 362}
347 363
@@ -358,22 +374,31 @@ handle_message_get (void *cls,
358 .channel_key = req->channel_key, .slave_key = req->slave_key, 374 .channel_key = req->channel_key, .slave_key = req->slave_key,
359 .membership_test = req->do_membership_test }; 375 .membership_test = req->do_membership_test };
360 376
377 int64_t ret;
361 uint64_t ret_frags = 0; 378 uint64_t ret_frags = 0;
362 int64_t ret = db->message_get (db->cls, &req->channel_key, 379 uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
363 GNUNET_ntohll (req->message_id), 380 uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
364 &ret_frags, &send_fragment, &sc); 381 uint64_t limit = GNUNET_ntohll (req->message_limit);
382
383 if (0 == limit)
384 ret = db->message_get (db->cls, &req->channel_key,
385 first_message_id, last_message_id,
386 &ret_frags, &send_fragment, &sc);
387 else
388 ret = db->message_get_latest (db->cls, &req->channel_key, limit,
389 &ret_frags, &send_fragment, &sc);
390
365 switch (ret) 391 switch (ret)
366 { 392 {
367 case GNUNET_YES: 393 case GNUNET_YES:
368 case GNUNET_NO: 394 case GNUNET_NO:
369 break; 395 break;
370 default: 396 default:
371 ret_frags = ret;
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 _("Failed to get message!\n")); 398 _("Failed to get message!\n"));
374 } 399 }
375 400
376 send_result_code (client, ret_frags, req->op_id, NULL); 401 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
377 GNUNET_SERVER_receive_done (client, GNUNET_OK); 402 GNUNET_SERVER_receive_done (client, GNUNET_OK);
378} 403}
379 404
@@ -404,7 +429,7 @@ handle_message_get_fragment (void *cls,
404 _("Failed to get message fragment!\n")); 429 _("Failed to get message fragment!\n"));
405 } 430 }
406 431
407 send_result_code (client, ret, req->op_id, NULL); 432 send_result_code (client, req->op_id, ret, NULL);
408 GNUNET_SERVER_receive_done (client, GNUNET_OK); 433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
409} 434}
410 435
@@ -434,7 +459,7 @@ handle_counters_get (void *cls,
434 459
435 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS); 460 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
436 res.header.size = htons (sizeof (res)); 461 res.header.size = htons (sizeof (res));
437 res.result_code = htonl (ret); 462 res.result_code = htonl (ret - INT32_MIN);
438 res.op_id = req->op_id; 463 res.op_id = req->op_id;
439 res.max_fragment_id = GNUNET_htonll (res.max_fragment_id); 464 res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
440 res.max_message_id = GNUNET_htonll (res.max_message_id); 465 res.max_message_id = GNUNET_htonll (res.max_message_id);
@@ -517,7 +542,7 @@ handle_state_modify (void *cls,
517 _("Failed to end modifying state!\n")); 542 _("Failed to end modifying state!\n"));
518 } 543 }
519 } 544 }
520 send_result_code (client, ret, req->op_id, NULL); 545 send_result_code (client, req->op_id, ret, NULL);
521 GNUNET_SERVER_receive_done (client, GNUNET_OK); 546 GNUNET_SERVER_receive_done (client, GNUNET_OK);
522} 547}
523 548
@@ -571,7 +596,7 @@ handle_state_sync (void *cls,
571 _("Failed to end synchronizing state!\n")); 596 _("Failed to end synchronizing state!\n"));
572 } 597 }
573 } 598 }
574 send_result_code (client, ret, req->op_id, NULL); 599 send_result_code (client, req->op_id, ret, NULL);
575 GNUNET_SERVER_receive_done (client, GNUNET_OK); 600 GNUNET_SERVER_receive_done (client, GNUNET_OK);
576} 601}
577 602
@@ -590,7 +615,7 @@ handle_state_reset (void *cls,
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 _("Failed to reset state!\n")); 616 _("Failed to reset state!\n"));
592 617
593 send_result_code (client, ret, req->op_id, NULL); 618 send_result_code (client, req->op_id, ret, NULL);
594 GNUNET_SERVER_receive_done (client, GNUNET_OK); 619 GNUNET_SERVER_receive_done (client, GNUNET_OK);
595} 620}
596 621
@@ -609,7 +634,7 @@ handle_state_hash_update (void *cls,
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610 _("Failed to reset state!\n")); 635 _("Failed to reset state!\n"));
611 636
612 send_result_code (client, ret, req->op_id, NULL); 637 send_result_code (client, req->op_id, ret, NULL);
613 GNUNET_SERVER_receive_done (client, GNUNET_OK); 638 GNUNET_SERVER_receive_done (client, GNUNET_OK);
614} 639}
615 640
@@ -660,7 +685,7 @@ handle_state_get (void *cls,
660 _("Failed to get state variable!\n")); 685 _("Failed to get state variable!\n"));
661 } 686 }
662 687
663 send_result_code (client, ret, req->op_id, NULL); 688 send_result_code (client, req->op_id, ret, NULL);
664 GNUNET_SERVER_receive_done (client, GNUNET_OK); 689 GNUNET_SERVER_receive_done (client, GNUNET_OK);
665} 690}
666 691
@@ -699,7 +724,7 @@ handle_state_get_prefix (void *cls,
699 _("Failed to get state variable!\n")); 724 _("Failed to get state variable!\n"));
700 } 725 }
701 726
702 send_result_code (client, ret, req->op_id, NULL); 727 send_result_code (client, req->op_id, ret, NULL);
703 GNUNET_SERVER_receive_done (client, GNUNET_OK); 728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
704} 729}
705 730