diff options
author | Christian Grothoff <christian@grothoff.org> | 2014-06-02 18:47:15 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2014-06-02 18:47:15 +0000 |
commit | 5fe13812e969155a5d57cba0c19d4e70924d6161 (patch) | |
tree | d8de0e228900956717a352ba0abd219dfde84ce6 | |
parent | 21ad99e540121a1fef25e955b1e3738972a74008 (diff) | |
download | gnunet-gtk-5fe13812e969155a5d57cba0c19d4e70924d6161.tar.gz gnunet-gtk-5fe13812e969155a5d57cba0c19d4e70924d6161.zip |
adding logic to publish our phone record
-rw-r--r-- | src/conversation/gnunet-conversation-gtk.c | 2 | ||||
-rw-r--r-- | src/conversation/gnunet-conversation-gtk_import.c | 268 |
2 files changed, 253 insertions, 17 deletions
diff --git a/src/conversation/gnunet-conversation-gtk.c b/src/conversation/gnunet-conversation-gtk.c index efe7f082..dfec0e62 100644 --- a/src/conversation/gnunet-conversation-gtk.c +++ b/src/conversation/gnunet-conversation-gtk.c | |||
@@ -31,6 +31,8 @@ | |||
31 | #include "gnunet-conversation-gtk_history.h" | 31 | #include "gnunet-conversation-gtk_history.h" |
32 | #include "gnunet-conversation-gtk_import.h" | 32 | #include "gnunet-conversation-gtk_import.h" |
33 | #include "gnunet-conversation-gtk_phone.h" | 33 | #include "gnunet-conversation-gtk_phone.h" |
34 | #include "gnunet-conversation-gtk_zones.h" | ||
35 | |||
34 | 36 | ||
35 | /** | 37 | /** |
36 | * Handle to our main loop. | 38 | * Handle to our main loop. |
diff --git a/src/conversation/gnunet-conversation-gtk_import.c b/src/conversation/gnunet-conversation-gtk_import.c index 5458bac3..0d78445b 100644 --- a/src/conversation/gnunet-conversation-gtk_import.c +++ b/src/conversation/gnunet-conversation-gtk_import.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "gnunet-conversation-gtk.h" | 28 | #include "gnunet-conversation-gtk.h" |
29 | #include "gnunet-conversation-gtk_contacts.h" | 29 | #include "gnunet-conversation-gtk_contacts.h" |
30 | #include "gnunet-conversation-gtk_egos.h" | 30 | #include "gnunet-conversation-gtk_egos.h" |
31 | #include "gnunet-conversation-gtk_import.h" | ||
31 | 32 | ||
32 | /** | 33 | /** |
33 | * Handle to the namestore. | 34 | * Handle to the namestore. |
@@ -35,9 +36,50 @@ | |||
35 | static struct GNUNET_NAMESTORE_Handle *ns; | 36 | static struct GNUNET_NAMESTORE_Handle *ns; |
36 | 37 | ||
37 | /** | 38 | /** |
38 | * Queue entry for the 'add' operation. | 39 | * Queue entry for the 'add contact' operation. |
39 | */ | 40 | */ |
40 | static struct GNUNET_NAMESTORE_QueueEntry *add_qe; | 41 | static struct GNUNET_NAMESTORE_QueueEntry *add_contact_qe; |
42 | |||
43 | /** | ||
44 | * Queue entry for the 'add phone' operation. | ||
45 | */ | ||
46 | static struct GNUNET_NAMESTORE_QueueEntry *add_phone_qe; | ||
47 | |||
48 | /** | ||
49 | * Queue entry for the 'remove phone' operation. | ||
50 | */ | ||
51 | static struct GNUNET_NAMESTORE_QueueEntry *remove_phone_qe; | ||
52 | |||
53 | /** | ||
54 | * Flag set to #GNUNET_YES if we are in shutdown. | ||
55 | */ | ||
56 | static int in_shutdown; | ||
57 | |||
58 | /** | ||
59 | * Flag set to #GNUNET_YES if an 'add phone' operation is | ||
60 | * waiting for a 'remove phone' operation to complete first. | ||
61 | */ | ||
62 | static int add_waiting; | ||
63 | |||
64 | /** | ||
65 | * Our current phone label. | ||
66 | */ | ||
67 | static char *phone_label; | ||
68 | |||
69 | /** | ||
70 | * Current private key for the phone entry's zone. | ||
71 | */ | ||
72 | static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey; | ||
73 | |||
74 | /** | ||
75 | * Our current phone record. | ||
76 | */ | ||
77 | static struct GNUNET_GNSRECORD_Data my_rd; | ||
78 | |||
79 | /** | ||
80 | * Binary data for the current phone record (`my_rd.data`). | ||
81 | */ | ||
82 | static char *my_rd_data; | ||
41 | 83 | ||
42 | 84 | ||
43 | /** | 85 | /** |
@@ -51,14 +93,14 @@ static struct GNUNET_NAMESTORE_QueueEntry *add_qe; | |||
51 | * @param emsg NULL on success, otherwise an error message | 93 | * @param emsg NULL on success, otherwise an error message |
52 | */ | 94 | */ |
53 | static void | 95 | static void |
54 | add_continuation (void *cls, | 96 | add_contact_continuation (void *cls, |
55 | int32_t success, | 97 | int32_t success, |
56 | const char *emsg) | 98 | const char *emsg) |
57 | { | 99 | { |
58 | if (GNUNET_YES != success) | 100 | if (GNUNET_YES != success) |
59 | GCG_log (_("Adding contact failed: %s\n"), | 101 | GCG_log (_("Adding contact failed: %s\n"), |
60 | (GNUNET_NO == success) ? _("record exists") : emsg); | 102 | (GNUNET_NO == success) ? _("record exists") : emsg); |
61 | add_qe = NULL; | 103 | add_contact_qe = NULL; |
62 | } | 104 | } |
63 | 105 | ||
64 | 106 | ||
@@ -77,7 +119,7 @@ GSC_add_contact (const gchar *name, | |||
77 | struct GNUNET_IDENTITY_Ego *ego; | 119 | struct GNUNET_IDENTITY_Ego *ego; |
78 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey; | 120 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey; |
79 | 121 | ||
80 | if (NULL != add_qe) | 122 | if (NULL != add_contact_qe) |
81 | { | 123 | { |
82 | GCG_log (_("Adding contact failed: %s\n"), | 124 | GCG_log (_("Adding contact failed: %s\n"), |
83 | _("previous operation still pending")); | 125 | _("previous operation still pending")); |
@@ -107,12 +149,89 @@ GSC_add_contact (const gchar *name, | |||
107 | zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | 149 | zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); |
108 | /* FIXME: we should check if the 'name' already exists, | 150 | /* FIXME: we should check if the 'name' already exists, |
109 | and not do this if it does... */ | 151 | and not do this if it does... */ |
110 | add_qe | 152 | add_contact_qe |
111 | = GNUNET_NAMESTORE_records_store (ns, | 153 | = GNUNET_NAMESTORE_records_store (ns, |
112 | zone_pkey, | 154 | zone_pkey, |
113 | name, | 155 | name, |
114 | 1, &rd, | 156 | 1, &rd, |
115 | &add_continuation, NULL); | 157 | &add_contact_continuation, NULL); |
158 | } | ||
159 | |||
160 | |||
161 | /** | ||
162 | * Continuation called to notify client about result of the | ||
163 | * add operation. Finish 'add phone' operation. | ||
164 | * | ||
165 | * @param cls closure | ||
166 | * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate) | ||
167 | * #GNUNET_NO if content was already there or not found | ||
168 | * #GNUNET_YES (or other positive value) on success | ||
169 | * @param emsg NULL on success, otherwise an error message | ||
170 | */ | ||
171 | static void | ||
172 | add_phone_continuation (void *cls, | ||
173 | int32_t success, | ||
174 | const char *emsg) | ||
175 | { | ||
176 | add_phone_qe = NULL; | ||
177 | if (GNUNET_SYSERR == success) | ||
178 | { | ||
179 | GCG_log (_("Failed to publish my PHONE record: %s\n"), | ||
180 | emsg); | ||
181 | GNUNET_free (phone_label); | ||
182 | phone_label = NULL; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Process a record that was stored in the namestore. | ||
189 | * | ||
190 | * @param cls closure, NULL | ||
191 | * @param zone private key of the zone; NULL on disconnect | ||
192 | * @param label label of the records; NULL on disconnect | ||
193 | * @param rd_count number of entries in @a rd array, 0 if label was deleted | ||
194 | * @param rd array of records with data to store | ||
195 | */ | ||
196 | static void | ||
197 | add_phone_handle_existing_records (void *cls, | ||
198 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
199 | const char *label, | ||
200 | unsigned int rd_count, | ||
201 | const struct GNUNET_GNSRECORD_Data *rd) | ||
202 | { | ||
203 | struct GNUNET_GNSRECORD_Data rd_new[rd_count + 1]; | ||
204 | |||
205 | add_phone_qe = NULL; | ||
206 | memcpy (rd_new, rd, sizeof (struct GNUNET_GNSRECORD_Data) * rd_count); | ||
207 | rd_new[rd_count] = my_rd; | ||
208 | add_phone_qe = GNUNET_NAMESTORE_records_store (ns, | ||
209 | zone, | ||
210 | label, | ||
211 | rd_count + 1, rd_new, | ||
212 | &add_phone_continuation, | ||
213 | NULL); | ||
214 | } | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Add the data from #my_rd under the #phone_label to the namestore. | ||
219 | */ | ||
220 | static void | ||
221 | add_phone () | ||
222 | { | ||
223 | struct GNUNET_IDENTITY_Ego *ego; | ||
224 | |||
225 | GNUNET_assert (NULL == remove_phone_qe); | ||
226 | GNUNET_assert (NULL == add_phone_qe); | ||
227 | add_waiting = GNUNET_NO; | ||
228 | ego = GCG_EGOS_get_selected_ego (); | ||
229 | zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego); | ||
230 | add_phone_qe = GNUNET_NAMESTORE_records_lookup (ns, | ||
231 | &zone_pkey, | ||
232 | phone_label, | ||
233 | &add_phone_handle_existing_records, | ||
234 | NULL); | ||
116 | } | 235 | } |
117 | 236 | ||
118 | 237 | ||
@@ -126,7 +245,106 @@ void | |||
126 | GSC_add_phone (const gchar *label, | 245 | GSC_add_phone (const gchar *label, |
127 | const struct GNUNET_GNSRECORD_Data *rd) | 246 | const struct GNUNET_GNSRECORD_Data *rd) |
128 | { | 247 | { |
129 | GNUNET_break (0); // FIXME: not implemented! | 248 | if (NULL != add_phone_qe) |
249 | { | ||
250 | GNUNET_NAMESTORE_cancel (add_phone_qe); | ||
251 | add_phone_qe = NULL; | ||
252 | } | ||
253 | if (NULL != phone_label) | ||
254 | GSC_remove_phone (); | ||
255 | GNUNET_break (NULL == phone_label); | ||
256 | GNUNET_free_non_null (my_rd_data); | ||
257 | phone_label = GNUNET_strdup (label); | ||
258 | my_rd = *rd; | ||
259 | my_rd.data = my_rd_data = GNUNET_malloc (rd->data_size); | ||
260 | memcpy (my_rd_data, rd->data, rd->data_size); | ||
261 | add_waiting = GNUNET_YES; | ||
262 | if (NULL != remove_phone_qe) | ||
263 | return; /* do not add another record until previous one is removed */ | ||
264 | add_phone (); | ||
265 | } | ||
266 | |||
267 | |||
268 | /** | ||
269 | * Run last parts of shutdown operation (after last | ||
270 | * remove has completed). | ||
271 | */ | ||
272 | static void | ||
273 | finish_shutdown () | ||
274 | { | ||
275 | if (NULL != ns) | ||
276 | { | ||
277 | GNUNET_NAMESTORE_disconnect (ns); | ||
278 | ns = NULL; | ||
279 | } | ||
280 | GNUNET_free_non_null (my_rd_data); | ||
281 | memset (&my_rd, 0, sizeof (my_rd)); | ||
282 | GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey); | ||
283 | } | ||
284 | |||
285 | |||
286 | /** | ||
287 | * Continuation called to notify client about result of the | ||
288 | * remove operation. Finish remove operation and continue | ||
289 | * with 'add phone' operation if one is pending. | ||
290 | * | ||
291 | * @param cls closure | ||
292 | * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate) | ||
293 | * #GNUNET_NO if content was already there or not found | ||
294 | * #GNUNET_YES (or other positive value) on success | ||
295 | * @param emsg NULL on success, otherwise an error message | ||
296 | */ | ||
297 | static void | ||
298 | remove_phone_continuation (void *cls, | ||
299 | int32_t success, | ||
300 | const char *emsg) | ||
301 | { | ||
302 | remove_phone_qe = NULL; | ||
303 | if (GNUNET_SYSERR == success) | ||
304 | GCG_log (_("Failed to remove PHONE record: %s\n"), | ||
305 | emsg); | ||
306 | if (GNUNET_YES == add_waiting) | ||
307 | add_phone (); | ||
308 | if (GNUNET_YES == in_shutdown) | ||
309 | finish_shutdown (); | ||
310 | } | ||
311 | |||
312 | |||
313 | /** | ||
314 | * Process records stored in the namestore. Locates our | ||
315 | * PHONE record and removes it. | ||
316 | * | ||
317 | * @param cls closure, NULL | ||
318 | * @param zone private key of the zone; NULL on disconnect | ||
319 | * @param label label of the records; NULL on disconnect | ||
320 | * @param rd_count number of entries in @a rd array, 0 if label was deleted | ||
321 | * @param rd array of records with data to store | ||
322 | */ | ||
323 | static void | ||
324 | remove_phone_handle_existing_records (void *cls, | ||
325 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
326 | const char *label, | ||
327 | unsigned int rd_count, | ||
328 | const struct GNUNET_GNSRECORD_Data *rd) | ||
329 | { | ||
330 | struct GNUNET_GNSRECORD_Data rd_left[rd_count]; | ||
331 | unsigned int i; | ||
332 | unsigned int j; | ||
333 | |||
334 | remove_phone_qe = NULL; | ||
335 | j = 0; | ||
336 | for (i=0;i<rd_count;i++) | ||
337 | { | ||
338 | if (GNUNET_GNSRECORD_TYPE_PHONE == rd[i].record_type) | ||
339 | continue; | ||
340 | rd_left[j++] = rd[i]; | ||
341 | } | ||
342 | remove_phone_qe = GNUNET_NAMESTORE_records_store (ns, | ||
343 | zone, | ||
344 | label, | ||
345 | j, rd_left, | ||
346 | &remove_phone_continuation, | ||
347 | NULL); | ||
130 | } | 348 | } |
131 | 349 | ||
132 | 350 | ||
@@ -136,7 +354,16 @@ GSC_add_phone (const gchar *label, | |||
136 | void | 354 | void |
137 | GSC_remove_phone () | 355 | GSC_remove_phone () |
138 | { | 356 | { |
139 | GNUNET_break (0); // FIXME: not implemented! | 357 | if (NULL == phone_label) |
358 | return; | ||
359 | GNUNET_assert (NULL == remove_phone_qe); | ||
360 | remove_phone_qe = GNUNET_NAMESTORE_records_lookup (ns, | ||
361 | &zone_pkey, | ||
362 | phone_label, | ||
363 | &remove_phone_handle_existing_records, | ||
364 | NULL); | ||
365 | GNUNET_free (phone_label); | ||
366 | phone_label = NULL; | ||
140 | } | 367 | } |
141 | 368 | ||
142 | 369 | ||
@@ -156,17 +383,24 @@ GCG_IMPORT_init () | |||
156 | void | 383 | void |
157 | GCG_IMPORT_shutdown () | 384 | GCG_IMPORT_shutdown () |
158 | { | 385 | { |
159 | if (NULL != add_qe) | 386 | if (NULL != add_contact_qe) |
160 | { | 387 | { |
161 | GNUNET_NAMESTORE_cancel (add_qe); | 388 | GNUNET_NAMESTORE_cancel (add_contact_qe); |
162 | add_qe = NULL; | 389 | add_contact_qe = NULL; |
163 | } | 390 | } |
164 | if (NULL != ns) | 391 | if (NULL != add_phone_qe) |
165 | { | 392 | { |
166 | GNUNET_NAMESTORE_disconnect (ns); | 393 | GNUNET_NAMESTORE_cancel (add_phone_qe); |
167 | ns = NULL; | 394 | add_phone_qe = NULL; |
168 | } | 395 | } |
396 | add_waiting = GNUNET_NO; | ||
397 | in_shutdown = GNUNET_YES; | ||
398 | GSC_remove_phone (); | ||
399 | if (NULL != remove_phone_qe) | ||
400 | return; | ||
401 | finish_shutdown (); | ||
169 | } | 402 | } |
170 | 403 | ||
404 | |||
171 | /* end of gnunet-conversation-gtk_import.c */ | 405 | /* end of gnunet-conversation-gtk_import.c */ |
172 | 406 | ||