diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-08-30 20:48:51 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-08-30 20:48:51 +0000 |
commit | d986f2c7460120ffc3f199b29a06126d0b97b75b (patch) | |
tree | 5f8d7f3e3461db2b3959bcc9bf434aa81e0515d2 /src | |
parent | 485f20f1de5adbbb0ab05b42aaced78feb9bf58c (diff) | |
download | gnunet-d986f2c7460120ffc3f199b29a06126d0b97b75b.tar.gz gnunet-d986f2c7460120ffc3f199b29a06126d0b97b75b.zip |
adding convenience API
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_client_lib.h | 24 | ||||
-rw-r--r-- | src/util/client.c | 112 |
2 files changed, 136 insertions, 0 deletions
diff --git a/src/include/gnunet_client_lib.h b/src/include/gnunet_client_lib.h index 8651b273c..d391aed2b 100644 --- a/src/include/gnunet_client_lib.h +++ b/src/include/gnunet_client_lib.h | |||
@@ -119,6 +119,30 @@ struct GNUNET_CONNECTION_TransmitHandle | |||
119 | notify, void *notify_cls); | 119 | notify, void *notify_cls); |
120 | 120 | ||
121 | 121 | ||
122 | |||
123 | /** | ||
124 | * Convenience API that combines sending a request | ||
125 | * to the service and waiting for a response. | ||
126 | * If either operation times out, the callback | ||
127 | * will be called with a "NULL" response (in which | ||
128 | * case the connection should probably be destroyed). | ||
129 | * | ||
130 | * @param sock connection to use | ||
131 | * @param hdr message to transmit | ||
132 | * @param timeout when to give up (for both transmission | ||
133 | * and for waiting for a response) | ||
134 | * @param rn function to call with the response | ||
135 | * @param rn_cls closure for rn | ||
136 | */ | ||
137 | void | ||
138 | GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, | ||
139 | const struct GNUNET_MessageHeader *hdr, | ||
140 | struct GNUNET_TIME_Relative timeout, | ||
141 | GNUNET_CLIENT_MessageHandler rn, | ||
142 | void *rn_cls); | ||
143 | |||
144 | |||
145 | |||
122 | /** | 146 | /** |
123 | * Request that the service should shutdown. | 147 | * Request that the service should shutdown. |
124 | * Afterwards, the connection should be disconnected. | 148 | * Afterwards, the connection should be disconnected. |
diff --git a/src/util/client.c b/src/util/client.c index 0c639c905..8b356ce45 100644 --- a/src/util/client.c +++ b/src/util/client.c | |||
@@ -521,4 +521,116 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock, | |||
521 | } | 521 | } |
522 | 522 | ||
523 | 523 | ||
524 | /** | ||
525 | * Context for processing | ||
526 | * "GNUNET_CLIENT_transmit_and_get_response" requests. | ||
527 | */ | ||
528 | struct TARCtx | ||
529 | { | ||
530 | /** | ||
531 | * Client handle. | ||
532 | */ | ||
533 | struct GNUNET_CLIENT_Connection *sock; | ||
534 | |||
535 | /** | ||
536 | * Message to transmit; do not free, allocated | ||
537 | * right after this struct. | ||
538 | */ | ||
539 | const struct GNUNET_MessageHeader *hdr; | ||
540 | |||
541 | /** | ||
542 | * Timeout to use. | ||
543 | */ | ||
544 | struct GNUNET_TIME_Absolute timeout; | ||
545 | |||
546 | /** | ||
547 | * Function to call when done. | ||
548 | */ | ||
549 | GNUNET_CLIENT_MessageHandler rn; | ||
550 | |||
551 | /** | ||
552 | * Closure for "rn". | ||
553 | */ | ||
554 | void *rn_cls; | ||
555 | }; | ||
556 | |||
557 | |||
558 | /** | ||
559 | * Function called to notify a client about the socket | ||
560 | * begin ready to queue the message. "buf" will be | ||
561 | * NULL and "size" zero if the socket was closed for | ||
562 | * writing in the meantime. | ||
563 | * | ||
564 | * @param cls closure of type "struct TARCtx*" | ||
565 | * @param size number of bytes available in buf | ||
566 | * @param buf where the callee should write the message | ||
567 | * @return number of bytes written to buf | ||
568 | */ | ||
569 | static size_t | ||
570 | transmit_for_response (void *cls, | ||
571 | size_t size, | ||
572 | void *buf) | ||
573 | { | ||
574 | struct TARCtx *tc = cls; | ||
575 | uint16_t msize; | ||
576 | |||
577 | msize = ntohs(tc->hdr->size); | ||
578 | if (NULL == buf) | ||
579 | { | ||
580 | tc->rn (tc->rn_cls, NULL); | ||
581 | GNUNET_free (tc); | ||
582 | return 0; | ||
583 | } | ||
584 | GNUNET_assert (size >= msize); | ||
585 | memcpy (buf, tc->hdr, msize); | ||
586 | GNUNET_CLIENT_receive (tc->sock, | ||
587 | tc->rn, | ||
588 | tc->rn_cls, | ||
589 | GNUNET_TIME_absolute_get_remaining (tc->timeout)); | ||
590 | GNUNET_free (tc); | ||
591 | return msize; | ||
592 | } | ||
593 | |||
594 | |||
595 | /** | ||
596 | * Convenience API that combines sending a request | ||
597 | * to the service and waiting for a response. | ||
598 | * If either operation times out, the callback | ||
599 | * will be called with a "NULL" response (in which | ||
600 | * case the connection should probably be destroyed). | ||
601 | * | ||
602 | * @param sock connection to use | ||
603 | * @param hdr message to transmit | ||
604 | * @param timeout when to give up (for both transmission | ||
605 | * and for waiting for a response) | ||
606 | * @param rn function to call with the response | ||
607 | * @param rn_cls closure for rn | ||
608 | */ | ||
609 | void | ||
610 | GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, | ||
611 | const struct GNUNET_MessageHeader *hdr, | ||
612 | struct GNUNET_TIME_Relative timeout, | ||
613 | GNUNET_CLIENT_MessageHandler rn, | ||
614 | void *rn_cls) | ||
615 | { | ||
616 | struct TARCtx *tc; | ||
617 | uint16_t msize; | ||
618 | |||
619 | msize = ntohs(hdr->size); | ||
620 | tc = GNUNET_malloc(sizeof (struct TARCtx) + msize); | ||
621 | tc->sock = sock; | ||
622 | tc->hdr = (const struct GNUNET_MessageHeader*) &tc[1]; | ||
623 | memcpy (&tc[1], hdr, msize); | ||
624 | tc->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
625 | tc->rn = rn; | ||
626 | tc->rn_cls = rn_cls; | ||
627 | GNUNET_CLIENT_notify_transmit_ready (sock, | ||
628 | msize, | ||
629 | timeout, | ||
630 | &transmit_for_response, | ||
631 | tc); | ||
632 | } | ||
633 | |||
634 | |||
635 | |||
524 | /* end of client.c */ | 636 | /* end of client.c */ |