diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-05-30 16:06:00 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-05-30 16:06:00 +0000 |
commit | 95cdeb5c0bb1f14f3959863e6bf4675db48ea177 (patch) | |
tree | 545e2a910c0efdae9dc2e2af8da45efa007cdf32 /src/peerstore/gnunet-service-peerstore.c | |
parent | 02f9d1e7389d0da0a475ae0035b67801c7ca2d06 (diff) | |
download | gnunet-95cdeb5c0bb1f14f3959863e6bf4675db48ea177.tar.gz gnunet-95cdeb5c0bb1f14f3959863e6bf4675db48ea177.zip |
peerstore: towards watch functionality
Diffstat (limited to 'src/peerstore/gnunet-service-peerstore.c')
-rw-r--r-- | src/peerstore/gnunet-service-peerstore.c | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c index c410630c9..70d79ea5e 100644 --- a/src/peerstore/gnunet-service-peerstore.c +++ b/src/peerstore/gnunet-service-peerstore.c | |||
@@ -29,7 +29,23 @@ | |||
29 | #include "gnunet_peerstore_plugin.h" | 29 | #include "gnunet_peerstore_plugin.h" |
30 | #include "peerstore_common.h" | 30 | #include "peerstore_common.h" |
31 | 31 | ||
32 | //TODO: GNUNET_SERVER_receive_done() ? | 32 | /** |
33 | * Context of a PEERSTORE watch | ||
34 | */ | ||
35 | struct WatchContext | ||
36 | { | ||
37 | |||
38 | /** | ||
39 | * Hash of key of watched record | ||
40 | */ | ||
41 | struct GNUNET_HashCode keyhash; | ||
42 | |||
43 | /** | ||
44 | * Client requested the watch | ||
45 | */ | ||
46 | struct GNUNET_SERVER_Client *client; | ||
47 | |||
48 | }; | ||
33 | 49 | ||
34 | /** | 50 | /** |
35 | * Interval for expired records cleanup (in seconds) | 51 | * Interval for expired records cleanup (in seconds) |
@@ -52,6 +68,11 @@ char *db_lib_name; | |||
52 | static struct GNUNET_PEERSTORE_PluginFunctions *db; | 68 | static struct GNUNET_PEERSTORE_PluginFunctions *db; |
53 | 69 | ||
54 | /** | 70 | /** |
71 | * Hashmap with all watch requests | ||
72 | */ | ||
73 | static struct GNUNET_CONTAINER_MultiHashMap *watchers; | ||
74 | |||
75 | /** | ||
55 | * Task run during shutdown. | 76 | * Task run during shutdown. |
56 | * | 77 | * |
57 | * @param cls unused | 78 | * @param cls unused |
@@ -67,7 +88,8 @@ shutdown_task (void *cls, | |||
67 | GNUNET_free (db_lib_name); | 88 | GNUNET_free (db_lib_name); |
68 | db_lib_name = NULL; | 89 | db_lib_name = NULL; |
69 | } | 90 | } |
70 | 91 | if(NULL != watchers) | |
92 | GNUNET_CONTAINER_multihashmap_destroy(watchers); | ||
71 | GNUNET_SCHEDULER_shutdown(); | 93 | GNUNET_SCHEDULER_shutdown(); |
72 | } | 94 | } |
73 | 95 | ||
@@ -132,6 +154,55 @@ int record_iterator(void *cls, | |||
132 | } | 154 | } |
133 | 155 | ||
134 | /** | 156 | /** |
157 | * Handle a watch cancel request from client | ||
158 | * | ||
159 | * @param cls unused | ||
160 | * @param client identification of the client | ||
161 | * @param message the actual message | ||
162 | */ | ||
163 | void handle_watch_cancel (void *cls, | ||
164 | struct GNUNET_SERVER_Client *client, | ||
165 | const struct GNUNET_MessageHeader *message) | ||
166 | { | ||
167 | struct StoreKeyHashMessage *hm; | ||
168 | |||
169 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request from client.\n"); | ||
170 | if(NULL == watchers) | ||
171 | { | ||
172 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
173 | "Received a watch cancel request when we don't have any watchers.\n"); | ||
174 | GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); | ||
175 | return; | ||
176 | } | ||
177 | hm = (struct StoreKeyHashMessage *) message; | ||
178 | GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client); | ||
179 | GNUNET_SERVER_receive_done(client, GNUNET_OK); | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * Handle a watch request from client | ||
184 | * | ||
185 | * @param cls unused | ||
186 | * @param client identification of the client | ||
187 | * @param message the actual message | ||
188 | */ | ||
189 | void handle_watch (void *cls, | ||
190 | struct GNUNET_SERVER_Client *client, | ||
191 | const struct GNUNET_MessageHeader *message) | ||
192 | { | ||
193 | struct StoreKeyHashMessage *hm; | ||
194 | |||
195 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request from client.\n"); | ||
196 | hm = (struct StoreKeyHashMessage *) message; | ||
197 | GNUNET_SERVER_client_mark_monitor(client); | ||
198 | if(NULL == watchers) | ||
199 | watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); | ||
200 | GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash, | ||
201 | client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
202 | GNUNET_SERVER_receive_done(client, GNUNET_OK); | ||
203 | } | ||
204 | |||
205 | /** | ||
135 | * Handle an iterate request from client | 206 | * Handle an iterate request from client |
136 | * | 207 | * |
137 | * @param cls unused | 208 | * @param cls unused |
@@ -232,7 +303,7 @@ void handle_store (void *cls, | |||
232 | tc = GNUNET_SERVER_transmit_context_create (client); | 303 | tc = GNUNET_SERVER_transmit_context_create (client); |
233 | GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type); | 304 | GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type); |
234 | GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); | 305 | GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); |
235 | 306 | //TODO: notify watchers, if a client is disconnected, remove its watch entry | |
236 | } | 307 | } |
237 | 308 | ||
238 | /** | 309 | /** |
@@ -250,6 +321,8 @@ run (void *cls, | |||
250 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | 321 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { |
251 | {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, | 322 | {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0}, |
252 | {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, | 323 | {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0}, |
324 | {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)}, | ||
325 | {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)}, | ||
253 | {NULL, NULL, 0, 0} | 326 | {NULL, NULL, 0, 0} |
254 | }; | 327 | }; |
255 | char *database; | 328 | char *database; |