nse_api.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011, 2016 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 nse/nse_api.c
  18. * @brief api to get information from the network size estimation service
  19. * @author Nathan Evans
  20. */
  21. #include "platform.h"
  22. #include "gnunet_constants.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_hello_lib.h"
  25. #include "gnunet_protocols.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_nse_service.h"
  28. #include "nse.h"
  29. #define LOG(kind, ...) GNUNET_log_from (kind, "nse-api", __VA_ARGS__)
  30. /**
  31. * Handle for talking with the NSE service.
  32. */
  33. struct GNUNET_NSE_Handle
  34. {
  35. /**
  36. * Configuration to use.
  37. */
  38. const struct GNUNET_CONFIGURATION_Handle *cfg;
  39. /**
  40. * Message queue (if available).
  41. */
  42. struct GNUNET_MQ_Handle *mq;
  43. /**
  44. * Task doing exponential back-off trying to reconnect.
  45. */
  46. struct GNUNET_SCHEDULER_Task *reconnect_task;
  47. /**
  48. * Time for next connect retry.
  49. */
  50. struct GNUNET_TIME_Relative reconnect_delay;
  51. /**
  52. * Callback function to call when message is received.
  53. */
  54. GNUNET_NSE_Callback recv_cb;
  55. /**
  56. * Closure to pass to @e recv_cb callback.
  57. */
  58. void *recv_cb_cls;
  59. };
  60. /**
  61. * Try again to connect to network size estimation service.
  62. *
  63. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  64. */
  65. static void
  66. reconnect (void *cls);
  67. /**
  68. * Generic error handler, called with the appropriate
  69. * error code and the same closure specified at the creation of
  70. * the message queue.
  71. * Not every message queue implementation supports an error handler.
  72. *
  73. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  74. * @param error error code
  75. */
  76. static void
  77. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  78. {
  79. struct GNUNET_NSE_Handle *h = cls;
  80. (void) error;
  81. GNUNET_MQ_destroy (h->mq);
  82. h->mq = NULL;
  83. h->reconnect_task =
  84. GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
  85. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  86. }
  87. /**
  88. * Type of a function to call when we receive a message
  89. * from the service.
  90. *
  91. * @param cls closure
  92. * @param client_msg message received
  93. */
  94. static void
  95. handle_estimate (void *cls, const struct GNUNET_NSE_ClientMessage *client_msg)
  96. {
  97. struct GNUNET_NSE_Handle *h = cls;
  98. h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
  99. h->recv_cb (h->recv_cb_cls,
  100. GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
  101. GNUNET_ntoh_double (client_msg->size_estimate),
  102. GNUNET_ntoh_double (client_msg->std_deviation));
  103. }
  104. /**
  105. * Try again to connect to network size estimation service.
  106. *
  107. * @param cls the `struct GNUNET_NSE_Handle *`
  108. */
  109. static void
  110. reconnect (void *cls)
  111. {
  112. struct GNUNET_NSE_Handle *h = cls;
  113. struct GNUNET_MQ_MessageHandler handlers[] =
  114. { GNUNET_MQ_hd_fixed_size (estimate,
  115. GNUNET_MESSAGE_TYPE_NSE_ESTIMATE,
  116. struct GNUNET_NSE_ClientMessage,
  117. h),
  118. GNUNET_MQ_handler_end () };
  119. struct GNUNET_MessageHeader *msg;
  120. struct GNUNET_MQ_Envelope *env;
  121. h->reconnect_task = NULL;
  122. LOG (GNUNET_ERROR_TYPE_DEBUG,
  123. "Connecting to network size estimation service.\n");
  124. GNUNET_assert (NULL == h->mq);
  125. h->mq = GNUNET_CLIENT_connect (h->cfg, "nse", handlers, &mq_error_handler, h);
  126. if (NULL == h->mq)
  127. return;
  128. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NSE_START);
  129. GNUNET_MQ_send (h->mq, env);
  130. }
  131. /**
  132. * Connect to the network size estimation service.
  133. *
  134. * @param cfg the configuration to use
  135. * @param func funtion to call with network size estimate
  136. * @param func_cls closure to pass to @a func
  137. * @return handle to use
  138. */
  139. struct GNUNET_NSE_Handle *
  140. GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  141. GNUNET_NSE_Callback func,
  142. void *func_cls)
  143. {
  144. struct GNUNET_NSE_Handle *h;
  145. GNUNET_assert (NULL != func);
  146. h = GNUNET_new (struct GNUNET_NSE_Handle);
  147. h->cfg = cfg;
  148. h->recv_cb = func;
  149. h->recv_cb_cls = func_cls;
  150. h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
  151. reconnect (h);
  152. if (NULL == h->mq)
  153. {
  154. GNUNET_free (h);
  155. return NULL;
  156. }
  157. return h;
  158. }
  159. /**
  160. * Disconnect from network size estimation service
  161. *
  162. * @param h handle to destroy
  163. */
  164. void
  165. GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
  166. {
  167. if (NULL != h->reconnect_task)
  168. {
  169. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  170. h->reconnect_task = NULL;
  171. }
  172. if (NULL != h->mq)
  173. {
  174. GNUNET_MQ_destroy (h->mq);
  175. h->mq = NULL;
  176. }
  177. GNUNET_free (h);
  178. }
  179. /* end of nse_api.c */