diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-08-24 15:54:21 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-08-24 15:54:21 +0000 |
commit | 0baf38e846d5a4cd5b5b4c2ec900762fceb87712 (patch) | |
tree | 0f54a86cd481c2ea93aea18e217b71a6a578114c /src/identity | |
parent | 5d8db7e97036433978f995389eac3997dae282e1 (diff) | |
download | gnunet-0baf38e846d5a4cd5b5b4c2ec900762fceb87712.tar.gz gnunet-0baf38e846d5a4cd5b5b4c2ec900762fceb87712.zip |
-more convenient API to lookup egos
Diffstat (limited to 'src/identity')
-rw-r--r-- | src/identity/Makefile.am | 4 | ||||
-rw-r--r-- | src/identity/identity_api.c | 46 | ||||
-rw-r--r-- | src/identity/identity_api_lookup.c | 154 |
3 files changed, 182 insertions, 22 deletions
diff --git a/src/identity/Makefile.am b/src/identity/Makefile.am index 29fded518..ff0ff2472 100644 --- a/src/identity/Makefile.am +++ b/src/identity/Makefile.am | |||
@@ -20,7 +20,9 @@ pkgcfg_DATA = \ | |||
20 | lib_LTLIBRARIES = libgnunetidentity.la | 20 | lib_LTLIBRARIES = libgnunetidentity.la |
21 | 21 | ||
22 | libgnunetidentity_la_SOURCES = \ | 22 | libgnunetidentity_la_SOURCES = \ |
23 | identity_api.c identity.h | 23 | identity_api.c \ |
24 | identity_api_lookup.c \ | ||
25 | identity.h | ||
24 | libgnunetidentity_la_LIBADD = \ | 26 | libgnunetidentity_la_LIBADD = \ |
25 | $(top_builddir)/src/util/libgnunetutil.la \ | 27 | $(top_builddir)/src/util/libgnunetutil.la \ |
26 | $(GN_LIBINTL) $(XLIB) | 28 | $(GN_LIBINTL) $(XLIB) |
diff --git a/src/identity/identity_api.c b/src/identity/identity_api.c index d5035ba49..57a541471 100644 --- a/src/identity/identity_api.c +++ b/src/identity/identity_api.c | |||
@@ -24,14 +24,9 @@ | |||
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_client_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include "gnunet_constants.h" | 28 | #include "gnunet_constants.h" |
29 | #include "gnunet_container_lib.h" | ||
30 | #include "gnunet_arm_service.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_protocols.h" | 29 | #include "gnunet_protocols.h" |
33 | #include "gnunet_server_lib.h" | ||
34 | #include "gnunet_time_lib.h" | ||
35 | #include "gnunet_identity_service.h" | 30 | #include "gnunet_identity_service.h" |
36 | #include "identity.h" | 31 | #include "identity.h" |
37 | 32 | ||
@@ -292,6 +287,8 @@ message_handler (void *cls, | |||
292 | GNUNET_CONTAINER_DLL_remove (h->op_head, | 287 | GNUNET_CONTAINER_DLL_remove (h->op_head, |
293 | h->op_tail, | 288 | h->op_tail, |
294 | op); | 289 | op); |
290 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
291 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
295 | if (NULL != op->cont) | 292 | if (NULL != op->cont) |
296 | op->cont (op->cls, | 293 | op->cont (op->cls, |
297 | str); | 294 | str); |
@@ -321,6 +318,8 @@ message_handler (void *cls, | |||
321 | if (GNUNET_YES == ntohs (um->end_of_list)) | 318 | if (GNUNET_YES == ntohs (um->end_of_list)) |
322 | { | 319 | { |
323 | /* end of initial list of data */ | 320 | /* end of initial list of data */ |
321 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
322 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
324 | if (NULL != h->cb) | 323 | if (NULL != h->cb) |
325 | h->cb (h->cb_cls, NULL, NULL, NULL); | 324 | h->cb (h->cb_cls, NULL, NULL, NULL); |
326 | break; | 325 | break; |
@@ -355,12 +354,6 @@ message_handler (void *cls, | |||
355 | ego, | 354 | ego, |
356 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 355 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); |
357 | } | 356 | } |
358 | /* inform application about change */ | ||
359 | if (NULL != h->cb) | ||
360 | h->cb (h->cb_cls, | ||
361 | ego, | ||
362 | &ego->ctx, | ||
363 | str); | ||
364 | if (NULL == str) | 357 | if (NULL == str) |
365 | { | 358 | { |
366 | /* ego was deleted */ | 359 | /* ego was deleted */ |
@@ -368,16 +361,27 @@ message_handler (void *cls, | |||
368 | GNUNET_CONTAINER_multihashmap_remove (h->egos, | 361 | GNUNET_CONTAINER_multihashmap_remove (h->egos, |
369 | &ego->id, | 362 | &ego->id, |
370 | ego)); | 363 | ego)); |
371 | GNUNET_free (ego->pk); | ||
372 | GNUNET_free (ego->name); | ||
373 | GNUNET_free (ego); | ||
374 | } | 364 | } |
375 | else | 365 | else |
376 | { | 366 | { |
377 | /* ego changed name */ | 367 | /* ego changed name */ |
378 | GNUNET_free (ego->name); | 368 | GNUNET_free (ego->name); |
379 | ego->name = GNUNET_strdup (str); | 369 | ego->name = GNUNET_strdup (str); |
380 | } | 370 | } |
371 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
372 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
373 | /* inform application about change */ | ||
374 | if (NULL != h->cb) | ||
375 | h->cb (h->cb_cls, | ||
376 | ego, | ||
377 | &ego->ctx, | ||
378 | str); | ||
379 | if (NULL == str) | ||
380 | { | ||
381 | GNUNET_free (ego->pk); | ||
382 | GNUNET_free (ego->name); | ||
383 | GNUNET_free (ego); | ||
384 | } | ||
381 | break; | 385 | break; |
382 | case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT: | 386 | case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT: |
383 | if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) | 387 | if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) |
@@ -416,6 +420,8 @@ message_handler (void *cls, | |||
416 | h->op_tail, | 420 | h->op_tail, |
417 | op); | 421 | op); |
418 | GNUNET_free (ego->name); | 422 | GNUNET_free (ego->name); |
423 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
424 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
419 | if (NULL != op->cb) | 425 | if (NULL != op->cb) |
420 | op->cb (op->cls, | 426 | op->cb (op->cls, |
421 | ego, | 427 | ego, |
@@ -428,8 +434,6 @@ message_handler (void *cls, | |||
428 | reschedule_connect (h); | 434 | reschedule_connect (h); |
429 | return; | 435 | return; |
430 | } | 436 | } |
431 | GNUNET_CLIENT_receive (h->client, &message_handler, h, | ||
432 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
433 | } | 437 | } |
434 | 438 | ||
435 | 439 | ||
@@ -565,7 +569,7 @@ GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
565 | { | 569 | { |
566 | struct GNUNET_IDENTITY_Handle *h; | 570 | struct GNUNET_IDENTITY_Handle *h; |
567 | 571 | ||
568 | h = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle)); | 572 | h = GNUNET_new (struct GNUNET_IDENTITY_Handle); |
569 | h->cfg = cfg; | 573 | h->cfg = cfg; |
570 | h->cb = cb; | 574 | h->cb = cb; |
571 | h->cb_cls = cb_cls; | 575 | h->cb_cls = cb_cls; |
@@ -821,7 +825,7 @@ GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id, | |||
821 | * @param id identity service to use | 825 | * @param id identity service to use |
822 | * @param name name of the identity to delete | 826 | * @param name name of the identity to delete |
823 | * @param cb function to call with the result (will only be called once) | 827 | * @param cb function to call with the result (will only be called once) |
824 | * @param cb_cls closure for cb | 828 | * @param cb_cls closure for @a cb |
825 | * @return handle to abort the operation | 829 | * @return handle to abort the operation |
826 | */ | 830 | */ |
827 | struct GNUNET_IDENTITY_Operation * | 831 | struct GNUNET_IDENTITY_Operation * |
@@ -910,7 +914,7 @@ GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op) | |||
910 | * @param cls identity service handle | 914 | * @param cls identity service handle |
911 | * @param key unused | 915 | * @param key unused |
912 | * @param value ego to free | 916 | * @param value ego to free |
913 | * @return GNUNET_OK (continue to iterate) | 917 | * @return #GNUNET_OK (continue to iterate) |
914 | */ | 918 | */ |
915 | static int | 919 | static int |
916 | free_ego (void *cls, | 920 | free_ego (void *cls, |
diff --git a/src/identity/identity_api_lookup.c b/src/identity/identity_api_lookup.c new file mode 100644 index 000000000..1d45136e3 --- /dev/null +++ b/src/identity/identity_api_lookup.c | |||
@@ -0,0 +1,154 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public Liceidentity as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public Liceidentity for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public Liceidentity | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file identity/identity_api_lookup.c | ||
23 | * @brief api to lookup an ego | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_identity_service.h" | ||
29 | |||
30 | #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__) | ||
31 | |||
32 | |||
33 | /** | ||
34 | * Handle for ego lookup. | ||
35 | */ | ||
36 | struct GNUNET_IDENTITY_EgoLookup | ||
37 | { | ||
38 | |||
39 | /** | ||
40 | * Handle to the identity service. | ||
41 | */ | ||
42 | struct GNUNET_IDENTITY_Handle *identity; | ||
43 | |||
44 | /** | ||
45 | * Name of the ego we are looking up. | ||
46 | */ | ||
47 | char *name; | ||
48 | |||
49 | /** | ||
50 | * Function to call with the result. | ||
51 | */ | ||
52 | GNUNET_IDENTITY_EgoCallback cb; | ||
53 | |||
54 | /** | ||
55 | * Closure for @e cb | ||
56 | */ | ||
57 | void *cb_cls; | ||
58 | }; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Method called to inform about the egos of this peer. | ||
63 | * | ||
64 | * When used with #GNUNET_IDENTITY_connect, this function is | ||
65 | * initially called for all egos and then again whenever a | ||
66 | * ego's name changes or if it is deleted. At the end of | ||
67 | * the initial pass over all egos, the function is once called | ||
68 | * with 'NULL' for @a ego. That does NOT mean that the callback won't | ||
69 | * be invoked in the future or that there was an error. | ||
70 | * | ||
71 | * If the @a name matches the name from @a cls, we found the zone | ||
72 | * for our computation and will invoke the callback. | ||
73 | * If we have iterated over all egos and not found the name, we | ||
74 | * invoke the callback with NULL. | ||
75 | * | ||
76 | * @param cls closure with the `struct GNUNET_IDENTITY_EgoLookup` | ||
77 | * @param ego ego handle | ||
78 | * @param ego_ctx context for application to store data for this ego | ||
79 | * (during the lifetime of this process, initially NULL) | ||
80 | * @param name name assigned by the user for this ego, | ||
81 | * NULL if the user just deleted the ego and it | ||
82 | * must thus no longer be used | ||
83 | */ | ||
84 | static void | ||
85 | identity_cb (void *cls, | ||
86 | struct GNUNET_IDENTITY_Ego *ego, | ||
87 | void **ctx, | ||
88 | const char *name) | ||
89 | { | ||
90 | struct GNUNET_IDENTITY_EgoLookup *el = cls; | ||
91 | |||
92 | if ( (NULL != name) && | ||
93 | (0 == strcmp (name, | ||
94 | el->name)) ) | ||
95 | { | ||
96 | el->cb (el->cb_cls, | ||
97 | ego); | ||
98 | GNUNET_IDENTITY_ego_lookup_cancel (el); | ||
99 | return; | ||
100 | } | ||
101 | if (NULL == ego) | ||
102 | { | ||
103 | /* not found */ | ||
104 | el->cb (el->cb_cls, | ||
105 | NULL); | ||
106 | GNUNET_IDENTITY_ego_lookup_cancel (el); | ||
107 | return; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Lookup an ego by name. | ||
114 | * | ||
115 | * @param cfg configuration to use | ||
116 | * @param name name to look up | ||
117 | * @param cb callback to invoke with the result | ||
118 | * @param cb_cls closure for @a cb | ||
119 | * @return NULL on error | ||
120 | */ | ||
121 | struct GNUNET_IDENTITY_EgoLookup * | ||
122 | GNUNET_IDENTITY_ego_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
123 | const char *name, | ||
124 | GNUNET_IDENTITY_EgoCallback cb, | ||
125 | void *cb_cls) | ||
126 | { | ||
127 | struct GNUNET_IDENTITY_EgoLookup *el; | ||
128 | |||
129 | el = GNUNET_new (struct GNUNET_IDENTITY_EgoLookup); | ||
130 | el->name = GNUNET_strdup (name); | ||
131 | el->cb = cb; | ||
132 | el->cb_cls = cb_cls; | ||
133 | el->identity = GNUNET_IDENTITY_connect (cfg, | ||
134 | &identity_cb, | ||
135 | el); | ||
136 | return el; | ||
137 | } | ||
138 | |||
139 | |||
140 | /** | ||
141 | * Abort ego lookup attempt. | ||
142 | * | ||
143 | * @param el handle for lookup to abort | ||
144 | */ | ||
145 | void | ||
146 | GNUNET_IDENTITY_ego_lookup_cancel (struct GNUNET_IDENTITY_EgoLookup *el) | ||
147 | { | ||
148 | GNUNET_IDENTITY_disconnect (el->identity); | ||
149 | GNUNET_free (el->name); | ||
150 | GNUNET_free (el); | ||
151 | } | ||
152 | |||
153 | |||
154 | /* end of identity_api_lookup.c */ | ||