Commit f3a9a433 authored by Marek Vavruša's avatar Marek Vavruša

overlay: moved state checking to overlay, layers are independent

parent f9ecf696
......@@ -141,13 +141,13 @@ static int process_answer(knot_layer_t *ctx, knot_pkt_t *pkt)
return next_state;
}
#undef ANSWER_REQUIRES
static int prepare_query(knot_layer_t *ctx, knot_pkt_t *pkt)
{
/* \note Don't touch the query, expect answer. */
return NS_PROC_MORE;
}
#undef ANSWER_REQUIRES
/*! \brief Module implementation. */
static const knot_layer_api_t PROCESS_ANSWER_MODULE = {
......
......@@ -21,6 +21,7 @@
/*! \brief Helper for conditional layer call. */
#define LAYER_CALL(layer, func, ...) \
assert(layer->api); \
if (layer->api->func) { \
layer->state = layer->api->func(layer, ##__VA_ARGS__); \
}
......@@ -39,11 +40,6 @@ static const char* _state_table[] = {
int knot_layer_begin(knot_layer_t *ctx, const knot_layer_api_t *api, void *param)
{
/* Only in inoperable state. */
if (ctx->state != NS_PROC_NOOP) {
return ctx->state;
}
ctx->api = api;
LAYER_CALL(ctx, begin, param);
......@@ -61,11 +57,6 @@ int knot_layer_reset(knot_layer_t *ctx)
int knot_layer_finish(knot_layer_t *ctx)
{
/* Only in operable state. */
if (ctx->state == NS_PROC_NOOP) {
return ctx->state;
}
LAYER_CALL(ctx, finish);
dbg_ns("%s -> %s\n", __func__, LAYER_STATE_STR(ctx->state));
return ctx->state;
......@@ -73,11 +64,6 @@ int knot_layer_finish(knot_layer_t *ctx)
int knot_layer_in(knot_layer_t *ctx, knot_pkt_t *pkt)
{
/* Only if expecting data. */
if (ctx->state != NS_PROC_MORE) {
return ctx->state;
}
LAYER_CALL(ctx, in, pkt);
dbg_ns("%s -> %s\n", __func__, LAYER_STATE_STR(ctx->state));
return ctx->state;
......@@ -86,10 +72,9 @@ int knot_layer_in(knot_layer_t *ctx, knot_pkt_t *pkt)
int knot_layer_out(knot_layer_t *ctx, knot_pkt_t *pkt)
{
switch (ctx->state) {
case NS_PROC_FULL: LAYER_CALL(ctx, out, pkt); break;
case NS_PROC_FAIL: LAYER_CALL(ctx, err, pkt); break;
default:
break;
case NS_PROC_FULL:
default: LAYER_CALL(ctx, out, pkt); break;
}
dbg_ns("%s -> %s\n", __func__, LAYER_STATE_STR(ctx->state));
......
......@@ -83,8 +83,6 @@ inline static int knot_layer_noop(knot_layer_t *ctx, knot_pkt_t *pkt)
/*!
* \brief Initialize packet processing context.
*
* Allowed from states: NOOP
*
* \param ctx Layer context.
* \param param Parameters for given module.
* \param api Layer API.
......@@ -124,9 +122,6 @@ int knot_layer_in(knot_layer_t *ctx, knot_pkt_t *pkt);
/*!
* \brief Generate output from layer.
*
* Allowed from states: FULL, FAIL
*
* \param ctx Layer context.
* \param ctx Layer context.
* \param pkt Data packet.
*
......
......@@ -51,11 +51,21 @@ int knot_overlay_reset(struct knot_overlay *overlay)
int knot_overlay_finish(struct knot_overlay *overlay)
{
/* Only in operable state. */
if (overlay->state == NS_PROC_NOOP) {
return overlay->state;
}
ITERATE_LAYERS(overlay, knot_layer_finish);
}
int knot_overlay_in(struct knot_overlay *overlay, knot_pkt_t *pkt)
{
/* Only if expecting data. */
if (overlay->state != NS_PROC_MORE) {
return overlay->state;
}
knot_pkt_parse(pkt, 0);
ITERATE_LAYERS(overlay, knot_layer_in, pkt);
......@@ -63,5 +73,10 @@ int knot_overlay_in(struct knot_overlay *overlay, knot_pkt_t *pkt)
int knot_overlay_out(struct knot_overlay *overlay, knot_pkt_t *pkt)
{
/* Only in operable state. */
if (overlay->state == NS_PROC_NOOP) {
return overlay->state;
}
ITERATE_LAYERS(overlay, knot_layer_out, pkt);
}
......@@ -51,6 +51,7 @@ void knot_overlay_deinit(struct knot_overlay *overlay);
/*!
* \brief Add an overlay on top of the list and begin execution.
*
* \fn knot_layer_begin
*
* \param overlay
......@@ -63,25 +64,35 @@ int knot_overlay_add(struct knot_overlay *overlay, const knot_layer_api_t *modul
void *module_param);
/*!
* \brief Reset processing layers.
* \brief Reset layer processing.
*
* \fn knot_layer_reset
*/
int knot_overlay_reset(struct knot_overlay *overlay);
/*!
* \brief Finish processing layers.
* \brief Finish layer processing.
*
* Allowed from states: MORE, FULL, DONE, FAIL
*
* \fn knot_layer_finish
*/
int knot_overlay_finish(struct knot_overlay *overlay);
/*!
* \brief Input more data.
* \brief Add more data to layer processing.
*
* Allowed from states: MORE
*
* \fn knot_layer_in
*/
int knot_overlay_in(struct knot_overlay *overlay, knot_pkt_t *pkt);
/*!
* \brief Write out output.
* \brief Generate output from layers.
*
* Allowed from states: FULL, FAIL
*
* \fn knot_layer_out
*/
int knot_overlay_out(struct knot_overlay *overlay, knot_pkt_t *pkt);
......
......@@ -171,6 +171,7 @@ void knot_requestor_clear(struct knot_requestor *requestor)
while (knot_requestor_dequeue(requestor) == KNOT_EOK)
;
knot_overlay_finish(&requestor->overlay);
knot_overlay_deinit(&requestor->overlay);
}
......@@ -262,25 +263,22 @@ static int exec_request(struct knot_requestor *req, struct knot_request *last, s
{
int ret = KNOT_EOK;
/* Reset overlay. */
knot_overlay_reset(&req->overlay);
/* Do I/O until the processing is satisifed or fails. */
while (req->overlay.state & (NS_PROC_FULL|NS_PROC_MORE)) {
ret = request_io(req, last, timeout);
if (ret != KNOT_EOK) {
knot_overlay_finish(&req->overlay);
knot_overlay_reset(&req->overlay);
return ret;
}
}
/* Expect complete request. */
if (req->overlay.state != NS_PROC_DONE) {
if (req->overlay.state == NS_PROC_FAIL) {
ret = KNOT_ERROR;
}
/* Finish processing. */
knot_overlay_finish(&req->overlay);
/* Finish current query processing. */
knot_overlay_reset(&req->overlay);
return ret;
}
......
......@@ -25,8 +25,8 @@
/* @note Purpose of this test is not to verify process_answer functionality,
* but simply if the requesting/receiving works, so mirror is okay. */
static int begin(knot_layer_t *ctx, void *module_param) { return NS_PROC_NOOP; }
static int reset(knot_layer_t *ctx) { return NS_PROC_FULL; }
static int begin(knot_layer_t *ctx, void *module_param) { return reset(ctx); }
static int finish(knot_layer_t *ctx) { return NS_PROC_NOOP; }
static int in(knot_layer_t *ctx, knot_pkt_t *pkt) { return NS_PROC_DONE; }
static int out(knot_layer_t *ctx, knot_pkt_t *pkt) { return NS_PROC_MORE; }
......
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