gnunet-service-testbed_connectionpool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008--2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/gnunet-service-testbed_connectionpool.h
  18. * @brief Interface for connection pooling subroutines
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "gnunet_ats_service.h"
  22. #include "gnunet_core_service.h"
  23. #include "gnunet_transport_service.h"
  24. /**
  25. * The request handle for obtaining a pooled connection
  26. */
  27. struct GST_ConnectionPool_GetHandle;
  28. /**
  29. * The type of service
  30. */
  31. enum GST_ConnectionPool_Service
  32. {
  33. /**
  34. * Transport service
  35. */
  36. GST_CONNECTIONPOOL_SERVICE_TRANSPORT = 1,
  37. /**
  38. * Core service
  39. */
  40. GST_CONNECTIONPOOL_SERVICE_CORE,
  41. /**
  42. * ATS service
  43. */
  44. GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY
  45. };
  46. /**
  47. * Initialise the connection pool.
  48. *
  49. * @param size the size of the connection pool. Each entry in the connection
  50. * pool can handle a connection to each of the services enumerated in
  51. * #GST_ConnectionPool_Service
  52. */
  53. void
  54. GST_connection_pool_init (unsigned int size);
  55. /**
  56. * Cleanup the connection pool
  57. */
  58. void
  59. GST_connection_pool_destroy (void);
  60. /**
  61. * Functions of this type are called when the needed handle is available for
  62. * usage. These functions are to be registered with the function
  63. * GST_connection_pool_get_handle(). The corresponding handles will be set upon
  64. * success. If they are not set, then it signals an error while opening the
  65. * handles.
  66. *
  67. * @param cls the closure passed to GST_connection_pool_get_handle()
  68. * @param ch the handle to CORE. Can be NULL if it is not requested
  69. * @param th the handle to TRANSPORT. Can be NULL if it is not requested
  70. * @param ac the handle to ATS, can be NULL if it is not requested
  71. * @param peer_id the identity of the peer. Will be NULL if ch is NULL. In other
  72. * cases, its value being NULL means that CORE connection has failed.
  73. * @param cfg configuration of the peer
  74. */
  75. typedef void (*GST_connection_pool_connection_ready_cb) (
  76. void *cls,
  77. struct GNUNET_CORE_Handle *ch,
  78. struct GNUNET_TRANSPORT_CoreHandle *th,
  79. struct GNUNET_ATS_ConnectivityHandle *ac,
  80. const struct GNUNET_PeerIdentity *peer_id,
  81. const struct GNUNET_CONFIGURATION_Handle *cfg);
  82. /**
  83. * Callback to notify when the target peer given to
  84. * GST_connection_pool_get_handle() is connected.
  85. *
  86. * @param cls the closure given to GST_connection_pool_get_handle() for this
  87. * callback
  88. * @param target the peer identity of the target peer
  89. */
  90. typedef void (*GST_connection_pool_peer_connect_notify) (
  91. void *cls,
  92. const struct GNUNET_PeerIdentity *target);
  93. /**
  94. * Get a connection handle to @a service. If the connection is opened before
  95. * and the connection handle is present in the connection pool, it is returned
  96. * through @a cb. @a peer_id is used for the lookup in the connection pool. If
  97. * the connection handle is not present in the connection pool, a new connection
  98. * handle is opened for the @a service using @a cfg. Additionally, @a target,
  99. * @a connect_notify_cb can be specified to get notified when @a target is
  100. * connected at @a service.
  101. *
  102. * @note @a connect_notify_cb will not be called if @a target is
  103. * already connected @a service level. Use
  104. * GNUNET_TRANSPORT_check_peer_connected() or a similar function from the
  105. * respective @a service's API to check if the target peer is already connected
  106. * or not. @a connect_notify_cb will be called only once or never (in case @a
  107. * target cannot be connected or is already connected).
  108. *
  109. * @param peer_id the index of the peer
  110. * @param cfg the configuration with which the transport handle has to be
  111. * created if it was not present in the cache
  112. * @param service the service of interest
  113. * @param cb the callback to notify when the transport handle is available
  114. * @param cb_cls the closure for @a cb
  115. * @param target the peer identify of the peer whose connection to our TRANSPORT
  116. * subsystem will be notified through the @a connect_notify_cb. Can be
  117. * NULL
  118. * @param connect_notify_cb the callback to call when the @a target peer is
  119. * connected. This callback will only be called once or never again (in
  120. * case the target peer cannot be connected). Can be NULL
  121. * @param connect_notify_cb_cls the closure for @a connect_notify_cb
  122. * @return the handle which can be used cancel or mark that the handle is no
  123. * longer being used
  124. */
  125. struct GST_ConnectionPool_GetHandle *
  126. GST_connection_pool_get_handle (
  127. unsigned int peer_id,
  128. const struct GNUNET_CONFIGURATION_Handle *cfg,
  129. enum GST_ConnectionPool_Service service,
  130. GST_connection_pool_connection_ready_cb cb,
  131. void *cb_cls,
  132. const struct GNUNET_PeerIdentity *target,
  133. GST_connection_pool_peer_connect_notify connect_notify_cb,
  134. void *connect_notify_cb_cls);
  135. /**
  136. * Relinquish a #GST_ConnectionPool_GetHandle object. If the connection
  137. * associated with the object is currently being used by other
  138. * #GST_ConnectionPool_GetHandle objects, it is left in the connection pool. If
  139. * no other objects are using the connection and the connection pool is not full
  140. * then it is placed in a LRU queue. If the connection pool is full, then
  141. * connections from the LRU queue are evicted and closed to create place for
  142. * this connection. If the connection pool if full and the LRU queue is empty,
  143. * then the connection is closed.
  144. *
  145. * @param gh the handle
  146. */
  147. void
  148. GST_connection_pool_get_handle_done (struct GST_ConnectionPool_GetHandle *gh);
  149. /* End of gnunet-service-testbed_connectionpool.h */