/* Drop hook. * * Author: Steffen Vogel * SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University * SPDX-License-Identifier: Apache-2.0 */ #include #include #include 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 p; } // namespace node } // namespace villas