diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-07-15 21:41:11 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-07-15 21:41:11 +0000 |
commit | d0fdba92b0c8c861d223312f19f9278ba63e7f98 (patch) | |
tree | b3794d2fa0490917513c2faeeaf89d4b075ee23c /src/transport/transport-testing.c | |
parent | 948b042072a8e50f36ad63a0f0c5b78c9140f59d (diff) | |
download | gnunet-d0fdba92b0c8c861d223312f19f9278ba63e7f98.tar.gz gnunet-d0fdba92b0c8c861d223312f19f9278ba63e7f98.zip |
towards having sending in transport-testing API (not yet flexible enough)
Diffstat (limited to 'src/transport/transport-testing.c')
-rw-r--r-- | src/transport/transport-testing.c | 223 |
1 files changed, 184 insertions, 39 deletions
diff --git a/src/transport/transport-testing.c b/src/transport/transport-testing.c index 322627459..50c297389 100644 --- a/src/transport/transport-testing.c +++ b/src/transport/transport-testing.c | |||
@@ -44,11 +44,21 @@ find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, | |||
44 | } | 44 | } |
45 | 45 | ||
46 | 46 | ||
47 | static void | 47 | /** |
48 | notify_connecting_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, | 48 | * Find any connecting context matching the given pair of peers. |
49 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, | 49 | * |
50 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2) | 50 | * @param p1 first peer |
51 | * @param p2 second peer | ||
52 | * @param cb function to call | ||
53 | * @param cb_cls closure for @a cb | ||
54 | */ | ||
55 | void | ||
56 | GNUNET_TRANSPORT_TESTING_find_connecting_context (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
57 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
58 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb, | ||
59 | void *cb_cls) | ||
51 | { | 60 | { |
61 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; | ||
52 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | 62 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
53 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | 63 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; |
54 | 64 | ||
@@ -56,28 +66,55 @@ notify_connecting_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, | |||
56 | { | 66 | { |
57 | ccn = cc->next; | 67 | ccn = cc->next; |
58 | if ( (cc->p1 == p1) && | 68 | if ( (cc->p1 == p1) && |
59 | (cc->p2 == p2) ) | 69 | (cc->p2 == p2) ) |
60 | cc->p1_c = GNUNET_YES; | 70 | cb (cb_cls, |
61 | if ( (cc->p1 == p2) && | 71 | cc); |
62 | (cc->p2 == p1) ) | ||
63 | cc->p2_c = GNUNET_YES; | ||
64 | if ( (cc->p1_c == GNUNET_YES) && | ||
65 | (cc->p2_c == GNUNET_YES) ) | ||
66 | { | ||
67 | cc->cb (cc->cb_cls); | ||
68 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
69 | } | ||
70 | } | 72 | } |
71 | } | 73 | } |
72 | 74 | ||
73 | 75 | ||
74 | static void | 76 | static void |
77 | set_p1c (void *cls, | ||
78 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
79 | { | ||
80 | cx->p1_c = GNUNET_YES; | ||
81 | } | ||
82 | |||
83 | |||
84 | static void | ||
85 | set_p2c (void *cls, | ||
86 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
87 | { | ||
88 | cx->p2_c = GNUNET_YES; | ||
89 | } | ||
90 | |||
91 | |||
92 | static void | ||
93 | clear_p1c (void *cls, | ||
94 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
95 | { | ||
96 | cx->p1_c = GNUNET_NO; | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | clear_p2c (void *cls, | ||
102 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
103 | { | ||
104 | cx->p2_c = GNUNET_NO; | ||
105 | } | ||
106 | |||
107 | |||
108 | static void | ||
75 | notify_connect (void *cls, | 109 | notify_connect (void *cls, |
76 | const struct GNUNET_PeerIdentity *peer) | 110 | const struct GNUNET_PeerIdentity *peer) |
77 | { | 111 | { |
78 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | 112 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; |
113 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
79 | char *p2_s; | 114 | char *p2_s; |
80 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | 115 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; |
116 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
117 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
81 | 118 | ||
82 | p2 = find_peer_context (p->tth, | 119 | p2 = find_peer_context (p->tth, |
83 | peer); | 120 | peer); |
@@ -100,30 +137,83 @@ notify_connect (void *cls, | |||
100 | p->no, | 137 | p->no, |
101 | GNUNET_i2s (&p->id)); | 138 | GNUNET_i2s (&p->id)); |
102 | GNUNET_free (p2_s); | 139 | GNUNET_free (p2_s); |
103 | notify_connecting_context (p->tth, | 140 | /* update flags in connecting contexts */ |
104 | p, | 141 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, |
105 | p2); | 142 | p2, |
143 | &set_p1c, | ||
144 | NULL); | ||
145 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
146 | p, | ||
147 | &set_p2c, | ||
148 | NULL); | ||
149 | /* update set connected flag for all requests */ | ||
150 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
151 | { | ||
152 | if (GNUNET_YES == cc->connected) | ||
153 | continue; | ||
154 | if ( (GNUNET_YES == cc->p1_c) && | ||
155 | (GNUNET_YES == cc->p2_c) ) | ||
156 | { | ||
157 | cc->connected = GNUNET_YES; | ||
158 | /* stop trying to connect */ | ||
159 | if (NULL != cc->tct) | ||
160 | { | ||
161 | GNUNET_SCHEDULER_cancel (cc->tct); | ||
162 | cc->tct = NULL; | ||
163 | } | ||
164 | if (NULL != cc->oh) | ||
165 | { | ||
166 | GNUNET_TRANSPORT_offer_hello_cancel (cc->oh); | ||
167 | cc->oh = NULL; | ||
168 | } | ||
169 | if (NULL != cc->ats_sh) | ||
170 | { | ||
171 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
172 | cc->ats_sh = NULL; | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | /* then notify application */ | ||
177 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
178 | { | ||
179 | ccn = cc->next; | ||
180 | if ( (GNUNET_YES == cc->connected) && | ||
181 | (NULL != cc->cb) ) | ||
182 | { | ||
183 | cc->cb (cc->cb_cls); | ||
184 | cc->cb = NULL; /* only notify once! */ | ||
185 | } | ||
186 | } | ||
106 | } | 187 | } |
107 | 188 | ||
108 | 189 | ||
190 | /** | ||
191 | * Offer the current HELLO of P2 to P1. | ||
192 | * | ||
193 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
194 | */ | ||
195 | static void | ||
196 | offer_hello (void *cls); | ||
197 | |||
198 | |||
109 | static void | 199 | static void |
110 | notify_disconnect (void *cls, | 200 | notify_disconnect (void *cls, |
111 | const struct GNUNET_PeerIdentity *peer) | 201 | const struct GNUNET_PeerIdentity *peer) |
112 | { | 202 | { |
113 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | 203 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; |
204 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
114 | char *p2_s; | 205 | char *p2_s; |
115 | /* Find PeerContext */ | 206 | /* Find PeerContext */ |
116 | int no = 0; | 207 | int no = 0; |
117 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL; | 208 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL; |
118 | 209 | struct TRANSPORT_TESTING_SendJob *sj; | |
119 | if (NULL != p) | 210 | struct TRANSPORT_TESTING_SendJob *sjn; |
120 | { | 211 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
121 | p2 = find_peer_context (p->tth, | 212 | |
122 | peer); | 213 | p2 = find_peer_context (p->tth, |
123 | no = p->no; | 214 | peer); |
124 | } | 215 | no = p->no; |
125 | 216 | if (NULL != p2) | |
126 | if (p2 != NULL) | ||
127 | GNUNET_asprintf (&p2_s, | 217 | GNUNET_asprintf (&p2_s, |
128 | "%u (`%s')", | 218 | "%u (`%s')", |
129 | p2->no, | 219 | p2->no, |
@@ -138,12 +228,57 @@ notify_disconnect (void *cls, | |||
138 | no, | 228 | no, |
139 | GNUNET_i2s (&p->id)); | 229 | GNUNET_i2s (&p->id)); |
140 | GNUNET_free (p2_s); | 230 | GNUNET_free (p2_s); |
141 | 231 | /* notify about disconnect */ | |
142 | if (NULL == p) | ||
143 | return; | ||
144 | if (NULL != p->nd) | 232 | if (NULL != p->nd) |
145 | p->nd (p->cb_cls, | 233 | p->nd (p->cb_cls, |
146 | peer); | 234 | peer); |
235 | if (NULL == p2) | ||
236 | return; | ||
237 | /* abort all transmissions this disconnected pair is involved in */ | ||
238 | for (sj = tth->sj_head; NULL != sj; sj = sjn) | ||
239 | { | ||
240 | sjn = sj->next; | ||
241 | if ( ( (sj->sender == p2) && | ||
242 | (sj->receiver == p) ) || | ||
243 | ( (sj->receiver == p2) && | ||
244 | (sj->sender == p) ) ) | ||
245 | { | ||
246 | GNUNET_CONTAINER_DLL_remove (tth->sj_head, | ||
247 | tth->sj_tail, | ||
248 | sj); | ||
249 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (sj->th); | ||
250 | GNUNET_free (sj); | ||
251 | } | ||
252 | } | ||
253 | /* update set connected flags for all requests */ | ||
254 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
255 | p2, | ||
256 | &clear_p1c, | ||
257 | NULL); | ||
258 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
259 | p, | ||
260 | &clear_p2c, | ||
261 | NULL); | ||
262 | /* resume connectivity requests as necessary */ | ||
263 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
264 | { | ||
265 | if (GNUNET_NO == cc->connected) | ||
266 | continue; | ||
267 | if ( (GNUNET_YES != cc->p1_c) || | ||
268 | (GNUNET_YES != cc->p2_c) ) | ||
269 | { | ||
270 | cc->connected = GNUNET_NO; | ||
271 | /* start trying to connect */ | ||
272 | if ( (NULL == cc->tct) && | ||
273 | (NULL == cc->oh) ) | ||
274 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
275 | cc); | ||
276 | if (NULL == cc->ats_sh) | ||
277 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
278 | &p2->id, | ||
279 | 1); | ||
280 | } | ||
281 | } | ||
147 | } | 282 | } |
148 | 283 | ||
149 | 284 | ||
@@ -352,6 +487,9 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerConte | |||
352 | GNUNET_TRANSPORT_TESTING_StartCallback restart_cb, | 487 | GNUNET_TRANSPORT_TESTING_StartCallback restart_cb, |
353 | void *restart_cb_cls) | 488 | void *restart_cb_cls) |
354 | { | 489 | { |
490 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
491 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
492 | |||
355 | /* shutdown */ | 493 | /* shutdown */ |
356 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 494 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
357 | "Stopping peer %u (`%s')\n", | 495 | "Stopping peer %u (`%s')\n", |
@@ -367,6 +505,13 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerConte | |||
367 | GNUNET_TRANSPORT_disconnect (p->th); | 505 | GNUNET_TRANSPORT_disconnect (p->th); |
368 | p->th = NULL; | 506 | p->th = NULL; |
369 | } | 507 | } |
508 | for (cc = p->tth->cc_head; NULL != cc; cc = ccn) | ||
509 | { | ||
510 | ccn = cc->next; | ||
511 | if ( (cc->p1 == p) || | ||
512 | (cc->p2 == p) ) | ||
513 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
514 | } | ||
370 | if (NULL != p->ats) | 515 | if (NULL != p->ats) |
371 | { | 516 | { |
372 | GNUNET_ATS_connectivity_done (p->ats); | 517 | GNUNET_ATS_connectivity_done (p->ats); |
@@ -427,7 +572,16 @@ void | |||
427 | GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p) | 572 | GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p) |
428 | { | 573 | { |
429 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | 574 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; |
575 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
576 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
430 | 577 | ||
578 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
579 | { | ||
580 | ccn = cc->next; | ||
581 | if ( (cc->p1 == p) || | ||
582 | (cc->p2 == p) ) | ||
583 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
584 | } | ||
431 | if (NULL != p->ghh) | 585 | if (NULL != p->ghh) |
432 | { | 586 | { |
433 | GNUNET_TRANSPORT_get_hello_cancel (p->ghh); | 587 | GNUNET_TRANSPORT_get_hello_cancel (p->ghh); |
@@ -478,15 +632,6 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext | |||
478 | 632 | ||
479 | 633 | ||
480 | /** | 634 | /** |
481 | * Offer the current HELLO of P2 to P1. | ||
482 | * | ||
483 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
484 | */ | ||
485 | static void | ||
486 | offer_hello (void *cls); | ||
487 | |||
488 | |||
489 | /** | ||
490 | * Function called after the HELLO was passed to the | 635 | * Function called after the HELLO was passed to the |
491 | * transport service. | 636 | * transport service. |
492 | */ | 637 | */ |