diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-16 16:15:52 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-16 16:15:52 +0000 |
commit | 74b34ed9b400f74a7977e268626b85b51acfedd4 (patch) | |
tree | 0b6dcb1a2936d3784b46863e1ea265b703b26e84 /src/transport/plugin_transport_http_server.c | |
parent | 3ebd0f182125b2c0dbbadf54c5bf723753a7a705 (diff) | |
download | gnunet-74b34ed9b400f74a7977e268626b85b51acfedd4.tar.gz gnunet-74b34ed9b400f74a7977e268626b85b51acfedd4.zip |
transmitting data
Diffstat (limited to 'src/transport/plugin_transport_http_server.c')
-rw-r--r-- | src/transport/plugin_transport_http_server.c | 152 |
1 files changed, 146 insertions, 6 deletions
diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c index 43d9171eb..98fbfda1c 100644 --- a/src/transport/plugin_transport_http_server.c +++ b/src/transport/plugin_transport_http_server.c | |||
@@ -232,6 +232,81 @@ server_load_certificate (struct Plugin *plugin) | |||
232 | 232 | ||
233 | 233 | ||
234 | /** | 234 | /** |
235 | * Callback called by MessageStreamTokenizer when a message has arrived | ||
236 | * @param cls current session as closure | ||
237 | * @param client clien | ||
238 | * @param message the message to be forwarded to transport service | ||
239 | */ | ||
240 | static void | ||
241 | server_receive_mst_cb (void *cls, void *client, | ||
242 | const struct GNUNET_MessageHeader *message) | ||
243 | { | ||
244 | struct Session *s = cls; | ||
245 | struct Plugin *plugin = s->plugin; | ||
246 | struct GNUNET_TIME_Relative delay; | ||
247 | |||
248 | delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen); | ||
249 | |||
250 | s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay); | ||
251 | |||
252 | if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value) | ||
253 | { | ||
254 | #if VERBOSE_CLIENT | ||
255 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n", | ||
256 | GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay); | ||
257 | #endif | ||
258 | } | ||
259 | } | ||
260 | |||
261 | /** | ||
262 | * Callback called by MHD when it needs data to send | ||
263 | * @param cls current session | ||
264 | * @param pos position in buffer | ||
265 | * @param buf the buffer to write data to | ||
266 | * @param max max number of bytes available in buffer | ||
267 | * @return bytes written to buffer | ||
268 | */ | ||
269 | static ssize_t | ||
270 | mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max) | ||
271 | { | ||
272 | struct Session *s = cls; | ||
273 | struct HTTP_Message *msg; | ||
274 | int bytes_read = 0; | ||
275 | |||
276 | msg = s->msg_head; | ||
277 | if (msg != NULL) | ||
278 | { | ||
279 | /* sending */ | ||
280 | if ((msg->size - msg->pos) <= max) | ||
281 | { | ||
282 | memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos)); | ||
283 | bytes_read = msg->size - msg->pos; | ||
284 | msg->pos += (msg->size - msg->pos); | ||
285 | } | ||
286 | else | ||
287 | { | ||
288 | memcpy (buf, &msg->buf[msg->pos], max); | ||
289 | msg->pos += max; | ||
290 | bytes_read = max; | ||
291 | } | ||
292 | |||
293 | /* removing message */ | ||
294 | if (msg->pos == msg->size) | ||
295 | { | ||
296 | if (NULL != msg->transmit_cont) | ||
297 | msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK); | ||
298 | GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg); | ||
299 | GNUNET_free (msg); | ||
300 | } | ||
301 | } | ||
302 | #if DEBUG_CONNECTIONS | ||
303 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: MHD has sent %u bytes\n", | ||
304 | s, bytes_read); | ||
305 | #endif | ||
306 | return bytes_read; | ||
307 | } | ||
308 | |||
309 | /** | ||
235 | * Process GET or PUT request received via MHD. For | 310 | * Process GET or PUT request received via MHD. For |
236 | * GET, queue response that will send back our pending | 311 | * GET, queue response that will send back our pending |
237 | * messages. For PUT, process incoming data and send | 312 | * messages. For PUT, process incoming data and send |
@@ -403,11 +478,7 @@ error: | |||
403 | res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response); | 478 | res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response); |
404 | MHD_destroy_response (response); | 479 | MHD_destroy_response (response); |
405 | return res; | 480 | return res; |
406 | |||
407 | |||
408 | found: | 481 | found: |
409 | |||
410 | |||
411 | sc = GNUNET_malloc (sizeof (struct ServerConnection)); | 482 | sc = GNUNET_malloc (sizeof (struct ServerConnection)); |
412 | sc->mhd_conn = mhd_connection; | 483 | sc->mhd_conn = mhd_connection; |
413 | sc->direction = direction; | 484 | sc->direction = direction; |
@@ -418,8 +489,9 @@ found: | |||
418 | s->server_recv = sc; | 489 | s->server_recv = sc; |
419 | 490 | ||
420 | (*httpSessionCache) = sc; | 491 | (*httpSessionCache) = sc; |
421 | return MHD_YES; | ||
422 | } | 492 | } |
493 | |||
494 | |||
423 | /* existing connection */ | 495 | /* existing connection */ |
424 | sc = (*httpSessionCache); | 496 | sc = (*httpSessionCache); |
425 | s = sc->session; | 497 | s = sc->session; |
@@ -437,6 +509,67 @@ found: | |||
437 | return MHD_YES; | 509 | return MHD_YES; |
438 | } | 510 | } |
439 | 511 | ||
512 | GNUNET_assert (s != NULL); | ||
513 | if (sc->direction == _SEND) | ||
514 | { | ||
515 | response = | ||
516 | MHD_create_response_from_callback (-1, 32 * 1024, &mhd_send_callback, | ||
517 | s, NULL); | ||
518 | res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); | ||
519 | MHD_destroy_response (response); | ||
520 | return MHD_YES; | ||
521 | } | ||
522 | if (sc->direction == _RECEIVE) | ||
523 | { | ||
524 | if (*upload_data_size == 0) | ||
525 | { | ||
526 | #if VERBOSE_SERVER | ||
527 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, | ||
528 | "Server: peer `%s' PUT on address `%s' connected\n", | ||
529 | GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); | ||
530 | #endif | ||
531 | return MHD_YES; | ||
532 | } | ||
533 | |||
534 | /* Recieving data */ | ||
535 | if ((*upload_data_size > 0)) | ||
536 | { | ||
537 | #if VERBOSE_SERVER | ||
538 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, | ||
539 | "Server: peer `%s' PUT on address `%s' received %u bytes\n", | ||
540 | GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); | ||
541 | #endif | ||
542 | if ((GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)) | ||
543 | { | ||
544 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
545 | "Connection %X: PUT with %u bytes forwarded to MST\n", s, | ||
546 | *upload_data_size); | ||
547 | |||
548 | if (s->msg_tk == NULL) | ||
549 | { | ||
550 | s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s); | ||
551 | } | ||
552 | res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO); | ||
553 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
554 | "Server: Received %u bytes\n", | ||
555 | *upload_data_size); | ||
556 | (*upload_data_size) = 0; | ||
557 | } | ||
558 | else | ||
559 | { | ||
560 | /* | ||
561 | #if DEBUG_HTTP | ||
562 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
563 | "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n", | ||
564 | s, ps->peercontext->delay.rel_value); | ||
565 | #endif | ||
566 | */ | ||
567 | } | ||
568 | return MHD_YES; | ||
569 | } | ||
570 | else | ||
571 | return MHD_NO; | ||
572 | } | ||
440 | return res; | 573 | return res; |
441 | } | 574 | } |
442 | 575 | ||
@@ -484,6 +617,8 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, | |||
484 | tc = s->server_send; | 617 | tc = s->server_send; |
485 | tc->disconnect = GNUNET_YES; | 618 | tc->disconnect = GNUNET_YES; |
486 | } | 619 | } |
620 | if (s->msg_tk != NULL) | ||
621 | GNUNET_SERVER_mst_destroy(s->msg_tk); | ||
487 | } | 622 | } |
488 | GNUNET_free (sc); | 623 | GNUNET_free (sc); |
489 | 624 | ||
@@ -500,6 +635,7 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, | |||
500 | } | 635 | } |
501 | plugin->cur_connections--; | 636 | plugin->cur_connections--; |
502 | 637 | ||
638 | |||
503 | if ((s->server_send == NULL) && (s->server_recv == NULL)) | 639 | if ((s->server_send == NULL) && (s->server_recv == NULL)) |
504 | { | 640 | { |
505 | #if VERBOSE_SERVER | 641 | #if VERBOSE_SERVER |
@@ -507,6 +643,7 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, | |||
507 | "Server: peer `%s' on address `%s' disconnected\n", | 643 | "Server: peer `%s' on address `%s' disconnected\n", |
508 | GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); | 644 | GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); |
509 | #endif | 645 | #endif |
646 | |||
510 | notify_session_end(s->plugin, &s->target, s); | 647 | notify_session_end(s->plugin, &s->target, s); |
511 | } | 648 | } |
512 | } | 649 | } |
@@ -538,8 +675,9 @@ server_disconnect (struct Session *s) | |||
538 | } | 675 | } |
539 | 676 | ||
540 | int | 677 | int |
541 | server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size) | 678 | server_send (struct Session *s, struct HTTP_Message * msg) |
542 | { | 679 | { |
680 | GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg); | ||
543 | return GNUNET_OK; | 681 | return GNUNET_OK; |
544 | } | 682 | } |
545 | 683 | ||
@@ -809,6 +947,8 @@ server_stop (struct Plugin *plugin) | |||
809 | while (s != NULL) | 947 | while (s != NULL) |
810 | { | 948 | { |
811 | t = s->next; | 949 | t = s->next; |
950 | if (s->msg_tk != NULL) | ||
951 | GNUNET_SERVER_mst_destroy(s->msg_tk); | ||
812 | delete_session (s); | 952 | delete_session (s); |
813 | s = t; | 953 | s = t; |
814 | } | 954 | } |