aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-05-28 18:21:02 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-05-28 18:21:02 +0000
commitf7c240021dc6f06e3e143713261305b127f20735 (patch)
treec50f5b777ae661b81f1342fcd131706018adf41c /src/peerstore
parent3663cace0b209eef3d7c4ed2e852e3548cf5ba38 (diff)
downloadgnunet-f7c240021dc6f06e3e143713261305b127f20735.tar.gz
gnunet-f7c240021dc6f06e3e143713261305b127f20735.zip
peerstore: towards watch functionality
Diffstat (limited to 'src/peerstore')
-rw-r--r--src/peerstore/peerstore_api.c133
-rw-r--r--src/peerstore/test_peerstore_api.c2
2 files changed, 134 insertions, 1 deletions
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index 57c631852..14b1c3e88 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -75,6 +75,16 @@ struct GNUNET_PEERSTORE_Handle
75 */ 75 */
76 struct GNUNET_PEERSTORE_IterateContext *iterate_tail; 76 struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
77 77
78 /**
79 * Head of WATCH requests (active and inactive).
80 */
81 struct GNUNET_PEERSTORE_WatchContext *watch_head;
82
83 /**
84 * Tail of WATCH requests (active and inactive).
85 */
86 struct GNUNET_PEERSTORE_WatchContext *watch_tail;
87
78}; 88};
79 89
80/** 90/**
@@ -169,6 +179,49 @@ struct GNUNET_PEERSTORE_IterateContext
169 179
170}; 180};
171 181
182/**
183 * Context for a watch request
184 */
185struct GNUNET_PEERSTORE_WatchContext
186{
187 /**
188 * Kept in a DLL.
189 */
190 struct GNUNET_PEERSTORE_WatchContext *next;
191
192 /**
193 * Kept in a DLL.
194 */
195 struct GNUNET_PEERSTORE_WatchContext *prev;
196
197 /**
198 * Handle to the PEERSTORE service.
199 */
200 struct GNUNET_PEERSTORE_Handle *h;
201
202 /**
203 * MQ Envelope with watch request message
204 */
205 struct GNUNET_MQ_Envelope *ev;
206
207 /**
208 * Callback with each record received
209 */
210 GNUNET_PEERSTORE_Processor callback;
211
212 /**
213 * Closure for 'callback'
214 */
215 void *callback_cls;
216
217 /**
218 * #GNUNET_YES / #GNUNET_NO
219 * if sent, cannot be canceled
220 */
221 int request_sent;
222
223};
224
172/******************************************************************************/ 225/******************************************************************************/
173/******************* DECLARATIONS *********************/ 226/******************* DECLARATIONS *********************/
174/******************************************************************************/ 227/******************************************************************************/
@@ -190,6 +243,14 @@ void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg);
190void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg); 243void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
191 244
192/** 245/**
246 * When a watch record is received
247 *
248 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
249 * @param msg message received, NULL on timeout or fatal error
250 */
251void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
252
253/**
193 * Close the existing connection to PEERSTORE and reconnect. 254 * Close the existing connection to PEERSTORE and reconnect.
194 * 255 *
195 * @param h handle to the service 256 * @param h handle to the service
@@ -205,6 +266,7 @@ static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
205 {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL, sizeof(struct GNUNET_MessageHeader)}, 266 {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL, sizeof(struct GNUNET_MessageHeader)},
206 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0}, 267 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
207 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)}, 268 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
269 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
208 GNUNET_MQ_HANDLERS_END 270 GNUNET_MQ_HANDLERS_END
209}; 271};
210 272
@@ -624,4 +686,75 @@ GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
624 return ic; 686 return ic;
625} 687}
626 688
689/******************************************************************************/
690/******************* WATCH FUNCTIONS *********************/
691/******************************************************************************/
692
693/**
694 * When a watch record is received
695 *
696 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
697 * @param msg message received, NULL on timeout or fatal error
698 */
699void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
700{
701
702}
703
704/**
705 * Callback after MQ envelope is sent
706 *
707 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
708 */
709void watch_request_sent (void *cls)
710{
711 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
712
713 wc->request_sent = GNUNET_YES;
714 wc->ev = NULL;
715}
716
717/**
718 * Request watching a given key
719 * User will be notified with any new values added to key
720 *
721 * @param h handle to the PEERSTORE service
722 * @param sub_system name of sub system
723 * @param peer Peer identity
724 * @param key entry key string
725 * @param callback function called with each new value
726 * @param callback_cls closure for @a callback
727 * @return Handle to watch request
728 */
729struct GNUNET_PEERSTORE_WatchContext *
730GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
731 char *sub_system,
732 const struct GNUNET_PeerIdentity *peer,
733 const char *key,
734 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
735{
736 struct GNUNET_MQ_Envelope *ev;
737 struct GNUNET_PEERSTORE_WatchContext *wc;
738
739 ev = PEERSTORE_create_record_mq_envelope(sub_system,
740 peer,
741 key,
742 NULL,
743 0,
744 NULL,
745 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
746 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
747 wc->callback = callback;
748 wc->callback_cls = callback_cls;
749 wc->ev = ev;
750 wc->h = h;
751 wc->request_sent = GNUNET_NO;
752 GNUNET_CONTAINER_DLL_insert(h->watch_head, h->watch_tail, wc);
753 LOG(GNUNET_ERROR_TYPE_DEBUG,
754 "Sending a watch request for sub system `%s'.\n", sub_system);
755 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
756 GNUNET_MQ_send(h->mq, ev);
757 return wc;
758}
759
627/* end of peerstore_api.c */ 760/* end of peerstore_api.c */
diff --git a/src/peerstore/test_peerstore_api.c b/src/peerstore/test_peerstore_api.c
index ea5843b2d..fe62933e7 100644
--- a/src/peerstore/test_peerstore_api.c
+++ b/src/peerstore/test_peerstore_api.c
@@ -86,7 +86,7 @@ run (void *cls,
86 size_t val_size = strlen(val); 86 size_t val_size = strlen(val);
87 struct GNUNET_TIME_Absolute expiry; 87 struct GNUNET_TIME_Absolute expiry;
88 88
89 ok = 0; 89 ok = 1;
90 memset (&pid, 32, sizeof (pid)); 90 memset (&pid, 32, sizeof (pid));
91 expiry = GNUNET_TIME_absolute_get(); 91 expiry = GNUNET_TIME_absolute_get();
92 h = GNUNET_PEERSTORE_connect(cfg); 92 h = GNUNET_PEERSTORE_connect(cfg);