arm_monitor_api.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2012, 2013, 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 arm/arm_monitor_api.c
  18. * @brief API for monitoring the ARM service
  19. * @author Christian Grothoff
  20. * @author LRN
  21. */
  22. #include "platform.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_protocols.h"
  26. #include "arm.h"
  27. #define INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  28. #define LOG(kind,...) GNUNET_log_from (kind, "arm-monitor-api",__VA_ARGS__)
  29. /**
  30. * Handle for interacting with ARM.
  31. */
  32. struct GNUNET_ARM_MonitorHandle
  33. {
  34. /**
  35. * Our control connection to the ARM service.
  36. */
  37. struct GNUNET_MQ_Handle *mq;
  38. /**
  39. * The configuration that we are using.
  40. */
  41. const struct GNUNET_CONFIGURATION_Handle *cfg;
  42. /**
  43. * ID of the reconnect task (if any).
  44. */
  45. struct GNUNET_SCHEDULER_Task *reconnect_task;
  46. /**
  47. * Current delay we use for re-trying to connect to core.
  48. */
  49. struct GNUNET_TIME_Relative retry_backoff;
  50. /**
  51. * Callback to invoke on status updates.
  52. */
  53. GNUNET_ARM_ServiceStatusCallback service_status;
  54. /**
  55. * Closure for @e service_status.
  56. */
  57. void *service_status_cls;
  58. };
  59. /**
  60. * Connect to the ARM service for monitoring.
  61. *
  62. * @param h handle to connect
  63. * @return #GNUNET_OK on success
  64. */
  65. static int
  66. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
  67. /**
  68. * Task scheduled to try to re-connect to arm.
  69. *
  70. * @param cls the `struct GNUNET_ARM_MonitorHandle`
  71. */
  72. static void
  73. reconnect_arm_monitor_task (void *cls)
  74. {
  75. struct GNUNET_ARM_MonitorHandle *h = cls;
  76. h->reconnect_task = NULL;
  77. LOG (GNUNET_ERROR_TYPE_DEBUG,
  78. "Connecting to ARM service for monitoring after delay\n");
  79. GNUNET_break (GNUNET_OK == reconnect_arm_monitor (h));
  80. }
  81. /**
  82. * Close down any existing connection to the ARM service and
  83. * try re-establishing it later.
  84. *
  85. * @param h our handle
  86. */
  87. static void
  88. reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
  89. {
  90. if (NULL != h->mq)
  91. {
  92. GNUNET_MQ_destroy (h->mq);
  93. h->mq = NULL;
  94. }
  95. GNUNET_assert (NULL == h->reconnect_task);
  96. h->reconnect_task
  97. = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
  98. &reconnect_arm_monitor_task, h);
  99. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  100. }
  101. /**
  102. * Check notification messages received from ARM is well-formed.
  103. *
  104. * @param cls our `struct GNUNET_ARM_MonitorHandle`
  105. * @param msg the message received from the arm service
  106. * @return #GNUNET_OK if the message is well-formed
  107. */
  108. static int
  109. check_monitor_notify (void *cls,
  110. const struct GNUNET_ARM_StatusMessage *msg)
  111. {
  112. size_t sl = ntohs (msg->header.size) - sizeof (struct GNUNET_ARM_StatusMessage);
  113. const char *name = (const char *) &msg[1];
  114. if ( (0 == sl) ||
  115. ('\0' != name[sl-1]) )
  116. {
  117. GNUNET_break (0);
  118. return GNUNET_SYSERR;
  119. }
  120. return GNUNET_OK;
  121. }
  122. /**
  123. * Handler for notification messages received from ARM.
  124. *
  125. * @param cls our `struct GNUNET_ARM_MonitorHandle`
  126. * @param res the message received from the arm service
  127. */
  128. static void
  129. handle_monitor_notify (void *cls,
  130. const struct GNUNET_ARM_StatusMessage *res)
  131. {
  132. struct GNUNET_ARM_MonitorHandle *h = cls;
  133. enum GNUNET_ARM_ServiceStatus status;
  134. status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
  135. LOG (GNUNET_ERROR_TYPE_DEBUG,
  136. "Received notification from ARM for service `%s' with status %d\n",
  137. (const char *) &res[1],
  138. (int) status);
  139. if (NULL != h->service_status)
  140. h->service_status (h->service_status_cls,
  141. (const char *) &res[1],
  142. status);
  143. }
  144. /**
  145. * Generic error handler, called with the appropriate error code and
  146. * the same closure specified at the creation of the message queue.
  147. * Not every message queue implementation supports an error handler.
  148. *
  149. * @param cls closure with the `struct GNUNET_ARM_MonitorHandle *`
  150. * @param error error code
  151. */
  152. static void
  153. mq_error_handler (void *cls,
  154. enum GNUNET_MQ_Error error)
  155. {
  156. struct GNUNET_ARM_MonitorHandle *h = cls;
  157. reconnect_arm_monitor_later (h);
  158. }
  159. /**
  160. * Connect to the ARM service for monitoring.
  161. *
  162. * @param h handle to connect
  163. * @return #GNUNET_OK on success
  164. */
  165. static int
  166. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
  167. {
  168. struct GNUNET_MQ_MessageHandler handlers[] = {
  169. GNUNET_MQ_hd_var_size (monitor_notify,
  170. GNUNET_MESSAGE_TYPE_ARM_STATUS,
  171. struct GNUNET_ARM_StatusMessage,
  172. h),
  173. GNUNET_MQ_handler_end ()
  174. };
  175. struct GNUNET_MessageHeader *msg;
  176. struct GNUNET_MQ_Envelope *env;
  177. GNUNET_assert (NULL == h->mq);
  178. h->mq = GNUNET_CLIENT_connect (h->cfg,
  179. "arm",
  180. handlers,
  181. &mq_error_handler,
  182. h);
  183. if (NULL == h->mq)
  184. {
  185. if (NULL != h->service_status)
  186. h->service_status (h->service_status_cls,
  187. NULL,
  188. GNUNET_ARM_SERVICE_STOPPED);
  189. return GNUNET_SYSERR;
  190. }
  191. env = GNUNET_MQ_msg (msg,
  192. GNUNET_MESSAGE_TYPE_ARM_MONITOR);
  193. GNUNET_MQ_send (h->mq,
  194. env);
  195. return GNUNET_OK;
  196. }
  197. /**
  198. * Setup a context for monitoring ARM, then
  199. * start connecting to the ARM service for monitoring using that context.
  200. *
  201. * @param cfg configuration to use (needed to contact ARM;
  202. * the ARM service may internally use a different
  203. * configuration to determine how to start the service).
  204. * @param cont callback to invoke on status updates
  205. * @param cont_cls closure for @a cont
  206. * @return context to use for further ARM monitor operations, NULL on error.
  207. */
  208. struct GNUNET_ARM_MonitorHandle *
  209. GNUNET_ARM_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
  210. GNUNET_ARM_ServiceStatusCallback cont,
  211. void *cont_cls)
  212. {
  213. struct GNUNET_ARM_MonitorHandle *h;
  214. h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
  215. h->cfg = cfg;
  216. h->service_status = cont;
  217. h->service_status_cls = cont_cls;
  218. if (GNUNET_OK != reconnect_arm_monitor (h))
  219. {
  220. GNUNET_free (h);
  221. return NULL;
  222. }
  223. return h;
  224. }
  225. /**
  226. * Disconnect from the ARM service (if connected) and destroy the context.
  227. *
  228. * @param h the handle that was being used
  229. */
  230. void
  231. GNUNET_ARM_monitor_stop (struct GNUNET_ARM_MonitorHandle *h)
  232. {
  233. if (NULL != h->mq)
  234. {
  235. GNUNET_MQ_destroy (h->mq);
  236. h->mq = NULL;
  237. }
  238. if (NULL != h->reconnect_task)
  239. {
  240. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  241. h->reconnect_task = NULL;
  242. }
  243. GNUNET_free (h);
  244. }
  245. /* end of arm_api.c */