arm_monitor_api.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2012, 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file arm/arm_monitor_api.c
  19. * @brief API for monitoring the ARM service
  20. * @author Christian Grothoff, 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_CLIENT_Connection *monitor;
  38. /**
  39. * The configuration that we are using.
  40. */
  41. struct GNUNET_CONFIGURATION_Handle *cfg;
  42. /**
  43. * Handle for our current transmission request.
  44. */
  45. struct GNUNET_CLIENT_TransmitHandle *cth;
  46. /**
  47. * ID of the reconnect task (if any).
  48. */
  49. struct GNUNET_SCHEDULER_Task * reconnect_task;
  50. /**
  51. * Current delay we use for re-trying to connect to core.
  52. */
  53. struct GNUNET_TIME_Relative retry_backoff;
  54. /**
  55. * Are we currently disconnected and hence unable to send?
  56. */
  57. unsigned char currently_down;
  58. /**
  59. * Callback to invoke on status updates.
  60. */
  61. GNUNET_ARM_ServiceStatusCallback service_status;
  62. /**
  63. * Closure for service_status.
  64. */
  65. void *cls;
  66. /**
  67. * ID of a task to run if we fail to get a reply to the init message in time.
  68. */
  69. struct GNUNET_SCHEDULER_Task * init_timeout_task_id;
  70. };
  71. static void
  72. monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
  73. static int
  74. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
  75. /**
  76. * Task scheduled to try to re-connect to arm.
  77. *
  78. * @param cls the 'struct GNUNET_ARM_MonitorHandle'
  79. * @param tc task context
  80. */
  81. static void
  82. reconnect_arm_monitor_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  83. {
  84. struct GNUNET_ARM_MonitorHandle *h = cls;
  85. h->reconnect_task = NULL;
  86. LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to ARM service for monitoring after delay\n");
  87. reconnect_arm_monitor (h);
  88. }
  89. /**
  90. * Close down any existing connection to the ARM service and
  91. * try re-establishing it later.
  92. *
  93. * @param h our handle
  94. */
  95. static void
  96. reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
  97. {
  98. if (NULL != h->cth)
  99. {
  100. GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
  101. h->cth = NULL;
  102. }
  103. if (NULL != h->monitor)
  104. {
  105. GNUNET_CLIENT_disconnect (h->monitor);
  106. h->monitor = NULL;
  107. }
  108. if (NULL != h->init_timeout_task_id)
  109. {
  110. GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
  111. h->init_timeout_task_id = NULL;
  112. }
  113. GNUNET_assert (NULL == h->reconnect_task);
  114. h->reconnect_task =
  115. GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_monitor_task, h);
  116. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  117. }
  118. /**
  119. * Init message timed out. Disconnect and try again.
  120. *
  121. * @param cls arm monitor handle
  122. * @param tc task context
  123. */
  124. static void
  125. init_timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  126. {
  127. struct GNUNET_ARM_MonitorHandle *h = cls;
  128. LOG (GNUNET_ERROR_TYPE_DEBUG,
  129. "Init message timed out\n");
  130. h->init_timeout_task_id = NULL;
  131. reconnect_arm_monitor_later (h);
  132. }
  133. /**
  134. * Transmit the monitoring initialization message to the arm service.
  135. *
  136. * @param cls closure with the 'struct GNUNET_ARM_MonitorHandle'
  137. * @param size number of bytes available in buf
  138. * @param buf where the callee should write the message
  139. * @return number of bytes written to buf
  140. */
  141. static size_t
  142. transmit_monitoring_init_message (void *cls, size_t size, void *buf)
  143. {
  144. struct GNUNET_ARM_MonitorHandle *h = cls;
  145. struct GNUNET_MessageHeader *msg;
  146. uint16_t msize;
  147. GNUNET_assert (NULL == h->reconnect_task);
  148. GNUNET_assert (NULL == h->init_timeout_task_id);
  149. h->cth = NULL;
  150. if (NULL == buf)
  151. {
  152. LOG (GNUNET_ERROR_TYPE_DEBUG,
  153. "Transmission failed, initiating reconnect\n");
  154. reconnect_arm_monitor_later (h);
  155. return 0;
  156. }
  157. msize = sizeof (struct GNUNET_MessageHeader);
  158. if (size < msize)
  159. {
  160. LOG (GNUNET_ERROR_TYPE_DEBUG,
  161. "Request is too big (%u < %u), not sending it\n", size, msize);
  162. h->cth = GNUNET_CLIENT_notify_transmit_ready (h->monitor, msize,
  163. GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
  164. transmit_monitoring_init_message, h);
  165. return 0;
  166. }
  167. msg = buf;
  168. msg->size = htons (msize);
  169. msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_MONITOR);
  170. LOG (GNUNET_ERROR_TYPE_DEBUG,
  171. "Transmitting ARM monitoring init message with %u bytes to arm.\n",
  172. (unsigned int) msize);
  173. h->init_timeout_task_id = GNUNET_SCHEDULER_add_delayed (
  174. INIT_TIMEOUT, init_timeout_task, h);
  175. GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
  176. GNUNET_TIME_UNIT_FOREVER_REL);
  177. return msize;
  178. }
  179. static int
  180. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
  181. {
  182. GNUNET_assert (NULL == h->monitor);
  183. h->monitor = GNUNET_CLIENT_connect ("arm", h->cfg);
  184. if (NULL == h->monitor)
  185. {
  186. LOG (GNUNET_ERROR_TYPE_DEBUG,
  187. "arm_api, GNUNET_CLIENT_connect returned NULL\n");
  188. if (NULL != h->service_status)
  189. h->service_status (h->cls, NULL, GNUNET_ARM_SERVICE_STOPPED);
  190. return GNUNET_SYSERR;
  191. }
  192. LOG (GNUNET_ERROR_TYPE_DEBUG,
  193. "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
  194. h->cth = GNUNET_CLIENT_notify_transmit_ready (h->monitor,
  195. sizeof (struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL,
  196. GNUNET_NO, &transmit_monitoring_init_message, h);
  197. return GNUNET_OK;
  198. }
  199. /**
  200. * Setup a context for monitoring ARM, then
  201. * start connecting to the ARM service for monitoring using that context.
  202. *
  203. * @param cfg configuration to use (needed to contact ARM;
  204. * the ARM service may internally use a different
  205. * configuration to determine how to start the service).
  206. * @param cont callback to invoke on status updates
  207. * @param cont_cls closure
  208. * @return context to use for further ARM monitor operations, NULL on error.
  209. */
  210. struct GNUNET_ARM_MonitorHandle *
  211. GNUNET_ARM_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg,
  212. GNUNET_ARM_ServiceStatusCallback cont, void *cont_cls)
  213. {
  214. struct GNUNET_ARM_MonitorHandle *h;
  215. h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
  216. h->cfg = GNUNET_CONFIGURATION_dup (cfg);
  217. h->currently_down = GNUNET_YES;
  218. h->reconnect_task = NULL;
  219. h->init_timeout_task_id = NULL;
  220. h->service_status = cont;
  221. h->cls = cont_cls;
  222. if (GNUNET_OK != reconnect_arm_monitor (h))
  223. {
  224. GNUNET_free (h);
  225. return NULL;
  226. }
  227. return h;
  228. }
  229. /**
  230. * Disconnect from the ARM service (if connected) and destroy the context.
  231. *
  232. * @param h the handle that was being used
  233. */
  234. void
  235. GNUNET_ARM_monitor_disconnect_and_free (struct GNUNET_ARM_MonitorHandle *h)
  236. {
  237. LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
  238. if (NULL != h->cth)
  239. {
  240. GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
  241. h->cth = NULL;
  242. }
  243. if (NULL != h->init_timeout_task_id)
  244. {
  245. GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
  246. h->init_timeout_task_id = NULL;
  247. }
  248. if (NULL != h->monitor)
  249. {
  250. GNUNET_CLIENT_disconnect (h->monitor);
  251. h->monitor = NULL;
  252. }
  253. if (NULL != h->reconnect_task)
  254. {
  255. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  256. h->reconnect_task = NULL;
  257. }
  258. GNUNET_CONFIGURATION_destroy (h->cfg);
  259. GNUNET_free (h);
  260. }
  261. /**
  262. * Handler for notification messages received from ARM.
  263. *
  264. * @param cls our "struct GNUNET_ARM_MonitorHandle"
  265. * @param msg the message received from the arm service
  266. */
  267. static void
  268. monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
  269. {
  270. struct GNUNET_ARM_MonitorHandle *h = cls;
  271. uint16_t msize;
  272. const struct GNUNET_ARM_StatusMessage *res;
  273. enum GNUNET_ARM_ServiceStatus status;
  274. if (NULL == msg)
  275. {
  276. LOG (GNUNET_ERROR_TYPE_INFO,
  277. _("Monitoring client was disconnected from arm service, trying to reconnect.\n"));
  278. reconnect_arm_monitor_later (h);
  279. return;
  280. }
  281. msize = ntohs (msg->size);
  282. LOG (GNUNET_ERROR_TYPE_DEBUG,
  283. "Processing message of type %u and size %u from arm service\n",
  284. ntohs (msg->type), msize);
  285. switch (ntohs (msg->type))
  286. {
  287. case GNUNET_MESSAGE_TYPE_ARM_STATUS:
  288. if (msize <= sizeof (struct GNUNET_ARM_StatusMessage))
  289. {
  290. GNUNET_break (0);
  291. reconnect_arm_monitor_later (h);
  292. return;
  293. }
  294. if (NULL != h->init_timeout_task_id)
  295. {
  296. GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
  297. h->init_timeout_task_id = NULL;
  298. }
  299. res = (const struct GNUNET_ARM_StatusMessage *) msg;
  300. LOG (GNUNET_ERROR_TYPE_DEBUG,
  301. "Received response from ARM for service `%s': %u\n",
  302. (const char *) &res[1], ntohs (msg->type));
  303. status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
  304. GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
  305. GNUNET_TIME_UNIT_FOREVER_REL);
  306. if (NULL != h->service_status)
  307. h->service_status (h->cls, (const char *) &res[1], status);
  308. break;
  309. default:
  310. reconnect_arm_monitor_later (h);
  311. return;
  312. }
  313. }
  314. /* end of arm_api.c */