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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
1.6 KiB
C++
Raw Permalink 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 <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;
2019-03-26 15:33:47 +01:00
virtual void start() {
assert(state == State::PREPARED || state == State::STOPPED);
2019-03-26 15:33:47 +01:00
prev = nullptr;
2019-03-26 15:33:47 +01:00
state = State::STARTED;
}
2019-03-26 15:33:47 +01:00
virtual void stop() {
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (prev)
sample_decref(prev);
2019-03-26 15:33:47 +01:00
state = State::STOPPED;
}
2019-03-26 15:33:47 +01:00
virtual Hook::Reason process(struct Sample *smp) {
int dist;
2019-03-26 15:33:47 +01: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-03-26 15:33:47 +01:00
return Hook::Reason::SKIP_SAMPLE;
}
}
2019-03-26 15:33:47 +01:00
sample_incref(smp);
if (prev)
sample_decref(prev);
2019-03-26 15:33:47 +01:00
prev = smp;
2019-03-26 15:33:47 +01:00
return Reason::OK;
}
2019-03-26 15:33:47 +01:00
virtual void restart() {
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (prev) {
sample_decref(prev);
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