test_transport_testing_startstop.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 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 transport/test_transport_testing_startstop.c
  19. * @brief test case for transport testing library:
  20. * start the peer, get the HELLO message and stop the peer
  21. *
  22. */
  23. #include "platform.h"
  24. #include "gnunet_transport_service.h"
  25. #include "transport-testing.h"
  26. /**
  27. * How long until we give up on transmitting the message?
  28. */
  29. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  30. GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  31. static struct PeerContext *p;
  32. struct GNUNET_TRANSPORT_TESTING_handle *tth;
  33. static int ret = 0;
  34. static void
  35. end ()
  36. {
  37. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
  38. if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
  39. GNUNET_SCHEDULER_cancel (timeout_task);
  40. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
  41. GNUNET_TRANSPORT_TESTING_done (tth);
  42. }
  43. static void
  44. end_badly ()
  45. {
  46. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  47. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
  48. if (NULL != p)
  49. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
  50. if (NULL != tth)
  51. GNUNET_TRANSPORT_TESTING_done (tth);
  52. ret = GNUNET_SYSERR;
  53. }
  54. static void
  55. start_cb (struct PeerContext *p, void *cls)
  56. {
  57. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') successfully started\n",
  58. p->no,
  59. GNUNET_i2s (&p->id));
  60. ret = 0;
  61. GNUNET_SCHEDULER_add_now (&end, NULL);
  62. }
  63. static void
  64. run (void *cls, char *const *args, const char *cfgfile,
  65. const struct GNUNET_CONFIGURATION_Handle *cfg)
  66. {
  67. ret = 1;
  68. tth = GNUNET_TRANSPORT_TESTING_init ();
  69. GNUNET_assert (NULL != tth);
  70. timeout_task =
  71. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
  72. p = GNUNET_TRANSPORT_TESTING_start_peer(tth, cfgfile, 1,
  73. NULL, /* receive cb */
  74. NULL, /* connect cb */
  75. NULL, /* disconnect cb */
  76. start_cb, /* startup cb */
  77. NULL); /* closure */
  78. if (NULL == p)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start peer\n");
  81. if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
  82. GNUNET_SCHEDULER_cancel (timeout_task);
  83. timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  84. }
  85. }
  86. int
  87. main (int argc, char *argv[])
  88. {
  89. GNUNET_log_setup ("test_transport_testing_startstop",
  90. "WARNING",
  91. NULL);
  92. char *const argv_1[] = { "test_transport_testing",
  93. "-c",
  94. "test_transport_api_data.conf",
  95. NULL
  96. };
  97. struct GNUNET_GETOPT_CommandLineOption options[] = {
  98. GNUNET_GETOPT_OPTION_END
  99. };
  100. GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
  101. "test_transport_testing_startstop", "nohelp", options, &run, &ret);
  102. return ret;
  103. }
  104. /* end of test_transport_testing_startstop.c */