test_ats_api_delayed_service_performance_monitor.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_delayed_service_performance_monitor.c
  19. * @brief test performance API's address monitor feature with a delayed service
  20. * start up
  21. * @author Christian Grothoff
  22. * @author Matthias Wachs
  23. */
  24. #include "platform.h"
  25. #include "gnunet_ats_service.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "ats.h"
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  29. #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  30. static struct GNUNET_SCHEDULER_Task * die_task;
  31. /**
  32. * Statistics handle
  33. */
  34. static struct GNUNET_STATISTICS_Handle *stats;
  35. /**
  36. * Configuration handle
  37. */
  38. static struct GNUNET_CONFIGURATION_Handle *cfg;
  39. /**
  40. * ATS scheduling handle
  41. */
  42. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  43. /**
  44. * ATS performance handle
  45. */
  46. static struct GNUNET_ATS_PerformanceHandle *perf_ats;
  47. static int ret;
  48. struct Address
  49. {
  50. char *plugin;
  51. size_t plugin_len;
  52. void *addr;
  53. size_t addr_len;
  54. struct GNUNET_ATS_Information *ats;
  55. int ats_count;
  56. void *session;
  57. };
  58. struct PeerContext
  59. {
  60. struct GNUNET_PeerIdentity id;
  61. struct Address *addr;
  62. };
  63. static struct PeerContext p[2];
  64. static struct Address p0_addresses[2];
  65. static struct Address p1_addresses[2];
  66. static struct GNUNET_HELLO_Address p0_ha[2];
  67. static struct GNUNET_HELLO_Address p1_ha[2];
  68. static void
  69. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  70. static void
  71. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  72. static void
  73. ats_perf_cb (void *cls,
  74. const struct GNUNET_HELLO_Address *address,
  75. int address_active,
  76. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  77. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  78. const struct GNUNET_ATS_Information *ats,
  79. uint32_t ats_count)
  80. {
  81. static int peer0 = GNUNET_NO;
  82. static int peer1 = GNUNET_NO;
  83. static int done = GNUNET_NO;
  84. if (NULL == address)
  85. return;
  86. if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[0].id, sizeof (p[0].id))))
  87. {
  88. peer0 = GNUNET_YES;
  89. }
  90. if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[1].id, sizeof (p[1].id))))
  91. {
  92. peer1 = GNUNET_YES;
  93. }
  94. if ((peer0 == GNUNET_YES) && (peer1 = GNUNET_YES) && (GNUNET_NO == done))
  95. {
  96. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  97. "Done\n");
  98. done = GNUNET_YES;
  99. GNUNET_SCHEDULER_add_now (&end, NULL);
  100. }
  101. }
  102. static int
  103. stat_cb(void *cls, const char *subsystem,
  104. const char *name, uint64_t value,
  105. int is_persistent)
  106. {
  107. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
  108. subsystem,name, value);
  109. if (4 == value)
  110. {
  111. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  112. "All addresses added\n");
  113. }
  114. return GNUNET_OK;
  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_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
  125. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  126. }
  127. static void
  128. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  129. {
  130. die_task = NULL;
  131. end ( NULL, NULL);
  132. ret = GNUNET_SYSERR;
  133. }
  134. static void
  135. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  136. {
  137. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
  138. if (die_task != NULL )
  139. {
  140. GNUNET_SCHEDULER_cancel (die_task);
  141. die_task = NULL;
  142. }
  143. if (NULL != sched_ats)
  144. {
  145. GNUNET_ATS_scheduling_done (sched_ats);
  146. sched_ats = NULL;
  147. }
  148. if (NULL != perf_ats)
  149. {
  150. GNUNET_ATS_performance_done (perf_ats);
  151. perf_ats = NULL;
  152. }
  153. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  154. if (NULL != stats)
  155. {
  156. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  157. stats = NULL;
  158. }
  159. GNUNET_free_non_null(p0_addresses[0].addr);
  160. GNUNET_free_non_null(p0_addresses[1].addr);
  161. GNUNET_free_non_null(p1_addresses[0].addr);
  162. GNUNET_free_non_null(p1_addresses[1].addr);
  163. ret = 0;
  164. }
  165. static void
  166. run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
  167. struct GNUNET_TESTING_Peer *peer)
  168. {
  169. ret = 1;
  170. cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
  171. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
  172. if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
  173. {
  174. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  175. "Failed to connect to performance API\n");
  176. GNUNET_SCHEDULER_add_now (end_badly, NULL);
  177. }
  178. stats = GNUNET_STATISTICS_create ("ats", cfg);
  179. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  180. /* set up peer 0 */
  181. memset (&p[0].id, '1', sizeof (p[0].id));
  182. p0_addresses[0].plugin = "test";
  183. p0_addresses[0].session = NULL;
  184. p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
  185. p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
  186. p0_ha[0].address = p0_addresses[0].addr;
  187. p0_ha[0].address_length = p0_addresses[0].addr_len;
  188. p0_ha[0].peer = p[0].id;
  189. p0_ha[0].transport_name = p0_addresses[0].plugin;
  190. p0_addresses[1].plugin = "test";
  191. p0_addresses[1].session = NULL;
  192. p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
  193. p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
  194. p0_ha[1].address = p0_addresses[1].addr;
  195. p0_ha[1].address_length = p0_addresses[1].addr_len;
  196. p0_ha[1].peer = p[0].id;
  197. p0_ha[1].transport_name = p0_addresses[1].plugin;
  198. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
  199. GNUNET_i2s (&p[0].id));
  200. memset (&p[1].id, '2', sizeof (p[1].id));
  201. p1_addresses[0].plugin = "test";
  202. p1_addresses[0].session = NULL;
  203. p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
  204. p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
  205. p1_ha[0].address = p1_addresses[0].addr;
  206. p1_ha[0].address_length = p1_addresses[0].addr_len;
  207. p1_ha[0].peer = p[1].id;
  208. p1_ha[0].transport_name = p1_addresses[0].plugin;
  209. p1_addresses[1].plugin = "test";
  210. p1_addresses[1].session = NULL;
  211. p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
  212. p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
  213. p1_ha[1].address = p1_addresses[1].addr;
  214. p1_ha[1].address_length = p1_addresses[1].addr_len;
  215. p1_ha[1].peer = p[1].id;
  216. p1_ha[1].transport_name = p1_addresses[1].plugin;
  217. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
  218. GNUNET_i2s (&p[1].id));
  219. /* Add addresses */
  220. sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
  221. if (sched_ats == NULL )
  222. {
  223. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
  224. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  225. return;
  226. }
  227. GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
  228. GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
  229. GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
  230. GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
  231. }
  232. int
  233. main (int argc, char *argv[])
  234. {
  235. if (0
  236. != GNUNET_TESTING_peer_run ("test_ats_api_performance",
  237. "test_ats_api_delayed.conf", &run, NULL ))
  238. return 1;
  239. return ret;
  240. }
  241. /* end of file test_ats_api_delayed_service_performance_monitor.c */