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

89 lines
1.5 KiB
C++
Raw Normal View History

/* Drop hook.
2019-03-26 15:33:47 +01:00
*
* 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
*/
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-03-26 15:33:47 +01:00
{
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-03-26 15:33:47 +01:00
{
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-03-26 15:33:47 +01:00
{
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