test_ats_api_scheduling_add_address.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_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 struct GNUNET_SCHEDULER_Task * 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 != NULL)
  87. {
  88. GNUNET_SCHEDULER_cancel (die_task);
  89. die_task = NULL;
  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 = NULL;
  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. {
  120. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
  121. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  122. }
  123. static void
  124. got_initial_value (void *cls, int success)
  125. {
  126. struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  127. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got initial value\n");
  128. /* Connect to ATS scheduling */
  129. sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
  130. if (sched_ats == NULL)
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
  133. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  134. return;
  135. }
  136. /* Set up peer */
  137. memset (&p.id, '1', sizeof (p.id));
  138. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  139. GNUNET_i2s_full(&p.id));
  140. /* Prepare ATS Information */
  141. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  142. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  143. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  144. test_ats_info[1].value = htonl(1);
  145. test_ats_count = 2;
  146. /* Adding address without session */
  147. test_session = NULL;
  148. create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
  149. test_hello_address.peer = p.id;
  150. test_hello_address.transport_name = test_addr.plugin;
  151. test_hello_address.address = test_addr.addr;
  152. test_hello_address.address_length = test_addr.addr_len;
  153. /* Adding address */
  154. GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
  155. }
  156. static int
  157. dummy_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
  158. int is_persistent)
  159. {
  160. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got dummy stat %s%s:%s = %llu\n",
  161. is_persistent ? "!" : " ", subsystem, name, value);
  162. return GNUNET_OK;
  163. }
  164. static void
  165. run (void *cls,
  166. const struct GNUNET_CONFIGURATION_Handle *cfg,
  167. struct GNUNET_TESTING_Peer *peer)
  168. {
  169. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  170. stats = GNUNET_STATISTICS_create ("ats", cfg);
  171. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  172. GNUNET_STATISTICS_get (stats, "ats", "# addresses", TIMEOUT,
  173. &got_initial_value, &dummy_stat,
  174. GNUNET_CONFIGURATION_dup (cfg));
  175. }
  176. int
  177. main (int argc, char *argv[])
  178. {
  179. ret = 0;
  180. if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
  181. "test_ats_api.conf",
  182. &run, NULL))
  183. return 1;
  184. return ret;
  185. }
  186. /* end of file test_ats_api_scheduling_add_address.c */