diff options
author | Bart Polot <bart@net.in.tum.de> | 2011-05-13 13:30:03 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2011-05-13 13:30:03 +0000 |
commit | 8a6d4585745ca9b7d4ea4638a5f0334edbd3a5e9 (patch) | |
tree | 6396e4a9b750405615698e2c5c2e3f10ab2f3f3c /src/mesh | |
parent | e1dd69dbee56a5b3b3b19fb6914fd4945f3416cb (diff) | |
download | gnunet-8a6d4585745ca9b7d4ea4638a5f0334edbd3a5e9.tar.gz gnunet-8a6d4585745ca9b7d4ea4638a5f0334edbd3a5e9.zip |
WiP
Diffstat (limited to 'src/mesh')
-rw-r--r-- | src/mesh/mesh_api_new.c | 127 |
1 files changed, 113 insertions, 14 deletions
diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c index 1fe9714e5..750e3cc48 100644 --- a/src/mesh/mesh_api_new.c +++ b/src/mesh/mesh_api_new.c | |||
@@ -115,6 +115,11 @@ struct GNUNET_MESH_Tunnel { | |||
115 | * Closure for the connect/disconnect handlers | 115 | * Closure for the connect/disconnect handlers |
116 | */ | 116 | */ |
117 | void *cls; | 117 | void *cls; |
118 | |||
119 | /** | ||
120 | * Handle to the mesh this tunnel belongs to | ||
121 | */ | ||
122 | struct GNUNET_MESH_Handle *mesh; | ||
118 | }; | 123 | }; |
119 | 124 | ||
120 | struct GNUNET_MESH_TransmitHandle { | 125 | struct GNUNET_MESH_TransmitHandle { |
@@ -195,6 +200,53 @@ send_connect_packet (void *cls, size_t size, void *buf) | |||
195 | 200 | ||
196 | 201 | ||
197 | /** | 202 | /** |
203 | * Function called to notify a client about the socket begin ready to queue more | ||
204 | * data. "buf" will be NULL and "size" zero if the socket was closed for | ||
205 | * writing in the meantime. | ||
206 | * | ||
207 | * @param cls closure | ||
208 | * @param size number of bytes available in buf | ||
209 | * @param buf where the callee should write the message | ||
210 | * @return number of bytes written to buf | ||
211 | */ | ||
212 | static size_t | ||
213 | send_tunnel_create_packet (void *cls, size_t size, void *buf) | ||
214 | { | ||
215 | struct GNUNET_MESH_Tunnel *t = cls; | ||
216 | struct GNUNET_MESH_Handle *h; | ||
217 | struct GNUNET_MESH_TunnelMessage *msg; | ||
218 | |||
219 | h = t->mesh; | ||
220 | h->th = NULL; | ||
221 | if (0 == size || buf == NULL) { | ||
222 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
223 | "Send connect packet: buffer size 0 or buffer invalid\n"); | ||
224 | // FIXME: disconnect, reconnect, retry! | ||
225 | return 0; | ||
226 | } | ||
227 | if (sizeof(struct GNUNET_MessageHeader) > size) { | ||
228 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
229 | "Send connect packet: buffer size too small\n"); | ||
230 | // FIXME: disconnect, reconnect, retry! | ||
231 | return 0; | ||
232 | } | ||
233 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
234 | "Send connect packet: %lu bytes buffer\n", | ||
235 | size); | ||
236 | msg = (struct GNUNET_MESH_TunnelMessage *) buf; | ||
237 | msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT); | ||
238 | |||
239 | msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage)); | ||
240 | |||
241 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
242 | "Sent %lu bytes long message\n", | ||
243 | ntohs(msg->header.size)); | ||
244 | |||
245 | return ntohs(msg->header.size); | ||
246 | } | ||
247 | |||
248 | |||
249 | /** | ||
198 | * Type of a function to call when we receive a message | 250 | * Type of a function to call when we receive a message |
199 | * from the service. | 251 | * from the service. |
200 | * | 252 | * |
@@ -204,15 +256,52 @@ send_connect_packet (void *cls, size_t size, void *buf) | |||
204 | void | 256 | void |
205 | msg_received (void *cls, const struct GNUNET_MessageHeader * msg) | 257 | msg_received (void *cls, const struct GNUNET_MessageHeader * msg) |
206 | { | 258 | { |
207 | uint16_t t; | 259 | struct GNUNET_MESH_Handle *h = cls; |
208 | if(msg != NULL){ | 260 | const struct GNUNET_MessageHeader *payload; |
209 | t = ntohs(msg->type); | 261 | const struct GNUNET_MESH_MessageHandler *handler; |
210 | } else { | 262 | uint16_t type; |
211 | t = 0; | 263 | int i; |
264 | |||
265 | if (msg == NULL) { | ||
266 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
267 | "received a NULL message from mesh\n"); | ||
268 | return; | ||
269 | } | ||
270 | |||
271 | switch (ntohs(msg->type)) { | ||
272 | case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED: | ||
273 | break; | ||
274 | case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA: | ||
275 | payload = &msg[1]; | ||
276 | for (i = 0, type = ntohs(payload->type); i < h->n_handlers; i++) { | ||
277 | handler = &h->message_handlers[i]; | ||
278 | if (handler->type == type) { | ||
279 | if (GNUNET_OK == handler->callback (cls, | ||
280 | NULL, | ||
281 | NULL, | ||
282 | NULL, | ||
283 | NULL, | ||
284 | NULL | ||
285 | )) | ||
286 | { | ||
287 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
288 | "MESH: callback completed successfully\n"); | ||
289 | } else { | ||
290 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
291 | "MESH: callback caused disconnection\n"); | ||
292 | GNUNET_MESH_disconnect(h); | ||
293 | } | ||
294 | } | ||
295 | } | ||
296 | break; | ||
297 | default: | ||
298 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
299 | "MESH: unsolited message form service (type %d)\n", | ||
300 | ntohs(msg->type)); | ||
212 | } | 301 | } |
302 | |||
213 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 303 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
214 | "received a message from mesh (of size %d)\n", | 304 | "received a message from mesh\n"); |
215 | t); | ||
216 | return; | 305 | return; |
217 | } | 306 | } |
218 | 307 | ||
@@ -267,11 +356,11 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
267 | size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType); | 356 | size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType); |
268 | 357 | ||
269 | h->th = GNUNET_CLIENT_notify_transmit_ready(h->client, | 358 | h->th = GNUNET_CLIENT_notify_transmit_ready(h->client, |
270 | size, | 359 | size, |
271 | GNUNET_TIME_UNIT_FOREVER_REL, | 360 | GNUNET_TIME_UNIT_FOREVER_REL, |
272 | GNUNET_YES, | 361 | GNUNET_YES, |
273 | &send_connect_packet, | 362 | &send_connect_packet, |
274 | (void *)h); | 363 | (void *)h); |
275 | 364 | ||
276 | return h; | 365 | return h; |
277 | } | 366 | } |
@@ -314,11 +403,21 @@ GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, | |||
314 | { | 403 | { |
315 | struct GNUNET_MESH_Tunnel *tunnel; | 404 | struct GNUNET_MESH_Tunnel *tunnel; |
316 | 405 | ||
406 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
407 | "MESH: Creating new tunnel\n"); | ||
317 | tunnel = GNUNET_malloc(sizeof(struct GNUNET_MESH_Tunnel)); | 408 | tunnel = GNUNET_malloc(sizeof(struct GNUNET_MESH_Tunnel)); |
318 | 409 | ||
319 | tunnel->connect_handler = connect_handler; | 410 | tunnel->connect_handler = connect_handler; |
320 | tunnel->disconnect_handler = disconnect_handler; | 411 | tunnel->disconnect_handler = disconnect_handler; |
321 | tunnel->cls = handler_cls; | 412 | tunnel->cls = handler_cls; |
413 | tunnel->mesh = h; | ||
414 | |||
415 | h->th = GNUNET_CLIENT_notify_transmit_ready(h->client, | ||
416 | sizeof(struct GNUNET_MESH_TunnelMessage), | ||
417 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
418 | GNUNET_YES, | ||
419 | &send_tunnel_create_packet, | ||
420 | (void *)tunnel); | ||
322 | 421 | ||
323 | return tunnel; | 422 | return tunnel; |
324 | } | 423 | } |
@@ -338,9 +437,9 @@ GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel, | |||
338 | const struct GNUNET_PeerIdentity *peer) | 437 | const struct GNUNET_PeerIdentity *peer) |
339 | { | 438 | { |
340 | static GNUNET_PEER_Id peer_id; | 439 | static GNUNET_PEER_Id peer_id; |
341 | 440 | ||
342 | peer_id = GNUNET_PEER_intern(peer); | 441 | peer_id = GNUNET_PEER_intern(peer); |
343 | 442 | ||
344 | /* FIXME ACTUALLY DO STUFF */ | 443 | /* FIXME ACTUALLY DO STUFF */ |
345 | tunnel->peers = &peer_id; | 444 | tunnel->peers = &peer_id; |
346 | tunnel->connect_handler(tunnel->cls, peer, NULL); | 445 | tunnel->connect_handler(tunnel->cls, peer, NULL); |