aboutsummaryrefslogtreecommitdiff
path: root/src/nse/test_nse_multipeer.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-15 18:53:45 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-15 18:53:45 +0000
commit5fba3531d7afd2ef67045f4d9e7af72c36c53c95 (patch)
tree32437969a8e1d219fdad1c063d6937b59707e058 /src/nse/test_nse_multipeer.c
parentbaf16447ccaa8a1e18a00767d35cc05a9da99256 (diff)
downloadgnunet-5fba3531d7afd2ef67045f4d9e7af72c36c53c95.tar.gz
gnunet-5fba3531d7afd2ef67045f4d9e7af72c36c53c95.zip
making nse multipeer test use testbed
Diffstat (limited to 'src/nse/test_nse_multipeer.c')
-rw-r--r--src/nse/test_nse_multipeer.c296
1 files changed, 123 insertions, 173 deletions
diff --git a/src/nse/test_nse_multipeer.c b/src/nse/test_nse_multipeer.c
index 28d066bf6..ec83e3e8a 100644
--- a/src/nse/test_nse_multipeer.c
+++ b/src/nse/test_nse_multipeer.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -26,86 +26,63 @@
26 * for one message from each peer. 26 * for one message from each peer.
27 */ 27 */
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_testing_lib.h" 29#include "gnunet_testbed_service.h"
30#include "gnunet_nse_service.h" 30#include "gnunet_nse_service.h"
31 31
32#define VERBOSE GNUNET_NO
33 32
33/**
34 * How many peers do we start?
35 */
34#define NUM_PEERS 4 36#define NUM_PEERS 4
35 37
36struct NSEPeer 38/**
37{ 39 * How long do we run the test?
38 struct NSEPeer *prev; 40 */
39 41#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 struct NSEPeer *next;
41 42
42 struct GNUNET_TESTING_Daemon *daemon;
43 43
44/**
45 * Information we track for each peer.
46 */
47struct NSEPeer
48{
49 /**
50 * Handle for NSE connect operation.
51 */
52 struct GNUNET_TESTBED_Operation *op;
53
54 /**
55 * Handle to NSE service.
56 */
44 struct GNUNET_NSE_Handle *nse_handle; 57 struct GNUNET_NSE_Handle *nse_handle;
45}; 58};
46 59
47struct NSEPeer *peer_head;
48
49struct NSEPeer *peer_tail;
50 60
51/** 61/**
52 * How long do we run the test? 62 * Information for all the peers.
53 */ 63 */
54#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) 64static struct NSEPeer nse_peers[NUM_PEERS];
55 65
66/**
67 * Return value from 'main'.
68 */
56static int ok; 69static int ok;
57 70
58static int peers_left;
59
60static unsigned int num_peers;
61
62static unsigned int total_connections;
63
64static struct GNUNET_TESTING_PeerGroup *pg;
65 71
66/** 72/**
67 * Check whether peers successfully shut down. 73 * Task run on timeout to shut everything down.
68 */ 74 */
69static void 75static void
70shutdown_callback (void *cls, const char *emsg)
71{
72 if (emsg != NULL)
73 {
74#if VERBOSE
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed: %s!\n",
76 emsg);
77#endif
78 if (ok == 0)
79 ok = 666;
80 }
81 else
82 {
83#if VERBOSE
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
85#endif
86 ok = 0;
87 }
88}
89
90static void
91shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 76shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92{ 77{
93 struct NSEPeer *pos; 78 unsigned int i;
94
95#if VERBOSE
96 FPRINTF (stderr, "%s", "Ending test.\n");
97#endif
98
99 while (NULL != (pos = peer_head))
100 {
101 GNUNET_NSE_disconnect (pos->nse_handle);
102 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
103 GNUNET_free (pos);
104 }
105 79
106 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); 80 for (i=0;i<NUM_PEERS;i++)
81 GNUNET_TESTBED_operation_done (nse_peers[i].op);
82 GNUNET_SCHEDULER_shutdown ();
107} 83}
108 84
85
109/** 86/**
110 * Callback to call when network size estimate is updated. 87 * Callback to call when network size estimate is updated.
111 * 88 *
@@ -123,153 +100,126 @@ handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
123 struct NSEPeer *peer = cls; 100 struct NSEPeer *peer = cls;
124 101
125 FPRINTF (stderr, 102 FPRINTF (stderr,
126 "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n", 103 "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
127 GNUNET_i2s (&peer->daemon->id), estimate, std_dev, 104 (unsigned int) (peer - nse_peers),
128 GNUNET_NSE_log_estimate_to_n (estimate), num_peers); 105 estimate, std_dev,
106 GNUNET_NSE_log_estimate_to_n (estimate),
107 NUM_PEERS);
129} 108}
130 109
131 110
111/**
112 * Callback to be called when NSE service connect operation is completed
113 *
114 * @param cls the callback closure from functions generating an operation
115 * @param op the operation that has been finished
116 * @param ca_result the NSE service handle returned from nse_connect_adapter
117 * @param emsg error message in case the operation has failed; will be NULL if
118 * operation has executed successfully.
119 */
132static void 120static void
133connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 121nse_connect_complete_cb (void *cls,
122 struct GNUNET_TESTBED_Operation *op,
123 void *ca_result,
124 const char *emsg)
134{ 125{
135 struct NSEPeer *current_peer; 126 struct NSEPeer *peer = cls;
136 unsigned int i; 127 struct GNUNET_NSE_Handle *nse = ca_result;
137 128
138#if VERBOSE 129 GNUNET_assert (op == peer->op);
139 FPRINTF (stderr, "%s", "TEST_NSE_MULTIPEER: connecting to nse service of peers\n"); 130 if (NULL != emsg)
140#endif 131 {
141 for (i = 0; i < num_peers; i++) 132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 { 133 "Failed to connect to NSE service: %s\n",
143 current_peer = GNUNET_malloc (sizeof (struct NSEPeer)); 134 emsg);
144 current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i); 135 ok = 1;
145 current_peer->nse_handle = 136 GNUNET_SCHEDULER_shutdown ();
146 GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate, 137 return;
147 current_peer); 138 }
148 GNUNET_assert (current_peer->nse_handle != NULL); 139 peer->nse_handle = nse;
149 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
150 }
151} 140}
152 141
153 142
154static void 143/**
155my_cb (void *cls, const char *emsg) 144 * Adapter function called to establish a connection to
145 * the NSE service.
146 *
147 * @param cls closure
148 * @param cfg configuration of the peer to connect to; will be available until
149 * GNUNET_TESTBED_operation_done() is called on the operation returned
150 * from GNUNET_TESTBED_service_connect()
151 * @return service handle to return in 'op_result', NULL on error
152 */
153static void *
154nse_connect_adapter (void *cls,
155 const struct GNUNET_CONFIGURATION_Handle *cfg)
156{ 156{
157 if (emsg != NULL) 157 return GNUNET_NSE_connect (cfg,
158 { 158 &handle_estimate,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 159 cls);
160 "Peergroup callback called with error, aborting test!\n");
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
162 ok = 1;
163 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
164 return;
165 }
166#if VERBOSE
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Peer Group started successfully, connecting to NSE service for each peer!\n");
169#endif
170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
171 total_connections);
172
173 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
174} 160}
175 161
176 162
177/** 163/**
178 * Function that will be called whenever 164 * Adapter function called to destroy connection to
179 * two daemons are connected by the testing library. 165 * NSE service.
180 * 166 *
181 * @param cls closure 167 * @param cls closure
182 * @param first peer id for first daemon 168 * @param op_result service handle returned from the connect adapter
183 * @param second peer id for the second daemon
184 * @param distance distance between the connected peers
185 * @param first_cfg config for the first daemon
186 * @param second_cfg config for the second daemon
187 * @param first_daemon handle for the first daemon
188 * @param second_daemon handle for the second daemon
189 * @param emsg error message (NULL on success)
190 */ 169 */
191static void 170static void
192connect_cb (void *cls, const struct GNUNET_PeerIdentity *first, 171nse_disconnect_adapter (void *cls,
193 const struct GNUNET_PeerIdentity *second, uint32_t distance, 172 void *op_result)
194 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
195 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
196 struct GNUNET_TESTING_Daemon *first_daemon,
197 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
198{ 173{
199 if (emsg == NULL) 174 GNUNET_NSE_disconnect (op_result);
200 {
201 //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
202 total_connections++;
203 }
204} 175}
205 176
206 177
207 178/**
179 * Actual "main" function for the testcase.
180 *
181 * @param cls closure
182 * @param num_peers number of peers in 'peers'
183 * @param peers handle to peers run in the testbed
184 */
208static void 185static void
209run (void *cls, char *const *args, const char *cfgfile, 186run (void *cls,
210 const struct GNUNET_CONFIGURATION_Handle *cfg) 187 unsigned int num_peers,
188 struct GNUNET_TESTBED_Peer **peers)
211{ 189{
212 struct GNUNET_CONFIGURATION_Handle *testing_cfg; 190 unsigned int i;
213 unsigned long long total_peers;
214
215 ok = 1;
216 testing_cfg = GNUNET_CONFIGURATION_create ();
217 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (testing_cfg, cfgfile));
218
219#if VERBOSE
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
221 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
222 "use_progressbars", "YES");
223#endif
224 if (GNUNET_OK !=
225 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
226 "num_peers", &total_peers))
227 total_peers = NUM_PEERS;
228 191
229 peers_left = total_peers; 192 GNUNET_assert (NUM_PEERS == num_peers);
230 num_peers = peers_left; 193 for (i=0;i<num_peers;i++)
231 pg = GNUNET_TESTING_peergroup_start (testing_cfg, peers_left, TIMEOUT, 194 nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
232 &connect_cb, &my_cb, NULL, NULL); 195 peers[i],
233 GNUNET_assert (pg != NULL); 196 "nse",
197 &nse_connect_complete_cb,
198 &nse_peers[i],
199 &nse_connect_adapter,
200 &nse_disconnect_adapter,
201 &nse_peers[i]);
234 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL); 202 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
235} 203}
236 204
237 205
238static int 206/**
239check () 207 * Entry point for the testcase, sets up the testbed.
240{ 208 *
241 char *const argv[] = { "test-nse-multipeer", 209 * @param argc unused
242 "-c", 210 * @param argv unused
243 "test_nse.conf", 211 * @return 0 on success
244#if VERBOSE 212 */
245 "-L", "DEBUG",
246#endif
247 NULL
248 };
249 struct GNUNET_GETOPT_CommandLineOption options[] = {
250 GNUNET_GETOPT_OPTION_END
251 };
252 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
253 "test-nse-multipeer", "nohelp", options, &run, &ok);
254 return ok;
255}
256
257
258int 213int
259main (int argc, char *argv[]) 214main (int argc, char *argv[])
260{ 215{
261 int ret; 216 ok = 1;
262 217 GNUNET_TESTBED_test_run ("test-nse-multipeer",
263 GNUNET_log_setup ("test-nse-multipeer", 218 "test_nse.conf",
264#if VERBOSE 219 NUM_PEERS,
265 "DEBUG", 220 0, NULL, NULL,
266#else 221 &run, NULL);
267 "WARNING", 222 return ok;
268#endif
269 NULL);
270 ret = check ();
271 // GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
272 return ret;
273} 223}
274 224
275/* end of test_nse_multipeer.c */ 225/* end of test_nse_multipeer.c */