1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/lib/hooks/drop.cpp
Steffen Vogel 0735eb0f89 Make project REUSE compliant
And various other cleanups and harmonizations

Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
2023-09-07 11:16:04 +02:00

88 lines
1.5 KiB
C++

/* Drop hook.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cinttypes>
#include <villas/hook.hpp>
#include <villas/sample.hpp>
namespace villas {
namespace node {
class DropHook : public Hook {
protected:
struct Sample *prev;
public:
using Hook::Hook;
virtual
void start()
{
assert(state == State::PREPARED || state == State::STOPPED);
prev = nullptr;
state = State::STARTED;
}
virtual
void stop()
{
assert(state == State::STARTED);
if (prev)
sample_decref(prev);
state = State::STOPPED;
}
virtual
Hook::Reason process(struct Sample *smp)
{
int dist;
assert(state == State::STARTED);
if (prev) {
dist = smp->sequence - (int64_t) prev->sequence;
if (dist <= 0) {
logger->debug("Dropping reordered sample: sequence={}, distance={}", smp->sequence, dist);
return Hook::Reason::SKIP_SAMPLE;
}
}
sample_incref(smp);
if (prev)
sample_decref(prev);
prev = smp;
return Reason::OK;
}
virtual
void restart()
{
assert(state == State::STARTED);
if (prev) {
sample_decref(prev);
prev = nullptr;
}
}
};
// Register hook
static char n[] = "drop";
static char d[] = "Drop messages with reordered sequence numbers";
static HookPlugin<DropHook, n, d, (int) Hook::Flags::BUILTIN | (int) Hook::Flags::NODE_READ, 3> p;
} // namespace node
} // namespace villas