aboutsummaryrefslogtreecommitdiff
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.c1044
1 files changed, 0 insertions, 1044 deletions
diff --git a/src/testbed/gnunet-service-testbed_connectionpool.c b/src/testbed/gnunet-service-testbed_connectionpool.c
deleted file mode 100644
index 59780e6c1..000000000
--- a/src/testbed/gnunet-service-testbed_connectionpool.c
+++ /dev/null
@@ -1,1044 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2008--2015 GNUnet e.V.
4
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testbed/gnunet-service-testbed_connectionpool.c
23 * @brief connection pooling for connections to peers' services
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 */
26
27#include "gnunet-service-testbed.h"
28#include "gnunet-service-testbed_connectionpool.h"
29#include "testbed_api_operations.h"
30#include "gnunet_transport_service.h"
31
32/**
33 * Redefine LOG with a changed log component string
34 */
35#ifdef LOG
36#undef LOG
37#endif
38#define LOG(kind, ...) \
39 GNUNET_log_from (kind, "testbed-connectionpool", __VA_ARGS__)
40
41
42/**
43 * Time to expire a cache entry
44 */
45#define CACHE_EXPIRY \
46 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
47
48
49/**
50 * The request handle for obtaining a pooled connection
51 */
52struct GST_ConnectionPool_GetHandle;
53
54
55/**
56 * A pooled connection
57 */
58struct PooledConnection
59{
60 /**
61 * Next ptr for placing this object in the DLL of least recently used pooled
62 * connections
63 */
64 struct PooledConnection *next;
65
66 /**
67 * Prev ptr for placing this object in the DLL of the least recently used
68 * pooled connections
69 */
70 struct PooledConnection *prev;
71
72 /**
73 * The transport handle to the peer corresponding to this entry; can be NULL
74 */
75 struct GNUNET_TRANSPORT_CoreHandle *handle_transport;
76
77 /**
78 * The core handle to the peer corresponding to this entry; can be NULL
79 */
80 struct GNUNET_CORE_Handle *handle_core;
81
82 /**
83 * The ATS handle to the peer correspondign to this entry; can be NULL.
84 */
85 struct GNUNET_ATS_ConnectivityHandle *handle_ats_connectivity;
86
87 /**
88 * The operation handle for transport handle
89 */
90 struct GNUNET_TESTBED_Operation *op_transport;
91
92 /**
93 * The operation handle for core handle
94 */
95 struct GNUNET_TESTBED_Operation *op_core;
96
97 /**
98 * The operation handle for ATS handle
99 */
100 struct GNUNET_TESTBED_Operation *op_ats_connectivity;
101
102 /**
103 * The peer identity of this peer. Will be set upon opening a connection to
104 * the peers CORE service. Will be NULL until then and after the CORE
105 * connection is closed
106 */
107 struct GNUNET_PeerIdentity *peer_identity;
108
109 /**
110 * The configuration of the peer. Should be not NULL as long as the
111 * core_handle or transport_handle are valid
112 */
113 struct GNUNET_CONFIGURATION_Handle *cfg;
114
115 /**
116 * DLL head for the queue to serve notifications when a peer is connected
117 */
118 struct GST_ConnectionPool_GetHandle *head_notify;
119
120 /**
121 * DLL tail for the queue to serve notifications when a peer is connected
122 */
123 struct GST_ConnectionPool_GetHandle *tail_notify;
124
125 /**
126 * DLL head for the queue of #GST_ConnectionPool_GetHandle requests that are
127 * waiting for this connection to be opened
128 */
129 struct GST_ConnectionPool_GetHandle *head_waiting;
130
131 /**
132 * DLL tail for the queue of #GST_ConnectionPool_GetHandle requests that are
133 * waiting for this connection to be opened
134 */
135 struct GST_ConnectionPool_GetHandle *tail_waiting;
136
137 /**
138 * The task to expire this connection from the connection pool
139 */
140 struct GNUNET_SCHEDULER_Task *expire_task;
141
142 /**
143 * The task to notify a waiting #GST_ConnectionPool_GetHandle object
144 */
145 struct GNUNET_SCHEDULER_Task *notify_task;
146
147 /**
148 * Number of active requests using this pooled connection
149 */
150 unsigned int demand;
151
152 /**
153 * Is this entry in LRU
154 */
155 int in_lru;
156
157 /**
158 * Is this entry present in the connection pool
159 */
160 int in_pool;
161
162 /**
163 * The index of this peer
164 */
165 uint32_t index;
166};
167
168
169/**
170 * The request handle for obtaining a pooled connection
171 */
172struct GST_ConnectionPool_GetHandle
173{
174 /**
175 * The next ptr for inclusion in the notification DLLs. At first the object
176 * is placed in the waiting DLL of the corresponding #PooledConnection
177 * object. After the handle is opened it is moved to the notification DLL if
178 * @p connect_notify_cb and @p target are not NULL
179 */
180 struct GST_ConnectionPool_GetHandle *next;
181
182 /**
183 * The prev ptr for inclusion in the notification DLLs
184 */
185 struct GST_ConnectionPool_GetHandle *prev;
186
187 /**
188 * The pooled connection object this handle corresponds to
189 */
190 struct PooledConnection *entry;
191
192 /**
193 * The cache callback to call when a handle is available
194 */
195 GST_connection_pool_connection_ready_cb cb;
196
197 /**
198 * The closure for the above callback
199 */
200 void *cb_cls;
201
202 /**
203 * The peer identity of the target peer. When this target peer is connected,
204 * call the notify callback
205 */
206 const struct GNUNET_PeerIdentity *target;
207
208 /**
209 * The callback to be called for serving notification that the target peer is
210 * connected
211 */
212 GST_connection_pool_peer_connect_notify connect_notify_cb;
213
214 /**
215 * The closure for the notify callback
216 */
217 void *connect_notify_cb_cls;
218
219 /**
220 * The service we want to connect to
221 */
222 enum GST_ConnectionPool_Service service;
223
224 /**
225 * Did we call the pool_connection_ready_cb already?
226 */
227 int connection_ready_called;
228
229 /**
230 * Are we waiting for any peer connect notifications?
231 */
232 int notify_waiting;
233};
234
235
236/**
237 * A hashmap for quickly finding connections in the connection pool
238 */
239static struct GNUNET_CONTAINER_MultiHashMap32 *map;
240
241/**
242 * DLL head for maitaining the least recently used #PooledConnection objects.
243 * The head is the least recently used object.
244 */
245static struct PooledConnection *head_lru;
246
247/**
248 * DLL tail for maitaining the least recently used #PooledConnection objects
249 */
250static struct PooledConnection *tail_lru;
251
252/**
253 * DLL head for maintaining #PooledConnection objects that are not added into
254 * the connection pool as it was full at the time the object's creation
255 * FIXME
256 */
257static struct PooledConnection *head_not_pooled;
258
259/**
260 * DLL tail for maintaining #PooledConnection objects that are not added into
261 * the connection pool as it was full at the time the object's creation
262 */
263static struct PooledConnection *tail_not_pooled;
264
265/**
266 * The maximum number of entries that can be present in the connection pool
267 */
268static unsigned int max_size;
269
270
271/**
272 * Cancel the expiration task of the give #PooledConnection object
273 *
274 * @param entry the #PooledConnection object
275 */
276static void
277expire_task_cancel (struct PooledConnection *entry);
278
279
280/**
281 * Destroy a #PooledConnection object
282 *
283 * @param entry the #PooledConnection object
284 */
285static void
286destroy_pooled_connection (struct PooledConnection *entry)
287{
288 GNUNET_assert ((NULL == entry->head_notify) && (NULL == entry->tail_notify));
289 GNUNET_assert ((NULL == entry->head_waiting) &&
290 (NULL == entry->tail_waiting));
291 GNUNET_assert (0 == entry->demand);
292 expire_task_cancel (entry);
293 if (entry->in_lru)
294 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
295 if (entry->in_pool)
296 GNUNET_assert (
297 GNUNET_OK ==
298 GNUNET_CONTAINER_multihashmap32_remove (map, entry->index, entry));
299 if (NULL != entry->notify_task)
300 {
301 GNUNET_SCHEDULER_cancel (entry->notify_task);
302 entry->notify_task = NULL;
303 }
304 LOG_DEBUG ("Cleaning up handles of a pooled connection\n");
305 if (NULL != entry->handle_transport)
306 GNUNET_assert (NULL != entry->op_transport);
307 if (NULL != entry->op_transport)
308 {
309 GNUNET_TESTBED_operation_done (entry->op_transport);
310 entry->op_transport = NULL;
311 }
312 if (NULL != entry->handle_ats_connectivity)
313 GNUNET_assert (NULL != entry->op_ats_connectivity);
314 if (NULL != entry->op_ats_connectivity)
315 {
316 GNUNET_TESTBED_operation_done (entry->op_ats_connectivity);
317 entry->op_ats_connectivity = NULL;
318 }
319 if (NULL != entry->op_core)
320 {
321 GNUNET_TESTBED_operation_done (entry->op_core);
322 entry->op_core = NULL;
323 }
324 GNUNET_assert (NULL == entry->handle_core);
325 GNUNET_assert (NULL == entry->handle_ats_connectivity);
326 GNUNET_assert (NULL == entry->handle_transport);
327 GNUNET_CONFIGURATION_destroy (entry->cfg);
328 GNUNET_free (entry);
329}
330
331
332/**
333 * Expire a #PooledConnection object
334 *
335 * @param cls the #PooledConnection object
336 */
337static void
338expire (void *cls)
339{
340 struct PooledConnection *entry = cls;
341
342 entry->expire_task = NULL;
343 destroy_pooled_connection (entry);
344}
345
346
347/**
348 * Cancel the expiration task of the give #PooledConnection object
349 *
350 * @param entry the #PooledConnection object
351 */
352static void
353expire_task_cancel (struct PooledConnection *entry)
354{
355 if (NULL != entry->expire_task)
356 {
357 GNUNET_SCHEDULER_cancel (entry->expire_task);
358 entry->expire_task = NULL;
359 }
360}
361
362
363/**
364 * Function to add a #PooledConnection object into LRU and begin the expiry task
365 *
366 * @param entry the #PooledConnection object
367 */
368static void
369add_to_lru (struct PooledConnection *entry)
370{
371 GNUNET_assert (0 == entry->demand);
372 GNUNET_assert (! entry->in_lru);
373 GNUNET_CONTAINER_DLL_insert_tail (head_lru, tail_lru, entry);
374 entry->in_lru = GNUNET_YES;
375 GNUNET_assert (NULL == entry->expire_task);
376 entry->expire_task =
377 GNUNET_SCHEDULER_add_delayed (CACHE_EXPIRY, &expire, entry);
378}
379
380
381/**
382 * Function to find a #GST_ConnectionPool_GetHandle which is waiting for one of
383 * the handles in given entry which are now available.
384 *
385 * @param entry the pooled connection whose active list has to be searched
386 * @param head the starting list element in the GSTCacheGetHandle where the
387 * search has to be begin
388 * @return a suitable GSTCacheGetHandle whose handle ready notify callback
389 * hasn't been called yet. NULL if no such suitable GSTCacheGetHandle
390 * is found
391 */
392static struct GST_ConnectionPool_GetHandle *
393search_waiting (const struct PooledConnection *entry,
394 struct GST_ConnectionPool_GetHandle *head)
395{
396 struct GST_ConnectionPool_GetHandle *gh;
397
398 for (gh = head; NULL != gh; gh = gh->next)
399 {
400 switch (gh->service)
401 {
402 case GST_CONNECTIONPOOL_SERVICE_CORE:
403 if (NULL == entry->handle_core)
404 continue;
405 if (NULL == entry->peer_identity)
406 continue; /* CORE connection isn't ready yet */
407 break;
408
409 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
410 if (NULL == entry->handle_transport)
411 continue;
412 break;
413
414 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
415 if (NULL == entry->handle_ats_connectivity)
416 continue;
417 break;
418 }
419 break;
420 }
421 return gh;
422}
423
424
425/**
426 * A handle in the #PooledConnection object pointed by @a cls is ready and there
427 * is a #GST_ConnectionPool_GetHandle object waiting in the waiting list. This
428 * function retrieves that object and calls the handle ready callback. It
429 * further schedules itself if there are similar waiting objects which can be
430 * notified.
431 *
432 * @param cls the #PooledConnection object
433 */
434static void
435connection_ready (void *cls)
436{
437 struct PooledConnection *entry = cls;
438 struct GST_ConnectionPool_GetHandle *gh;
439 struct GST_ConnectionPool_GetHandle *gh_next;
440
441 GNUNET_assert (NULL != entry->notify_task);
442 entry->notify_task = NULL;
443 gh = search_waiting (entry, entry->head_waiting);
444 GNUNET_assert (NULL != gh);
445 gh_next = NULL;
446 if (NULL != gh->next)
447 gh_next = search_waiting (entry, gh->next);
448 GNUNET_CONTAINER_DLL_remove (entry->head_waiting,
449 entry->tail_waiting,
450 gh);
451 gh->connection_ready_called = 1;
452 if (NULL != gh_next)
453 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready,
454 entry);
455 if ((NULL != gh->target) && (NULL != gh->connect_notify_cb))
456 {
457 GNUNET_CONTAINER_DLL_insert_tail (entry->head_notify,
458 entry->tail_notify,
459 gh);
460 gh->notify_waiting = 1;
461 }
462 LOG_DEBUG ("Connection ready to %u for handle type %u\n",
463 (unsigned int) entry->index,
464 gh->service);
465 gh->cb (gh->cb_cls,
466 entry->handle_core,
467 entry->handle_transport,
468 entry->handle_ats_connectivity,
469 entry->peer_identity,
470 entry->cfg);
471}
472
473
474/**
475 * Function called from peer connect notify callbacks from CORE and TRANSPORT
476 * connections. This function calls the pending peer connect notify callbacks
477 * which are queued in an entry.
478 *
479 * @param cls the #PooledConnection object
480 * @param peer the peer that connected
481 * @param service the service where this notification has originated
482 */
483static void
484peer_connect_notify_cb (void *cls,
485 const struct GNUNET_PeerIdentity *peer,
486 const enum GST_ConnectionPool_Service service)
487{
488 struct PooledConnection *entry = cls;
489 struct GST_ConnectionPool_GetHandle *gh;
490 struct GST_ConnectionPool_GetHandle *gh_next;
491 GST_connection_pool_peer_connect_notify cb;
492 void *cb_cls;
493
494 for (gh = entry->head_notify; NULL != gh;)
495 {
496 GNUNET_assert (NULL != gh->target);
497 GNUNET_assert (NULL != gh->connect_notify_cb);
498 GNUNET_assert (gh->connection_ready_called);
499 if (service != gh->service)
500 {
501 gh = gh->next;
502 continue;
503 }
504 if (0 != memcmp (gh->target, peer, sizeof(struct GNUNET_PeerIdentity)))
505 {
506 gh = gh->next;
507 continue;
508 }
509 cb = gh->connect_notify_cb;
510 cb_cls = gh->connect_notify_cb_cls;
511 gh_next = gh->next;
512 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
513 gh->notify_waiting = 0;
514 LOG_DEBUG ("Peer connected to peer %u at service %u\n",
515 entry->index,
516 gh->service);
517 gh = gh_next;
518 cb (cb_cls, peer);
519 }
520}
521
522
523/**
524 * Function called to notify transport users that another
525 * peer connected to us.
526 *
527 * @param cls the #PooledConnection object
528 * @param peer the peer that connected
529 * @param mq queue for sending data to @a peer
530 * @return NULL
531 */
532static void *
533transport_peer_connect_notify_cb (void *cls,
534 const struct GNUNET_PeerIdentity *peer,
535 struct GNUNET_MQ_Handle *mq)
536{
537 struct PooledConnection *entry = cls;
538
539 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_TRANSPORT);
540 return NULL;
541}
542
543
544/**
545 * Function called when resources for opening a connection to TRANSPORT are
546 * available.
547 *
548 * @param cls the #PooledConnection object
549 */
550static void
551opstart_get_handle_transport (void *cls)
552{
553 struct PooledConnection *entry = cls;
554
555 GNUNET_assert (NULL != entry);
556 LOG_DEBUG ("Opening a transport connection to peer %u\n", entry->index);
557 entry->handle_transport =
558 GNUNET_TRANSPORT_core_connect (entry->cfg,
559 NULL,
560 NULL,
561 entry,
562 &transport_peer_connect_notify_cb,
563 NULL,
564 NULL);
565 if (NULL == entry->handle_transport)
566 {
567 GNUNET_break (0);
568 return;
569 }
570 if (0 == entry->demand)
571 return;
572 if (NULL != entry->notify_task)
573 return;
574 if (NULL != search_waiting (entry, entry->head_waiting))
575 {
576 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
577 return;
578 }
579}
580
581
582/**
583 * Function called when the operation responsible for opening a TRANSPORT
584 * connection is marked as done.
585 *
586 * @param cls the cache entry
587 */
588static void
589oprelease_get_handle_transport (void *cls)
590{
591 struct PooledConnection *entry = cls;
592
593 if (NULL == entry->handle_transport)
594 return;
595 GNUNET_TRANSPORT_core_disconnect (entry->handle_transport);
596 entry->handle_transport = NULL;
597}
598
599
600/**
601 * Method called whenever a given peer connects at CORE level
602 *
603 * @param cls the #PooledConnection object
604 * @param peer peer identity this notification is about
605 * @param mq message queue for talking to @a peer
606 * @return peer
607 */
608static void *
609core_peer_connect_cb (void *cls,
610 const struct GNUNET_PeerIdentity *peer,
611 struct GNUNET_MQ_Handle *mq)
612{
613 struct PooledConnection *entry = cls;
614
615 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_CORE);
616 return (void *) peer;
617}
618
619
620/**
621 * Function called after #GNUNET_CORE_connect() has succeeded (or failed
622 * for good). Note that the private key of the peer is intentionally
623 * not exposed here; if you need it, your process should try to read
624 * the private key file directly (which should work if you are
625 * authorized...). Implementations of this function must not call
626 * #GNUNET_CORE_disconnect() (other than by scheduling a new task to
627 * do this later).
628 *
629 * @param cls the #PooledConnection object
630 * @param my_identity ID of this peer, NULL if we failed
631 */
632static void
633core_startup_cb (void *cls,
634 const struct GNUNET_PeerIdentity *my_identity)
635{
636 struct PooledConnection *entry = cls;
637
638 if (NULL == my_identity)
639 {
640 GNUNET_break (0);
641 return;
642 }
643 GNUNET_assert (NULL == entry->peer_identity);
644 entry->peer_identity = GNUNET_new (struct GNUNET_PeerIdentity);
645 *entry->peer_identity = *my_identity;
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Established CORE connection for peer %s (%u)\n",
648 GNUNET_i2s (my_identity),
649 (unsigned int) entry->index);
650 if (0 == entry->demand)
651 return;
652 if (NULL != entry->notify_task)
653 return;
654 if (NULL != search_waiting (entry, entry->head_waiting))
655 {
656 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
657 return;
658 }
659}
660
661
662/**
663 * Function called when resources for opening a connection to CORE are
664 * available.
665 *
666 * @param cls the #PooledConnection object
667 */
668static void
669opstart_get_handle_core (void *cls)
670{
671 struct PooledConnection *entry = cls;
672
673 GNUNET_assert (NULL != entry);
674 LOG_DEBUG ("Opening a CORE connection to peer %u\n", entry->index);
675 entry->handle_core =
676 GNUNET_CORE_connect (entry->cfg,
677 entry, /* closure */
678 &core_startup_cb, /* core startup notify */
679 &core_peer_connect_cb, /* peer connect notify */
680 NULL, /* peer disconnect notify */
681 NULL);
682}
683
684
685/**
686 * Function called when the operation responsible for opening a CORE
687 * connection is marked as done.
688 *
689 * @param cls the #PooledConnection object
690 */
691static void
692oprelease_get_handle_core (void *cls)
693{
694 struct PooledConnection *entry = cls;
695
696 if (NULL == entry->handle_core)
697 return;
698 GNUNET_CORE_disconnect (entry->handle_core);
699 entry->handle_core = NULL;
700 GNUNET_free (entry->peer_identity);
701 entry->peer_identity = NULL;
702}
703
704
705/**
706 * Function called when resources for opening a connection to ATS are
707 * available.
708 *
709 * @param cls the #PooledConnection object
710 */
711static void
712opstart_get_handle_ats_connectivity (void *cls)
713{
714 struct PooledConnection *entry = cls;
715
716 entry->handle_ats_connectivity = GNUNET_ATS_connectivity_init (entry->cfg);
717}
718
719
720/**
721 * Function called when the operation responsible for opening a ATS
722 * connection is marked as done.
723 *
724 * @param cls the #PooledConnection object
725 */
726static void
727oprelease_get_handle_ats_connectivity (void *cls)
728{
729 struct PooledConnection *entry = cls;
730
731 if (NULL == entry->handle_ats_connectivity)
732 return;
733 GNUNET_ATS_connectivity_done (entry->handle_ats_connectivity);
734 entry->handle_ats_connectivity = NULL;
735}
736
737
738/**
739 * This function will be called for every #PooledConnection object in @p map
740 *
741 * @param cls NULL
742 * @param key current key code
743 * @param value the #PooledConnection object
744 * @return #GNUNET_YES if we should continue to
745 * iterate,
746 * #GNUNET_NO if not.
747 */
748static int
749cleanup_iterator (void *cls, uint32_t key, void *value)
750{
751 struct PooledConnection *entry = value;
752
753 GNUNET_assert (NULL != entry);
754 destroy_pooled_connection (entry);
755 return GNUNET_YES;
756}
757
758
759/**
760 * Initialise the connection pool.
761 *
762 * @param size the size of the connection pool. Each entry in the connection
763 * pool can handle a connection to each of the services enumerated in
764 * #GST_ConnectionPool_Service
765 */
766void
767GST_connection_pool_init (unsigned int size)
768{
769 max_size = size;
770 if (0 == max_size)
771 return;
772 GNUNET_assert (NULL == map);
773 map = GNUNET_CONTAINER_multihashmap32_create (((size * 3) / 4) + 1);
774}
775
776
777/**
778 * Cleanup the connection pool
779 */
780void
781GST_connection_pool_destroy ()
782{
783 struct PooledConnection *entry;
784
785 if (NULL != map)
786 {
787 GNUNET_assert (
788 GNUNET_SYSERR !=
789 GNUNET_CONTAINER_multihashmap32_iterate (map, &cleanup_iterator, NULL));
790 GNUNET_CONTAINER_multihashmap32_destroy (map);
791 map = NULL;
792 }
793 while (NULL != (entry = head_lru))
794 {
795 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
796 destroy_pooled_connection (entry);
797 }
798 GNUNET_assert (NULL == head_not_pooled);
799}
800
801
802/**
803 * Get a connection handle to @a service. If the connection is opened before
804 * and the connection handle is present in the connection pool, it is returned
805 * through @a cb. @a peer_id is used for the lookup in the connection pool. If
806 * the connection handle is not present in the connection pool, a new connection
807 * handle is opened for the @a service using @a cfg. Additionally, @a target,
808 * @a connect_notify_cb can be specified to get notified when @a target is
809 * connected at @a service.
810 *
811 * @note @a connect_notify_cb will not be called if @a target is
812 * already connected @a service level. Use
813 * GNUNET_TRANSPORT_check_peer_connected() or a similar function from the
814 * respective @a service's API to check if the target peer is already connected
815 * or not. @a connect_notify_cb will be called only once or never (in case @a
816 * target cannot be connected or is already connected).
817 *
818 * @param peer_id the index of the peer
819 * @param cfg the configuration with which the transport handle has to be
820 * created if it was not present in the cache
821 * @param service the service of interest
822 * @param cb the callback to notify when the transport handle is available
823 * @param cb_cls the closure for @a cb
824 * @param target the peer identify of the peer whose connection to our TRANSPORT
825 * subsystem will be notified through the @a connect_notify_cb. Can be
826 * NULL
827 * @param connect_notify_cb the callback to call when the @a target peer is
828 * connected. This callback will only be called once or never again (in
829 * case the target peer cannot be connected). Can be NULL
830 * @param connect_notify_cb_cls the closure for @a connect_notify_cb
831 * @return the handle which can be used cancel or mark that the handle is no
832 * longer being used
833 */
834struct GST_ConnectionPool_GetHandle *
835GST_connection_pool_get_handle (
836 unsigned int peer_id,
837 const struct GNUNET_CONFIGURATION_Handle *cfg,
838 enum GST_ConnectionPool_Service service,
839 GST_connection_pool_connection_ready_cb cb,
840 void *cb_cls,
841 const struct GNUNET_PeerIdentity *target,
842 GST_connection_pool_peer_connect_notify connect_notify_cb,
843 void *connect_notify_cb_cls)
844{
845 struct GST_ConnectionPool_GetHandle *gh;
846 struct PooledConnection *entry;
847 struct GNUNET_TESTBED_Operation *op;
848 void *handle;
849 uint32_t peer_id32;
850
851 peer_id32 = (uint32_t) peer_id;
852 handle = NULL;
853 entry = NULL;
854 if (NULL != map)
855 entry = GNUNET_CONTAINER_multihashmap32_get (map, peer_id32);
856 if (NULL != entry)
857 {
858 if (entry->in_lru)
859 {
860 GNUNET_assert (0 == entry->demand);
861 expire_task_cancel (entry);
862 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
863 entry->in_lru = GNUNET_NO;
864 }
865 switch (service)
866 {
867 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
868 handle = entry->handle_transport;
869 if (NULL != handle)
870 LOG_DEBUG ("Found TRANSPORT handle for peer %u\n",
871 entry->index);
872 break;
873 case GST_CONNECTIONPOOL_SERVICE_CORE:
874 handle = entry->handle_core;
875 if (NULL != handle)
876 LOG_DEBUG ("Found CORE handle for peer %u\n",
877 entry->index);
878 break;
879 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
880 handle = entry->handle_ats_connectivity;
881 if (NULL != handle)
882 LOG_DEBUG ("Found ATS CONNECTIVITY handle for peer %u\n",
883 entry->index);
884 break;
885 }
886 }
887 else
888 {
889 entry = GNUNET_new (struct PooledConnection);
890 entry->index = peer_id32;
891 if ((NULL != map) &&
892 (GNUNET_CONTAINER_multihashmap32_size (map) < max_size))
893 {
894 GNUNET_assert (GNUNET_OK ==
895 GNUNET_CONTAINER_multihashmap32_put (
896 map,
897 entry->index,
898 entry,
899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
900 entry->in_pool = GNUNET_YES;
901 }
902 else
903 {
904 GNUNET_CONTAINER_DLL_insert_tail (head_not_pooled,
905 tail_not_pooled,
906 entry);
907 }
908 entry->cfg = GNUNET_CONFIGURATION_dup (cfg);
909 }
910 entry->demand++;
911 gh = GNUNET_new (struct GST_ConnectionPool_GetHandle);
912 gh->entry = entry;
913 gh->cb = cb;
914 gh->cb_cls = cb_cls;
915 gh->target = target;
916 gh->connect_notify_cb = connect_notify_cb;
917 gh->connect_notify_cb_cls = connect_notify_cb_cls;
918 gh->service = service;
919 GNUNET_CONTAINER_DLL_insert (entry->head_waiting,
920 entry->tail_waiting,
921 gh);
922 if (NULL != handle)
923 {
924 if (NULL == entry->notify_task)
925 {
926 if (NULL != search_waiting (entry, entry->head_waiting))
927 entry->notify_task =
928 GNUNET_SCHEDULER_add_now (&connection_ready, entry);
929 }
930 return gh;
931 }
932 op = NULL;
933 switch (gh->service)
934 {
935 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
936 if (NULL != entry->op_transport)
937 return gh; /* Operation pending */
938 op = GNUNET_TESTBED_operation_create_ (entry,
939 &opstart_get_handle_transport,
940 &oprelease_get_handle_transport);
941 entry->op_transport = op;
942 break;
943
944 case GST_CONNECTIONPOOL_SERVICE_CORE:
945 if (NULL != entry->op_core)
946 return gh; /* Operation pending */
947 op = GNUNET_TESTBED_operation_create_ (entry,
948 &opstart_get_handle_core,
949 &oprelease_get_handle_core);
950 entry->op_core = op;
951 break;
952
953 case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
954 if (NULL != entry->op_ats_connectivity)
955 return gh; /* Operation pending */
956 op =
957 GNUNET_TESTBED_operation_create_ (entry,
958 &opstart_get_handle_ats_connectivity,
959 &oprelease_get_handle_ats_connectivity);
960 entry->op_ats_connectivity = op;
961 break;
962 }
963 GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, op);
964 GNUNET_TESTBED_operation_begin_wait_ (op);
965 return gh;
966}
967
968
969/**
970 * Relinquish a #GST_ConnectionPool_GetHandle object. If the connection
971 * associated with the object is currently being used by other
972 * #GST_ConnectionPool_GetHandle objects, it is left in the connection pool. If
973 * no other objects are using the connection and the connection pool is not full
974 * then it is placed in a LRU queue. If the connection pool is full, then
975 * connections from the LRU queue are evicted and closed to create place for
976 * this connection. If the connection pool if full and the LRU queue is empty,
977 * then the connection is closed.
978 *
979 * @param gh the handle
980 */
981void
982GST_connection_pool_get_handle_done (struct GST_ConnectionPool_GetHandle *gh)
983{
984 struct PooledConnection *entry;
985
986 if (NULL == gh)
987 return;
988 entry = gh->entry;
989 LOG_DEBUG ("Cleaning up get handle %p for service %u, peer %u\n",
990 gh,
991 gh->service,
992 entry->index);
993 if (! gh->connection_ready_called)
994 {
995 GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
996 if ((NULL == search_waiting (entry, entry->head_waiting)) &&
997 (NULL != entry->notify_task))
998 {
999 GNUNET_SCHEDULER_cancel (entry->notify_task);
1000 entry->notify_task = NULL;
1001 }
1002 }
1003 if (gh->notify_waiting)
1004 {
1005 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
1006 gh->notify_waiting = 0;
1007 }
1008 GNUNET_free (gh);
1009 gh = NULL;
1010 GNUNET_assert (! entry->in_lru);
1011 if (! entry->in_pool)
1012 GNUNET_CONTAINER_DLL_remove (head_not_pooled, tail_not_pooled, entry);
1013 if (NULL != map)
1014 {
1015 if (GNUNET_YES ==
1016 GNUNET_CONTAINER_multihashmap32_contains (map, entry->index))
1017 goto unallocate;
1018 if (GNUNET_CONTAINER_multihashmap32_size (map) == max_size)
1019 {
1020 if (NULL == head_lru)
1021 goto unallocate;
1022 destroy_pooled_connection (head_lru);
1023 }
1024 GNUNET_assert (GNUNET_OK ==
1025 GNUNET_CONTAINER_multihashmap32_put (
1026 map,
1027 entry->index,
1028 entry,
1029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1030 entry->in_pool = GNUNET_YES;
1031 }
1032
1033unallocate:
1034 GNUNET_assert (0 < entry->demand);
1035 entry->demand--;
1036 if (0 != entry->demand)
1037 return;
1038 if (entry->in_pool)
1039 {
1040 add_to_lru (entry);
1041 return;
1042 }
1043 destroy_pooled_connection (entry);
1044}