test_ats_api_scheduling_add_address_inbound.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010,2011 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 ats/test_ats_api_scheduling_add_address_inbound.c
  19. * @brief adding addresses with scheduling API
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_ats_service.h"
  25. #include "gnunet_testing_lib.h"
  26. #include "ats.h"
  27. #include "test_ats_api_common.h"
  28. /**
  29. * Timeout task
  30. */
  31. static struct GNUNET_SCHEDULER_Task * die_task;
  32. /**
  33. * Statistics handle
  34. */
  35. static struct GNUNET_STATISTICS_Handle *stats;
  36. /**
  37. * Scheduling handle
  38. */
  39. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  40. /**
  41. * Return value
  42. */
  43. static int ret;
  44. /**
  45. * Test address
  46. */
  47. static struct Test_Address test_addr;
  48. /**
  49. * Test peer
  50. */
  51. static struct PeerContext p;
  52. /**
  53. * HELLO address
  54. */
  55. static struct GNUNET_HELLO_Address test_hello_address;
  56. /**
  57. * Test ats info
  58. */
  59. static struct GNUNET_ATS_Information test_ats_info[2];
  60. /**
  61. * Test ats count
  62. */
  63. static uint32_t test_ats_count;
  64. static void
  65. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  66. static void
  67. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  68. static int
  69. stat_cb(void *cls, const char *subsystem,
  70. const char *name, uint64_t value,
  71. int is_persistent)
  72. {
  73. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
  74. subsystem,name, value);
  75. if (1 == value)
  76. {
  77. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
  78. }
  79. if (1 < value)
  80. {
  81. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  82. }
  83. return GNUNET_OK;
  84. }
  85. static void
  86. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  87. {
  88. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
  89. if (die_task != NULL)
  90. {
  91. GNUNET_SCHEDULER_cancel (die_task);
  92. die_task = NULL;
  93. }
  94. if (NULL != sched_ats)
  95. {
  96. GNUNET_ATS_scheduling_done (sched_ats);
  97. sched_ats = NULL;
  98. }
  99. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  100. if (NULL != stats)
  101. {
  102. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  103. stats = NULL;
  104. }
  105. free_test_address (&test_addr);
  106. ret = 0;
  107. }
  108. static void
  109. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  110. {
  111. die_task = NULL;
  112. end ( NULL, NULL);
  113. ret = GNUNET_SYSERR;
  114. }
  115. static void
  116. address_suggest_cb (void *cls,
  117. const struct GNUNET_PeerIdentity *peer,
  118. const struct GNUNET_HELLO_Address *address,
  119. struct Session *session,
  120. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  121. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  122. {
  123. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  124. "Did not expect suggestion callback!\n");
  125. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  126. }
  127. static void
  128. got_initial_value (void *cls, int success)
  129. {
  130. struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  131. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got initial value\n");
  132. /* Connect to ATS scheduling */
  133. sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
  134. if (sched_ats == NULL)
  135. {
  136. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
  137. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  138. return;
  139. }
  140. /* Set up peer */
  141. memset (&p.id, '1', sizeof (p.id));
  142. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  143. GNUNET_i2s_full(&p.id));
  144. /* Prepare ATS Information */
  145. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  146. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  147. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  148. test_ats_info[1].value = htonl(1);
  149. test_ats_count = 2;
  150. /* Adding address without session */
  151. create_test_address (&test_addr, "test-plugin", NULL, NULL, 0);
  152. test_hello_address.peer = p.id;
  153. test_hello_address.transport_name = test_addr.plugin;
  154. test_hello_address.address = NULL;
  155. test_hello_address.address_length = 0;
  156. /* Adding address */
  157. GNUNET_ATS_address_add (sched_ats,
  158. &test_hello_address, NULL,
  159. test_ats_info, test_ats_count);
  160. }
  161. static int
  162. dummy_stat (void *cls,
  163. const char *subsystem,
  164. const char *name,
  165. uint64_t value,
  166. int is_persistent)
  167. {
  168. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  169. "Got dummy stat %s%s:%s = %llu\n",
  170. is_persistent ? "!" : " ",
  171. subsystem,
  172. name,
  173. value);
  174. return GNUNET_OK;
  175. }
  176. static void
  177. run (void *cls,
  178. const struct GNUNET_CONFIGURATION_Handle *cfg,
  179. struct GNUNET_TESTING_Peer *peer)
  180. {
  181. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  182. stats = GNUNET_STATISTICS_create ("ats", cfg);
  183. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  184. GNUNET_STATISTICS_get (stats, "ats", "# addresses", TIMEOUT,
  185. &got_initial_value, &dummy_stat,
  186. GNUNET_CONFIGURATION_dup (cfg));
  187. }
  188. int
  189. main (int argc, char *argv[])
  190. {
  191. ret = 0;
  192. if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
  193. "test_ats_api.conf",
  194. &run, NULL))
  195. return 1;
  196. return ret;
  197. }
  198. /* end of file test_ats_api_scheduling_add_address_inbound.c */