test_ats_api_performance_list_all_addresses.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_performance_list_addresses.c
  19. * @brief test performance API's address listing feature
  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. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  28. static struct GNUNET_SCHEDULER_Task * die_task;
  29. /**
  30. * Statistics handle
  31. */
  32. static struct GNUNET_STATISTICS_Handle *stats;
  33. /**
  34. * Configuration handle
  35. */
  36. static struct GNUNET_CONFIGURATION_Handle *cfg;
  37. /**
  38. * ATS scheduling handle
  39. */
  40. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  41. /**
  42. * ATS performance handle
  43. */
  44. static struct GNUNET_ATS_PerformanceHandle *perf_ats;
  45. static struct GNUNET_ATS_AddressListHandle* phal;
  46. static int ret;
  47. struct Address
  48. {
  49. char *plugin;
  50. size_t plugin_len;
  51. void *addr;
  52. size_t addr_len;
  53. struct GNUNET_ATS_Information *ats;
  54. int ats_count;
  55. void *session;
  56. };
  57. struct PeerContext
  58. {
  59. struct GNUNET_PeerIdentity id;
  60. struct Address *addr;
  61. };
  62. static struct PeerContext p[2];
  63. static struct Address p0_addresses[2];
  64. static struct Address p1_addresses[2];
  65. static struct GNUNET_HELLO_Address p0_ha[2];
  66. static struct GNUNET_HELLO_Address p1_ha[2];
  67. static void
  68. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  69. static void
  70. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  71. static void
  72. ats_perf_cb (void *cls,
  73. const struct GNUNET_HELLO_Address *address,
  74. int address_active,
  75. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  76. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  77. const struct GNUNET_ATS_Information *ats,
  78. uint32_t ats_count)
  79. {
  80. static int counter = 0;
  81. fprintf (stderr, "HERE!\n");
  82. if (NULL == address)
  83. {
  84. phal = 0;
  85. if (4 == counter)
  86. {
  87. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
  88. GNUNET_SCHEDULER_add_now (end, NULL);
  89. }
  90. else
  91. {
  92. GNUNET_break (0);
  93. GNUNET_SCHEDULER_shutdown ();
  94. }
  95. return;
  96. }
  97. counter++;
  98. }
  99. static int
  100. stat_cb(void *cls, const char *subsystem,
  101. const char *name, uint64_t value,
  102. int is_persistent)
  103. {
  104. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  105. "ATS statistics: `%s' `%s' %llu\n",
  106. subsystem,name, value);
  107. if (4 == value)
  108. {
  109. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  110. "All addresses added, connecting to performance\n");
  111. if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
  112. {
  113. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  114. "Failed to connect to performance API\n");
  115. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  116. }
  117. phal = GNUNET_ATS_performance_list_addresses (perf_ats,
  118. NULL,
  119. GNUNET_YES,
  120. &ats_perf_cb, NULL);
  121. }
  122. return GNUNET_OK;
  123. }
  124. static void
  125. address_suggest_cb (void *cls,
  126. const struct GNUNET_PeerIdentity *peer,
  127. const struct GNUNET_HELLO_Address *address,
  128. struct Session *session,
  129. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  130. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  133. "Did not expect suggestion callback!\n");
  134. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  135. }
  136. static void
  137. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  138. {
  139. die_task = NULL;
  140. end ( NULL, NULL);
  141. ret = GNUNET_SYSERR;
  142. }
  143. static void
  144. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  145. {
  146. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  147. "Shutting down\n");
  148. if (die_task != NULL )
  149. {
  150. GNUNET_SCHEDULER_cancel (die_task);
  151. die_task = NULL;
  152. }
  153. if (NULL != sched_ats)
  154. {
  155. GNUNET_ATS_scheduling_done (sched_ats);
  156. sched_ats = NULL;
  157. }
  158. if (phal != NULL )
  159. {
  160. GNUNET_ATS_performance_list_addresses_cancel (phal);
  161. phal = NULL;
  162. }
  163. if (perf_ats != NULL )
  164. {
  165. GNUNET_ATS_performance_done (perf_ats);
  166. perf_ats = NULL;
  167. }
  168. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  169. if (NULL != stats)
  170. {
  171. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  172. stats = NULL;
  173. }
  174. GNUNET_free_non_null(p0_addresses[0].addr);
  175. GNUNET_free_non_null(p0_addresses[1].addr);
  176. GNUNET_free_non_null(p1_addresses[0].addr);
  177. GNUNET_free_non_null(p1_addresses[1].addr);
  178. ret = 0;
  179. }
  180. static void
  181. run (void *cls,
  182. const struct GNUNET_CONFIGURATION_Handle *mycfg,
  183. struct GNUNET_TESTING_Peer *peer)
  184. {
  185. ret = 1;
  186. cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
  187. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
  188. stats = GNUNET_STATISTICS_create ("ats", cfg);
  189. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  190. /* set up peer 0 */
  191. memset (&p[0].id, '1', sizeof (p[0].id));
  192. p0_addresses[0].plugin = "test";
  193. p0_addresses[0].session = NULL;
  194. p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
  195. p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
  196. p0_ha[0].address = p0_addresses[0].addr;
  197. p0_ha[0].address_length = p0_addresses[0].addr_len;
  198. p0_ha[0].peer = p[0].id;
  199. p0_ha[0].transport_name = p0_addresses[0].plugin;
  200. p0_addresses[1].plugin = "test";
  201. p0_addresses[1].session = NULL;
  202. p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
  203. p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
  204. p0_ha[1].address = p0_addresses[1].addr;
  205. p0_ha[1].address_length = p0_addresses[1].addr_len;
  206. p0_ha[1].peer = p[0].id;
  207. p0_ha[1].transport_name = p0_addresses[1].plugin;
  208. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
  209. GNUNET_i2s (&p[0].id));
  210. memset (&p[1].id, '2', sizeof (p[1].id));
  211. p1_addresses[0].plugin = "test";
  212. p1_addresses[0].session = NULL;
  213. p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
  214. p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
  215. p1_ha[0].address = p1_addresses[0].addr;
  216. p1_ha[0].address_length = p1_addresses[0].addr_len;
  217. p1_ha[0].peer = p[1].id;
  218. p1_ha[0].transport_name = p1_addresses[0].plugin;
  219. p1_addresses[1].plugin = "test";
  220. p1_addresses[1].session = NULL;
  221. p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
  222. p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
  223. p1_ha[1].address = p1_addresses[1].addr;
  224. p1_ha[1].address_length = p1_addresses[1].addr_len;
  225. p1_ha[1].peer = p[1].id;
  226. p1_ha[1].transport_name = p1_addresses[1].plugin;
  227. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  228. "Created peer 1: `%s'\n",
  229. GNUNET_i2s (&p[1].id));
  230. /* Add addresses */
  231. sched_ats = GNUNET_ATS_scheduling_init (cfg,
  232. &address_suggest_cb, NULL);
  233. if (sched_ats == NULL )
  234. {
  235. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
  236. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  237. return;
  238. }
  239. GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
  240. GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
  241. GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
  242. GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
  243. }
  244. int
  245. main (int argc, char *argv[])
  246. {
  247. if (0
  248. != GNUNET_TESTING_peer_run ("test_ats_api_performance",
  249. "test_ats_api.conf", &run, NULL ))
  250. return 1;
  251. return ret;
  252. }
  253. /* end of file test_ats_api_performance_list_addresses.c */