2019-03-26 15:33:47 +01:00
|
|
|
/** Drop hook.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2019-03-26 15:33:47 +01:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cinttypes>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class DropHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
sample *prev;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
virtual Hook::Reason process(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 */
|
2020-06-14 15:00:02 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
/** @} */
|