test_ats_solver_add_address_and_request.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
  3. {
  4. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  5. "Failed to connect to performance API\n");
  6. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  7. }
  8. This file is part of GNUnet.
  9. Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors)
  10. GNUnet is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published
  12. by the Free Software Foundation; either version 3, or (at your
  13. option) any later version.
  14. GNUnet is distributed in the hope that it will be useful, but
  15. WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with GNUnet; see the file COPYING. If not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. /**
  24. * @file ats/test_ats_solver_add_address.c
  25. * @brief solver test: add address, request address and wait for suggest
  26. * @author Christian Grothoff
  27. * @author Matthias Wachs
  28. */
  29. #include "platform.h"
  30. #include "gnunet_util_lib.h"
  31. #include "gnunet_testbed_service.h"
  32. #include "gnunet_ats_service.h"
  33. #include "test_ats_api_common.h"
  34. /**
  35. * Timeout task
  36. */
  37. static struct GNUNET_SCHEDULER_Task *die_task;
  38. /**
  39. * Statistics handle
  40. */
  41. static struct GNUNET_STATISTICS_Handle *stats;
  42. /**
  43. * Scheduling handle
  44. */
  45. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  46. /**
  47. * Connectivity handle
  48. */
  49. static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
  50. /**
  51. * Return value
  52. */
  53. static int ret;
  54. /**
  55. * Test address
  56. */
  57. static struct Test_Address test_addr;
  58. /**
  59. * Test peer
  60. */
  61. static struct PeerContext p;
  62. /**
  63. * HELLO address
  64. */
  65. static struct GNUNET_HELLO_Address test_hello_address;
  66. /**
  67. * Session
  68. */
  69. static void *test_session;
  70. /**
  71. * Test ats info
  72. */
  73. static struct GNUNET_ATS_Information test_ats_info[2];
  74. /**
  75. * Test ats count
  76. */
  77. static uint32_t test_ats_count;
  78. static int
  79. stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
  80. int is_persistent);
  81. static void
  82. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  83. {
  84. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
  85. if (die_task != NULL)
  86. {
  87. GNUNET_SCHEDULER_cancel (die_task);
  88. die_task = NULL;
  89. }
  90. if (NULL != sched_ats)
  91. {
  92. GNUNET_ATS_scheduling_done (sched_ats);
  93. sched_ats = NULL;
  94. }
  95. if (NULL != connect_ats)
  96. {
  97. GNUNET_ATS_connectivity_done (connect_ats);
  98. connect_ats = NULL;
  99. }
  100. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  101. if (NULL != stats)
  102. {
  103. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  104. stats = NULL;
  105. }
  106. free_test_address (&test_addr);
  107. ret = 0;
  108. }
  109. static void
  110. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  111. {
  112. die_task = NULL;
  113. end ( NULL, NULL);
  114. ret = GNUNET_SYSERR;
  115. }
  116. static void
  117. address_suggest_cb (void *cls,
  118. const struct GNUNET_PeerIdentity *peer,
  119. const struct GNUNET_HELLO_Address *address,
  120. struct Session *session,
  121. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  122. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  123. {
  124. GNUNET_assert (NULL != address);
  125. GNUNET_assert (NULL == session);
  126. GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
  127. GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
  128. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  129. "Received sugggestion for peer `%s'\n",
  130. GNUNET_i2s (peer));
  131. GNUNET_SCHEDULER_add_now (&end, NULL);
  132. }
  133. static int
  134. stat_cb (void *cls, const char *subsystem,
  135. const char *name, uint64_t value,
  136. int is_persistent)
  137. {
  138. static struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
  139. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  140. "ATS statistics: `%s' `%s' %llu\n",
  141. subsystem,
  142. name,
  143. value);
  144. if (NULL == sh)
  145. sh = GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
  146. return GNUNET_OK;
  147. }
  148. static void
  149. run (void *cls,
  150. const struct GNUNET_CONFIGURATION_Handle *mycfg,
  151. struct GNUNET_TESTING_Peer *peer)
  152. {
  153. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  154. stats = GNUNET_STATISTICS_create ("ats", mycfg);
  155. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  156. connect_ats = GNUNET_ATS_connectivity_init (mycfg);
  157. /* Connect to ATS scheduling */
  158. sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
  159. if (sched_ats == NULL)
  160. {
  161. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  162. "Could not connect to ATS scheduling!\n");
  163. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  164. return;
  165. }
  166. /* Set up peer */
  167. memset (&p.id, '1', sizeof (p.id));
  168. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  169. "Created peer `%s'\n",
  170. GNUNET_i2s_full(&p.id));
  171. /* Prepare ATS Information */
  172. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  173. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  174. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  175. test_ats_info[1].value = htonl(1);
  176. test_ats_count = 2;
  177. /* Adding address without session */
  178. test_session = NULL;
  179. create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
  180. test_hello_address.peer = p.id;
  181. test_hello_address.transport_name = test_addr.plugin;
  182. test_hello_address.address = test_addr.addr;
  183. test_hello_address.address_length = test_addr.addr_len;
  184. /* Adding address */
  185. GNUNET_ATS_address_add (sched_ats,
  186. &test_hello_address, NULL,
  187. test_ats_info, test_ats_count);
  188. }
  189. int
  190. main (int argc, char *argv[])
  191. {
  192. char *sep;
  193. char *src_filename = GNUNET_strdup (__FILE__);
  194. char *test_filename = GNUNET_strdup (argv[0]);
  195. char *config_file;
  196. char *solver;
  197. int delayed = GNUNET_NO;
  198. ret = 0;
  199. if (NULL == (sep = (strstr (src_filename,".c"))))
  200. {
  201. GNUNET_break (0);
  202. return -1;
  203. }
  204. sep[0] = '\0';
  205. if (NULL != (sep = strstr (test_filename, ".exe")))
  206. sep[0] = '\0';
  207. if (NULL == (solver = strstr (test_filename, src_filename)))
  208. {
  209. GNUNET_break (0);
  210. return -1;
  211. }
  212. solver += strlen (src_filename) +1;
  213. if (NULL != strstr (solver, "delayed_"))
  214. {
  215. delayed = GNUNET_YES;
  216. solver += strlen ("delayed_");
  217. }
  218. if (0 == strcmp(solver, "proportional"))
  219. {
  220. if (delayed)
  221. config_file = "test_ats_solver_delayed_proportional.conf";
  222. else
  223. config_file = "test_ats_solver_proportional.conf";
  224. }
  225. else if (0 == strcmp(solver, "mlp"))
  226. {
  227. if (delayed)
  228. config_file = "test_ats_solver_delayed_mlp.conf";
  229. else
  230. config_file = "test_ats_solver_mlp.conf";
  231. }
  232. else if ((0 == strcmp(solver, "ril")))
  233. {
  234. if (delayed)
  235. config_file = "test_ats_solver_delayed_ril.conf";
  236. else
  237. config_file = "test_ats_solver_ril.conf";
  238. }
  239. else
  240. {
  241. GNUNET_break (0);
  242. GNUNET_free (src_filename);
  243. GNUNET_free (test_filename);
  244. FPRINTF (stderr, "Invalid test name or configuration not found `%s'\n",src_filename);
  245. return 1;
  246. }
  247. GNUNET_free (src_filename);
  248. GNUNET_free (test_filename);
  249. if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
  250. config_file, &run, NULL ))
  251. return GNUNET_SYSERR;
  252. return ret;
  253. }
  254. /* end of file test_ats_solver_add_address.c */