test_gnunet_daemon_hostlist_reconnect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. This file is part of GNUnet
  3. (C) 2010 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 hostlist/test_gnunet_daemon_hostlist_reconnect.c
  19. * @brief test for gnunet_daemon_hostslist.c; tries to re-start the peers
  20. * and connect a second time
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_arm_service.h"
  26. #include "gnunet_transport_service.h"
  27. /**
  28. * How long until we give up on transmitting the message?
  29. */
  30. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
  31. static int ok;
  32. static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  33. struct PeerContext
  34. {
  35. struct GNUNET_CONFIGURATION_Handle *cfg;
  36. struct GNUNET_TRANSPORT_Handle *th;
  37. struct GNUNET_MessageHeader *hello;
  38. struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
  39. struct GNUNET_OS_Process *arm_proc;
  40. };
  41. static struct PeerContext p1;
  42. static struct PeerContext p2;
  43. static void
  44. clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  45. {
  46. if (NULL != p1.ghh)
  47. {
  48. GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
  49. p1.ghh = NULL;
  50. }
  51. if (p1.th != NULL)
  52. {
  53. GNUNET_TRANSPORT_disconnect (p1.th);
  54. p1.th = NULL;
  55. }
  56. if (NULL != p2.ghh)
  57. {
  58. GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
  59. p2.ghh = NULL;
  60. }
  61. if (p2.th != NULL)
  62. {
  63. GNUNET_TRANSPORT_disconnect (p2.th);
  64. p2.th = NULL;
  65. }
  66. GNUNET_SCHEDULER_shutdown ();
  67. }
  68. /**
  69. * Timeout, give up.
  70. */
  71. static void
  72. timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  73. {
  74. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  75. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  76. "Timeout trying to connect peers, test failed.\n");
  77. clean_up (NULL, tc);
  78. }
  79. /**
  80. * Function called to notify transport users that another
  81. * peer connected to us.
  82. *
  83. * @param cls closure
  84. * @param peer the peer that connected
  85. * @param latency current latency of the connection
  86. * @param distance in overlay hops, as given by transport plugin
  87. */
  88. static void
  89. notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
  90. {
  91. if (peer == NULL)
  92. return;
  93. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
  94. ok = 0;
  95. if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
  96. {
  97. GNUNET_SCHEDULER_cancel (timeout_task);
  98. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  99. }
  100. GNUNET_SCHEDULER_add_now (&clean_up, NULL);
  101. }
  102. static void
  103. process_hello (void *cls, const struct GNUNET_MessageHeader *message)
  104. {
  105. struct PeerContext *p = cls;
  106. GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
  107. p->ghh = NULL;
  108. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  109. "Received HELLO, starting hostlist service.\n");
  110. }
  111. static void
  112. setup_peer (struct PeerContext *p, const char *cfgname)
  113. {
  114. char *binary;
  115. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  116. p->cfg = GNUNET_CONFIGURATION_create ();
  117. p->arm_proc =
  118. GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  119. NULL, NULL, NULL,
  120. binary,
  121. "gnunet-service-arm",
  122. "-c", cfgname, NULL);
  123. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  124. p->th =
  125. GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
  126. GNUNET_assert (p->th != NULL);
  127. p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
  128. GNUNET_free (binary);
  129. }
  130. static void
  131. waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  132. {
  133. struct PeerContext *p = cls;
  134. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
  135. if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
  136. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  137. if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
  138. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  139. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
  140. GNUNET_OS_process_get_pid (p->arm_proc));
  141. GNUNET_OS_process_destroy (p->arm_proc);
  142. p->arm_proc = NULL;
  143. GNUNET_CONFIGURATION_destroy (p->cfg);
  144. }
  145. static void
  146. stop_arm (struct PeerContext *p)
  147. {
  148. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
  149. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
  150. }
  151. /**
  152. * Try again to connect to transport service.
  153. */
  154. static void
  155. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  156. {
  157. stop_arm (&p1);
  158. stop_arm (&p2);
  159. }
  160. static void
  161. run (void *cls, char *const *args, const char *cfgfile,
  162. const struct GNUNET_CONFIGURATION_Handle *cfg)
  163. {
  164. GNUNET_assert (ok == 1);
  165. ok++;
  166. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
  167. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  168. NULL);
  169. setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
  170. setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
  171. }
  172. int
  173. main (int argcx, char *argvx[])
  174. {
  175. static char *const argv[] = {
  176. "test-gnunet-daemon-hostlist",
  177. "-c", "test_gnunet_daemon_hostlist_data.conf",
  178. NULL
  179. };
  180. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  181. GNUNET_GETOPT_OPTION_END
  182. };
  183. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
  184. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
  185. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
  186. GNUNET_log_setup ("test-gnunet-daemon-hostlist",
  187. "WARNING",
  188. NULL);
  189. ok = 1;
  190. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
  191. "test-gnunet-daemon-hostlist", "nohelp", options, &run,
  192. &ok);
  193. if (0 == ok)
  194. {
  195. FPRINTF (stderr, "%s", ".");
  196. /* now do it again */
  197. ok = 1;
  198. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
  199. "test-gnunet-daemon-hostlist", "nohelp", options, &run,
  200. &ok);
  201. FPRINTF (stderr, "%s", ".\n");
  202. }
  203. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
  204. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
  205. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
  206. return ok;
  207. }
  208. /* end of test_gnunet_daemon_hostlist_reconnect.c */