test_ats_solver_request_and_add_address.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. (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: request address, add 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 GNUNET_SCHEDULER_TaskIdentifier die_task;
  38. /**
  39. * Statistics handle
  40. */
  41. struct GNUNET_STATISTICS_Handle *stats;
  42. /**
  43. * Scheduling handle
  44. */
  45. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  46. /**
  47. * Return value
  48. */
  49. static int ret;
  50. /**
  51. * Test address
  52. */
  53. static struct Test_Address test_addr;
  54. /**
  55. * Test peer
  56. */
  57. static struct PeerContext p;
  58. /**
  59. * HELLO address
  60. */
  61. struct GNUNET_HELLO_Address test_hello_address;
  62. /**
  63. * Session
  64. */
  65. static void *test_session;
  66. /**
  67. * Test ats info
  68. */
  69. struct GNUNET_ATS_Information test_ats_info[2];
  70. /**
  71. * Test ats count
  72. */
  73. uint32_t test_ats_count;
  74. static int
  75. stat_cb(void *cls, const char *subsystem, 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_INFO, "Done!\n");
  81. if (die_task != GNUNET_SCHEDULER_NO_TASK)
  82. {
  83. GNUNET_SCHEDULER_cancel (die_task);
  84. die_task = GNUNET_SCHEDULER_NO_TASK;
  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", &stat_cb, NULL);
  92. if (NULL != stats)
  93. {
  94. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  95. stats = NULL;
  96. }
  97. free_test_address (&test_addr);
  98. ret = 0;
  99. }
  100. static void
  101. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  102. {
  103. die_task = GNUNET_SCHEDULER_NO_TASK;
  104. end ( NULL, NULL);
  105. ret = GNUNET_SYSERR;
  106. }
  107. static void
  108. address_suggest_cb (void *cls,
  109. const struct GNUNET_PeerIdentity *peer,
  110. const struct GNUNET_HELLO_Address *address,
  111. struct Session *session,
  112. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  113. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  114. const struct GNUNET_ATS_Information *atsi,
  115. uint32_t ats_count)
  116. {
  117. GNUNET_assert (NULL != address);
  118. GNUNET_assert (NULL == session);
  119. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received sugggestion for peer `%s'\n",
  120. GNUNET_i2s (&address->peer));
  121. GNUNET_SCHEDULER_add_now (&end, NULL);
  122. return;
  123. }
  124. static int
  125. stat_cb(void *cls, const char *subsystem,
  126. const char *name, uint64_t value,
  127. int is_persistent)
  128. {
  129. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
  130. subsystem,name, value);
  131. return GNUNET_OK;
  132. }
  133. static void
  134. run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
  135. struct GNUNET_TESTING_Peer *peer)
  136. {
  137. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  138. stats = GNUNET_STATISTICS_create ("ats", mycfg);
  139. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  140. /* Connect to ATS scheduling */
  141. sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
  142. if (sched_ats == NULL)
  143. {
  144. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
  145. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  146. return;
  147. }
  148. /* Set up peer */
  149. memset (&p.id, '1', sizeof (p.id));
  150. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  151. GNUNET_i2s_full(&p.id));
  152. /* Prepare ATS Information */
  153. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  154. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  155. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  156. test_ats_info[1].value = htonl(1);
  157. test_ats_count = 2;
  158. /* Adding address without session */
  159. test_session = NULL;
  160. create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
  161. test_hello_address.peer = p.id;
  162. test_hello_address.transport_name = test_addr.plugin;
  163. test_hello_address.address = test_addr.addr;
  164. test_hello_address.address_length = test_addr.addr_len;
  165. /* Request */
  166. GNUNET_ATS_suggest_address (sched_ats, &p.id, NULL, NULL);
  167. /* Adding address */
  168. GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
  169. }
  170. int
  171. main (int argc, char *argv[])
  172. {
  173. char *sep;
  174. char *src_filename = GNUNET_strdup (__FILE__);
  175. char *test_filename = GNUNET_strdup (argv[0]);
  176. char *config_file;
  177. char *solver;
  178. ret = 0;
  179. if (NULL == (sep = (strstr (src_filename,".c"))))
  180. {
  181. GNUNET_break (0);
  182. return -1;
  183. }
  184. sep[0] = '\0';
  185. if (NULL != (sep = strstr (test_filename, ".exe")))
  186. sep[0] = '\0';
  187. if (NULL == (solver = strstr (test_filename, src_filename)))
  188. {
  189. GNUNET_break (0);
  190. return -1;
  191. }
  192. solver += strlen (src_filename) +1;
  193. if (0 == strcmp(solver, "proportional"))
  194. {
  195. config_file = "test_ats_solver_proportional.conf";
  196. }
  197. else if (0 == strcmp(solver, "mlp"))
  198. {
  199. config_file = "test_ats_solver_mlp.conf";
  200. }
  201. else if ((0 == strcmp(solver, "ril")))
  202. {
  203. config_file = "test_ats_solver_ril.conf";
  204. }
  205. else
  206. {
  207. GNUNET_break (0);
  208. GNUNET_free (src_filename);
  209. GNUNET_free (test_filename);
  210. return 1;
  211. }
  212. GNUNET_free (src_filename);
  213. GNUNET_free (test_filename);
  214. if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
  215. config_file, &run, NULL ))
  216. return GNUNET_SYSERR;
  217. return ret;
  218. }
  219. /* end of file test_ats_solver_add_address.c */