test_ats_api_scheduling_add_address.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. This file is part of GNUnet.
  3. (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_add_address.c
  19. * @brief adding addresses with scheduling API
  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. #include "test_ats_api_common.h"
  28. /**
  29. * Timeout task
  30. */
  31. static GNUNET_SCHEDULER_TaskIdentifier die_task;
  32. /**
  33. * Statistics handle
  34. */
  35. struct GNUNET_STATISTICS_Handle *stats;
  36. /**
  37. * Scheduling handle
  38. */
  39. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  40. /**
  41. * Return value
  42. */
  43. static int ret;
  44. /**
  45. * Test address
  46. */
  47. static struct Test_Address test_addr;
  48. /**
  49. * Test peer
  50. */
  51. static struct PeerContext p;
  52. /**
  53. * HELLO address
  54. */
  55. struct GNUNET_HELLO_Address test_hello_address;
  56. /**
  57. * Session
  58. */
  59. static void *test_session;
  60. /**
  61. * Test ats info
  62. */
  63. struct GNUNET_ATS_Information test_ats_info[2];
  64. /**
  65. * Test ats count
  66. */
  67. uint32_t test_ats_count;
  68. static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  69. static int
  70. stat_cb(void *cls, const char *subsystem,
  71. const char *name, uint64_t value,
  72. int is_persistent)
  73. {
  74. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
  75. subsystem,name, value);
  76. if (1 == value)
  77. {
  78. GNUNET_SCHEDULER_add_now (&end, NULL);
  79. }
  80. return GNUNET_OK;
  81. }
  82. static void
  83. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  84. {
  85. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
  86. if (die_task != GNUNET_SCHEDULER_NO_TASK)
  87. {
  88. GNUNET_SCHEDULER_cancel (die_task);
  89. die_task = GNUNET_SCHEDULER_NO_TASK;
  90. }
  91. if (NULL != sched_ats)
  92. {
  93. GNUNET_ATS_scheduling_done (sched_ats);
  94. sched_ats = NULL;
  95. }
  96. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  97. if (NULL != stats)
  98. {
  99. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  100. stats = NULL;
  101. }
  102. free_test_address (&test_addr);
  103. ret = 0;
  104. }
  105. static void
  106. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  107. {
  108. die_task = GNUNET_SCHEDULER_NO_TASK;
  109. end ( NULL, NULL);
  110. ret = GNUNET_SYSERR;
  111. }
  112. static void
  113. address_suggest_cb (void *cls,
  114. const struct GNUNET_PeerIdentity *peer,
  115. const struct GNUNET_HELLO_Address *address,
  116. struct Session *session,
  117. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  118. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  119. const struct GNUNET_ATS_Information *atsi,
  120. uint32_t ats_count)
  121. {
  122. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
  123. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  124. return;
  125. }
  126. static void
  127. got_initial_value (void *cls, int success)
  128. {
  129. struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  130. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got initial value\n");
  131. /* Connect to ATS scheduling */
  132. sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
  133. if (sched_ats == NULL)
  134. {
  135. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
  136. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  137. return;
  138. }
  139. /* Set up peer */
  140. memset (&p.id, '1', sizeof (p.id));
  141. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  142. GNUNET_i2s_full(&p.id));
  143. /* Prepare ATS Information */
  144. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  145. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  146. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  147. test_ats_info[1].value = htonl(1);
  148. test_ats_count = 2;
  149. /* Adding address without session */
  150. test_session = NULL;
  151. create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
  152. test_hello_address.peer = p.id;
  153. test_hello_address.transport_name = test_addr.plugin;
  154. test_hello_address.address = test_addr.addr;
  155. test_hello_address.address_length = test_addr.addr_len;
  156. /* Adding address */
  157. GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
  158. }
  159. static int
  160. dummy_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
  161. int is_persistent)
  162. {
  163. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got dummy stat %s%s:%s = %llu\n",
  164. is_persistent ? "!" : " ", subsystem, name, value);
  165. return GNUNET_OK;
  166. }
  167. static void
  168. run (void *cls,
  169. const struct GNUNET_CONFIGURATION_Handle *cfg,
  170. struct GNUNET_TESTING_Peer *peer)
  171. {
  172. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  173. stats = GNUNET_STATISTICS_create ("ats", cfg);
  174. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  175. GNUNET_STATISTICS_get (stats, "ats", "# addresses", TIMEOUT,
  176. &got_initial_value, &dummy_stat,
  177. GNUNET_CONFIGURATION_dup (cfg));
  178. }
  179. int
  180. main (int argc, char *argv[])
  181. {
  182. ret = 0;
  183. if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
  184. "test_ats_api.conf",
  185. &run, NULL))
  186. return 1;
  187. return ret;
  188. }
  189. /* end of file test_ats_api_scheduling_add_address.c */