test_ats_api_performance_list_all_addresses_active.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. if (NULL == address)
  82. {
  83. phal = 0;
  84. if (0 == counter)
  85. {
  86. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
  87. GNUNET_SCHEDULER_add_now (end, NULL);
  88. }
  89. else
  90. {
  91. GNUNET_break (0);
  92. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  93. }
  94. return;
  95. }
  96. else
  97. {
  98. if (0 != memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
  99. {
  100. GNUNET_break (0);
  101. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  102. }
  103. }
  104. counter ++;
  105. }
  106. static int
  107. stat_cb(void *cls, const char *subsystem,
  108. const char *name, uint64_t value,
  109. int is_persistent)
  110. {
  111. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
  112. subsystem,name, value);
  113. if (4 == value)
  114. {
  115. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  116. "All addresses added, connecting to performance\n");
  117. if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
  118. {
  119. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  120. "Failed to connect to performance API\n");
  121. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  122. }
  123. phal = GNUNET_ATS_performance_list_addresses (perf_ats, &p[0].id, GNUNET_NO, ats_perf_cb, NULL);
  124. }
  125. return GNUNET_OK;
  126. }
  127. static void
  128. address_suggest_cb (void *cls,
  129. const struct GNUNET_PeerIdentity *peer,
  130. const struct GNUNET_HELLO_Address *address,
  131. struct Session *session,
  132. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  133. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  134. {
  135. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
  136. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  137. }
  138. static void
  139. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  140. {
  141. die_task = NULL;
  142. end ( NULL, NULL);
  143. ret = GNUNET_SYSERR;
  144. }
  145. static void
  146. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  147. {
  148. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
  149. if (die_task != NULL )
  150. {
  151. GNUNET_SCHEDULER_cancel (die_task);
  152. die_task = NULL;
  153. }
  154. if (NULL != sched_ats)
  155. {
  156. GNUNET_ATS_scheduling_done (sched_ats);
  157. sched_ats = NULL;
  158. }
  159. if (phal != NULL )
  160. {
  161. GNUNET_ATS_performance_list_addresses_cancel (phal);
  162. phal = NULL;
  163. }
  164. if (perf_ats != NULL )
  165. {
  166. GNUNET_ATS_performance_done (perf_ats);
  167. perf_ats = NULL;
  168. }
  169. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  170. if (NULL != stats)
  171. {
  172. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  173. stats = NULL;
  174. }
  175. GNUNET_free_non_null(p0_addresses[0].addr);
  176. GNUNET_free_non_null(p0_addresses[1].addr);
  177. GNUNET_free_non_null(p1_addresses[0].addr);
  178. GNUNET_free_non_null(p1_addresses[1].addr);
  179. ret = 0;
  180. }
  181. static void
  182. run (void *cls, 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, "Created peer 1: `%s'\n",
  228. GNUNET_i2s (&p[1].id));
  229. /* Add addresses */
  230. sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
  231. if (sched_ats == NULL )
  232. {
  233. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
  234. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  235. return;
  236. }
  237. GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
  238. GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
  239. GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
  240. GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
  241. }
  242. int
  243. main (int argc, char *argv[])
  244. {
  245. if (0
  246. != GNUNET_TESTING_peer_run ("test_ats_api_performance",
  247. "test_ats_api.conf", &run, NULL ))
  248. return 1;
  249. return ret;
  250. }
  251. /* end of file test_ats_api_performance_list_addresses.c */