diff --git a/src/algo_rr.c b/src/algo_rr.c index 336c9b0..b7c69fe 100644 --- a/src/algo_rr.c +++ b/src/algo_rr.c @@ -79,6 +79,7 @@ int set_timeout(struct evt_core_ctx* evts, uint64_t micro_sec, struct waited_pkt fdinfo.cat = &cat; fdinfo.url = url; + printf("Will add a timeout of %ld ms\n", micro_sec); if (clock_gettime(CLOCK_REALTIME, &now) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); @@ -120,6 +121,7 @@ void rr_pkt_register(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, } // 2. If packet arrived too late, we discard it + printf("== %d > %d ? %d\n", rr->recv_id, bp->ip.ap.str.id - 1, ring_gt(rr->recv_id, bp->ip.ap.str.id - 1)); if (ring_gt(rr->recv_id, bp->ip.ap.str.id - 1)) { // Packet has already been delivered or dropped, we free the buffer mv_buffer_wtor (app_ctx, fdinfo, bp); @@ -127,6 +129,7 @@ void rr_pkt_register(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, } // 3. If packet arrived too early, we register a timer + printf("== %d < %d ? %d\n", rr->recv_id, bp->ip.ap.str.id - 1, ring_lt(rr->recv_id, bp->ip.ap.str.id - 1)); if (ring_lt(rr->recv_id, bp->ip.ap.str.id - 1)) { int64_t timeout = rr->mjit - (int64_t) bp->ip.ap.str.deltat; if (timeout <= 0) timeout = 0; @@ -362,6 +365,8 @@ int rr_on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { struct algo_ctx* app_ctx = fdinfo->cat->app_ctx; struct rr_ctx* rr = app_ctx->misc; + printf("Timer has been triggered\n"); + struct waited_pkt* pkt = fdinfo->other; pkt->on = 0; evt_core_rm_fd(ctx, fdinfo->fd); diff --git a/src/utils.c b/src/utils.c index 281238c..24e98b8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -61,22 +61,22 @@ int ring_buffer_used_space(struct ring_buffer* rb) { return RING_BUFFER_SIZE - ring_buffer_free_space (rb); } -int ring_gt(uint16_t v1, uint16_t v2) { - int32_t vv1 = (int32_t) v1, vv2 = (int32_t) v2; - return v1 != v2 && (vv1 - vv2) % UINT16_MAX <= UINT16_MAX / 2; -} - +// Why we are using modulo, plus and modulo again: +// https://stackoverflow.com/a/1907585 int ring_ge(uint16_t v1, uint16_t v2) { - int32_t vv1 = (int32_t) v1, vv2 = (int32_t) v2; - return (vv1 - vv2) % UINT16_MAX <= UINT16_MAX / 2; + uint32_t vv1 = (uint32_t) v1, vv2 = (uint32_t) v2; + return (((vv1 - vv2) % UINT16_MAX) + UINT16_MAX) % UINT16_MAX <= UINT16_MAX / 2; } -int ring_lt(uint16_t v1, uint16_t v2) { - int32_t vv1 = (int32_t) v1, vv2 = (int32_t) v2; - return v1 != v2 && (vv1 - vv2) % UINT16_MAX > UINT16_MAX / 2; +int ring_gt(uint16_t v1, uint16_t v2) { + if (v1 == v2) return 0; + return ring_ge(v1,v2); } int ring_le(uint16_t v1, uint16_t v2) { - int32_t vv1 = (int32_t) v1, vv2 = (int32_t) v2; - return (vv1 - vv2) % UINT16_MAX > UINT16_MAX / 2; + return ring_gt(v2, v1); +} + +int ring_lt(uint16_t v1, uint16_t v2) { + return ring_ge(v2, v1); }