summaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed_connectionpool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/gnunet-service-testbed_connectionpool.c')
-rw-r--r--src/testbed/gnunet-service-testbed_connectionpool.c676
1 files changed, 340 insertions, 336 deletions
diff --git a/src/testbed/gnunet-service-testbed_connectionpool.c b/src/testbed/gnunet-service-testbed_connectionpool.c
index e173a2349..fdb10f987 100644
--- a/src/testbed/gnunet-service-testbed_connectionpool.c
+++ b/src/testbed/gnunet-service-testbed_connectionpool.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2008--2015 GNUnet e.V. 3 Copyright (C) 2008--2015 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file testbed/gnunet-service-testbed_connectionpool.c 22 * @file testbed/gnunet-service-testbed_connectionpool.c
@@ -36,14 +36,14 @@
36#undef LOG 36#undef LOG
37#endif 37#endif
38#define LOG(kind, ...) \ 38#define LOG(kind, ...) \
39 GNUNET_log_from (kind, "testbed-connectionpool", __VA_ARGS__) 39 GNUNET_log_from(kind, "testbed-connectionpool", __VA_ARGS__)
40 40
41 41
42/** 42/**
43 * Time to expire a cache entry 43 * Time to expire a cache entry
44 */ 44 */
45#define CACHE_EXPIRY \ 45#define CACHE_EXPIRY \
46 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 46 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
47 47
48 48
49/** 49/**
@@ -55,8 +55,7 @@ struct GST_ConnectionPool_GetHandle;
55/** 55/**
56 * A pooled connection 56 * A pooled connection
57 */ 57 */
58struct PooledConnection 58struct PooledConnection {
59{
60 /** 59 /**
61 * Next ptr for placing this object in the DLL of least recently used pooled 60 * Next ptr for placing this object in the DLL of least recently used pooled
62 * connections 61 * connections
@@ -169,8 +168,7 @@ struct PooledConnection
169/** 168/**
170 * The request handle for obtaining a pooled connection 169 * The request handle for obtaining a pooled connection
171 */ 170 */
172struct GST_ConnectionPool_GetHandle 171struct GST_ConnectionPool_GetHandle {
173{
174 /** 172 /**
175 * The next ptr for inclusion in the notification DLLs. At first the object 173 * The next ptr for inclusion in the notification DLLs. At first the object
176 * is placed in the waiting DLL of the corresponding #PooledConnection 174 * is placed in the waiting DLL of the corresponding #PooledConnection
@@ -274,7 +272,7 @@ static unsigned int max_size;
274 * @param entry the #PooledConnection object 272 * @param entry the #PooledConnection object
275 */ 273 */
276static void 274static void
277expire_task_cancel (struct PooledConnection *entry); 275expire_task_cancel(struct PooledConnection *entry);
278 276
279 277
280/** 278/**
@@ -283,49 +281,49 @@ expire_task_cancel (struct PooledConnection *entry);
283 * @param entry the #PooledConnection object 281 * @param entry the #PooledConnection object
284 */ 282 */
285static void 283static void
286destroy_pooled_connection (struct PooledConnection *entry) 284destroy_pooled_connection(struct PooledConnection *entry)
287{ 285{
288 GNUNET_assert ((NULL == entry->head_notify) && (NULL == entry->tail_notify)); 286 GNUNET_assert((NULL == entry->head_notify) && (NULL == entry->tail_notify));
289 GNUNET_assert ((NULL == entry->head_waiting) && 287 GNUNET_assert((NULL == entry->head_waiting) &&
290 (NULL == entry->tail_waiting)); 288 (NULL == entry->tail_waiting));
291 GNUNET_assert (0 == entry->demand); 289 GNUNET_assert(0 == entry->demand);
292 expire_task_cancel (entry); 290 expire_task_cancel(entry);
293 if (entry->in_lru) 291 if (entry->in_lru)
294 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry); 292 GNUNET_CONTAINER_DLL_remove(head_lru, tail_lru, entry);
295 if (entry->in_pool) 293 if (entry->in_pool)
296 GNUNET_assert ( 294 GNUNET_assert(
297 GNUNET_OK == 295 GNUNET_OK ==
298 GNUNET_CONTAINER_multihashmap32_remove (map, entry->index, entry)); 296 GNUNET_CONTAINER_multihashmap32_remove(map, entry->index, entry));
299 if (NULL != entry->notify_task) 297 if (NULL != entry->notify_task)
300 { 298 {
301 GNUNET_SCHEDULER_cancel (entry->notify_task); 299 GNUNET_SCHEDULER_cancel(entry->notify_task);
302 entry->notify_task = NULL; 300 entry->notify_task = NULL;
303 } 301 }
304 LOG_DEBUG ("Cleaning up handles of a pooled connection\n"); 302 LOG_DEBUG("Cleaning up handles of a pooled connection\n");
305 if (NULL != entry->handle_transport) 303 if (NULL != entry->handle_transport)
306 GNUNET_assert (NULL != entry->op_transport); 304 GNUNET_assert(NULL != entry->op_transport);
307 if (NULL != entry->op_transport) 305 if (NULL != entry->op_transport)
308 { 306 {
309 GNUNET_TESTBED_operation_done (entry->op_transport); 307 GNUNET_TESTBED_operation_done(entry->op_transport);
310 entry->op_transport = NULL; 308 entry->op_transport = NULL;
311 } 309 }
312 if (NULL != entry->handle_ats_connectivity) 310 if (NULL != entry->handle_ats_connectivity)
313 GNUNET_assert (NULL != entry->op_ats_connectivity); 311 GNUNET_assert(NULL != entry->op_ats_connectivity);
314 if (NULL != entry->op_ats_connectivity) 312 if (NULL != entry->op_ats_connectivity)
315 { 313 {
316 GNUNET_TESTBED_operation_done (entry->op_ats_connectivity); 314 GNUNET_TESTBED_operation_done(entry->op_ats_connectivity);
317 entry->op_ats_connectivity = NULL; 315 entry->op_ats_connectivity = NULL;
318 } 316 }
319 if (NULL != entry->op_core) 317 if (NULL != entry->op_core)
320 { 318 {
321 GNUNET_TESTBED_operation_done (entry->op_core); 319 GNUNET_TESTBED_operation_done(entry->op_core);
322 entry->op_core = NULL; 320 entry->op_core = NULL;
323 } 321 }
324 GNUNET_assert (NULL == entry->handle_core); 322 GNUNET_assert(NULL == entry->handle_core);
325 GNUNET_assert (NULL == entry->handle_ats_connectivity); 323 GNUNET_assert(NULL == entry->handle_ats_connectivity);
326 GNUNET_assert (NULL == entry->handle_transport); 324 GNUNET_assert(NULL == entry->handle_transport);
327 GNUNET_CONFIGURATION_destroy (entry->cfg); 325 GNUNET_CONFIGURATION_destroy(entry->cfg);
328 GNUNET_free (entry); 326 GNUNET_free(entry);
329} 327}
330 328
331 329
@@ -335,12 +333,12 @@ destroy_pooled_connection (struct PooledConnection *entry)
335 * @param cls the #PooledConnection object 333 * @param cls the #PooledConnection object
336 */ 334 */
337static void 335static void
338expire (void *cls) 336expire(void *cls)
339{ 337{
340 struct PooledConnection *entry = cls; 338 struct PooledConnection *entry = cls;
341 339
342 entry->expire_task = NULL; 340 entry->expire_task = NULL;
343 destroy_pooled_connection (entry); 341 destroy_pooled_connection(entry);
344} 342}
345 343
346 344
@@ -350,13 +348,13 @@ expire (void *cls)
350 * @param entry the #PooledConnection object 348 * @param entry the #PooledConnection object
351 */ 349 */
352static void 350static void
353expire_task_cancel (struct PooledConnection *entry) 351expire_task_cancel(struct PooledConnection *entry)
354{ 352{
355 if (NULL != entry->expire_task) 353 if (NULL != entry->expire_task)
356 { 354 {
357 GNUNET_SCHEDULER_cancel (entry->expire_task); 355 GNUNET_SCHEDULER_cancel(entry->expire_task);
358 entry->expire_task = NULL; 356 entry->expire_task = NULL;
359 } 357 }
360} 358}
361 359
362 360
@@ -366,15 +364,15 @@ expire_task_cancel (struct PooledConnection *entry)
366 * @param entry the #PooledConnection object 364 * @param entry the #PooledConnection object
367 */ 365 */
368static void 366static void
369add_to_lru (struct PooledConnection *entry) 367add_to_lru(struct PooledConnection *entry)
370{ 368{
371 GNUNET_assert (0 == entry->demand); 369 GNUNET_assert(0 == entry->demand);
372 GNUNET_assert (! entry->in_lru); 370 GNUNET_assert(!entry->in_lru);
373 GNUNET_CONTAINER_DLL_insert_tail (head_lru, tail_lru, entry); 371 GNUNET_CONTAINER_DLL_insert_tail(head_lru, tail_lru, entry);
374 entry->in_lru = GNUNET_YES; 372 entry->in_lru = GNUNET_YES;
375 GNUNET_assert (NULL == entry->expire_task); 373 GNUNET_assert(NULL == entry->expire_task);
376 entry->expire_task = 374 entry->expire_task =
377 GNUNET_SCHEDULER_add_delayed (CACHE_EXPIRY, &expire, entry); 375 GNUNET_SCHEDULER_add_delayed(CACHE_EXPIRY, &expire, entry);
378} 376}
379 377
380 378
@@ -390,32 +388,34 @@ add_to_lru (struct PooledConnection *entry)
390 * is found 388 * is found
391 */ 389 */
392static struct GST_ConnectionPool_GetHandle * 390static struct GST_ConnectionPool_GetHandle *
393search_waiting (const struct PooledConnection *entry, 391search_waiting(const struct PooledConnection *entry,
394 struct GST_ConnectionPool_GetHandle *head) 392 struct GST_ConnectionPool_GetHandle *head)
395{ 393{
396 struct GST_ConnectionPool_GetHandle *gh; 394 struct GST_ConnectionPool_GetHandle *gh;
397 395
398 for (gh = head; NULL != gh; gh = gh->next) 396 for (gh = head; NULL != gh; gh = gh->next)
399 {
400 switch (gh->service)
401 { 397 {
402 case GST_CONNECTIONPOOL_SERVICE_CORE: 398 switch (gh->service)
403 if (NULL == entry->handle_core) 399 {
404 continue; 400 case GST_CONNECTIONPOOL_SERVICE_CORE:
405 if (NULL == entry->peer_identity) 401 if (NULL == entry->handle_core)
406 continue; /* CORE connection isn't ready yet */ 402 continue;
407 break; 403 if (NULL == entry->peer_identity)
408 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT: 404 continue; /* CORE connection isn't ready yet */
409 if (NULL == entry->handle_transport) 405 break;
410 continue; 406
411 break; 407 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
412 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY: 408 if (NULL == entry->handle_transport)
413 if (NULL == entry->handle_ats_connectivity) 409 continue;
414 continue; 410 break;
411
412 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
413 if (NULL == entry->handle_ats_connectivity)
414 continue;
415 break;
416 }
415 break; 417 break;
416 } 418 }
417 break;
418 }
419 return gh; 419 return gh;
420} 420}
421 421
@@ -430,37 +430,37 @@ search_waiting (const struct PooledConnection *entry,
430 * @param cls the #PooledConnection object 430 * @param cls the #PooledConnection object
431 */ 431 */
432static void 432static void
433connection_ready (void *cls) 433connection_ready(void *cls)
434{ 434{
435 struct PooledConnection *entry = cls; 435 struct PooledConnection *entry = cls;
436 struct GST_ConnectionPool_GetHandle *gh; 436 struct GST_ConnectionPool_GetHandle *gh;
437 struct GST_ConnectionPool_GetHandle *gh_next; 437 struct GST_ConnectionPool_GetHandle *gh_next;
438 438
439 GNUNET_assert (NULL != entry->notify_task); 439 GNUNET_assert(NULL != entry->notify_task);
440 entry->notify_task = NULL; 440 entry->notify_task = NULL;
441 gh = search_waiting (entry, entry->head_waiting); 441 gh = search_waiting(entry, entry->head_waiting);
442 GNUNET_assert (NULL != gh); 442 GNUNET_assert(NULL != gh);
443 gh_next = NULL; 443 gh_next = NULL;
444 if (NULL != gh->next) 444 if (NULL != gh->next)
445 gh_next = search_waiting (entry, gh->next); 445 gh_next = search_waiting(entry, gh->next);
446 GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh); 446 GNUNET_CONTAINER_DLL_remove(entry->head_waiting, entry->tail_waiting, gh);
447 gh->connection_ready_called = 1; 447 gh->connection_ready_called = 1;
448 if (NULL != gh_next) 448 if (NULL != gh_next)
449 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry); 449 entry->notify_task = GNUNET_SCHEDULER_add_now(&connection_ready, entry);
450 if ((NULL != gh->target) && (NULL != gh->connect_notify_cb)) 450 if ((NULL != gh->target) && (NULL != gh->connect_notify_cb))
451 { 451 {
452 GNUNET_CONTAINER_DLL_insert_tail (entry->head_notify, 452 GNUNET_CONTAINER_DLL_insert_tail(entry->head_notify,
453 entry->tail_notify, 453 entry->tail_notify,
454 gh); 454 gh);
455 gh->notify_waiting = 1; 455 gh->notify_waiting = 1;
456 } 456 }
457 LOG_DEBUG ("Connection ready for handle type %u\n", gh->service); 457 LOG_DEBUG("Connection ready for handle type %u\n", gh->service);
458 gh->cb (gh->cb_cls, 458 gh->cb(gh->cb_cls,
459 entry->handle_core, 459 entry->handle_core,
460 entry->handle_transport, 460 entry->handle_transport,
461 entry->handle_ats_connectivity, 461 entry->handle_ats_connectivity,
462 entry->peer_identity, 462 entry->peer_identity,
463 entry->cfg); 463 entry->cfg);
464} 464}
465 465
466 466
@@ -474,9 +474,9 @@ connection_ready (void *cls)
474 * @param service the service where this notification has originated 474 * @param service the service where this notification has originated
475 */ 475 */
476static void 476static void
477peer_connect_notify_cb (void *cls, 477peer_connect_notify_cb(void *cls,
478 const struct GNUNET_PeerIdentity *peer, 478 const struct GNUNET_PeerIdentity *peer,
479 const enum GST_ConnectionPool_Service service) 479 const enum GST_ConnectionPool_Service service)
480{ 480{
481 struct PooledConnection *entry = cls; 481 struct PooledConnection *entry = cls;
482 struct GST_ConnectionPool_GetHandle *gh; 482 struct GST_ConnectionPool_GetHandle *gh;
@@ -485,31 +485,31 @@ peer_connect_notify_cb (void *cls,
485 void *cb_cls; 485 void *cb_cls;
486 486
487 for (gh = entry->head_notify; NULL != gh;) 487 for (gh = entry->head_notify; NULL != gh;)
488 {
489 GNUNET_assert (NULL != gh->target);
490 GNUNET_assert (NULL != gh->connect_notify_cb);
491 GNUNET_assert (gh->connection_ready_called);
492 if (service != gh->service)
493 { 488 {
494 gh = gh->next; 489 GNUNET_assert(NULL != gh->target);
495 continue; 490 GNUNET_assert(NULL != gh->connect_notify_cb);
491 GNUNET_assert(gh->connection_ready_called);
492 if (service != gh->service)
493 {
494 gh = gh->next;
495 continue;
496 }
497 if (0 != memcmp(gh->target, peer, sizeof(struct GNUNET_PeerIdentity)))
498 {
499 gh = gh->next;
500 continue;
501 }
502 cb = gh->connect_notify_cb;
503 cb_cls = gh->connect_notify_cb_cls;
504 gh_next = gh->next;
505 GNUNET_CONTAINER_DLL_remove(entry->head_notify, entry->tail_notify, gh);
506 gh->notify_waiting = 0;
507 LOG_DEBUG("Peer connected to peer %u at service %u\n",
508 entry->index,
509 gh->service);
510 gh = gh_next;
511 cb(cb_cls, peer);
496 } 512 }
497 if (0 != memcmp (gh->target, peer, sizeof (struct GNUNET_PeerIdentity)))
498 {
499 gh = gh->next;
500 continue;
501 }
502 cb = gh->connect_notify_cb;
503 cb_cls = gh->connect_notify_cb_cls;
504 gh_next = gh->next;
505 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
506 gh->notify_waiting = 0;
507 LOG_DEBUG ("Peer connected to peer %u at service %u\n",
508 entry->index,
509 gh->service);
510 gh = gh_next;
511 cb (cb_cls, peer);
512 }
513} 513}
514 514
515 515
@@ -523,13 +523,13 @@ peer_connect_notify_cb (void *cls,
523 * @return NULL 523 * @return NULL
524 */ 524 */
525static void * 525static void *
526transport_peer_connect_notify_cb (void *cls, 526transport_peer_connect_notify_cb(void *cls,
527 const struct GNUNET_PeerIdentity *peer, 527 const struct GNUNET_PeerIdentity *peer,
528 struct GNUNET_MQ_Handle *mq) 528 struct GNUNET_MQ_Handle *mq)
529{ 529{
530 struct PooledConnection *entry = cls; 530 struct PooledConnection *entry = cls;
531 531
532 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_TRANSPORT); 532 peer_connect_notify_cb(entry, peer, GST_CONNECTIONPOOL_SERVICE_TRANSPORT);
533 return NULL; 533 return NULL;
534} 534}
535 535
@@ -541,34 +541,34 @@ transport_peer_connect_notify_cb (void *cls,
541 * @param cls the #PooledConnection object 541 * @param cls the #PooledConnection object
542 */ 542 */
543static void 543static void
544opstart_get_handle_transport (void *cls) 544opstart_get_handle_transport(void *cls)
545{ 545{
546 struct PooledConnection *entry = cls; 546 struct PooledConnection *entry = cls;
547 547
548 GNUNET_assert (NULL != entry); 548 GNUNET_assert(NULL != entry);
549 LOG_DEBUG ("Opening a transport connection to peer %u\n", entry->index); 549 LOG_DEBUG("Opening a transport connection to peer %u\n", entry->index);
550 entry->handle_transport = 550 entry->handle_transport =
551 GNUNET_TRANSPORT_core_connect (entry->cfg, 551 GNUNET_TRANSPORT_core_connect(entry->cfg,
552 NULL, 552 NULL,
553 NULL, 553 NULL,
554 entry, 554 entry,
555 &transport_peer_connect_notify_cb, 555 &transport_peer_connect_notify_cb,
556 NULL, 556 NULL,
557 NULL); 557 NULL);
558 if (NULL == entry->handle_transport) 558 if (NULL == entry->handle_transport)
559 { 559 {
560 GNUNET_break (0); 560 GNUNET_break(0);
561 return; 561 return;
562 } 562 }
563 if (0 == entry->demand) 563 if (0 == entry->demand)
564 return; 564 return;
565 if (NULL != entry->notify_task) 565 if (NULL != entry->notify_task)
566 return; 566 return;
567 if (NULL != search_waiting (entry, entry->head_waiting)) 567 if (NULL != search_waiting(entry, entry->head_waiting))
568 { 568 {
569 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry); 569 entry->notify_task = GNUNET_SCHEDULER_add_now(&connection_ready, entry);
570 return; 570 return;
571 } 571 }
572} 572}
573 573
574 574
@@ -579,13 +579,13 @@ opstart_get_handle_transport (void *cls)
579 * @param cls the cache entry 579 * @param cls the cache entry
580 */ 580 */
581static void 581static void
582oprelease_get_handle_transport (void *cls) 582oprelease_get_handle_transport(void *cls)
583{ 583{
584 struct PooledConnection *entry = cls; 584 struct PooledConnection *entry = cls;
585 585
586 if (NULL == entry->handle_transport) 586 if (NULL == entry->handle_transport)
587 return; 587 return;
588 GNUNET_TRANSPORT_core_disconnect (entry->handle_transport); 588 GNUNET_TRANSPORT_core_disconnect(entry->handle_transport);
589 entry->handle_transport = NULL; 589 entry->handle_transport = NULL;
590} 590}
591 591
@@ -599,14 +599,14 @@ oprelease_get_handle_transport (void *cls)
599 * @return peer 599 * @return peer
600 */ 600 */
601static void * 601static void *
602core_peer_connect_cb (void *cls, 602core_peer_connect_cb(void *cls,
603 const struct GNUNET_PeerIdentity *peer, 603 const struct GNUNET_PeerIdentity *peer,
604 struct GNUNET_MQ_Handle *mq) 604 struct GNUNET_MQ_Handle *mq)
605{ 605{
606 struct PooledConnection *entry = cls; 606 struct PooledConnection *entry = cls;
607 607
608 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_CORE); 608 peer_connect_notify_cb(entry, peer, GST_CONNECTIONPOOL_SERVICE_CORE);
609 return (void *) peer; 609 return (void *)peer;
610} 610}
611 611
612 612
@@ -623,27 +623,27 @@ core_peer_connect_cb (void *cls,
623 * @param my_identity ID of this peer, NULL if we failed 623 * @param my_identity ID of this peer, NULL if we failed
624 */ 624 */
625static void 625static void
626core_startup_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity) 626core_startup_cb(void *cls, const struct GNUNET_PeerIdentity *my_identity)
627{ 627{
628 struct PooledConnection *entry = cls; 628 struct PooledConnection *entry = cls;
629 629
630 if (NULL == my_identity) 630 if (NULL == my_identity)
631 { 631 {
632 GNUNET_break (0); 632 GNUNET_break(0);
633 return; 633 return;
634 } 634 }
635 GNUNET_assert (NULL == entry->peer_identity); 635 GNUNET_assert(NULL == entry->peer_identity);
636 entry->peer_identity = GNUNET_new (struct GNUNET_PeerIdentity); 636 entry->peer_identity = GNUNET_new(struct GNUNET_PeerIdentity);
637 *entry->peer_identity = *my_identity; 637 *entry->peer_identity = *my_identity;
638 if (0 == entry->demand) 638 if (0 == entry->demand)
639 return; 639 return;
640 if (NULL != entry->notify_task) 640 if (NULL != entry->notify_task)
641 return; 641 return;
642 if (NULL != search_waiting (entry, entry->head_waiting)) 642 if (NULL != search_waiting(entry, entry->head_waiting))
643 { 643 {
644 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry); 644 entry->notify_task = GNUNET_SCHEDULER_add_now(&connection_ready, entry);
645 return; 645 return;
646 } 646 }
647} 647}
648 648
649 649
@@ -654,19 +654,19 @@ core_startup_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
654 * @param cls the #PooledConnection object 654 * @param cls the #PooledConnection object
655 */ 655 */
656static void 656static void
657opstart_get_handle_core (void *cls) 657opstart_get_handle_core(void *cls)
658{ 658{
659 struct PooledConnection *entry = cls; 659 struct PooledConnection *entry = cls;
660 660
661 GNUNET_assert (NULL != entry); 661 GNUNET_assert(NULL != entry);
662 LOG_DEBUG ("Opening a CORE connection to peer %u\n", entry->index); 662 LOG_DEBUG("Opening a CORE connection to peer %u\n", entry->index);
663 entry->handle_core = 663 entry->handle_core =
664 GNUNET_CORE_connect (entry->cfg, 664 GNUNET_CORE_connect(entry->cfg,
665 entry, /* closure */ 665 entry, /* closure */
666 &core_startup_cb, /* core startup notify */ 666 &core_startup_cb, /* core startup notify */
667 &core_peer_connect_cb, /* peer connect notify */ 667 &core_peer_connect_cb, /* peer connect notify */
668 NULL, /* peer disconnect notify */ 668 NULL, /* peer disconnect notify */
669 NULL); 669 NULL);
670} 670}
671 671
672 672
@@ -677,15 +677,15 @@ opstart_get_handle_core (void *cls)
677 * @param cls the #PooledConnection object 677 * @param cls the #PooledConnection object
678 */ 678 */
679static void 679static void
680oprelease_get_handle_core (void *cls) 680oprelease_get_handle_core(void *cls)
681{ 681{
682 struct PooledConnection *entry = cls; 682 struct PooledConnection *entry = cls;
683 683
684 if (NULL == entry->handle_core) 684 if (NULL == entry->handle_core)
685 return; 685 return;
686 GNUNET_CORE_disconnect (entry->handle_core); 686 GNUNET_CORE_disconnect(entry->handle_core);
687 entry->handle_core = NULL; 687 entry->handle_core = NULL;
688 GNUNET_free_non_null (entry->peer_identity); 688 GNUNET_free_non_null(entry->peer_identity);
689 entry->peer_identity = NULL; 689 entry->peer_identity = NULL;
690} 690}
691 691
@@ -697,11 +697,11 @@ oprelease_get_handle_core (void *cls)
697 * @param cls the #PooledConnection object 697 * @param cls the #PooledConnection object
698 */ 698 */
699static void 699static void
700opstart_get_handle_ats_connectivity (void *cls) 700opstart_get_handle_ats_connectivity(void *cls)
701{ 701{
702 struct PooledConnection *entry = cls; 702 struct PooledConnection *entry = cls;
703 703
704 entry->handle_ats_connectivity = GNUNET_ATS_connectivity_init (entry->cfg); 704 entry->handle_ats_connectivity = GNUNET_ATS_connectivity_init(entry->cfg);
705} 705}
706 706
707 707
@@ -712,13 +712,13 @@ opstart_get_handle_ats_connectivity (void *cls)
712 * @param cls the #PooledConnection object 712 * @param cls the #PooledConnection object
713 */ 713 */
714static void 714static void
715oprelease_get_handle_ats_connectivity (void *cls) 715oprelease_get_handle_ats_connectivity(void *cls)
716{ 716{
717 struct PooledConnection *entry = cls; 717 struct PooledConnection *entry = cls;
718 718
719 if (NULL == entry->handle_ats_connectivity) 719 if (NULL == entry->handle_ats_connectivity)
720 return; 720 return;
721 GNUNET_ATS_connectivity_done (entry->handle_ats_connectivity); 721 GNUNET_ATS_connectivity_done(entry->handle_ats_connectivity);
722 entry->handle_ats_connectivity = NULL; 722 entry->handle_ats_connectivity = NULL;
723} 723}
724 724
@@ -734,12 +734,12 @@ oprelease_get_handle_ats_connectivity (void *cls)
734 * #GNUNET_NO if not. 734 * #GNUNET_NO if not.
735 */ 735 */
736static int 736static int
737cleanup_iterator (void *cls, uint32_t key, void *value) 737cleanup_iterator(void *cls, uint32_t key, void *value)
738{ 738{
739 struct PooledConnection *entry = value; 739 struct PooledConnection *entry = value;
740 740
741 GNUNET_assert (NULL != entry); 741 GNUNET_assert(NULL != entry);
742 destroy_pooled_connection (entry); 742 destroy_pooled_connection(entry);
743 return GNUNET_YES; 743 return GNUNET_YES;
744} 744}
745 745
@@ -752,13 +752,13 @@ cleanup_iterator (void *cls, uint32_t key, void *value)
752 * #GST_ConnectionPool_Service 752 * #GST_ConnectionPool_Service
753 */ 753 */
754void 754void
755GST_connection_pool_init (unsigned int size) 755GST_connection_pool_init(unsigned int size)
756{ 756{
757 max_size = size; 757 max_size = size;
758 if (0 == max_size) 758 if (0 == max_size)
759 return; 759 return;
760 GNUNET_assert (NULL == map); 760 GNUNET_assert(NULL == map);
761 map = GNUNET_CONTAINER_multihashmap32_create (((size * 3) / 4) + 1); 761 map = GNUNET_CONTAINER_multihashmap32_create(((size * 3) / 4) + 1);
762} 762}
763 763
764 764
@@ -766,24 +766,24 @@ GST_connection_pool_init (unsigned int size)
766 * Cleanup the connection pool 766 * Cleanup the connection pool
767 */ 767 */
768void 768void
769GST_connection_pool_destroy () 769GST_connection_pool_destroy()
770{ 770{
771 struct PooledConnection *entry; 771 struct PooledConnection *entry;
772 772
773 if (NULL != map) 773 if (NULL != map)
774 { 774 {
775 GNUNET_assert ( 775 GNUNET_assert(
776 GNUNET_SYSERR != 776 GNUNET_SYSERR !=
777 GNUNET_CONTAINER_multihashmap32_iterate (map, &cleanup_iterator, NULL)); 777 GNUNET_CONTAINER_multihashmap32_iterate(map, &cleanup_iterator, NULL));
778 GNUNET_CONTAINER_multihashmap32_destroy (map); 778 GNUNET_CONTAINER_multihashmap32_destroy(map);
779 map = NULL; 779 map = NULL;
780 } 780 }
781 while (NULL != (entry = head_lru)) 781 while (NULL != (entry = head_lru))
782 { 782 {
783 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry); 783 GNUNET_CONTAINER_DLL_remove(head_lru, tail_lru, entry);
784 destroy_pooled_connection (entry); 784 destroy_pooled_connection(entry);
785 } 785 }
786 GNUNET_assert (NULL == head_not_pooled); 786 GNUNET_assert(NULL == head_not_pooled);
787} 787}
788 788
789 789
@@ -820,7 +820,7 @@ GST_connection_pool_destroy ()
820 * longer being used 820 * longer being used
821 */ 821 */
822struct GST_ConnectionPool_GetHandle * 822struct GST_ConnectionPool_GetHandle *
823GST_connection_pool_get_handle ( 823GST_connection_pool_get_handle(
824 unsigned int peer_id, 824 unsigned int peer_id,
825 const struct GNUNET_CONFIGURATION_Handle *cfg, 825 const struct GNUNET_CONFIGURATION_Handle *cfg,
826 enum GST_ConnectionPool_Service service, 826 enum GST_ConnectionPool_Service service,
@@ -836,64 +836,66 @@ GST_connection_pool_get_handle (
836 void *handle; 836 void *handle;
837 uint32_t peer_id32; 837 uint32_t peer_id32;
838 838
839 peer_id32 = (uint32_t) peer_id; 839 peer_id32 = (uint32_t)peer_id;
840 handle = NULL; 840 handle = NULL;
841 entry = NULL; 841 entry = NULL;
842 if (NULL != map) 842 if (NULL != map)
843 entry = GNUNET_CONTAINER_multihashmap32_get (map, peer_id32); 843 entry = GNUNET_CONTAINER_multihashmap32_get(map, peer_id32);
844 if (NULL != entry) 844 if (NULL != entry)
845 {
846 if (entry->in_lru)
847 { 845 {
848 GNUNET_assert (0 == entry->demand); 846 if (entry->in_lru)
849 expire_task_cancel (entry); 847 {
850 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry); 848 GNUNET_assert(0 == entry->demand);
851 entry->in_lru = GNUNET_NO; 849 expire_task_cancel(entry);
850 GNUNET_CONTAINER_DLL_remove(head_lru, tail_lru, entry);
851 entry->in_lru = GNUNET_NO;
852 }
853 switch (service)
854 {
855 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
856 handle = entry->handle_transport;
857 if (NULL != handle)
858 LOG_DEBUG("Found TRANSPORT handle for peer %u\n", entry->index);
859 break;
860
861 case GST_CONNECTIONPOOL_SERVICE_CORE:
862 handle = entry->handle_core;
863 if (NULL != handle)
864 LOG_DEBUG("Found CORE handle for peer %u\n", entry->index);
865 break;
866
867 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
868 handle = entry->handle_ats_connectivity;
869 if (NULL != handle)
870 LOG_DEBUG("Found ATS CONNECTIVITY handle for peer %u\n", entry->index);
871 break;
872 }
852 } 873 }
853 switch (service)
854 {
855 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
856 handle = entry->handle_transport;
857 if (NULL != handle)
858 LOG_DEBUG ("Found TRANSPORT handle for peer %u\n", entry->index);
859 break;
860 case GST_CONNECTIONPOOL_SERVICE_CORE:
861 handle = entry->handle_core;
862 if (NULL != handle)
863 LOG_DEBUG ("Found CORE handle for peer %u\n", entry->index);
864 break;
865 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
866 handle = entry->handle_ats_connectivity;
867 if (NULL != handle)
868 LOG_DEBUG ("Found ATS CONNECTIVITY handle for peer %u\n", entry->index);
869 break;
870 }
871 }
872 else 874 else
873 {
874 entry = GNUNET_new (struct PooledConnection);
875 entry->index = peer_id32;
876 if ((NULL != map) &&
877 (GNUNET_CONTAINER_multihashmap32_size (map) < max_size))
878 { 875 {
879 GNUNET_assert (GNUNET_OK == 876 entry = GNUNET_new(struct PooledConnection);
880 GNUNET_CONTAINER_multihashmap32_put ( 877 entry->index = peer_id32;
881 map, 878 if ((NULL != map) &&
882 entry->index, 879 (GNUNET_CONTAINER_multihashmap32_size(map) < max_size))
883 entry, 880 {
884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 881 GNUNET_assert(GNUNET_OK ==
885 entry->in_pool = GNUNET_YES; 882 GNUNET_CONTAINER_multihashmap32_put(
886 } 883 map,
887 else 884 entry->index,
888 { 885 entry,
889 GNUNET_CONTAINER_DLL_insert_tail (head_not_pooled, 886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
890 tail_not_pooled, 887 entry->in_pool = GNUNET_YES;
891 entry); 888 }
889 else
890 {
891 GNUNET_CONTAINER_DLL_insert_tail(head_not_pooled,
892 tail_not_pooled,
893 entry);
894 }
895 entry->cfg = GNUNET_CONFIGURATION_dup(cfg);
892 } 896 }
893 entry->cfg = GNUNET_CONFIGURATION_dup (cfg);
894 }
895 entry->demand++; 897 entry->demand++;
896 gh = GNUNET_new (struct GST_ConnectionPool_GetHandle); 898 gh = GNUNET_new(struct GST_ConnectionPool_GetHandle);
897 gh->entry = entry; 899 gh->entry = entry;
898 gh->cb = cb; 900 gh->cb = cb;
899 gh->cb_cls = cb_cls; 901 gh->cb_cls = cb_cls;
@@ -901,48 +903,50 @@ GST_connection_pool_get_handle (
901 gh->connect_notify_cb = connect_notify_cb; 903 gh->connect_notify_cb = connect_notify_cb;
902 gh->connect_notify_cb_cls = connect_notify_cb_cls; 904 gh->connect_notify_cb_cls = connect_notify_cb_cls;
903 gh->service = service; 905 gh->service = service;
904 GNUNET_CONTAINER_DLL_insert (entry->head_waiting, entry->tail_waiting, gh); 906 GNUNET_CONTAINER_DLL_insert(entry->head_waiting, entry->tail_waiting, gh);
905 if (NULL != handle) 907 if (NULL != handle)
906 {
907 if (NULL == entry->notify_task)
908 { 908 {
909 if (NULL != search_waiting (entry, entry->head_waiting)) 909 if (NULL == entry->notify_task)
910 entry->notify_task = 910 {
911 GNUNET_SCHEDULER_add_now (&connection_ready, entry); 911 if (NULL != search_waiting(entry, entry->head_waiting))
912 entry->notify_task =
913 GNUNET_SCHEDULER_add_now(&connection_ready, entry);
914 }
915 return gh;
912 } 916 }
913 return gh;
914 }
915 op = NULL; 917 op = NULL;
916 switch (gh->service) 918 switch (gh->service)
917 { 919 {
918 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT: 920 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
919 if (NULL != entry->op_transport) 921 if (NULL != entry->op_transport)
920 return gh; /* Operation pending */ 922 return gh; /* Operation pending */
921 op = GNUNET_TESTBED_operation_create_ (entry, 923 op = GNUNET_TESTBED_operation_create_(entry,
922 &opstart_get_handle_transport, 924 &opstart_get_handle_transport,
923 &oprelease_get_handle_transport); 925 &oprelease_get_handle_transport);
924 entry->op_transport = op; 926 entry->op_transport = op;
925 break; 927 break;
926 case GST_CONNECTIONPOOL_SERVICE_CORE: 928
927 if (NULL != entry->op_core) 929 case GST_CONNECTIONPOOL_SERVICE_CORE:
928 return gh; /* Operation pending */ 930 if (NULL != entry->op_core)
929 op = GNUNET_TESTBED_operation_create_ (entry, 931 return gh; /* Operation pending */
930 &opstart_get_handle_core, 932 op = GNUNET_TESTBED_operation_create_(entry,
931 &oprelease_get_handle_core); 933 &opstart_get_handle_core,
932 entry->op_core = op; 934 &oprelease_get_handle_core);
933 break; 935 entry->op_core = op;
934 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY: 936 break;
935 if (NULL != entry->op_ats_connectivity) 937
936 return gh; /* Operation pending */ 938 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
937 op = 939 if (NULL != entry->op_ats_connectivity)
938 GNUNET_TESTBED_operation_create_ (entry, 940 return gh; /* Operation pending */
939 &opstart_get_handle_ats_connectivity, 941 op =
940 &oprelease_get_handle_ats_connectivity); 942 GNUNET_TESTBED_operation_create_(entry,
941 entry->op_ats_connectivity = op; 943 &opstart_get_handle_ats_connectivity,
942 break; 944 &oprelease_get_handle_ats_connectivity);
943 } 945 entry->op_ats_connectivity = op;
944 GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, op); 946 break;
945 GNUNET_TESTBED_operation_begin_wait_ (op); 947 }
948 GNUNET_TESTBED_operation_queue_insert_(GST_opq_openfds, op);
949 GNUNET_TESTBED_operation_begin_wait_(op);
946 return gh; 950 return gh;
947} 951}
948 952
@@ -960,66 +964,66 @@ GST_connection_pool_get_handle (
960 * @param gh the handle 964 * @param gh the handle
961 */ 965 */
962void 966void
963GST_connection_pool_get_handle_done (struct GST_ConnectionPool_GetHandle *gh) 967GST_connection_pool_get_handle_done(struct GST_ConnectionPool_GetHandle *gh)
964{ 968{
965 struct PooledConnection *entry; 969 struct PooledConnection *entry;
966 970
967 if (NULL == gh) 971 if (NULL == gh)
968 return; 972 return;
969 entry = gh->entry; 973 entry = gh->entry;
970 LOG_DEBUG ("Cleaning up get handle %p for service %u, peer %u\n", 974 LOG_DEBUG("Cleaning up get handle %p for service %u, peer %u\n",
971 gh, 975 gh,
972 gh->service, 976 gh->service,
973 entry->index); 977 entry->index);
974 if (! gh->connection_ready_called) 978 if (!gh->connection_ready_called)
975 {
976 GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
977 if ((NULL == search_waiting (entry, entry->head_waiting)) &&
978 (NULL != entry->notify_task))
979 { 979 {
980 GNUNET_SCHEDULER_cancel (entry->notify_task); 980 GNUNET_CONTAINER_DLL_remove(entry->head_waiting, entry->tail_waiting, gh);
981 entry->notify_task = NULL; 981 if ((NULL == search_waiting(entry, entry->head_waiting)) &&
982 (NULL != entry->notify_task))
983 {
984 GNUNET_SCHEDULER_cancel(entry->notify_task);
985 entry->notify_task = NULL;
986 }
982 } 987 }
983 }
984 if (gh->notify_waiting) 988 if (gh->notify_waiting)
985 { 989 {
986 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh); 990 GNUNET_CONTAINER_DLL_remove(entry->head_notify, entry->tail_notify, gh);
987 gh->notify_waiting = 0; 991 gh->notify_waiting = 0;
988 } 992 }
989 GNUNET_free (gh); 993 GNUNET_free(gh);
990 gh = NULL; 994 gh = NULL;
991 GNUNET_assert (! entry->in_lru); 995 GNUNET_assert(!entry->in_lru);
992 if (! entry->in_pool) 996 if (!entry->in_pool)
993 GNUNET_CONTAINER_DLL_remove (head_not_pooled, tail_not_pooled, entry); 997 GNUNET_CONTAINER_DLL_remove(head_not_pooled, tail_not_pooled, entry);
994 if (NULL != map) 998 if (NULL != map)
995 {
996 if (GNUNET_YES ==
997 GNUNET_CONTAINER_multihashmap32_contains (map, entry->index))
998 goto unallocate;
999 if (GNUNET_CONTAINER_multihashmap32_size (map) == max_size)
1000 { 999 {
1001 if (NULL == head_lru) 1000 if (GNUNET_YES ==
1001 GNUNET_CONTAINER_multihashmap32_contains(map, entry->index))
1002 goto unallocate; 1002 goto unallocate;
1003 destroy_pooled_connection (head_lru); 1003 if (GNUNET_CONTAINER_multihashmap32_size(map) == max_size)
1004 {
1005 if (NULL == head_lru)
1006 goto unallocate;
1007 destroy_pooled_connection(head_lru);
1008 }
1009 GNUNET_assert(GNUNET_OK ==
1010 GNUNET_CONTAINER_multihashmap32_put(
1011 map,
1012 entry->index,
1013 entry,
1014 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1015 entry->in_pool = GNUNET_YES;
1004 } 1016 }
1005 GNUNET_assert (GNUNET_OK ==
1006 GNUNET_CONTAINER_multihashmap32_put (
1007 map,
1008 entry->index,
1009 entry,
1010 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1011 entry->in_pool = GNUNET_YES;
1012 }
1013 1017
1014unallocate: 1018unallocate:
1015 GNUNET_assert (0 < entry->demand); 1019 GNUNET_assert(0 < entry->demand);
1016 entry->demand--; 1020 entry->demand--;
1017 if (0 != entry->demand) 1021 if (0 != entry->demand)
1018 return; 1022 return;
1019 if (entry->in_pool) 1023 if (entry->in_pool)
1020 { 1024 {
1021 add_to_lru (entry); 1025 add_to_lru(entry);
1022 return; 1026 return;
1023 } 1027 }
1024 destroy_pooled_connection (entry); 1028 destroy_pooled_connection(entry);
1025} 1029}