diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-24 09:15:24 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-24 09:15:24 +0000 |
commit | 2e95a6961bf57170b215ab28e50dbf2376d46747 (patch) | |
tree | 051e4622d55647c9b42bb6d1405465924773f3f5 /src/regex | |
parent | fb80a8163d9ff402a4502e29712000d71543fe60 (diff) | |
download | gnunet-2e95a6961bf57170b215ab28e50dbf2376d46747.tar.gz gnunet-2e95a6961bf57170b215ab28e50dbf2376d46747.zip |
use new MQ API in regex_api_search
Diffstat (limited to 'src/regex')
-rw-r--r-- | src/regex/regex_api_search.c | 191 |
1 files changed, 113 insertions, 78 deletions
diff --git a/src/regex/regex_api_search.c b/src/regex/regex_api_search.c index 728e12beb..c14d8ba35 100644 --- a/src/regex/regex_api_search.c +++ b/src/regex/regex_api_search.c | |||
@@ -41,7 +41,7 @@ struct GNUNET_REGEX_Search | |||
41 | /** | 41 | /** |
42 | * Connection to the regex service. | 42 | * Connection to the regex service. |
43 | */ | 43 | */ |
44 | struct GNUNET_CLIENT_Connection *client; | 44 | struct GNUNET_MQ_Handle *mq; |
45 | 45 | ||
46 | /** | 46 | /** |
47 | * Our configuration. | 47 | * Our configuration. |
@@ -59,41 +59,43 @@ struct GNUNET_REGEX_Search | |||
59 | void *callback_cls; | 59 | void *callback_cls; |
60 | 60 | ||
61 | /** | 61 | /** |
62 | * Search message to transmit to the service. | 62 | * Search string to transmit to the service. |
63 | */ | 63 | */ |
64 | struct RegexSearchMessage *msg; | 64 | char *string; |
65 | }; | 65 | }; |
66 | 66 | ||
67 | 67 | ||
68 | /** | 68 | /** |
69 | * We got a response or disconnect after asking regex | 69 | * (Re)connect to the REGEX service for the given search @a s. |
70 | * to do the search. Handle it. | ||
71 | * | 70 | * |
72 | * @param cls the `struct GNUNET_REGEX_Search` to retry | 71 | * @param s context for the search search for |
73 | * @param msg NULL on disconnect | ||
74 | */ | 72 | */ |
75 | static void | 73 | static void |
76 | handle_search_response (void *cls, | 74 | search_reconnect (struct GNUNET_REGEX_Search *s); |
77 | const struct GNUNET_MessageHeader *msg); | ||
78 | 75 | ||
79 | 76 | ||
80 | /** | 77 | /** |
81 | * Try sending the search request to regex. On | 78 | * We got a response or disconnect after asking regex |
82 | * errors (i.e. regex died), try again. | 79 | * to do the search. Check it is well-formed. |
83 | * | 80 | * |
84 | * @param s the search to retry | 81 | * @param cls the `struct GNUNET_REGEX_Search` to handle reply for |
82 | * @param result the message | ||
83 | * @return #GNUNET_SYSERR if @a rm is not well-formed. | ||
85 | */ | 84 | */ |
86 | static void | 85 | static int |
87 | retry_search (struct GNUNET_REGEX_Search *s) | 86 | check_search_response (void *cls, |
87 | const struct ResultMessage *result) | ||
88 | { | 88 | { |
89 | GNUNET_assert (NULL != s->client); | 89 | uint16_t size = ntohs (result->header.size) - sizeof (*result); |
90 | GNUNET_assert (GNUNET_OK == | 90 | uint16_t gpl = ntohs (result->get_path_length); |
91 | GNUNET_CLIENT_transmit_and_get_response (s->client, | 91 | uint16_t ppl = ntohs (result->put_path_length); |
92 | &s->msg->header, | 92 | |
93 | GNUNET_TIME_UNIT_FOREVER_REL, | 93 | if (size != (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity)) |
94 | GNUNET_YES, | 94 | { |
95 | &handle_search_response, | 95 | GNUNET_break (0); |
96 | s)); | 96 | return GNUNET_SYSERR; |
97 | } | ||
98 | return GNUNET_OK; | ||
97 | } | 99 | } |
98 | 100 | ||
99 | 101 | ||
@@ -102,55 +104,84 @@ retry_search (struct GNUNET_REGEX_Search *s) | |||
102 | * to do the search. Handle it. | 104 | * to do the search. Handle it. |
103 | * | 105 | * |
104 | * @param cls the `struct GNUNET_REGEX_Search` to handle reply for | 106 | * @param cls the `struct GNUNET_REGEX_Search` to handle reply for |
105 | * @param msg NULL on disconnect, otherwise presumably a response | 107 | * @param result the message |
106 | */ | 108 | */ |
107 | static void | 109 | static void |
108 | handle_search_response (void *cls, | 110 | handle_search_response (void *cls, |
109 | const struct GNUNET_MessageHeader *msg) | 111 | const struct ResultMessage *result) |
110 | { | 112 | { |
111 | struct GNUNET_REGEX_Search *s = cls; | 113 | struct GNUNET_REGEX_Search *s = cls; |
112 | const struct ResultMessage *result; | 114 | uint16_t gpl = ntohs (result->get_path_length); |
113 | uint16_t size; | 115 | uint16_t ppl = ntohs (result->put_path_length); |
114 | uint16_t gpl; | 116 | const struct GNUNET_PeerIdentity *pid; |
115 | uint16_t ppl; | ||
116 | 117 | ||
117 | if (NULL == msg) | 118 | pid = &result->id; |
118 | { | 119 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
119 | GNUNET_CLIENT_disconnect (s->client); | 120 | "Got regex result %s\n", |
120 | s->client = GNUNET_CLIENT_connect ("regex", s->cfg); | 121 | GNUNET_i2s (pid)); |
121 | retry_search (s); | 122 | s->callback (s->callback_cls, |
123 | pid, | ||
124 | &pid[1], | ||
125 | gpl, | ||
126 | &pid[1 + gpl], | ||
127 | ppl); | ||
128 | } | ||
129 | |||
130 | |||
131 | /** | ||
132 | * We got a disconnect after asking regex to do the announcement. | ||
133 | * Retry. | ||
134 | * | ||
135 | * @param cls the `struct GNUNET_REGEX_Announcement` to retry | ||
136 | * @param error error code | ||
137 | */ | ||
138 | static void | ||
139 | mq_error_handler (void *cls, | ||
140 | enum GNUNET_MQ_Error error) | ||
141 | { | ||
142 | struct GNUNET_REGEX_Search *s = cls; | ||
143 | |||
144 | GNUNET_MQ_destroy (s->mq); | ||
145 | s->mq = NULL; | ||
146 | search_reconnect (s); | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * (Re)connect to the REGEX service for the given search @a s. | ||
152 | * | ||
153 | * @param s context for the search search for | ||
154 | */ | ||
155 | static void | ||
156 | search_reconnect (struct GNUNET_REGEX_Search *s) | ||
157 | { | ||
158 | GNUNET_MQ_hd_var_size (search_response, | ||
159 | GNUNET_MESSAGE_TYPE_REGEX_RESULT, | ||
160 | struct ResultMessage); | ||
161 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
162 | make_search_response_handler (s), | ||
163 | GNUNET_MQ_handler_end () | ||
164 | }; | ||
165 | size_t slen = strlen (s->string) + 1; | ||
166 | struct GNUNET_MQ_Envelope *env; | ||
167 | struct RegexSearchMessage *rsm; | ||
168 | |||
169 | GNUNET_assert (NULL == s->mq); | ||
170 | s->mq = GNUNET_CLIENT_connecT (s->cfg, | ||
171 | "regex", | ||
172 | handlers, | ||
173 | &mq_error_handler, | ||
174 | s); | ||
175 | if (NULL == s->mq) | ||
122 | return; | 176 | return; |
123 | } | 177 | env = GNUNET_MQ_msg_extra (rsm, |
124 | size = ntohs (msg->size); | 178 | slen, |
125 | if ( (GNUNET_MESSAGE_TYPE_REGEX_RESULT == ntohs (msg->type)) && | 179 | GNUNET_MESSAGE_TYPE_REGEX_SEARCH); |
126 | (size >= sizeof (struct ResultMessage)) ) | 180 | memcpy (&rsm[1], |
127 | { | 181 | s->string, |
128 | result = (const struct ResultMessage *) msg; | 182 | slen); |
129 | gpl = ntohs (result->get_path_length); | 183 | GNUNET_MQ_send (s->mq, |
130 | ppl = ntohs (result->put_path_length); | 184 | env); |
131 | if (size == (sizeof (struct ResultMessage) + | ||
132 | (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity))) | ||
133 | { | ||
134 | const struct GNUNET_PeerIdentity *pid; | ||
135 | |||
136 | GNUNET_CLIENT_receive (s->client, | ||
137 | &handle_search_response, s, | ||
138 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
139 | pid = &result->id; | ||
140 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
141 | "Got regex result %s\n", | ||
142 | GNUNET_i2s (pid)); | ||
143 | s->callback (s->callback_cls, | ||
144 | pid, | ||
145 | &pid[1], gpl, | ||
146 | &pid[1 + gpl], ppl); | ||
147 | return; | ||
148 | } | ||
149 | } | ||
150 | GNUNET_break (0); | ||
151 | GNUNET_CLIENT_disconnect (s->client); | ||
152 | s->client = GNUNET_CLIENT_connect ("regex", s->cfg); | ||
153 | retry_search (s); | ||
154 | } | 185 | } |
155 | 186 | ||
156 | 187 | ||
@@ -173,27 +204,31 @@ GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
173 | void *callback_cls) | 204 | void *callback_cls) |
174 | { | 205 | { |
175 | struct GNUNET_REGEX_Search *s; | 206 | struct GNUNET_REGEX_Search *s; |
176 | size_t slen; | 207 | size_t slen = strlen (string) + 1; |
177 | 208 | ||
209 | if (slen + sizeof (struct RegexSearchMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
210 | { | ||
211 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
212 | _("Search string `%s' is too long!\n"), | ||
213 | string); | ||
214 | GNUNET_break (0); | ||
215 | return NULL; | ||
216 | } | ||
178 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 217 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
179 | "Starting regex search for %s\n", | 218 | "Starting regex search for %s\n", |
180 | string); | 219 | string); |
181 | slen = strlen (string) + 1; | ||
182 | s = GNUNET_new (struct GNUNET_REGEX_Search); | 220 | s = GNUNET_new (struct GNUNET_REGEX_Search); |
183 | s->cfg = cfg; | 221 | s->cfg = cfg; |
184 | s->client = GNUNET_CLIENT_connect ("regex", cfg); | 222 | s->string = GNUNET_strdup (string); |
185 | if (NULL == s->client) | 223 | s->callback = callback; |
224 | s->callback_cls = callback_cls; | ||
225 | search_reconnect (s); | ||
226 | if (NULL == s->mq) | ||
186 | { | 227 | { |
228 | GNUNET_free (s->string); | ||
187 | GNUNET_free (s); | 229 | GNUNET_free (s); |
188 | return NULL; | 230 | return NULL; |
189 | } | 231 | } |
190 | s->callback = callback; | ||
191 | s->callback_cls = callback_cls; | ||
192 | s->msg = GNUNET_malloc (sizeof (struct RegexSearchMessage) + slen); | ||
193 | s->msg->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_SEARCH); | ||
194 | s->msg->header.size = htons (sizeof (struct RegexSearchMessage) + slen); | ||
195 | memcpy (&s->msg[1], string, slen); | ||
196 | retry_search (s); | ||
197 | return s; | 232 | return s; |
198 | } | 233 | } |
199 | 234 | ||
@@ -206,10 +241,10 @@ GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
206 | void | 241 | void |
207 | GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s) | 242 | GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s) |
208 | { | 243 | { |
209 | GNUNET_CLIENT_disconnect (s->client); | 244 | GNUNET_MQ_destroy (s->mq); |
210 | GNUNET_free (s->msg); | 245 | GNUNET_free (s->string); |
211 | GNUNET_free (s); | 246 | GNUNET_free (s); |
212 | } | 247 | } |
213 | 248 | ||
214 | 249 | ||
215 | /* end of regex_api.c */ | 250 | /* end of regex_api_search.c */ |