2016-10-22 20:43:36 -04:00
|
|
|
/** Receive messages from server snd print them on stdout.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2018-08-20 18:39:04 +02:00
|
|
|
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 13:08:17 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
2016-10-22 20:43:36 -04:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2016-10-22 20:43:36 -04:00
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
2017-04-27 13:08:17 +02:00
|
|
|
*/
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2018-07-03 21:04:28 +02:00
|
|
|
#include <iostream>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <atomic>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-03-20 09:03:48 -03:00
|
|
|
#include <villas/timing.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/sample.h>
|
2017-08-05 21:02:09 +02:00
|
|
|
#include <villas/io.h>
|
2016-11-20 13:11:37 -05:00
|
|
|
#include <villas/hook.h>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/utils.hpp>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/pool.h>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/exceptions.hpp>
|
2017-03-05 10:06:32 -04:00
|
|
|
#include <villas/plugin.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/config_helper.h>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/kernel/rt.hpp>
|
2018-08-23 17:31:01 +02:00
|
|
|
#include <villas/node/config.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
using namespace villas;
|
|
|
|
|
2017-07-13 22:15:29 +02:00
|
|
|
int cnt;
|
2017-03-20 09:03:03 -03:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
static std::atomic<bool> stop(false);
|
|
|
|
|
|
|
|
static Logger logger = logging.get("hook");
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2018-08-23 13:32:06 +02:00
|
|
|
struct pool p = { .state = STATE_DESTROYED };
|
2017-07-13 22:15:29 +02:00
|
|
|
struct hook h = { .state = STATE_DESTROYED };
|
2017-08-05 21:02:09 +02:00
|
|
|
struct io io = { .state = STATE_DESTROYED };
|
2017-07-13 22:15:29 +02:00
|
|
|
|
|
|
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
2017-03-17 02:52:59 -03:00
|
|
|
{
|
2018-10-20 14:20:51 +02:00
|
|
|
stop = true;
|
2017-03-17 02:52:59 -03:00
|
|
|
}
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
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
|
|
|
|
<< " -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
|
|
|
|
|
|
|
std::cout << "Supported hooks:" << std::endl;
|
2017-03-27 12:26:11 +02:00
|
|
|
plugin_dump(PLUGIN_TYPE_HOOK);
|
2018-07-03 21:04:28 +02:00
|
|
|
std::cout << std::endl;
|
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;
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
utils::print_copyright();
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-03-28 14:17:44 +02:00
|
|
|
int ret, recv, sent;
|
2018-07-03 20:42:37 +02:00
|
|
|
const char *format = "villas.human";
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
struct format_type *ft;
|
|
|
|
struct hook_type *ht;
|
2018-10-20 14:20:51 +02:00
|
|
|
struct sample **smps;
|
2018-05-13 13:52:02 +02:00
|
|
|
|
2017-03-14 11:19:59 -03:00
|
|
|
/* Default values */
|
|
|
|
cnt = 1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_t *cfg_cli = json_object();
|
|
|
|
|
2018-11-30 19:57:22 +01:00
|
|
|
int c;
|
|
|
|
char *endptr;
|
2017-09-16 11:52:30 +02:00
|
|
|
while ((c = getopt(argc, argv, "Vhv:d:f:o:")) != -1) {
|
2016-10-22 20:43:36 -04:00
|
|
|
switch (c) {
|
2017-09-16 11:52:30 +02:00
|
|
|
case 'V':
|
2018-10-20 14:20:51 +02:00
|
|
|
utils::print_version();
|
2017-09-16 11:52:30 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2018-08-17 11:29:28 +02:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
case 'f':
|
|
|
|
format = optarg;
|
2016-10-22 20:43:36 -04:00
|
|
|
break;
|
2018-08-17 11:29:28 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
case 'v':
|
|
|
|
cnt = strtoul(optarg, &endptr, 0);
|
|
|
|
goto check;
|
2018-08-17 11:29:28 +02:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
case 'd':
|
2018-10-20 14:20:51 +02:00
|
|
|
logging.setLevel(optarg);
|
|
|
|
break;
|
2018-08-17 11:29:28 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
case 'o':
|
|
|
|
ret = json_object_extend_str(cfg_cli, optarg);
|
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Invalid option: {}", optarg);
|
2016-10-22 20:43:36 -04:00
|
|
|
break;
|
2018-08-17 11:29:28 +02:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
case '?':
|
2017-08-03 00:19:27 +02:00
|
|
|
case 'h':
|
2016-10-30 22:05:29 -04:00
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
2017-03-12 17:10:04 -03:00
|
|
|
|
2017-03-14 11:18:12 -03:00
|
|
|
if (argc < optind + 1) {
|
2017-03-12 17:10:04 -03:00
|
|
|
usage();
|
2016-10-22 20:43:36 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
char *hook = argv[optind];
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
ret = utils::signals_init(quit);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to intialize signals");
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
if (cnt < 1)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Vectorize option must be greater than 0");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
ret = memory_init(DEFAULT_NR_HUGEPAGES);
|
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to initialize memory");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
smps = new struct sample*[cnt];
|
2017-07-13 22:15:29 +02:00
|
|
|
|
2018-08-23 13:32:06 +02:00
|
|
|
ret = pool_init(&p, 10 * cnt, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), &memory_hugepage);
|
2016-10-22 20:43:36 -04:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to initilize memory pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
/* Initialize IO */
|
2018-05-13 13:52:02 +02:00
|
|
|
ft = format_type_lookup(format);
|
|
|
|
if (!ft)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Unknown IO format '{}'", format);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
ret = io_init_auto(&io, ft, DEFAULT_SAMPLE_LENGTH, SAMPLE_HAS_ALL);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to initialize IO");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
ret = io_check(&io);
|
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to validate IO configuration");
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
ret = io_open(&io, nullptr);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to open IO");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
|
|
|
/* Initialize hook */
|
2018-05-13 13:52:02 +02:00
|
|
|
ht = hook_type_lookup(hook);
|
|
|
|
if (!ht)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Unknown hook function '{}'", hook);
|
2017-03-20 09:03:03 -03:00
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
ret = hook_init(&h, ht, nullptr, nullptr);
|
2017-03-27 12:26:11 +02:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to initialize hook");
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = hook_parse(&h, cfg_cli);
|
2017-03-27 12:26:11 +02:00
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to parse hook config");
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-07-09 14:38:58 +02:00
|
|
|
ret = hook_start(&h);
|
|
|
|
if (ret)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to start hook");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
while (!stop) {
|
2018-08-23 13:32:06 +02:00
|
|
|
ret = sample_alloc_many(&p, smps, cnt);
|
2017-03-14 11:21:05 -03:00
|
|
|
if (ret != cnt)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to allocate %d smps from pool", cnt);
|
2017-03-14 11:21:05 -03:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
recv = io_scan(&io, smps, cnt);
|
2018-03-28 14:17:44 +02:00
|
|
|
if (recv < 0) {
|
2018-10-20 14:20:51 +02:00
|
|
|
if (io_eof(&io))
|
|
|
|
break;
|
2018-03-28 14:17:44 +02:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to read from stdin");
|
2018-03-28 14:17:44 +02:00
|
|
|
}
|
2017-03-20 09:04:23 -03:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
logger->debug("Read {} smps from stdin", recv);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-03-28 14:17:44 +02:00
|
|
|
unsigned send = recv;
|
|
|
|
|
|
|
|
hook_process(&h, smps, (unsigned *) &send);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-03-28 14:17:44 +02:00
|
|
|
sent = io_print(&io, smps, send);
|
|
|
|
if (sent < 0)
|
2018-10-20 14:20:51 +02:00
|
|
|
throw new RuntimeError("Failed to write to stdout");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
sample_free_many(smps, cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
ret = hook_stop(&h);
|
|
|
|
if (ret)
|
|
|
|
throw new RuntimeError("Failed to stop hook");
|
|
|
|
|
|
|
|
ret = hook_destroy(&h);
|
|
|
|
if (ret)
|
|
|
|
throw new RuntimeError("Failed to destroy hook");
|
|
|
|
|
|
|
|
ret = io_close(&io);
|
|
|
|
if (ret)
|
|
|
|
throw new RuntimeError("Failed to close IO");
|
|
|
|
|
|
|
|
ret = io_destroy(&io);
|
|
|
|
if (ret)
|
|
|
|
throw new RuntimeError("Failed to destroy IO");
|
|
|
|
|
|
|
|
sample_free_many(smps, cnt);
|
|
|
|
|
|
|
|
delete smps;
|
|
|
|
|
|
|
|
ret = pool_destroy(&p);
|
|
|
|
if (ret)
|
|
|
|
throw new RuntimeError("Failed to destroy memory pool");
|
|
|
|
|
|
|
|
logger->info(CLR_GRN("Goodbye!"));
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
return 0;
|
2017-07-13 22:15:29 +02:00
|
|
|
}
|