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/src/villas-hook.cpp

292 lines
6.9 KiB
C++
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 13:08:17 +02:00
* @license GNU General Public License (version 3)
*
2017-04-27 13:08:17 +02:00
* 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.
*
2017-04-27 13:08:17 +02:00
* 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.
*
2017-04-27 13:08:17 +02:00
* 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 tools Test and debug tools
* @{
2017-04-27 13:08:17 +02:00
*/
2018-07-03 21:04:28 +02:00
#include <iostream>
2018-10-20 14:20:51 +02:00
#include <atomic>
#include <unistd.h>
#include <villas/timing.h>
#include <villas/sample.h>
2017-08-05 21:02:09 +02:00
#include <villas/io.h>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/utils.hpp>
#include <villas/pool.h>
2018-10-20 14:20:51 +02:00
#include <villas/log.hpp>
2019-04-23 13:11:08 +02:00
#include <villas/colors.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/exceptions.hpp>
#include <villas/copyright.hpp>
2019-03-26 15:33:47 +01:00
#include <villas/plugin.hpp>
#include <villas/plugin.h>
2019-03-26 07:01:10 +01:00
#include <villas/config_helper.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/kernel/rt.hpp>
#include <villas/node/config.h>
2018-10-20 14:20:51 +02:00
using namespace villas;
2019-03-26 15:33:47 +01:00
using namespace villas::node;
using namespace villas::plugin;
2018-10-20 14:20:51 +02:00
static std::atomic<bool> stop(false);
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
2018-10-20 14:20:51 +02:00
stop = true;
}
static void usage()
{
2018-08-27 11:25:56 +02:00
std::cout << "Usage: villas-hook [OPTIONS] NAME [[PARAM1] [PARAM2] ...]" << std::endl
<< " NAME the name of the hook function" << std::endl
<< " PARAM* a string of configuration settings for the hook" << std::endl
<< " OPTIONS is one or more of the following options:" << std::endl
<< " -f FMT the data format" << std::endl
<< " -t DT the data-type format string" << std::endl
2018-08-27 11:25:56 +02:00
<< " -d LVL set debug level to LVL" << std::endl
<< " -v CNT process CNT smps at once" << std::endl
<< " -h show this help" << std::endl
<< " -V show the version of the tool" << std::endl << std::endl;
2018-07-03 21:04:28 +02:00
2019-04-05 22:10:46 +02:00
#ifdef WITH_HOOKS
2018-07-03 21:04:28 +02:00
std::cout << "Supported hooks:" << std::endl;
2019-03-26 15:33:47 +01:00
for (Plugin *p : Registry::lookup<HookFactory>())
std::cout << " - " << p->getName() << ": " << p->getDescription() << std::endl;
2018-07-03 21:04:28 +02:00
std::cout << std::endl;
2019-04-05 22:10:46 +02:00
#endif /* WITH_HOOKS */
2017-08-05 21:02:09 +02:00
2018-07-03 21:04:28 +02:00
std::cout << "Supported IO formats:" << std::endl;
2018-05-12 13:56:12 +02:00
plugin_dump(PLUGIN_TYPE_FORMAT);
2018-07-03 21:04:28 +02:00
std::cout << std::endl;
2017-08-05 21:02:09 +02:00
2018-10-20 14:20:51 +02:00
std::cout << "Example:" << std::endl
<< " villas-signal random | villas-hook skip_first seconds=10" << std::endl
<< std::endl;
print_copyright();
}
int main(int argc, char *argv[])
{
int ret, recv, sent, cnt;
2018-07-03 20:42:37 +02:00
const char *format = "villas.human";
const char *dtypes = "64f";
struct format_type *ft;
2018-10-20 14:20:51 +02:00
struct sample **smps;
Logger logger = logging.get("hook");
2019-03-22 13:45:04 +01:00
try {
2019-04-17 18:46:18 +02:00
struct pool p;
struct io io;
p.state = STATE_DESTROYED;
io.state = STATE_DESTROYED;
2019-03-22 13:45:04 +01:00
/* Default values */
cnt = 1;
json_t *cfg_cli = json_object();
/* Parse optional command line arguments */
int c;
char *endptr;
while ((c = getopt(argc, argv, "Vhv:d:f:t:o:")) != -1) {
switch (c) {
case 'V':
print_version();
exit(EXIT_SUCCESS);
case 'f':
format = optarg;
break;
case 't':
dtypes = optarg;
break;
case 'v':
cnt = strtoul(optarg, &endptr, 0);
goto check;
case 'd':
logging.setLevel(optarg);
break;
case 'o':
ret = json_object_extend_str(cfg_cli, optarg);
if (ret)
throw RuntimeError("Invalid option: {}", optarg);
break;
case '?':
case 'h':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc < optind + 1) {
usage();
exit(EXIT_FAILURE);
}
2017-08-05 21:02:09 +02:00
char *hook = argv[optind];
2017-08-05 21:02:09 +02:00
ret = utils::signals_init(quit);
if (ret)
throw RuntimeError("Failed to intialize signals");
2018-08-20 18:31:27 +02:00
if (cnt < 1)
throw RuntimeError("Vectorize option must be greater than 0");
2017-08-05 21:02:09 +02:00
ret = memory_init(DEFAULT_NR_HUGEPAGES);
if (ret)
throw RuntimeError("Failed to initialize memory");
smps = new struct sample*[cnt];
2017-03-27 12:26:11 +02:00
ret = pool_init(&p, 10 * cnt, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), &memory_hugepage);
if (ret)
throw RuntimeError("Failed to initilize memory pool");
/* Initialize IO */
ft = format_type_lookup(format);
if (!ft)
throw RuntimeError("Unknown IO format '{}'", format);
ret = io_init2(&io, ft, dtypes, SAMPLE_HAS_ALL);
if (ret)
throw RuntimeError("Failed to initialize IO");
2018-03-28 14:17:44 +02:00
ret = io_check(&io);
if (ret)
throw RuntimeError("Failed to validate IO configuration");
2017-03-20 09:04:23 -03:00
ret = io_open(&io, nullptr);
if (ret)
throw RuntimeError("Failed to open IO");
/* Initialize hook */
auto hf = plugin::Registry::lookup<HookFactory>(hook);
if (!hf)
throw RuntimeError("Unknown hook function '{}'", hook);
auto h = hf->make(nullptr, nullptr);
if (!h)
throw RuntimeError("Failed to initialize hook");
h->parse(cfg_cli);
h->check();
h->prepare(io.signals);
h->start();
while (!stop) {
ret = sample_alloc_many(&p, smps, cnt);
if (ret != cnt)
throw RuntimeError("Failed to allocate %d smps from pool", cnt);
recv = io_scan(&io, smps, cnt);
if (recv < 0) {
if (io_eof(&io))
2019-03-26 15:33:47 +01:00
break;
throw RuntimeError("Failed to read from stdin");
}
2019-05-21 17:33:03 +02:00
if (!(smp->flags & SAMPLE_HAS_TS_RECEIVED)){
timespec now = time_now();
smp->ts.received = now;
smp->flags |= SAMPLE_HAS_TS_RECEIVED;
}
logger->debug("Read {} smps from stdin", recv);
unsigned send = 0;
for (int processed = 0; processed < recv; processed++) {
struct sample *smp = smps[processed];
ret = h->process(smp);
switch (ret) {
case HOOK_ERROR:
throw RuntimeError("Failed to process samples");
2018-10-20 14:20:51 +02:00
case HOOK_OK:
smps[send++] = smp;
break;
2018-10-20 14:20:51 +02:00
case HOOK_SKIP_SAMPLE:
break;
2018-10-20 14:20:51 +02:00
case HOOK_STOP_PROCESSING:
goto stop;
}
}
stop: sent = io_print(&io, smps, send);
if (sent < 0)
throw RuntimeError("Failed to write to stdout");
2018-10-20 14:20:51 +02:00
sample_free_many(smps, cnt);
}
2018-10-20 14:20:51 +02:00
h->stop();
2018-10-20 14:20:51 +02:00
delete h;
2018-10-20 14:20:51 +02:00
ret = io_close(&io);
if (ret)
throw RuntimeError("Failed to close IO");
2018-10-20 14:20:51 +02:00
ret = io_destroy(&io);
if (ret)
throw RuntimeError("Failed to destroy IO");
2019-03-22 13:45:04 +01:00
sample_free_many(smps, cnt);
delete smps;
ret = pool_destroy(&p);
if (ret)
throw RuntimeError("Failed to destroy memory pool");
logger->info(CLR_GRN("Goodbye!"));
return 0;
2019-03-22 13:45:04 +01:00
}
catch (std::runtime_error &e) {
logger->error("{}", e.what());
return -1;
2019-03-22 13:45:04 +01:00
}
}