testing_api_cmd_hello_world_birth.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2021 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 testing/testing_api_cmd_hello_world.c
  18. * @brief implementation of a hello world command.
  19. * @author t3sserakt
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_ng_lib.h"
  24. struct HelloWorldBirthState
  25. {
  26. struct GNUNET_TIME_Absolute *date;
  27. char *what_am_i;
  28. };
  29. /**
  30. *
  31. *
  32. * @param cls closure
  33. * @param cmd current CMD being cleaned up.
  34. */
  35. static void
  36. hello_world_birth_cleanup (void *cls,
  37. const struct GNUNET_TESTING_Command *cmd)
  38. {
  39. struct HelloWorldBirthState *hbs = cls;
  40. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  41. "Finished birth of %s\n",
  42. hbs->what_am_i);
  43. }
  44. /**
  45. *
  46. *
  47. * @param cls closure.
  48. * @param[out] ret result
  49. * @param trait name of the trait.
  50. * @param index index number of the object to offer.
  51. * @return #GNUNET_OK on success.
  52. */
  53. static int
  54. hello_world_birth_traits (void *cls,
  55. const void **ret,
  56. const char *trait,
  57. unsigned int index)
  58. {
  59. struct HelloWorldBirthState *hbs = cls;
  60. const char *what_am_i = hbs->what_am_i;
  61. struct GNUNET_TESTING_Trait traits[] = {
  62. {
  63. .index = 0,
  64. .trait_name = "what_am_i",
  65. .ptr = (const void *) what_am_i,
  66. },
  67. GNUNET_TESTING_trait_end ()
  68. };
  69. return GNUNET_TESTING_get_trait (traits,
  70. ret,
  71. trait,
  72. index);
  73. }
  74. /**
  75. * Run the "hello world" CMD.
  76. *
  77. * @param cls closure.
  78. * @param cmd CMD being run.
  79. * @param is interpreter state.
  80. */
  81. static void
  82. hello_world_birth_run (void *cls,
  83. const struct GNUNET_TESTING_Command *cmd,
  84. struct GNUNET_TESTING_Interpreter *is)
  85. {
  86. struct HelloWorldBirthState *hbs = cls;
  87. struct GNUNET_TIME_Relative relativ;
  88. relativ = GNUNET_TIME_absolute_get_difference (*hbs->date,
  89. GNUNET_TIME_absolute_get ());
  90. if (0 == relativ.rel_value_us % 10)
  91. {
  92. hbs->what_am_i = "creature!";
  93. }
  94. else if (0 == relativ.rel_value_us % 2)
  95. {
  96. hbs->what_am_i = "girl!";
  97. }
  98. else
  99. {
  100. hbs->what_am_i = "boy!";
  101. }
  102. }
  103. /**
  104. * Offer data from trait
  105. *
  106. * @param cmd command to extract the message from.
  107. * @param pt pointer to message.
  108. * @return #GNUNET_OK on success.
  109. */
  110. int
  111. GNUNET_TESTING_get_trait_what_am_i (const struct GNUNET_TESTING_Command *cmd,
  112. char **what_am_i)
  113. {
  114. return cmd->traits (cmd->cls,
  115. (const void **) what_am_i,
  116. "what_am_i",
  117. (unsigned int) 0);
  118. }
  119. /**
  120. * Create command.
  121. *
  122. * @param label name for command.
  123. * @param now when the command was started.
  124. * @return command.
  125. */
  126. struct GNUNET_TESTING_Command
  127. GNUNET_TESTING_cmd_hello_world_birth (const char *label,
  128. struct GNUNET_TIME_Absolute *now)
  129. {
  130. struct HelloWorldBirthState *hbs;
  131. hbs = GNUNET_new (struct HelloWorldBirthState);
  132. hbs->date = now;
  133. struct GNUNET_TESTING_Command cmd = {
  134. .cls = hbs,
  135. .label = label,
  136. .run = &hello_world_birth_run,
  137. .cleanup = &hello_world_birth_cleanup,
  138. .traits = &hello_world_birth_traits
  139. };
  140. return cmd;
  141. }