aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-05-07 14:14:38 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-05-07 14:14:38 +0000
commit36367311686410711f46fa3d82484a609c888acf (patch)
tree62a6afbe310a8479e18221d0606a715948adeb5b /src
parent0778d7e951b20f42af01597ae41ba8d2d26857d0 (diff)
downloadgnunet-36367311686410711f46fa3d82484a609c888acf.tar.gz
gnunet-36367311686410711f46fa3d82484a609c888acf.zip
PEERSTORE api update
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_peerstore_service.h11
-rw-r--r--src/include/gnunet_protocols.h11
-rw-r--r--src/peerstore/peerstore.h34
-rw-r--r--src/peerstore/peerstore_api.c111
-rw-r--r--src/peerstore/plugin_peerstore_sqlite.c18
5 files changed, 128 insertions, 57 deletions
diff --git a/src/include/gnunet_peerstore_service.h b/src/include/gnunet_peerstore_service.h
index 91baa3c0f..1fd068d27 100644
--- a/src/include/gnunet_peerstore_service.h
+++ b/src/include/gnunet_peerstore_service.h
@@ -53,16 +53,7 @@ struct GNUNET_PEERSTORE_AddContext;
53 * @param cls closure 53 * @param cls closure
54 * @param emsg error message, NULL on success 54 * @param emsg error message, NULL on success
55 */ 55 */
56typedef void (*GNUNET_PEERSTORE_Continuation)(void *cls, 56typedef void (*GNUNET_PEERSTORE_Continuation)(void *cls, const char *emsg);
57 const char *emsg);
58
59/**
60 * Continuation called after an Add is completed
61 *
62 * @param cls closure
63 * @param success #GNUNET_YES / #GNUNET_NO
64 */
65typedef void (*GNUNET_PEERSTORE_AddContinuation)(void *cls, int success);
66 57
67#if 0 /* keep Emacsens' auto-indent happy */ 58#if 0 /* keep Emacsens' auto-indent happy */
68{ 59{
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h
index af294c785..c79475c47 100644
--- a/src/include/gnunet_protocols.h
+++ b/src/include/gnunet_protocols.h
@@ -2454,12 +2454,17 @@ extern "C"
2454#define GNUNET_MESSAGE_TYPE_SENSOR_END 803 2454#define GNUNET_MESSAGE_TYPE_SENSOR_END 803
2455 2455
2456 2456
2457/** 2457/*******************************************************************************
2458 * Next available: 830 2458 * PEERSTORE message types
2459 */ 2459 ******************************************************************************/
2460 2460
2461#define GNUNET_MESSAGE_TYPE_PEERSTORE_ADD 820
2461 2462
2462 2463
2464/**
2465 * Next available: 840
2466 */
2467
2463/******************************************************************************* 2468/*******************************************************************************
2464 * SOCIAL message types 2469 * SOCIAL message types
2465 ******************************************************************************/ 2470 ******************************************************************************/
diff --git a/src/peerstore/peerstore.h b/src/peerstore/peerstore.h
index ffc13da2a..36ee8c16f 100644
--- a/src/peerstore/peerstore.h
+++ b/src/peerstore/peerstore.h
@@ -28,5 +28,39 @@
28 28
29GNUNET_NETWORK_STRUCT_BEGIN 29GNUNET_NETWORK_STRUCT_BEGIN
30 30
31/**
32 * Message carrying a peerstore entry
33 */
34struct AddEntryMessage
35{
36
37 /**
38 * Type will be GNUNET_MESSAGE_TYPE_PEERINFO_GET
39 */
40 struct GNUNET_MessageHeader header;
41
42 /**
43 * Peer Identity
44 */
45 struct GNUNET_PeerIdentity peer;
46
47 /**
48 * Size of the sub_system string
49 * Allocated at position 0 after this struct
50 */
51 size_t sub_system_size;
52
53 /**
54 * Size of value blob
55 * Allocated at position 1 after this struct
56 */
57 size_t value_size;
58
59 /**
60 * Lifetime of entry
61 */
62 struct GNUNET_TIME_Relative lifetime;
63
64};
31 65
32GNUNET_NETWORK_STRUCT_END 66GNUNET_NETWORK_STRUCT_END
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index a1e75e6e7..a49d2a720 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -34,7 +34,7 @@
34/******************************************************************************/ 34/******************************************************************************/
35 35
36/** 36/**
37 * Handle to the peerstore service. 37 * Handle to the PEERSTORE service.
38 */ 38 */
39struct GNUNET_PEERSTORE_Handle 39struct GNUNET_PEERSTORE_Handle
40{ 40{
@@ -52,12 +52,12 @@ struct GNUNET_PEERSTORE_Handle
52 /** 52 /**
53 * Head of transmission queue. 53 * Head of transmission queue.
54 */ 54 */
55 struct GNUNET_PEERSTORE_AddContext *rc_head; 55 struct GNUNET_PEERSTORE_AddContext *ac_head;
56 56
57 /** 57 /**
58 * Tail of transmission queue. 58 * Tail of transmission queue.
59 */ 59 */
60 struct GNUNET_PEERSTORE_AddContext *rc_tail; 60 struct GNUNET_PEERSTORE_AddContext *ac_tail;
61 61
62 /** 62 /**
63 * Handle for the current transmission request, or NULL if none is pending. 63 * Handle for the current transmission request, or NULL if none is pending.
@@ -140,6 +140,21 @@ trigger_transmit (struct GNUNET_PEERSTORE_Handle *h);
140/******************************************************************************/ 140/******************************************************************************/
141 141
142/** 142/**
143 * Task scheduled to re-try connecting to the peerstore service.
144 *
145 * @param cls the 'struct GNUNET_PEERSTORE_Handle'
146 * @param tc scheduler context
147 */
148static void
149reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150{
151 struct GNUNET_PEERSTORE_Handle *h = cls;
152
153 h->r_task = GNUNET_SCHEDULER_NO_TASK;
154 reconnect (h);
155}
156
157/**
143 * Connect to the PEERSTORE service. 158 * Connect to the PEERSTORE service.
144 * 159 *
145 * @return NULL on error 160 * @return NULL on error
@@ -176,21 +191,6 @@ GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
176} 191}
177 192
178/** 193/**
179 * Task scheduled to re-try connecting to the PEERSTORE service.
180 *
181 * @param cls the 'struct GNUNET_PEERSTORE_Handle'
182 * @param tc scheduler context
183 */
184static void
185reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
186{
187 struct GNUNET_PEERSTORE_Handle *h = cls;
188
189 h->r_task = GNUNET_SCHEDULER_NO_TASK;
190 reconnect (h);
191}
192
193/**
194 * Close the existing connection to PEERSTORE and reconnect. 194 * Close the existing connection to PEERSTORE and reconnect.
195 * 195 *
196 * @param h handle to the service 196 * @param h handle to the service
@@ -238,25 +238,25 @@ static size_t
238do_transmit (void *cls, size_t size, void *buf) 238do_transmit (void *cls, size_t size, void *buf)
239{ 239{
240 struct GNUNET_PEERSTORE_Handle *h = cls; 240 struct GNUNET_PEERSTORE_Handle *h = cls;
241 struct GNUNET_PEERSTORE_AddContext *rc = h->rc_head; 241 struct GNUNET_PEERSTORE_AddContext *ac = h->ac_head;
242 size_t ret; 242 size_t ret;
243 243
244 h->th = NULL; 244 h->th = NULL;
245 if (NULL == rc) 245 if (NULL == ac)
246 return 0; /* request was cancelled in the meantime */ 246 return 0; /* request was cancelled in the meantime */
247 if (NULL == buf) 247 if (NULL == buf)
248 { 248 {
249 /* PEERSTORE service died */ 249 /* peerstore service died */
250 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 250 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
251 "Failed to transmit message to `%s' service.\n", "PEERSTORE"); 251 "Failed to transmit message to `%s' service.\n", "PEERSTORE");
252 GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc); 252 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
253 reconnect (h); 253 reconnect (h);
254 if (NULL != rc->cont) 254 if (NULL != ac->cont)
255 rc->cont (rc->cont_cls, _("failed to transmit request (service down?)")); 255 ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
256 GNUNET_free (rc); 256 GNUNET_free (ac);
257 return 0; 257 return 0;
258 } 258 }
259 ret = rc->size; 259 ret = ac->size;
260 if (size < ret) 260 if (size < ret)
261 { 261 {
262 /* change in head of queue (i.e. cancel + add), try again */ 262 /* change in head of queue (i.e. cancel + add), try again */
@@ -265,12 +265,12 @@ do_transmit (void *cls, size_t size, void *buf)
265 } 265 }
266 LOG (GNUNET_ERROR_TYPE_DEBUG, 266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Transmitting request of size %u to `%s' service.\n", ret, "PEERSTORE"); 267 "Transmitting request of size %u to `%s' service.\n", ret, "PEERSTORE");
268 memcpy (buf, &rc[1], ret); 268 memcpy (buf, &ac[1], ret);
269 GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc); 269 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
270 trigger_transmit (h); 270 trigger_transmit (h);
271 if (NULL != rc->cont) 271 if (NULL != ac->cont)
272 rc->cont (rc->cont_cls, NULL); 272 ac->cont (ac->cont_cls, NULL);
273 GNUNET_free (rc); 273 GNUNET_free (ac);
274 return ret; 274 return ret;
275} 275}
276 276
@@ -283,9 +283,9 @@ do_transmit (void *cls, size_t size, void *buf)
283static void 283static void
284trigger_transmit (struct GNUNET_PEERSTORE_Handle *h) 284trigger_transmit (struct GNUNET_PEERSTORE_Handle *h)
285{ 285{
286 struct GNUNET_PEERSTORE_AddContext *rc; 286 struct GNUNET_PEERSTORE_AddContext *ac;
287 287
288 if (NULL == (rc = h->rc_head)) 288 if (NULL == (ac = h->ac_head))
289 return; /* no requests queued */ 289 return; /* no requests queued */
290 if (NULL != h->th) 290 if (NULL != h->th)
291 return; /* request already pending */ 291 return; /* request already pending */
@@ -296,16 +296,57 @@ trigger_transmit (struct GNUNET_PEERSTORE_Handle *h)
296 return; 296 return;
297 } 297 }
298 h->th = 298 h->th =
299 GNUNET_CLIENT_notify_transmit_ready (h->client, rc->size, 299 GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
300 GNUNET_TIME_UNIT_FOREVER_REL, 300 GNUNET_TIME_UNIT_FOREVER_REL,
301 GNUNET_YES, 301 GNUNET_YES,
302 &do_transmit, h); 302 &do_transmit, h);
303} 303}
304 304
305/******************************************************************************/ 305/******************************************************************************/
306/******************* STORE FUNCTIONS *********************/ 306/******************* ADD FUNCTIONS *********************/
307/******************************************************************************/ 307/******************************************************************************/
308 308
309struct GNUNET_PEERSTORE_AddContext *
310GNUNET_PEERSTORE_add (struct GNUNET_PEERSTORE_Handle *h,
311 const struct GNUNET_PeerIdentity *peer,
312 const char *sub_system,
313 const void *value,
314 size_t size,
315 struct GNUNET_TIME_Relative lifetime,
316 GNUNET_PEERSTORE_Continuation cont,
317 void *cont_cls)
318{
319 struct GNUNET_PEERSTORE_AddContext *ac;
320 struct AddEntryMessage *entry;
321 char *ss;
322 void *val;
323 size_t sub_system_size;
324 size_t request_size;
325
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "Storing value (size: %lu) for subsytem `%s' and peer `%s'",
328 size, sub_system, GNUNET_i2s (peer));
329 sub_system_size = strlen(sub_system);
330 request_size = sizeof(struct AddEntryMessage) + sub_system_size + size;
331 ac = GNUNET_malloc(sizeof(struct GNUNET_PEERSTORE_AddContext) + request_size);
332 ac->h = h;
333 ac->size = request_size;
334 entry = (struct AddEntryMessage *)&ac[1];
335 entry->header.size = htons(request_size);
336 entry->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ADD);
337 entry->peer = *peer;
338 entry->sub_system_size = sub_system_size;
339 entry->value_size = size;
340 entry->lifetime = lifetime;
341 ss = (char *)&entry[1];
342 memcpy(ss, sub_system, sub_system_size);
343 val = ss + sub_system_size;
344 memcpy(val, value, size);
345 GNUNET_CONTAINER_DLL_insert_tail(h->ac_head, h->ac_tail, ac);
346 trigger_transmit (h);
347 return ac;
348
349}
309 350
310 351
311/* end of peerstore_api.c */ 352/* end of peerstore_api.c */
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c
index 41b6044b3..1695938bb 100644
--- a/src/peerstore/plugin_peerstore_sqlite.c
+++ b/src/peerstore/plugin_peerstore_sqlite.c
@@ -85,26 +85,26 @@ struct Plugin
85 * One key can store multiple values. 85 * One key can store multiple values.
86 * 86 *
87 * @param cls closure (internal context for the plugin) 87 * @param cls closure (internal context for the plugin)
88 * @param sub_system name of the GNUnet sub system responsible
89 * @param peer peer identity 88 * @param peer peer identity
89 * @param sub_system name of the GNUnet sub system responsible
90 * @param value value to be stored 90 * @param value value to be stored
91 * @param size size of value to be stored 91 * @param size size of value to be stored
92 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 92 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
93 */ 93 */
94static int 94static int
95peerstore_sqlite_store_record (void *cls, 95peerstore_sqlite_store_record (void *cls,
96 const char *sub_system, 96 const struct GNUNET_PeerIdentity *peer,
97 const struct GNUNET_PeerIdentity *peer, 97 const char *sub_system,
98 const void *value, 98 const void *value,
99 size_t size) 99 size_t size)
100{ 100{
101 struct Plugin *plugin = cls; 101 struct Plugin *plugin = cls;
102 sqlite3_stmt *stmt = plugin->insert_peerstoredata; 102 sqlite3_stmt *stmt = plugin->insert_peerstoredata;
103 103
104 //FIXME: check if value exists with the same key first 104 //FIXME: check if value exists with the same key first
105 105
106 if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, sizeof(sub_system), SQLITE_STATIC) 106 if(SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)
107 || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) 107 || SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, sizeof(sub_system), SQLITE_STATIC)
108 || SQLITE_OK != sqlite3_bind_blob(stmt, 3, value, size, SQLITE_STATIC)) 108 || SQLITE_OK != sqlite3_bind_blob(stmt, 3, value, size, SQLITE_STATIC))
109 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 109 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
110 "sqlite3_bind"); 110 "sqlite3_bind");
@@ -227,15 +227,15 @@ database_setup (struct Plugin *plugin)
227 227
228 sql_exec (plugin->dbh, 228 sql_exec (plugin->dbh,
229 "CREATE TABLE IF NOT EXISTS peerstoredata (\n" 229 "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
230 " sub_system TEXT NOT NULL,\n"
231 " peer_id BLOB NOT NULL,\n" 230 " peer_id BLOB NOT NULL,\n"
231 " sub_system TEXT NOT NULL,\n"
232 " value BLOB NULL" 232 " value BLOB NULL"
233 ");"); 233 ");");
234 234
235 /* Prepare statements */ 235 /* Prepare statements */
236 236
237 sql_prepare (plugin->dbh, 237 sql_prepare (plugin->dbh,
238 "INSERT INTO peerstoredata (sub_system, peer_id, value) VALUES (?,?,?);", 238 "INSERT INTO peerstoredata (peer_id, sub_system, value) VALUES (?,?,?);",
239 &plugin->insert_peerstoredata); 239 &plugin->insert_peerstoredata);
240 240
241 return GNUNET_OK; 241 return GNUNET_OK;