diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-06-30 15:58:44 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-06-30 15:58:44 +0000 |
commit | 444781955f83b5d541d7ee4556bcf864f4a2d38a (patch) | |
tree | 44544465306c79f36b0aa6e8e988385440d8532a /src/dht/test_dht_twopeer.c | |
parent | f5eaab80bfdbe04703096554f442dcbfa970b745 (diff) | |
download | gnunet-444781955f83b5d541d7ee4556bcf864f4a2d38a.tar.gz gnunet-444781955f83b5d541d7ee4556bcf864f4a2d38a.zip |
two peer start, connect, ensure core connect message made it to the dht, disconnect, stop peers
Diffstat (limited to 'src/dht/test_dht_twopeer.c')
-rw-r--r-- | src/dht/test_dht_twopeer.c | 175 |
1 files changed, 139 insertions, 36 deletions
diff --git a/src/dht/test_dht_twopeer.c b/src/dht/test_dht_twopeer.c index 3890b7699..c1472210e 100644 --- a/src/dht/test_dht_twopeer.c +++ b/src/dht/test_dht_twopeer.c | |||
@@ -35,11 +35,21 @@ | |||
35 | #define DEFAULT_NUM_PEERS 2 | 35 | #define DEFAULT_NUM_PEERS 2 |
36 | 36 | ||
37 | /* Structs */ | 37 | /* Structs */ |
38 | /* ... */ | 38 | |
39 | struct PeerGetContext | ||
40 | { | ||
41 | struct GNUNET_PeerIdentity *peer; | ||
42 | |||
43 | struct GNUNET_DHT_Handle *dht_handle; | ||
44 | |||
45 | struct GNUNET_DHT_GetHandle *get_handle; | ||
46 | }; | ||
39 | 47 | ||
40 | /* Globals */ | 48 | /* Globals */ |
41 | static char *test_directory; | 49 | static char *test_directory; |
42 | 50 | ||
51 | static struct PeerGetContext curr_get_ctx; | ||
52 | |||
43 | static unsigned int expected_connections; | 53 | static unsigned int expected_connections; |
44 | 54 | ||
45 | static unsigned long long peers_left; | 55 | static unsigned long long peers_left; |
@@ -50,6 +60,10 @@ static struct GNUNET_SCHEDULER_Handle *sched; | |||
50 | 60 | ||
51 | static unsigned long long num_peers; | 61 | static unsigned long long num_peers; |
52 | 62 | ||
63 | static unsigned int total_gets; | ||
64 | |||
65 | static unsigned int gets_succeeded; | ||
66 | |||
53 | static unsigned int total_connections; | 67 | static unsigned int total_connections; |
54 | 68 | ||
55 | static unsigned int failed_connections; | 69 | static unsigned int failed_connections; |
@@ -58,6 +72,14 @@ GNUNET_SCHEDULER_TaskIdentifier die_task; | |||
58 | 72 | ||
59 | static int ok; | 73 | static int ok; |
60 | 74 | ||
75 | static struct GNUNET_PeerIdentity peer1id; | ||
76 | |||
77 | static struct GNUNET_PeerIdentity peer2id; | ||
78 | |||
79 | static struct GNUNET_DHT_Handle *peer1dht; | ||
80 | |||
81 | static struct GNUNET_DHT_Handle *peer2dht; | ||
82 | |||
61 | /** | 83 | /** |
62 | * Check whether peers successfully shut down. | 84 | * Check whether peers successfully shut down. |
63 | */ | 85 | */ |
@@ -75,18 +97,95 @@ static void | |||
75 | finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) | 97 | finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) |
76 | { | 98 | { |
77 | GNUNET_assert (pg != NULL); | 99 | GNUNET_assert (pg != NULL); |
100 | GNUNET_assert (peer1dht != NULL); | ||
101 | GNUNET_assert (peer2dht != NULL); | ||
102 | GNUNET_DHT_disconnect(peer1dht); | ||
103 | GNUNET_DHT_disconnect(peer2dht); | ||
78 | GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); | 104 | GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); |
79 | ok = 0; | 105 | ok = 0; |
80 | } | 106 | } |
81 | 107 | ||
82 | static void | 108 | static void |
83 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) | 109 | end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) |
84 | { | 110 | { |
111 | if (peer1dht != NULL) | ||
112 | GNUNET_DHT_disconnect(peer1dht); | ||
113 | |||
114 | if (peer2dht != NULL) | ||
115 | GNUNET_DHT_disconnect(peer2dht); | ||
116 | |||
85 | if (pg != NULL) | 117 | if (pg != NULL) |
86 | GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); | 118 | GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); |
119 | } | ||
120 | |||
121 | static void | ||
122 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) | ||
123 | { | ||
124 | if (curr_get_ctx.get_handle != NULL) | ||
125 | { | ||
126 | GNUNET_DHT_get_stop(curr_get_ctx.get_handle, &end_badly_cont, NULL); | ||
127 | } | ||
128 | |||
87 | ok = 1; | 129 | ok = 1; |
88 | } | 130 | } |
89 | 131 | ||
132 | /* Forward declaration */ | ||
133 | static void | ||
134 | do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc); | ||
135 | |||
136 | /** | ||
137 | * Iterator called on each result obtained for a DHT | ||
138 | * operation that expects a reply | ||
139 | * | ||
140 | * @param cls closure | ||
141 | * @param exp when will this value expire | ||
142 | * @param key key of the result | ||
143 | * @param type type of the result | ||
144 | * @param size number of bytes in data | ||
145 | * @param data pointer to the result data | ||
146 | */ | ||
147 | void get_result_iterator (void *cls, | ||
148 | struct GNUNET_TIME_Absolute exp, | ||
149 | const GNUNET_HashCode * key, | ||
150 | uint32_t type, | ||
151 | uint32_t size, | ||
152 | const void *data) | ||
153 | { | ||
154 | struct PeerGetContext *get_context = cls; | ||
155 | if (0 != memcmp(&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode))) | ||
156 | { | ||
157 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key returned is not the same key as was searched for!\n"); | ||
158 | GNUNET_SCHEDULER_cancel(sched, die_task); | ||
159 | GNUNET_SCHEDULER_add_now(sched, &end_badly, "key mismatch in get response!\n"); | ||
160 | return; | ||
161 | } | ||
162 | |||
163 | if (get_context->peer == &peer2id) | ||
164 | { | ||
165 | get_context->peer = &peer1id; | ||
166 | get_context->dht_handle = peer2dht; | ||
167 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received first correct GET request response!\n"); | ||
168 | GNUNET_DHT_get_stop(get_context->get_handle, &do_get, get_context); | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received second correct GET request response!\n"); | ||
173 | GNUNET_SCHEDULER_cancel(sched, die_task); | ||
174 | GNUNET_DHT_get_stop(get_context->get_handle, &finish_testing, NULL); | ||
175 | } | ||
176 | |||
177 | |||
178 | } | ||
179 | |||
180 | static void | ||
181 | do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) | ||
182 | { | ||
183 | struct PeerGetContext *get_context = cls; | ||
184 | |||
185 | get_context->get_handle = GNUNET_DHT_get_start(get_context->dht_handle, GNUNET_TIME_relative_get_forever(), 130, &get_context->peer->hashPubKey, &get_result_iterator, get_context, NULL, NULL); | ||
186 | } | ||
187 | |||
188 | |||
90 | void | 189 | void |
91 | topology_callback (void *cls, | 190 | topology_callback (void *cls, |
92 | const struct GNUNET_PeerIdentity *first, | 191 | const struct GNUNET_PeerIdentity *first, |
@@ -126,23 +225,19 @@ topology_callback (void *cls, | |||
126 | total_connections); | 225 | total_connections); |
127 | #endif | 226 | #endif |
128 | GNUNET_SCHEDULER_cancel (sched, die_task); | 227 | GNUNET_SCHEDULER_cancel (sched, die_task); |
129 | die_task = GNUNET_SCHEDULER_NO_TASK; | 228 | die_task = GNUNET_SCHEDULER_add_delayed (sched, TIMEOUT, |
130 | //GNUNET_SCHEDULER_add_now (sched, &next_phase, NULL); | 229 | &end_badly, "from test gets"); |
131 | GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL); | 230 | |
231 | curr_get_ctx.dht_handle = peer1dht; | ||
232 | curr_get_ctx.peer = &peer2id; | ||
233 | GNUNET_SCHEDULER_add_now (sched, &do_get, &curr_get_ctx); | ||
234 | //GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_get, &curr_get_ctx); | ||
132 | } | 235 | } |
133 | else if (total_connections + failed_connections == expected_connections) | 236 | else if (total_connections + failed_connections == expected_connections) |
134 | { | 237 | { |
135 | GNUNET_SCHEDULER_cancel (sched, die_task); | 238 | GNUNET_SCHEDULER_cancel (sched, die_task); |
136 | die_task = GNUNET_SCHEDULER_add_now (sched, | 239 | die_task = GNUNET_SCHEDULER_add_now (sched, |
137 | &end_badly, "from topology_callback (too many failed connections)"); | 240 | &end_badly, "from topology_callback (too many failed connections)"); |
138 | } | ||
139 | else | ||
140 | { | ||
141 | #if VERBOSE | ||
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
143 | "Have %d total connections, %d failed connections, Want %d\n", | ||
144 | total_connections, failed_connections, expected_connections); | ||
145 | #endif | ||
146 | } | 241 | } |
147 | } | 242 | } |
148 | 243 | ||
@@ -151,20 +246,13 @@ connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc) | |||
151 | { | 246 | { |
152 | expected_connections = -1; | 247 | expected_connections = -1; |
153 | if ((pg != NULL) && (peers_left == 0)) | 248 | if ((pg != NULL) && (peers_left == 0)) |
154 | { | 249 | expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0); |
155 | expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0); | ||
156 | #if VERBOSE | ||
157 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
158 | "Have %d expected connections\n", expected_connections); | ||
159 | #endif | ||
160 | } | ||
161 | 250 | ||
162 | GNUNET_SCHEDULER_cancel (sched, die_task); | 251 | GNUNET_SCHEDULER_cancel (sched, die_task); |
163 | if (expected_connections == GNUNET_SYSERR) | 252 | if (expected_connections == GNUNET_SYSERR) |
164 | { | 253 | die_task = GNUNET_SCHEDULER_add_now (sched, |
165 | die_task = GNUNET_SCHEDULER_add_now (sched, | 254 | &end_badly, "from connect topology (bad return)"); |
166 | &end_badly, "from connect topology (bad return)"); | 255 | |
167 | } | ||
168 | 256 | ||
169 | die_task = GNUNET_SCHEDULER_add_delayed (sched, | 257 | die_task = GNUNET_SCHEDULER_add_delayed (sched, |
170 | TIMEOUT, | 258 | TIMEOUT, |
@@ -179,32 +267,47 @@ peers_started_callback (void *cls, | |||
179 | { | 267 | { |
180 | if (emsg != NULL) | 268 | if (emsg != NULL) |
181 | { | 269 | { |
182 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n", | 270 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n", |
183 | emsg); | 271 | emsg); |
184 | return; | 272 | return; |
185 | } | 273 | } |
186 | GNUNET_assert (id != NULL); | 274 | GNUNET_assert (id != NULL); |
187 | #if VERBOSE | 275 | if (peers_left == num_peers) |
188 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n", | 276 | { |
189 | (num_peers - peers_left) + 1, num_peers); | 277 | memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity)); |
190 | #endif | 278 | peer1dht = GNUNET_DHT_connect(sched, cfg, 100); |
279 | if (peer1dht == NULL) | ||
280 | { | ||
281 | GNUNET_SCHEDULER_cancel (sched, die_task); | ||
282 | GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n"); | ||
283 | } | ||
284 | } | ||
285 | else | ||
286 | { | ||
287 | memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity)); | ||
288 | peer2dht = GNUNET_DHT_connect(sched, cfg, 100); | ||
289 | if (peer2dht == NULL) | ||
290 | { | ||
291 | GNUNET_SCHEDULER_cancel (sched, die_task); | ||
292 | GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n"); | ||
293 | } | ||
294 | } | ||
295 | |||
191 | 296 | ||
192 | //GNUNET_DHT_connect(); | ||
193 | peers_left--; | 297 | peers_left--; |
194 | 298 | ||
195 | if (peers_left == 0) | 299 | if (peers_left == 0) |
196 | { | 300 | { |
197 | #if VERBOSE | 301 | #if VERBOSE |
198 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 302 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
199 | "All %d daemons started, now creating topology!\n", | 303 | "All %d daemons started, now connecting peers!\n", |
200 | num_peers); | 304 | num_peers); |
201 | #endif | 305 | #endif |
202 | GNUNET_SCHEDULER_cancel (sched, die_task); | 306 | GNUNET_SCHEDULER_cancel (sched, die_task); |
203 | /* Set up task in case topology creation doesn't finish | 307 | /* Set up task in case topology creation doesn't finish |
204 | * within a reasonable amount of time */ | 308 | * within a reasonable amount of time */ |
205 | die_task = GNUNET_SCHEDULER_add_delayed (sched, | 309 | die_task = GNUNET_SCHEDULER_add_delayed (sched, |
206 | GNUNET_TIME_relative_multiply | 310 | TIMEOUT, |
207 | (GNUNET_TIME_UNIT_MINUTES, 5), | ||
208 | &end_badly, "from peers_started_callback"); | 311 | &end_badly, "from peers_started_callback"); |
209 | 312 | ||
210 | GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL); | 313 | GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL); |
@@ -219,7 +322,6 @@ run (void *cls, | |||
219 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) | 322 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) |
220 | { | 323 | { |
221 | sched = s; | 324 | sched = s; |
222 | ok = 1; | ||
223 | 325 | ||
224 | if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory)) | 326 | if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory)) |
225 | { | 327 | { |
@@ -233,7 +335,8 @@ run (void *cls, | |||
233 | num_peers = DEFAULT_NUM_PEERS; | 335 | num_peers = DEFAULT_NUM_PEERS; |
234 | 336 | ||
235 | peers_left = num_peers; | 337 | peers_left = num_peers; |
236 | 338 | total_gets = num_peers; | |
339 | gets_succeeded = 0; | ||
237 | /* Set up a task to end testing if peer start fails */ | 340 | /* Set up a task to end testing if peer start fails */ |
238 | die_task = GNUNET_SCHEDULER_add_delayed (sched, | 341 | die_task = GNUNET_SCHEDULER_add_delayed (sched, |
239 | TIMEOUT, | 342 | TIMEOUT, |