1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/hooks/drop.cpp

85 lines
1.5 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Drop hook.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2019-03-26 15:33:47 +01:00
*********************************************************************************/
#include <cinttypes>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
#include <villas/sample.hpp>
2019-03-26 15:33:47 +01:00
namespace villas {
namespace node {
class DropHook : public Hook {
protected:
struct Sample *prev;
2019-03-26 15:33:47 +01:00
public:
using Hook::Hook;
virtual void start()
{
2019-06-23 16:13:23 +02:00
assert(state == State::PREPARED || state == State::STOPPED);
2019-03-26 15:33:47 +01:00
2019-04-07 13:23:43 +02:00
prev = nullptr;
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::STARTED;
2019-03-26 15:33:47 +01:00
}
virtual void stop()
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (prev)
sample_decref(prev);
2019-06-23 16:13:23 +02:00
state = State::STOPPED;
2019-03-26 15:33:47 +01:00
}
virtual Hook::Reason process(struct Sample *smp)
2019-03-26 15:33:47 +01:00
{
int dist;
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (prev) {
dist = smp->sequence - (int64_t) prev->sequence;
if (dist <= 0) {
logger->debug("Dropping reordered sample: sequence={}, distance={}", smp->sequence, dist);
2019-06-23 16:13:23 +02:00
return Hook::Reason::SKIP_SAMPLE;
2019-03-26 15:33:47 +01:00
}
}
sample_incref(smp);
if (prev)
sample_decref(prev);
prev = smp;
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-03-26 15:33:47 +01:00
}
virtual void restart()
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (prev) {
sample_decref(prev);
2019-04-07 13:23:43 +02:00
prev = nullptr;
2019-03-26 15:33:47 +01:00
}
}
};
/* 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;
2019-03-26 15:33:47 +01:00
} /* namespace node */
} /* namespace villas */