gnunet-service-test-barriers.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 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 testbed/gnunet-service-test-barriers.c
  18. * @brief Daemon acting as a service for testing testbed barriers. It is
  19. * started as a peer service and waits for a barrier to be crossed.
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testbed_service.h"
  25. #include "test_testbed_api_barriers.h"
  26. /**
  27. * logging short hand
  28. */
  29. #define LOG(type,...) \
  30. GNUNET_log (type, __VA_ARGS__);
  31. /**
  32. * Our barrier wait handle
  33. */
  34. static struct GNUNET_TESTBED_BarrierWaitHandle *wh;
  35. static struct GNUNET_SCHEDULER_Task *tt;
  36. /**
  37. * Dummy task callback to keep us running forever
  38. *
  39. * @param cls NULL
  40. */
  41. static void
  42. do_shutdown (void *cls)
  43. {
  44. if (NULL != wh)
  45. {
  46. GNUNET_TESTBED_barrier_wait_cancel (wh);
  47. wh = NULL;
  48. }
  49. if (NULL != tt)
  50. {
  51. GNUNET_SCHEDULER_cancel (tt);
  52. tt = NULL;
  53. }
  54. }
  55. /**
  56. * Functions of this type are to be given as acallback argumetn to
  57. * GNUNET_TESTBED_barrier_wait(). The callback will be called when the barrier
  58. * corresponding given in GNUNET_TESTBED_barrier_wait() is crossed or cancelled.
  59. *
  60. * @param cls NULL
  61. * @param name the barrier name
  62. * @param status #GNUNET_SYSERR in case of error while waiting for the barrier;
  63. * #GNUNET_OK if the barrier is crossed
  64. */
  65. static void
  66. barrier_wait_cb (void *cls,
  67. const char *name,
  68. int status)
  69. {
  70. GNUNET_break (NULL == cls);
  71. wh = NULL;
  72. GNUNET_break (GNUNET_OK == status);
  73. }
  74. /**
  75. * Task to wait for the barrier
  76. *
  77. * @param cls NULL
  78. * @return
  79. */
  80. static void
  81. do_wait (void *cls)
  82. {
  83. tt = NULL;
  84. wh = GNUNET_TESTBED_barrier_wait (TEST_BARRIER_NAME,
  85. &barrier_wait_cb,
  86. NULL);
  87. GNUNET_break (NULL != wh);
  88. }
  89. /**
  90. * Main run function.
  91. *
  92. * @param cls NULL
  93. * @param args arguments passed to GNUNET_PROGRAM_run
  94. * @param cfgfile the path to configuration file
  95. * @param config the configuration file handle
  96. */
  97. static void
  98. run (void *cls,
  99. char *const *args,
  100. const char *cfgfile,
  101. const struct GNUNET_CONFIGURATION_Handle *config)
  102. {
  103. unsigned int rsec;
  104. rsec = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
  105. 10);
  106. tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  107. rsec),
  108. &do_wait,
  109. NULL);
  110. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  111. }
  112. /**
  113. * Main
  114. */
  115. int
  116. main (int argc, char **argv)
  117. {
  118. struct GNUNET_GETOPT_CommandLineOption options[] = {
  119. GNUNET_GETOPT_OPTION_END
  120. };
  121. int ret;
  122. ret =
  123. GNUNET_PROGRAM_run (argc, argv,
  124. "test-barriers",
  125. "nohelp",
  126. options,
  127. &run,
  128. NULL);
  129. return ret;
  130. }