aboutsummaryrefslogtreecommitdiff
path: root/src/ats/ats_api2_application.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ats/ats_api2_application.c')
-rw-r--r--src/ats/ats_api2_application.c202
1 files changed, 103 insertions, 99 deletions
diff --git a/src/ats/ats_api2_application.c b/src/ats/ats_api2_application.c
index 2a21a116a..46e57c5bb 100644
--- a/src/ats/ats_api2_application.c
+++ b/src/ats/ats_api2_application.c
@@ -28,13 +28,15 @@
28#include "ats2.h" 28#include "ats2.h"
29 29
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "ats-application-api", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "ats-application-api", \
32 __VA_ARGS__)
32 33
33 34
34/** 35/**
35 * Handle for ATS address suggestion requests. 36 * Handle for ATS address suggestion requests.
36 */ 37 */
37struct GNUNET_ATS_ApplicationSuggestHandle { 38struct GNUNET_ATS_ApplicationSuggestHandle
39{
38 /** 40 /**
39 * ID of the peer for which address suggestion was requested. 41 * ID of the peer for which address suggestion was requested.
40 */ 42 */
@@ -60,7 +62,8 @@ struct GNUNET_ATS_ApplicationSuggestHandle {
60/** 62/**
61 * Handle to the ATS subsystem for application management. 63 * Handle to the ATS subsystem for application management.
62 */ 64 */
63struct GNUNET_ATS_ApplicationHandle { 65struct GNUNET_ATS_ApplicationHandle
66{
64 /** 67 /**
65 * Our configuration. 68 * Our configuration.
66 */ 69 */
@@ -96,7 +99,7 @@ struct GNUNET_ATS_ApplicationHandle {
96 * @param ch handle to use to re-connect. 99 * @param ch handle to use to re-connect.
97 */ 100 */
98static void 101static void
99reconnect(struct GNUNET_ATS_ApplicationHandle *ch); 102reconnect (struct GNUNET_ATS_ApplicationHandle *ch);
100 103
101 104
102/** 105/**
@@ -105,12 +108,12 @@ reconnect(struct GNUNET_ATS_ApplicationHandle *ch);
105 * @param cls handle to use to re-connect. 108 * @param cls handle to use to re-connect.
106 */ 109 */
107static void 110static void
108reconnect_task(void *cls) 111reconnect_task (void *cls)
109{ 112{
110 struct GNUNET_ATS_ApplicationHandle *ch = cls; 113 struct GNUNET_ATS_ApplicationHandle *ch = cls;
111 114
112 ch->task = NULL; 115 ch->task = NULL;
113 reconnect(ch); 116 reconnect (ch);
114} 117}
115 118
116 119
@@ -120,17 +123,17 @@ reconnect_task(void *cls)
120 * @param ch our handle 123 * @param ch our handle
121 */ 124 */
122static void 125static void
123force_reconnect(struct GNUNET_ATS_ApplicationHandle *ch) 126force_reconnect (struct GNUNET_ATS_ApplicationHandle *ch)
124{ 127{
125 if (NULL != ch->mq) 128 if (NULL != ch->mq)
126 { 129 {
127 GNUNET_MQ_destroy(ch->mq); 130 GNUNET_MQ_destroy (ch->mq);
128 ch->mq = NULL; 131 ch->mq = NULL;
129 } 132 }
130 ch->backoff = GNUNET_TIME_STD_BACKOFF(ch->backoff); 133 ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
131 ch->task = GNUNET_SCHEDULER_add_delayed(ch->backoff, 134 ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff,
132 &reconnect_task, 135 &reconnect_task,
133 ch); 136 ch);
134} 137}
135 138
136 139
@@ -142,15 +145,15 @@ force_reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
142 * @param error details about the error 145 * @param error details about the error
143 */ 146 */
144static void 147static void
145error_handler(void *cls, 148error_handler (void *cls,
146 enum GNUNET_MQ_Error error) 149 enum GNUNET_MQ_Error error)
147{ 150{
148 struct GNUNET_ATS_ApplicationHandle *ch = cls; 151 struct GNUNET_ATS_ApplicationHandle *ch = cls;
149 152
150 LOG(GNUNET_ERROR_TYPE_DEBUG, 153 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 "ATS connection died (code %d), reconnecting\n", 154 "ATS connection died (code %d), reconnecting\n",
152 (int)error); 155 (int) error);
153 force_reconnect(ch); 156 force_reconnect (ch);
154} 157}
155 158
156 159
@@ -164,9 +167,9 @@ error_handler(void *cls,
164 * failure (message queue no longer exists) 167 * failure (message queue no longer exists)
165 */ 168 */
166static int 169static int
167transmit_suggestion(void *cls, 170transmit_suggestion (void *cls,
168 const struct GNUNET_PeerIdentity *peer, 171 const struct GNUNET_PeerIdentity *peer,
169 void *value) 172 void *value)
170{ 173{
171 struct GNUNET_ATS_ApplicationHandle *ch = cls; 174 struct GNUNET_ATS_ApplicationHandle *ch = cls;
172 struct GNUNET_ATS_ApplicationSuggestHandle *sh = value; 175 struct GNUNET_ATS_ApplicationSuggestHandle *sh = value;
@@ -175,12 +178,12 @@ transmit_suggestion(void *cls,
175 178
176 if (NULL == ch->mq) 179 if (NULL == ch->mq)
177 return GNUNET_SYSERR; 180 return GNUNET_SYSERR;
178 ev = GNUNET_MQ_msg(m, 181 ev = GNUNET_MQ_msg (m,
179 GNUNET_MESSAGE_TYPE_ATS_SUGGEST); 182 GNUNET_MESSAGE_TYPE_ATS_SUGGEST);
180 m->pk = htonl((uint32_t)sh->pk); 183 m->pk = htonl ((uint32_t) sh->pk);
181 m->bw = sh->bw; 184 m->bw = sh->bw;
182 m->peer = *peer; 185 m->peer = *peer;
183 GNUNET_MQ_send(ch->mq, ev); 186 GNUNET_MQ_send (ch->mq, ev);
184 return GNUNET_OK; 187 return GNUNET_OK;
185} 188}
186 189
@@ -191,26 +194,26 @@ transmit_suggestion(void *cls,
191 * @param ch handle to use to re-connect. 194 * @param ch handle to use to re-connect.
192 */ 195 */
193static void 196static void
194reconnect(struct GNUNET_ATS_ApplicationHandle *ch) 197reconnect (struct GNUNET_ATS_ApplicationHandle *ch)
195{ 198{
196 static const struct GNUNET_MQ_MessageHandler handlers[] = { 199 static const struct GNUNET_MQ_MessageHandler handlers[] = {
197 { NULL, 0, 0 } 200 { NULL, 0, 0 }
198 }; 201 };
199 202
200 GNUNET_assert(NULL == ch->mq); 203 GNUNET_assert (NULL == ch->mq);
201 ch->mq = GNUNET_CLIENT_connect(ch->cfg, 204 ch->mq = GNUNET_CLIENT_connect (ch->cfg,
202 "ats", 205 "ats",
203 handlers, 206 handlers,
204 &error_handler, 207 &error_handler,
205 ch); 208 ch);
206 if (NULL == ch->mq) 209 if (NULL == ch->mq)
207 { 210 {
208 force_reconnect(ch); 211 force_reconnect (ch);
209 return; 212 return;
210 } 213 }
211 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests, 214 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
212 &transmit_suggestion, 215 &transmit_suggestion,
213 ch); 216 ch);
214} 217}
215 218
216 219
@@ -221,15 +224,15 @@ reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
221 * @return ats application handle, NULL on error 224 * @return ats application handle, NULL on error
222 */ 225 */
223struct GNUNET_ATS_ApplicationHandle * 226struct GNUNET_ATS_ApplicationHandle *
224GNUNET_ATS_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg) 227GNUNET_ATS_application_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
225{ 228{
226 struct GNUNET_ATS_ApplicationHandle *ch; 229 struct GNUNET_ATS_ApplicationHandle *ch;
227 230
228 ch = GNUNET_new(struct GNUNET_ATS_ApplicationHandle); 231 ch = GNUNET_new (struct GNUNET_ATS_ApplicationHandle);
229 ch->cfg = cfg; 232 ch->cfg = cfg;
230 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create(32, 233 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
231 GNUNET_YES); 234 GNUNET_YES);
232 reconnect(ch); 235 reconnect (ch);
233 return ch; 236 return ch;
234} 237}
235 238
@@ -244,13 +247,13 @@ GNUNET_ATS_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
244 * @return #GNUNET_OK (continue to iterate) 247 * @return #GNUNET_OK (continue to iterate)
245 */ 248 */
246static int 249static int
247free_sug_handle(void *cls, 250free_sug_handle (void *cls,
248 const struct GNUNET_PeerIdentity *key, 251 const struct GNUNET_PeerIdentity *key,
249 void *value) 252 void *value)
250{ 253{
251 struct GNUNET_ATS_ApplicationSuggestHandle *cur = value; 254 struct GNUNET_ATS_ApplicationSuggestHandle *cur = value;
252 255
253 GNUNET_free(cur); 256 GNUNET_free (cur);
254 return GNUNET_OK; 257 return GNUNET_OK;
255} 258}
256 259
@@ -261,23 +264,23 @@ free_sug_handle(void *cls,
261 * @param ch handle to release 264 * @param ch handle to release
262 */ 265 */
263void 266void
264GNUNET_ATS_application_done(struct GNUNET_ATS_ApplicationHandle *ch) 267GNUNET_ATS_application_done (struct GNUNET_ATS_ApplicationHandle *ch)
265{ 268{
266 if (NULL != ch->mq) 269 if (NULL != ch->mq)
267 { 270 {
268 GNUNET_MQ_destroy(ch->mq); 271 GNUNET_MQ_destroy (ch->mq);
269 ch->mq = NULL; 272 ch->mq = NULL;
270 } 273 }
271 if (NULL != ch->task) 274 if (NULL != ch->task)
272 { 275 {
273 GNUNET_SCHEDULER_cancel(ch->task); 276 GNUNET_SCHEDULER_cancel (ch->task);
274 ch->task = NULL; 277 ch->task = NULL;
275 } 278 }
276 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests, 279 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
277 &free_sug_handle, 280 &free_sug_handle,
278 NULL); 281 NULL);
279 GNUNET_CONTAINER_multipeermap_destroy(ch->sug_requests); 282 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
280 GNUNET_free(ch); 283 GNUNET_free (ch);
281} 284}
282 285
283 286
@@ -295,31 +298,31 @@ GNUNET_ATS_application_done(struct GNUNET_ATS_ApplicationHandle *ch)
295 * @return suggest handle, NULL if a request is already pending 298 * @return suggest handle, NULL if a request is already pending
296 */ 299 */
297struct GNUNET_ATS_ApplicationSuggestHandle * 300struct GNUNET_ATS_ApplicationSuggestHandle *
298GNUNET_ATS_application_suggest(struct GNUNET_ATS_ApplicationHandle *ch, 301GNUNET_ATS_application_suggest (struct GNUNET_ATS_ApplicationHandle *ch,
299 const struct GNUNET_PeerIdentity *peer, 302 const struct GNUNET_PeerIdentity *peer,
300 enum GNUNET_MQ_PreferenceKind pk, 303 enum GNUNET_MQ_PreferenceKind pk,
301 struct GNUNET_BANDWIDTH_Value32NBO bw) 304 struct GNUNET_BANDWIDTH_Value32NBO bw)
302{ 305{
303 struct GNUNET_ATS_ApplicationSuggestHandle *s; 306 struct GNUNET_ATS_ApplicationSuggestHandle *s;
304 307
305 s = GNUNET_new(struct GNUNET_ATS_ApplicationSuggestHandle); 308 s = GNUNET_new (struct GNUNET_ATS_ApplicationSuggestHandle);
306 s->ch = ch; 309 s->ch = ch;
307 s->id = *peer; 310 s->id = *peer;
308 s->pk = pk; 311 s->pk = pk;
309 s->bw = bw; 312 s->bw = bw;
310 (void)GNUNET_CONTAINER_multipeermap_put(ch->sug_requests, 313 (void) GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
311 &s->id, 314 &s->id,
312 s, 315 s,
313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
314 LOG(GNUNET_ERROR_TYPE_DEBUG, 317 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "Requesting ATS to suggest address for `%s'\n", 318 "Requesting ATS to suggest address for `%s'\n",
316 GNUNET_i2s(peer)); 319 GNUNET_i2s (peer));
317 if (NULL == ch->mq) 320 if (NULL == ch->mq)
318 return s; 321 return s;
319 GNUNET_assert(GNUNET_OK == 322 GNUNET_assert (GNUNET_OK ==
320 transmit_suggestion(ch, 323 transmit_suggestion (ch,
321 &s->id, 324 &s->id,
322 s)); 325 s));
323 return s; 326 return s;
324} 327}
325 328
@@ -330,32 +333,33 @@ GNUNET_ATS_application_suggest(struct GNUNET_ATS_ApplicationHandle *ch,
330 * @param sh handle to stop 333 * @param sh handle to stop
331 */ 334 */
332void 335void
333GNUNET_ATS_application_suggest_cancel(struct GNUNET_ATS_ApplicationSuggestHandle *sh) 336GNUNET_ATS_application_suggest_cancel (struct
337 GNUNET_ATS_ApplicationSuggestHandle *sh)
334{ 338{
335 struct GNUNET_ATS_ApplicationHandle *ch = sh->ch; 339 struct GNUNET_ATS_ApplicationHandle *ch = sh->ch;
336 struct GNUNET_MQ_Envelope *ev; 340 struct GNUNET_MQ_Envelope *ev;
337 struct ExpressPreferenceMessage *m; 341 struct ExpressPreferenceMessage *m;
338 342
339 LOG(GNUNET_ERROR_TYPE_DEBUG, 343 LOG (GNUNET_ERROR_TYPE_DEBUG,
340 "Telling ATS we no longer care for an address for `%s'\n", 344 "Telling ATS we no longer care for an address for `%s'\n",
341 GNUNET_i2s(&sh->id)); 345 GNUNET_i2s (&sh->id));
342 GNUNET_assert(GNUNET_OK == 346 GNUNET_assert (GNUNET_OK ==
343 GNUNET_CONTAINER_multipeermap_remove(ch->sug_requests, 347 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
344 &sh->id, 348 &sh->id,
345 sh)); 349 sh));
346 if (NULL == ch->mq) 350 if (NULL == ch->mq)
347 { 351 {
348 GNUNET_free(sh); 352 GNUNET_free (sh);
349 return; 353 return;
350 } 354 }
351 ev = GNUNET_MQ_msg(m, 355 ev = GNUNET_MQ_msg (m,
352 GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL); 356 GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL);
353 m->pk = htonl((uint32_t)sh->pk); 357 m->pk = htonl ((uint32_t) sh->pk);
354 m->bw = sh->bw; 358 m->bw = sh->bw;
355 m->peer = sh->id; 359 m->peer = sh->id;
356 GNUNET_MQ_send(ch->mq, 360 GNUNET_MQ_send (ch->mq,
357 ev); 361 ev);
358 GNUNET_free(sh); 362 GNUNET_free (sh);
359} 363}
360 364
361 365