Commit 675613ac authored by Jan Včelák's avatar Jan Včelák 🚀

requestor: change API to accept timeout in miliseconds

This change follows the change in the internal networking API.

Breaks libknot API and ABI.
parent 1f94b48f
......@@ -59,12 +59,8 @@ static int request_ensure_connected(struct knot_request *request)
return KNOT_EOK;
}
static int request_send(struct knot_request *request,
const struct timeval *timeout)
static int request_send(struct knot_request *request, int timeout_ms)
{
/* Each request has unique timeout. */
struct timeval tv = *timeout;
/* Initiate non-blocking connect if not connected. */
int ret = request_ensure_connected(request);
if (ret != KNOT_EOK) {
......@@ -78,7 +74,7 @@ static int request_send(struct knot_request *request,
/* Send query. */
if (use_tcp(request)) {
ret = net_dns_tcp_send(request->fd, wire, wire_len, &tv);
ret = net_dns_tcp_send(request->fd, wire, wire_len, timeout_ms);
} else {
ret = net_dgram_send(request->fd, wire, wire_len, NULL);
}
......@@ -89,8 +85,7 @@ static int request_send(struct knot_request *request,
return KNOT_EOK;
}
static int request_recv(struct knot_request *request,
const struct timeval *timeout)
static int request_recv(struct knot_request *request, int timeout_ms)
{
knot_pkt_t *resp = request->resp;
knot_pkt_clear(resp);
......@@ -101,14 +96,11 @@ static int request_recv(struct knot_request *request,
return ret;
}
/* Each request has unique timeout. */
struct timeval tv = *timeout;
/* Receive it */
if (use_tcp(request)) {
ret = net_dns_tcp_recv(request->fd, resp->wire, resp->max_size, &tv);
ret = net_dns_tcp_recv(request->fd, resp->wire, resp->max_size, timeout_ms);
} else {
ret = net_dgram_recv(request->fd, resp->wire, resp->max_size, &tv);
ret = net_dgram_recv(request->fd, resp->wire, resp->max_size, timeout_ms);
}
if (ret <= 0) {
resp->size = 0;
......@@ -271,7 +263,7 @@ int knot_requestor_dequeue(struct knot_requestor *requestor)
}
static int request_io(struct knot_requestor *req, struct knot_request *last,
struct timeval *timeout)
int timeout_ms)
{
int ret = KNOT_EOK;
knot_pkt_t *query = last->query;
......@@ -284,7 +276,7 @@ static int request_io(struct knot_requestor *req, struct knot_request *last,
knot_overlay_produce(&req->overlay, query);
if (req->overlay.state == KNOT_STATE_CONSUME) {
ret = request_send(last, timeout);
ret = request_send(last, timeout_ms);
if (ret != KNOT_EOK) {
return ret;
}
......@@ -294,7 +286,7 @@ static int request_io(struct knot_requestor *req, struct knot_request *last,
/* Data to be read. */
if (req->overlay.state == KNOT_STATE_CONSUME) {
/* Read answer and process it. */
ret = request_recv(last, timeout);
ret = request_recv(last, timeout_ms);
if (ret < 0) {
return ret;
}
......@@ -307,13 +299,13 @@ static int request_io(struct knot_requestor *req, struct knot_request *last,
}
static int exec_request(struct knot_requestor *req, struct knot_request *last,
struct timeval *timeout)
int timeout_ms)
{
int ret = KNOT_EOK;
/* Do I/O until the processing is satisifed or fails. */
while (req->overlay.state & (KNOT_STATE_PRODUCE|KNOT_STATE_CONSUME)) {
ret = request_io(req, last, timeout);
ret = request_io(req, last, timeout_ms);
if (ret != KNOT_EOK) {
knot_overlay_reset(&req->overlay);
return ret;
......@@ -332,8 +324,7 @@ static int exec_request(struct knot_requestor *req, struct knot_request *last,
}
_public_
int knot_requestor_exec(struct knot_requestor *requestor,
struct timeval *timeout)
int knot_requestor_exec(struct knot_requestor *requestor, int timeout_ms)
{
if (knot_requestor_finished(requestor)) {
return KNOT_ENOENT;
......@@ -342,7 +333,7 @@ int knot_requestor_exec(struct knot_requestor *requestor,
/* Execute next request. */
ptrnode_t *node = HEAD(*PENDING(requestor));
struct knot_request *last = node->d;
int ret = exec_request(requestor, last, timeout);
int ret = exec_request(requestor, last, timeout_ms);
/* Remove it from processing. */
knot_requestor_dequeue(requestor);
......
......@@ -135,10 +135,9 @@ int knot_requestor_dequeue(struct knot_requestor *requestor);
/*!
* \brief Execute next pending query (FIFO).
*
* \param requestor Requestor instance.
* \param timeout Processing timeout.
* \param requestor Requestor instance.
* \param timeout_ms Timeout of each operation in miliseconds (-1 for infinity).
*
* \return KNOT_EOK or error
*/
int knot_requestor_exec(struct knot_requestor *requestor,
struct timeval *timeout);
int knot_requestor_exec(struct knot_requestor *requestor, int timeout_ms);
......@@ -37,7 +37,7 @@ static int finish(knot_layer_t *ctx) { return KNOT_STATE_NOOP; }
static int in(knot_layer_t *ctx, knot_pkt_t *pkt) { return KNOT_STATE_DONE; }
static int out(knot_layer_t *ctx, knot_pkt_t *pkt) { return KNOT_STATE_CONSUME; }
static const struct timeval TIMEOUT = { 2, 0 };
static const int TIMEOUT = 2000;
/*! \brief Dummy answer processing module. */
const knot_layer_api_t dummy_module = {
......@@ -62,13 +62,13 @@ static void* responder_thread(void *arg)
if (client < 0) {
break;
}
int len = net_dns_tcp_recv(client, buf, sizeof(buf), NULL);
int len = net_dns_tcp_recv(client, buf, sizeof(buf), -1);
if (len < KNOT_WIRE_HEADER_SIZE) {
close(client);
break;
}
knot_wire_set_qr(buf);
net_dns_tcp_send(client, buf, len, NULL);
net_dns_tcp_send(client, buf, len, -1);
close(client);
}
return NULL;
......@@ -95,8 +95,7 @@ static void test_disconnected(struct knot_requestor *requestor, conf_remote_t *r
is_int(KNOT_EOK, ret, "requestor: disconnected/enqueue");
/* Wait for completion. */
struct timeval tv = TIMEOUT;
ret = knot_requestor_exec(requestor, &tv);
ret = knot_requestor_exec(requestor, TIMEOUT);
is_int(KNOT_ECONN, ret, "requestor: disconnected/wait");
}
......@@ -107,8 +106,7 @@ static void test_connected(struct knot_requestor *requestor, conf_remote_t *remo
is_int(KNOT_EOK, ret, "requestor: connected/enqueue");
/* Wait for completion. */
struct timeval tv = TIMEOUT;
ret = knot_requestor_exec(requestor, &tv);
ret = knot_requestor_exec(requestor, TIMEOUT);
is_int(KNOT_EOK, ret, "requestor: connected/wait");
/* Enqueue multiple queries. */
......@@ -121,8 +119,7 @@ static void test_connected(struct knot_requestor *requestor, conf_remote_t *remo
/* Wait for multiple queries. */
ret = KNOT_EOK;
for (unsigned i = 0; i < 10; ++i) {
struct timeval tv = TIMEOUT;
ret |= knot_requestor_exec(requestor, &tv);
ret |= knot_requestor_exec(requestor, TIMEOUT);
}
is_int(KNOT_EOK, ret, "requestor: multiple wait");
}
......@@ -165,10 +162,9 @@ int main(int argc, char *argv[])
/*! \todo #243 TSIG secured requests test should be implemented. */
/* Terminate responder. */
struct timeval tv = TIMEOUT;
int responder = net_connected_socket(SOCK_STREAM, &remote.addr, NULL);
assert(responder > 0);
net_dns_tcp_send(responder, (const uint8_t *)"", 1, &tv);
net_dns_tcp_send(responder, (const uint8_t *)"", 1, TIMEOUT);
(void) pthread_join(thread, 0);
close(responder);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment