test_gnunet_daemon_hostlist_reconnect.c 6.8 KB

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