diff options
author | Philipp Tölke <toelke@in.tum.de> | 2011-03-28 15:05:21 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2011-03-28 15:05:21 +0000 |
commit | fc78e1f97f9dbbf64d21c1477b737428d85e461a (patch) | |
tree | 80d9cd2a4375d6c16d2911d9b9041b595bbe1df9 /src/mesh | |
parent | f9d33ea8e739084ae028d32ab722d4314fa1c1f3 (diff) | |
download | gnunet-fc78e1f97f9dbbf64d21c1477b737428d85e461a.tar.gz gnunet-fc78e1f97f9dbbf64d21c1477b737428d85e461a.zip |
Implement connect_by_type.
This does not yet work as intended
Diffstat (limited to 'src/mesh')
-rw-r--r-- | src/mesh/mesh_api.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/mesh/mesh_api.c b/src/mesh/mesh_api.c index a8833bb75..d030f9f8f 100644 --- a/src/mesh/mesh_api.c +++ b/src/mesh/mesh_api.c | |||
@@ -67,6 +67,11 @@ struct GNUNET_MESH_Tunnel | |||
67 | 67 | ||
68 | struct GNUNET_MESH_Handle* handle; | 68 | struct GNUNET_MESH_Handle* handle; |
69 | 69 | ||
70 | /* The message-type requested for this tunnel. Is only needed for pending | ||
71 | * by_tupe-tunnels | ||
72 | */ | ||
73 | uint16_t message_type; | ||
74 | |||
70 | /* The context of the receive-function. */ | 75 | /* The context of the receive-function. */ |
71 | void *ctx; | 76 | void *ctx; |
72 | }; | 77 | }; |
@@ -85,6 +90,13 @@ struct tunnel_list | |||
85 | struct peer_list_element | 90 | struct peer_list_element |
86 | { | 91 | { |
87 | struct GNUNET_PeerIdentity peer; | 92 | struct GNUNET_PeerIdentity peer; |
93 | |||
94 | /* how many Message-Types can this peer receive */ | ||
95 | unsigned int num_types; | ||
96 | |||
97 | /* array of message-types */ | ||
98 | uint16_t *types; | ||
99 | |||
88 | struct GNUNET_TRANSPORT_ATS_Information atsi; | 100 | struct GNUNET_TRANSPORT_ATS_Information atsi; |
89 | struct peer_list_element *next, *prev; | 101 | struct peer_list_element *next, *prev; |
90 | }; | 102 | }; |
@@ -103,8 +115,11 @@ struct GNUNET_MESH_Handle | |||
103 | struct peer_list connected_peers; | 115 | struct peer_list connected_peers; |
104 | struct tunnel_list established_tunnels; | 116 | struct tunnel_list established_tunnels; |
105 | struct tunnel_list pending_tunnels; | 117 | struct tunnel_list pending_tunnels; |
118 | struct tunnel_list pending_by_type_tunnels; | ||
106 | void *cls; | 119 | void *cls; |
107 | GNUNET_MESH_TunnelEndHandler *cleaner; | 120 | GNUNET_MESH_TunnelEndHandler *cleaner; |
121 | size_t hello_message_size; | ||
122 | uint16_t *hello_message; | ||
108 | }; | 123 | }; |
109 | 124 | ||
110 | static void | 125 | static void |
@@ -137,6 +152,22 @@ core_startup (void *cls, | |||
137 | handle->connected_to_core = GNUNET_YES; | 152 | handle->connected_to_core = GNUNET_YES; |
138 | } | 153 | } |
139 | 154 | ||
155 | static size_t | ||
156 | send_hello_message (void *cls, size_t size, void *buf) | ||
157 | { | ||
158 | struct GNUNET_MESH_Handle *handle = cls; | ||
159 | struct GNUNET_MessageHeader *hdr = buf; | ||
160 | |||
161 | size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size; | ||
162 | |||
163 | hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO); | ||
164 | hdr->size = htons(size); | ||
165 | |||
166 | memcpy(hdr+1, handle->hello_message, handle->hello_message_size); | ||
167 | return sent; | ||
168 | } | ||
169 | |||
170 | |||
140 | /** | 171 | /** |
141 | * Core calls this if we are connected to a new peer. | 172 | * Core calls this if we are connected to a new peer. |
142 | * | 173 | * |
@@ -189,6 +220,15 @@ core_connect (void *cls, | |||
189 | else | 220 | else |
190 | tunnel = tunnel->next; | 221 | tunnel = tunnel->next; |
191 | } | 222 | } |
223 | GNUNET_CORE_notify_transmit_ready(handle->core, | ||
224 | GNUNET_NO, | ||
225 | 42, | ||
226 | GNUNET_TIME_UNIT_SECONDS, | ||
227 | peer, | ||
228 | sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size, | ||
229 | &send_hello_message, | ||
230 | cls); | ||
231 | |||
192 | } | 232 | } |
193 | 233 | ||
194 | /** | 234 | /** |
@@ -214,6 +254,7 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
214 | { | 254 | { |
215 | GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head, | 255 | GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head, |
216 | handle->connected_peers.tail, element); | 256 | handle->connected_peers.tail, element); |
257 | GNUNET_free_non_null(element->types); | ||
217 | GNUNET_free (element); | 258 | GNUNET_free (element); |
218 | } | 259 | } |
219 | 260 | ||
@@ -250,6 +291,67 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
250 | 291 | ||
251 | /** | 292 | /** |
252 | * Receive a message from core. | 293 | * Receive a message from core. |
294 | * This is a hello-message, containing the message-types the other peer can receive | ||
295 | */ | ||
296 | static int | ||
297 | receive_hello (void *cls, | ||
298 | const struct GNUNET_PeerIdentity *other, | ||
299 | const struct GNUNET_MessageHeader *message, | ||
300 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
301 | { | ||
302 | struct GNUNET_MESH_Handle *handle = cls; | ||
303 | uint16_t *num = (uint16_t *) (message + 1); | ||
304 | uint16_t *ports = num + 1; | ||
305 | unsigned int i; | ||
306 | |||
307 | struct peer_list_element *element = handle->connected_peers.head; | ||
308 | while (element != NULL) | ||
309 | { | ||
310 | if (0 == | ||
311 | memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity))) | ||
312 | break; | ||
313 | element = element->next; | ||
314 | } | ||
315 | |||
316 | element->num_types = *num; | ||
317 | element->types = GNUNET_malloc (*num * sizeof (uint16_t)); | ||
318 | |||
319 | for (i = 0; i < *num; i++) | ||
320 | element->types[i] = ntohs (ports[i]); | ||
321 | |||
322 | struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head; | ||
323 | while (tunnel != NULL) | ||
324 | { | ||
325 | struct tunnel_list_element *next = tunnel->next; | ||
326 | for (i = 0; i < *num; i++) | ||
327 | { | ||
328 | if (ntohs (ports[i]) == tunnel->tunnel.message_type) | ||
329 | { | ||
330 | GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head, | ||
331 | handle->pending_tunnels.tail, | ||
332 | tunnel); | ||
333 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels. | ||
334 | head, | ||
335 | handle->established_tunnels. | ||
336 | tail, | ||
337 | handle->established_tunnels. | ||
338 | tail, tunnel); | ||
339 | tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, | ||
340 | &tunnel->tunnel.peer, atsi); | ||
341 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); | ||
342 | break; | ||
343 | } | ||
344 | } | ||
345 | if (ntohs (ports[i]) == tunnel->tunnel.message_type) | ||
346 | tunnel = next; | ||
347 | else | ||
348 | tunnel = tunnel->next; | ||
349 | } | ||
350 | return GNUNET_OK; | ||
351 | } | ||
352 | |||
353 | /** | ||
354 | * Receive a message from core. | ||
253 | */ | 355 | */ |
254 | static int | 356 | static int |
255 | core_receive (void *cls, | 357 | core_receive (void *cls, |
@@ -318,6 +420,53 @@ core_receive (void *cls, | |||
318 | &tunnel->tunnel.ctx, other, rmessage, atsi); | 420 | &tunnel->tunnel.ctx, other, rmessage, atsi); |
319 | } | 421 | } |
320 | 422 | ||
423 | struct GNUNET_MESH_Tunnel * | ||
424 | GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle, | ||
425 | struct GNUNET_TIME_Relative timeout, | ||
426 | uint16_t message_type, | ||
427 | GNUNET_MESH_TunnelConnectHandler | ||
428 | connect_handler, | ||
429 | GNUNET_MESH_TunnelDisconnectHandler | ||
430 | disconnect_handler, | ||
431 | void *handler_cls) | ||
432 | { | ||
433 | /* Look in the list of connected peers */ | ||
434 | struct peer_list_element *element = handle->connected_peers.head; | ||
435 | while (element != NULL) | ||
436 | { | ||
437 | unsigned int i; | ||
438 | for (i = 0; i < element->num_types; i++) | ||
439 | if (message_type == element->types[i]) | ||
440 | return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1, | ||
441 | &handle->myself, | ||
442 | connect_handler, | ||
443 | disconnect_handler, | ||
444 | handler_cls); | ||
445 | element = element->next; | ||
446 | } | ||
447 | |||
448 | /* Put into pending list */ | ||
449 | struct tunnel_list_element *tunnel = | ||
450 | GNUNET_malloc (sizeof (struct tunnel_list_element)); | ||
451 | |||
452 | tunnel->tunnel.connect_handler = connect_handler; | ||
453 | tunnel->tunnel.disconnect_handler = disconnect_handler; | ||
454 | tunnel->tunnel.handler_cls = handler_cls; | ||
455 | tunnel->tunnel.ctx = NULL; | ||
456 | tunnel->tunnel.handle = handle; | ||
457 | memcpy (&tunnel->tunnel.id.initiator, &handle->myself, | ||
458 | sizeof (struct GNUNET_PeerIdentity)); | ||
459 | tunnel->tunnel.id.id = current_id++; | ||
460 | tunnel->tunnel.message_type = message_type; | ||
461 | |||
462 | GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head, | ||
463 | handle->pending_by_type_tunnels.tail, | ||
464 | handle->pending_by_type_tunnels.tail, | ||
465 | tunnel); | ||
466 | return &tunnel->tunnel; | ||
467 | } | ||
468 | |||
469 | |||
321 | 470 | ||
322 | struct GNUNET_MESH_Tunnel * | 471 | struct GNUNET_MESH_Tunnel * |
323 | GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle, | 472 | GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle, |
@@ -509,6 +658,7 @@ GNUNET_MESH_connect (const struct | |||
509 | 658 | ||
510 | const static struct GNUNET_CORE_MessageHandler core_handlers[] = { | 659 | const static struct GNUNET_CORE_MessageHandler core_handlers[] = { |
511 | {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0}, | 660 | {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0}, |
661 | {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0}, | ||
512 | {NULL, 0, 0} | 662 | {NULL, 0, 0} |
513 | }; | 663 | }; |
514 | 664 | ||