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