diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-05-17 16:57:49 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-05-17 16:57:49 +0000 |
commit | 7eb684ec9f93d52ebae09729fcf01c16580b5cb9 (patch) | |
tree | 72a44f01de2dc157f47a0a70ccb90d36154ddcbf /src/peerstore | |
parent | 83c058a5ea11b6d7aa05cb71963c6063cb373603 (diff) | |
download | gnunet-7eb684ec9f93d52ebae09729fcf01c16580b5cb9.tar.gz gnunet-7eb684ec9f93d52ebae09729fcf01c16580b5cb9.zip |
peestore: towards iterate functionality
Diffstat (limited to 'src/peerstore')
-rw-r--r-- | src/peerstore/gnunet-service-peerstore.c | 41 | ||||
-rw-r--r-- | src/peerstore/peerstore_api.c | 222 | ||||
-rw-r--r-- | src/peerstore/peerstore_common.c | 14 | ||||
-rw-r--r-- | src/peerstore/peerstore_common.h | 23 | ||||
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 29 | ||||
-rw-r--r-- | src/peerstore/test_peerstore_api.c | 39 |
6 files changed, 317 insertions, 51 deletions
diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c index 50af4342c..fa674e1ad 100644 --- a/src/peerstore/gnunet-service-peerstore.c +++ b/src/peerstore/gnunet-service-peerstore.c | |||
@@ -87,28 +87,24 @@ handle_client_disconnect (void *cls, | |||
87 | * @param sub_system name of the GNUnet sub system responsible | 87 | * @param sub_system name of the GNUnet sub system responsible |
88 | * @param value stored value | 88 | * @param value stored value |
89 | * @param size size of stored value | 89 | * @param size size of stored value |
90 | * | 90 | */ |
91 | int record_iterator(void *cls, | 91 | int record_iterator(void *cls, |
92 | const char *sub_system, | 92 | struct GNUNET_PEERSTORE_Record *record, |
93 | const struct GNUNET_PeerIdentity *peer, | 93 | char *emsg) |
94 | const char *key, | ||
95 | const void *value, | ||
96 | size_t size, | ||
97 | struct GNUNET_TIME_Absolute expiry) | ||
98 | { | 94 | { |
99 | struct GNUNET_SERVER_TransmitContext *tc = cls; | 95 | struct GNUNET_SERVER_TransmitContext *tc = cls; |
100 | struct StoreRecordMessage *srm; | 96 | struct StoreRecordMessage *srm; |
101 | 97 | ||
102 | srm = PEERSTORE_create_record_message(sub_system, | 98 | srm = PEERSTORE_create_record_message(record->sub_system, |
103 | peer, | 99 | record->peer, |
104 | key, | 100 | record->key, |
105 | value, | 101 | record->value, |
106 | size, | 102 | record->value_size, |
107 | expiry, | 103 | record->expiry, |
108 | GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); | 104 | GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD); |
109 | GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm); | 105 | GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm); |
110 | return GNUNET_YES; | 106 | return GNUNET_YES; |
111 | }*/ | 107 | } |
112 | 108 | ||
113 | /** | 109 | /** |
114 | * Handle an iterate request from client | 110 | * Handle an iterate request from client |
@@ -116,7 +112,7 @@ int record_iterator(void *cls, | |||
116 | * @param cls unused | 112 | * @param cls unused |
117 | * @param client identification of the client | 113 | * @param client identification of the client |
118 | * @param message the actual message | 114 | * @param message the actual message |
119 | * | 115 | */ |
120 | void handle_iterate (void *cls, | 116 | void handle_iterate (void *cls, |
121 | struct GNUNET_SERVER_Client *client, | 117 | struct GNUNET_SERVER_Client *client, |
122 | const struct GNUNET_MessageHeader *message) | 118 | const struct GNUNET_MessageHeader *message) |
@@ -145,9 +141,16 @@ void handle_iterate (void *cls, | |||
145 | &record_iterator, | 141 | &record_iterator, |
146 | tc)) | 142 | tc)) |
147 | { | 143 | { |
148 | 144 | GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END); | |
145 | GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); | ||
149 | } | 146 | } |
150 | }*/ | 147 | else |
148 | { | ||
149 | GNUNET_free(tc); | ||
150 | GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); | ||
151 | } | ||
152 | GNUNET_free(record); | ||
153 | } | ||
151 | 154 | ||
152 | /** | 155 | /** |
153 | * Handle a store request from client | 156 | * Handle a store request from client |
@@ -220,7 +223,7 @@ run (void *cls, | |||
220 | { | 223 | { |
221 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | 224 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { |
222 | {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, | 225 | {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, |
223 | // {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, | 226 | {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, |
224 | {NULL, NULL, 0, 0} | 227 | {NULL, NULL, 0, 0} |
225 | }; | 228 | }; |
226 | char *database; | 229 | char *database; |
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index edf506f10..81d6c78a6 100644 --- a/src/peerstore/peerstore_api.c +++ b/src/peerstore/peerstore_api.c | |||
@@ -65,6 +65,16 @@ struct GNUNET_PEERSTORE_Handle | |||
65 | */ | 65 | */ |
66 | struct GNUNET_PEERSTORE_StoreContext *store_tail; | 66 | struct GNUNET_PEERSTORE_StoreContext *store_tail; |
67 | 67 | ||
68 | /** | ||
69 | * Head of active ITERATE requests. | ||
70 | */ | ||
71 | struct GNUNET_PEERSTORE_IterateContext *iterate_head; | ||
72 | |||
73 | /** | ||
74 | * Tail of active ITERATE requests. | ||
75 | */ | ||
76 | struct GNUNET_PEERSTORE_IterateContext *iterate_tail; | ||
77 | |||
68 | }; | 78 | }; |
69 | 79 | ||
70 | /** | 80 | /** |
@@ -110,6 +120,49 @@ struct GNUNET_PEERSTORE_StoreContext | |||
110 | 120 | ||
111 | }; | 121 | }; |
112 | 122 | ||
123 | /** | ||
124 | * Context for a iterate request | ||
125 | */ | ||
126 | struct GNUNET_PEERSTORE_IterateContext | ||
127 | { | ||
128 | /** | ||
129 | * Kept in a DLL. | ||
130 | */ | ||
131 | struct GNUNET_PEERSTORE_IterateContext *next; | ||
132 | |||
133 | /** | ||
134 | * Kept in a DLL. | ||
135 | */ | ||
136 | struct GNUNET_PEERSTORE_IterateContext *prev; | ||
137 | |||
138 | /** | ||
139 | * Handle to the PEERSTORE service. | ||
140 | */ | ||
141 | struct GNUNET_PEERSTORE_Handle *h; | ||
142 | |||
143 | /** | ||
144 | * MQ Envelope with iterate request message | ||
145 | */ | ||
146 | struct GNUNET_MQ_Envelope *ev; | ||
147 | |||
148 | /** | ||
149 | * Callback with each matching record | ||
150 | */ | ||
151 | GNUNET_PEERSTORE_Processor callback; | ||
152 | |||
153 | /** | ||
154 | * Closure for 'callback' | ||
155 | */ | ||
156 | void *callback_cls; | ||
157 | |||
158 | /** | ||
159 | * #GNUNET_YES / #GNUNET_NO | ||
160 | * if sent, cannot be canceled | ||
161 | */ | ||
162 | int request_sent; | ||
163 | |||
164 | }; | ||
165 | |||
113 | /******************************************************************************/ | 166 | /******************************************************************************/ |
114 | /******************* DECLARATIONS *********************/ | 167 | /******************* DECLARATIONS *********************/ |
115 | /******************************************************************************/ | 168 | /******************************************************************************/ |
@@ -122,6 +175,19 @@ struct GNUNET_PEERSTORE_StoreContext | |||
122 | */ | 175 | */ |
123 | void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg); | 176 | void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg); |
124 | 177 | ||
178 | /** | ||
179 | * When a response for iterate request is received | ||
180 | * | ||
181 | * @param cls a 'struct GNUNET_PEERSTORE_Handle *' | ||
182 | * @param msg message received, NULL on timeout or fatal error | ||
183 | */ | ||
184 | void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg); | ||
185 | |||
186 | /** | ||
187 | * Close the existing connection to PEERSTORE and reconnect. | ||
188 | * | ||
189 | * @param h handle to the service | ||
190 | */ | ||
125 | static void | 191 | static void |
126 | reconnect (struct GNUNET_PEERSTORE_Handle *h); | 192 | reconnect (struct GNUNET_PEERSTORE_Handle *h); |
127 | 193 | ||
@@ -131,6 +197,8 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h); | |||
131 | static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { | 197 | static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { |
132 | {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK, sizeof(struct GNUNET_MessageHeader)}, | 198 | {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK, sizeof(struct GNUNET_MessageHeader)}, |
133 | {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL, sizeof(struct GNUNET_MessageHeader)}, | 199 | {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL, sizeof(struct GNUNET_MessageHeader)}, |
200 | {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0}, | ||
201 | {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)}, | ||
134 | GNUNET_MQ_HANDLERS_END | 202 | GNUNET_MQ_HANDLERS_END |
135 | }; | 203 | }; |
136 | 204 | ||
@@ -154,12 +222,18 @@ static void | |||
154 | cleanup_handle(struct GNUNET_PEERSTORE_Handle *h) | 222 | cleanup_handle(struct GNUNET_PEERSTORE_Handle *h) |
155 | { | 223 | { |
156 | struct GNUNET_PEERSTORE_StoreContext *sc; | 224 | struct GNUNET_PEERSTORE_StoreContext *sc; |
225 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
157 | 226 | ||
158 | while (NULL != (sc = h->store_head)) | 227 | while (NULL != (sc = h->store_head)) |
159 | { | 228 | { |
160 | GNUNET_CONTAINER_DLL_remove(h->store_head, h->store_tail, sc); | 229 | GNUNET_CONTAINER_DLL_remove(h->store_head, h->store_tail, sc); |
161 | GNUNET_free(sc); | 230 | GNUNET_free(sc); |
162 | } | 231 | } |
232 | while (NULL != (ic = h->iterate_head)) | ||
233 | { | ||
234 | GNUNET_CONTAINER_DLL_remove(h->iterate_head, h->iterate_tail, ic); | ||
235 | GNUNET_free(ic); | ||
236 | } | ||
163 | } | 237 | } |
164 | 238 | ||
165 | /** | 239 | /** |
@@ -254,7 +328,7 @@ GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h) | |||
254 | /** | 328 | /** |
255 | * When a response for store request is received | 329 | * When a response for store request is received |
256 | * | 330 | * |
257 | * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' | 331 | * @param cls a 'struct GNUNET_PEERSTORE_Handle *' |
258 | * @param msg message received, NULL on timeout or fatal error | 332 | * @param msg message received, NULL on timeout or fatal error |
259 | */ | 333 | */ |
260 | void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg) | 334 | void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg) |
@@ -304,6 +378,7 @@ void store_request_sent (void *cls) | |||
304 | struct GNUNET_PEERSTORE_StoreContext *sc = cls; | 378 | struct GNUNET_PEERSTORE_StoreContext *sc = cls; |
305 | 379 | ||
306 | sc->request_sent = GNUNET_YES; | 380 | sc->request_sent = GNUNET_YES; |
381 | sc->ev = NULL; | ||
307 | } | 382 | } |
308 | 383 | ||
309 | /** | 384 | /** |
@@ -319,7 +394,10 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) | |||
319 | if(GNUNET_NO == sc->request_sent) | 394 | if(GNUNET_NO == sc->request_sent) |
320 | { | 395 | { |
321 | if(NULL != sc->ev) | 396 | if(NULL != sc->ev) |
322 | GNUNET_MQ_discard(sc->ev); | 397 | { |
398 | //GNUNET_MQ_discard(sc->ev); //FIXME: this should be GNUNET_MQ_send_cancel | ||
399 | sc->ev = NULL; | ||
400 | } | ||
323 | GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc); | 401 | GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc); |
324 | GNUNET_free(sc); | 402 | GNUNET_free(sc); |
325 | } | 403 | } |
@@ -365,10 +443,8 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, | |||
365 | key, | 443 | key, |
366 | value, | 444 | value, |
367 | size, | 445 | size, |
368 | expiry, | 446 | &expiry, |
369 | GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); | 447 | GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); |
370 | GNUNET_MQ_send(h->mq, ev); | ||
371 | GNUNET_MQ_notify_sent(ev, &store_request_sent, ev); | ||
372 | sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext); | 448 | sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext); |
373 | sc->ev = ev; | 449 | sc->ev = ev; |
374 | sc->cont = cont; | 450 | sc->cont = cont; |
@@ -376,8 +452,144 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, | |||
376 | sc->h = h; | 452 | sc->h = h; |
377 | sc->request_sent = GNUNET_NO; | 453 | sc->request_sent = GNUNET_NO; |
378 | GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc); | 454 | GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc); |
455 | GNUNET_MQ_notify_sent(ev, &store_request_sent, ev); | ||
456 | GNUNET_MQ_send(h->mq, ev); | ||
379 | return sc; | 457 | return sc; |
380 | 458 | ||
381 | } | 459 | } |
382 | 460 | ||
461 | /******************************************************************************/ | ||
462 | /******************* ITERATE FUNCTIONS *********************/ | ||
463 | /******************************************************************************/ | ||
464 | |||
465 | /** | ||
466 | * When a response for iterate request is received | ||
467 | * | ||
468 | * @param cls a 'struct GNUNET_PEERSTORE_Handle *' | ||
469 | * @param msg message received, NULL on timeout or fatal error | ||
470 | */ | ||
471 | void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) | ||
472 | { | ||
473 | struct GNUNET_PEERSTORE_Handle *h = cls; | ||
474 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
475 | GNUNET_PEERSTORE_Processor callback; | ||
476 | void *callback_cls; | ||
477 | uint16_t msg_type; | ||
478 | struct GNUNET_PEERSTORE_Record *record; | ||
479 | |||
480 | ic = h->iterate_head; | ||
481 | if(NULL == ic) | ||
482 | { | ||
483 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unexpected iteration response, this should not happen.\n"); | ||
484 | reconnect(h); | ||
485 | return; | ||
486 | } | ||
487 | callback = ic->callback; | ||
488 | callback_cls = ic->callback_cls; | ||
489 | if(NULL == msg) /* Connection error */ | ||
490 | { | ||
491 | |||
492 | if(NULL != callback) | ||
493 | callback(callback_cls, NULL, | ||
494 | _("Error communicating with `PEERSTORE' service.")); | ||
495 | reconnect(h); | ||
496 | return; | ||
497 | } | ||
498 | msg_type = ntohs(msg->type); | ||
499 | if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) | ||
500 | { | ||
501 | if(NULL != callback) | ||
502 | callback(callback_cls, NULL, NULL); | ||
503 | GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic); | ||
504 | GNUNET_free(ic); | ||
505 | return; | ||
506 | } | ||
507 | if(NULL != callback) | ||
508 | { | ||
509 | record = PEERSTORE_parse_record_message(msg); | ||
510 | if(NULL == record) | ||
511 | callback(callback_cls, record, _("Received a malformed response from service.")); | ||
512 | else | ||
513 | callback(callback_cls, record, NULL); | ||
514 | } | ||
515 | |||
516 | } | ||
517 | |||
518 | /** | ||
519 | * Callback after MQ envelope is sent | ||
520 | * | ||
521 | * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' | ||
522 | */ | ||
523 | void iterate_request_sent (void *cls) | ||
524 | { | ||
525 | struct GNUNET_PEERSTORE_IterateContext *ic = cls; | ||
526 | |||
527 | ic->request_sent = GNUNET_YES; | ||
528 | ic->ev = NULL; | ||
529 | } | ||
530 | |||
531 | /** | ||
532 | * Cancel an iterate request | ||
533 | * Please do not call after the iterate request is done | ||
534 | * | ||
535 | * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate() | ||
536 | */ | ||
537 | void | ||
538 | GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) | ||
539 | { | ||
540 | if(GNUNET_NO == ic->request_sent) | ||
541 | { | ||
542 | if(NULL != ic->ev) | ||
543 | { | ||
544 | //GNUNET_MQ_discard(ic->ev); //FIXME: this should be GNUNET_MQ_send_cancel | ||
545 | ic->ev = NULL; | ||
546 | } | ||
547 | GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic); | ||
548 | GNUNET_free(ic); | ||
549 | } | ||
550 | else | ||
551 | ic->callback = NULL; | ||
552 | } | ||
553 | |||
554 | /** | ||
555 | * Iterate over records matching supplied key information | ||
556 | * | ||
557 | * @param h handle to the PEERSTORE service | ||
558 | * @param sub_system name of sub system | ||
559 | * @param peer Peer identity (can be NULL) | ||
560 | * @param key entry key string (can be NULL) | ||
561 | * @param timeout time after which the iterate request is canceled | ||
562 | * @param callback function called with each matching record, all NULL's on end | ||
563 | * @param callback_cls closure for @a callback | ||
564 | */ | ||
565 | struct GNUNET_PEERSTORE_IterateContext * | ||
566 | GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, | ||
567 | char *sub_system, | ||
568 | const struct GNUNET_PeerIdentity *peer, | ||
569 | const char *key, | ||
570 | struct GNUNET_TIME_Relative timeout, //FIXME: handle timeout | ||
571 | GNUNET_PEERSTORE_Processor callback, void *callback_cls) | ||
572 | { | ||
573 | struct GNUNET_MQ_Envelope *ev; | ||
574 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
575 | |||
576 | ev = PEERSTORE_create_record_mq_envelope(sub_system, | ||
577 | peer, | ||
578 | key, | ||
579 | NULL, | ||
580 | 0, | ||
581 | NULL, | ||
582 | GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); | ||
583 | ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext); | ||
584 | ic->callback = callback; | ||
585 | ic->callback_cls = callback_cls; | ||
586 | ic->ev = ev; | ||
587 | ic->h = h; | ||
588 | ic->request_sent = GNUNET_NO; | ||
589 | GNUNET_CONTAINER_DLL_insert(h->iterate_head, h->iterate_tail, ic); | ||
590 | GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ev); | ||
591 | GNUNET_MQ_send(h->mq, ev); | ||
592 | return ic; | ||
593 | } | ||
594 | |||
383 | /* end of peerstore_api.c */ | 595 | /* end of peerstore_api.c */ |
diff --git a/src/peerstore/peerstore_common.c b/src/peerstore/peerstore_common.c index 2cb9d0077..5783973b6 100644 --- a/src/peerstore/peerstore_common.c +++ b/src/peerstore/peerstore_common.c | |||
@@ -36,14 +36,14 @@ | |||
36 | * @param expiry absolute time after which the record expires | 36 | * @param expiry absolute time after which the record expires |
37 | * @param msg_type message type to be set in header | 37 | * @param msg_type message type to be set in header |
38 | * @return pointer to record message struct | 38 | * @return pointer to record message struct |
39 | * | 39 | */ |
40 | struct StoreRecordMessage * | 40 | struct StoreRecordMessage * |
41 | PEERSTORE_create_record_message(const char *sub_system, | 41 | PEERSTORE_create_record_message(const char *sub_system, |
42 | const struct GNUNET_PeerIdentity *peer, | 42 | const struct GNUNET_PeerIdentity *peer, |
43 | const char *key, | 43 | const char *key, |
44 | const void *value, | 44 | const void *value, |
45 | size_t value_size, | 45 | size_t value_size, |
46 | struct GNUNET_TIME_Absolute expiry, | 46 | struct GNUNET_TIME_Absolute *expiry, |
47 | uint16_t msg_type) | 47 | uint16_t msg_type) |
48 | { | 48 | { |
49 | struct StoreRecordMessage *srm; | 49 | struct StoreRecordMessage *srm; |
@@ -65,7 +65,8 @@ PEERSTORE_create_record_message(const char *sub_system, | |||
65 | srm->header.size = htons(request_size); | 65 | srm->header.size = htons(request_size); |
66 | srm->header.type = htons(msg_type); | 66 | srm->header.type = htons(msg_type); |
67 | srm->key_size = htons(key_size); | 67 | srm->key_size = htons(key_size); |
68 | srm->expiry = expiry; | 68 | if(NULL != expiry) |
69 | srm->expiry = *expiry; | ||
69 | if(NULL == peer) | 70 | if(NULL == peer) |
70 | srm->peer_set = htons(GNUNET_NO); | 71 | srm->peer_set = htons(GNUNET_NO); |
71 | else | 72 | else |
@@ -83,7 +84,7 @@ PEERSTORE_create_record_message(const char *sub_system, | |||
83 | memcpy(dummy, value, value_size); | 84 | memcpy(dummy, value, value_size); |
84 | return srm; | 85 | return srm; |
85 | 86 | ||
86 | }*/ | 87 | } |
87 | 88 | ||
88 | /** | 89 | /** |
89 | * Creates a MQ envelope for a single record | 90 | * Creates a MQ envelope for a single record |
@@ -103,7 +104,7 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, | |||
103 | const char *key, | 104 | const char *key, |
104 | const void *value, | 105 | const void *value, |
105 | size_t value_size, | 106 | size_t value_size, |
106 | struct GNUNET_TIME_Absolute expiry, | 107 | struct GNUNET_TIME_Absolute *expiry, |
107 | uint16_t msg_type) | 108 | uint16_t msg_type) |
108 | { | 109 | { |
109 | struct StoreRecordMessage *srm; | 110 | struct StoreRecordMessage *srm; |
@@ -124,7 +125,8 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, | |||
124 | value_size; | 125 | value_size; |
125 | ev = GNUNET_MQ_msg_extra(srm, msg_size, msg_type); | 126 | ev = GNUNET_MQ_msg_extra(srm, msg_size, msg_type); |
126 | srm->key_size = htons(key_size); | 127 | srm->key_size = htons(key_size); |
127 | srm->expiry = expiry; | 128 | if(NULL != expiry) |
129 | srm->expiry = *expiry; | ||
128 | if(NULL == peer) | 130 | if(NULL == peer) |
129 | srm->peer_set = htons(GNUNET_NO); | 131 | srm->peer_set = htons(GNUNET_NO); |
130 | else | 132 | else |
diff --git a/src/peerstore/peerstore_common.h b/src/peerstore/peerstore_common.h index a15302ed4..cd918497b 100644 --- a/src/peerstore/peerstore_common.h +++ b/src/peerstore/peerstore_common.h | |||
@@ -27,6 +27,27 @@ | |||
27 | #include "peerstore.h" | 27 | #include "peerstore.h" |
28 | 28 | ||
29 | /** | 29 | /** |
30 | * Creates a record message ready to be sent | ||
31 | * | ||
32 | * @param sub_system sub system string | ||
33 | * @param peer Peer identity (can be NULL) | ||
34 | * @param key record key string (can be NULL) | ||
35 | * @param value record value BLOB (can be NULL) | ||
36 | * @param value_size record value size in bytes (set to 0 if value is NULL) | ||
37 | * @param expiry absolute time after which the record expires | ||
38 | * @param msg_type message type to be set in header | ||
39 | * @return pointer to record message struct | ||
40 | */ | ||
41 | struct StoreRecordMessage * | ||
42 | PEERSTORE_create_record_message(const char *sub_system, | ||
43 | const struct GNUNET_PeerIdentity *peer, | ||
44 | const char *key, | ||
45 | const void *value, | ||
46 | size_t value_size, | ||
47 | struct GNUNET_TIME_Absolute *expiry, | ||
48 | uint16_t msg_type); | ||
49 | |||
50 | /** | ||
30 | * Creates a MQ envelope for a single record | 51 | * Creates a MQ envelope for a single record |
31 | * | 52 | * |
32 | * @param sub_system sub system string | 53 | * @param sub_system sub system string |
@@ -44,7 +65,7 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, | |||
44 | const char *key, | 65 | const char *key, |
45 | const void *value, | 66 | const void *value, |
46 | size_t value_size, | 67 | size_t value_size, |
47 | struct GNUNET_TIME_Absolute expiry, | 68 | struct GNUNET_TIME_Absolute *expiry, |
48 | uint16_t msg_type); | 69 | uint16_t msg_type); |
49 | 70 | ||
50 | /** | 71 | /** |
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index c5eabac2d..1ae8301b2 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -122,12 +122,7 @@ peerstore_sqlite_iterate_records (void *cls, | |||
122 | sqlite3_stmt *stmt; | 122 | sqlite3_stmt *stmt; |
123 | int err = 0; | 123 | int err = 0; |
124 | int sret; | 124 | int sret; |
125 | const char *ret_sub_system; | 125 | struct GNUNET_PEERSTORE_Record *ret; |
126 | const struct GNUNET_PeerIdentity *ret_peer; | ||
127 | const char *ret_key; | ||
128 | const void *ret_value; | ||
129 | size_t ret_value_size; | ||
130 | struct GNUNET_TIME_Absolute ret_expiry; | ||
131 | 126 | ||
132 | if(NULL == peer && NULL == key) | 127 | if(NULL == peer && NULL == key) |
133 | { | 128 | { |
@@ -166,20 +161,18 @@ peerstore_sqlite_iterate_records (void *cls, | |||
166 | } | 161 | } |
167 | while (SQLITE_ROW == (sret = sqlite3_step (stmt))) | 162 | while (SQLITE_ROW == (sret = sqlite3_step (stmt))) |
168 | { | 163 | { |
169 | ret_sub_system = (const char *)sqlite3_column_text(stmt, 0); | 164 | ret = GNUNET_new(struct GNUNET_PEERSTORE_Record); |
170 | ret_peer = sqlite3_column_blob(stmt, 1); | 165 | ret->sub_system = (char *)sqlite3_column_text(stmt, 0); |
171 | ret_key = (const char *)sqlite3_column_text(stmt, 2); | 166 | ret->peer = (struct GNUNET_PeerIdentity *)sqlite3_column_blob(stmt, 1); |
172 | ret_value = sqlite3_column_blob(stmt, 3); | 167 | ret->key = (char *)sqlite3_column_text(stmt, 2); |
173 | ret_value_size = sqlite3_column_bytes(stmt, 3); | 168 | ret->value = (void *)sqlite3_column_blob(stmt, 3); |
174 | ret_expiry.abs_value_us = (uint64_t)sqlite3_column_int64(stmt, 4); | 169 | ret->value_size = sqlite3_column_bytes(stmt, 3); |
170 | ret->expiry = GNUNET_new(struct GNUNET_TIME_Absolute); | ||
171 | ret->expiry->abs_value_us = (uint64_t)sqlite3_column_int64(stmt, 4); | ||
175 | if (NULL != iter) | 172 | if (NULL != iter) |
176 | iter (iter_cls, | 173 | iter (iter_cls, |
177 | ret_sub_system, | 174 | ret, |
178 | ret_peer, | 175 | NULL); |
179 | ret_key, | ||
180 | ret_value, | ||
181 | ret_value_size, | ||
182 | ret_expiry); | ||
183 | } | 176 | } |
184 | if (SQLITE_DONE != sret) | 177 | if (SQLITE_DONE != sret) |
185 | { | 178 | { |
diff --git a/src/peerstore/test_peerstore_api.c b/src/peerstore/test_peerstore_api.c index d17c6fff0..f13239abb 100644 --- a/src/peerstore/test_peerstore_api.c +++ b/src/peerstore/test_peerstore_api.c | |||
@@ -25,19 +25,53 @@ | |||
25 | #include "gnunet_util_lib.h" | 25 | #include "gnunet_util_lib.h" |
26 | #include "gnunet_testing_lib.h" | 26 | #include "gnunet_testing_lib.h" |
27 | #include "gnunet_peerstore_service.h" | 27 | #include "gnunet_peerstore_service.h" |
28 | #include <inttypes.h> | ||
28 | 29 | ||
29 | static int ok = 1; | 30 | static int ok = 1; |
30 | 31 | ||
32 | static int counter = 0; | ||
33 | |||
31 | struct GNUNET_PEERSTORE_Handle *h; | 34 | struct GNUNET_PEERSTORE_Handle *h; |
32 | 35 | ||
36 | int iterate_cb (void *cls, | ||
37 | struct GNUNET_PEERSTORE_Record *record, | ||
38 | char *emsg) | ||
39 | { | ||
40 | if(NULL != emsg) | ||
41 | { | ||
42 | printf("Error received: %s.\n", emsg); | ||
43 | return GNUNET_YES; | ||
44 | } | ||
45 | printf("Record:\n"); | ||
46 | if(NULL == record) | ||
47 | { | ||
48 | counter = 0; | ||
49 | printf("END\n"); | ||
50 | GNUNET_PEERSTORE_disconnect(h); | ||
51 | return GNUNET_YES; | ||
52 | } | ||
53 | printf("Sub system: %s\n", record->sub_system); | ||
54 | printf("Peer: %s\n", GNUNET_i2s (record->peer)); | ||
55 | printf("Key: %s\n", record->key); | ||
56 | printf("Value: %.*s\n", record->value); | ||
57 | printf("Expiry: %" PRIu64 "\n", record->expiry->abs_value_us); | ||
58 | |||
59 | return GNUNET_YES; | ||
60 | } | ||
61 | |||
33 | void store_cont(void *cls, int success) | 62 | void store_cont(void *cls, int success) |
34 | { | 63 | { |
35 | if(GNUNET_OK == success) | 64 | if(GNUNET_OK == success) |
36 | ok = 0; | 65 | ok = 0; |
37 | else | 66 | else |
38 | ok = 1; | 67 | ok = 1; |
39 | printf("Success: %d\n", success); | 68 | printf("Store success: %d\n", success); |
40 | GNUNET_PEERSTORE_disconnect(h); | 69 | GNUNET_PEERSTORE_iterate(h, "peerstore-test-value", |
70 | NULL, | ||
71 | NULL, | ||
72 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
73 | &iterate_cb, | ||
74 | NULL); | ||
41 | } | 75 | } |
42 | 76 | ||
43 | static void | 77 | static void |
@@ -48,6 +82,7 @@ run (void *cls, | |||
48 | struct GNUNET_PeerIdentity pid; | 82 | struct GNUNET_PeerIdentity pid; |
49 | char *val = "peerstore-test-value"; | 83 | char *val = "peerstore-test-value"; |
50 | size_t val_size = strlen(val); | 84 | size_t val_size = strlen(val); |
85 | struct GNUNET_PEERSTORE_StoreContext *sc; | ||
51 | 86 | ||
52 | ok = 0; | 87 | ok = 0; |
53 | memset (&pid, 32, sizeof (pid)); | 88 | memset (&pid, 32, sizeof (pid)); |