test_ats_api_scheduling_destroy_address.c 6.6 KB

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