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/hook.c

251 lines
5.6 KiB
C
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2017-04-27 13:08:17 +02:00
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @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
*/
#include <stdio.h>
#include <unistd.h>
#include <villas/timing.h>
#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>
#include <villas/utils.h>
#include <villas/pool.h>
#include <villas/log.h>
#include <villas/plugin.h>
2017-12-09 02:19:28 +08:00
#include <villas/config_helper.h>
#include <villas/kernel/rt.h>
#include <villas/config.h>
int cnt;
2017-08-05 21:02:09 +02:00
struct sample **smps;
struct log l = { .state = STATE_DESTROYED };
struct pool q = { .state = STATE_DESTROYED };
struct hook h = { .state = STATE_DESTROYED };
2017-08-05 21:02:09 +02:00
struct io io = { .state = STATE_DESTROYED };
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
int ret;
ret = hook_stop(&h);
if (ret)
error("Failed to stop hook");
ret = hook_destroy(&h);
if (ret)
error("Failed to destroy hook");
2018-05-13 13:53:37 +02:00
ret = io_close(&io);
if (ret)
error("Failed to close IO");
2018-05-12 15:25:29 +02:00
ret = io_destroy(&io);
if (ret)
error("Failed to destroy IO");
sample_free_many(smps, cnt);
ret = pool_destroy(&q);
if (ret)
error("Failed to destroy memory pool");
info(CLR_GRN("Goodbye!"));
exit(EXIT_SUCCESS);
}
static void usage()
{
printf("Usage: villas-hook [OPTIONS] NAME [[PARAM1] [PARAM2] ...]\n");
printf(" NAME the name of the hook function\n");
printf(" PARAM* a string of configuration settings for the hook\n");
printf(" OPTIONS is one or more of the following options:\n");
2017-08-05 21:02:09 +02:00
printf(" -f FMT the data format\n");
printf(" -d LVL set debug level to LVL\n");
2017-08-05 21:02:09 +02:00
printf(" -v CNT process CNT smps at once\n");
printf(" -h show this help\n");
2018-05-08 11:43:16 +02:00
printf(" -V show the version of the tool\n\n");
2017-08-05 21:02:09 +02:00
2017-03-27 12:26:11 +02:00
printf("The following hook functions are supported:\n");
plugin_dump(PLUGIN_TYPE_HOOK);
printf("\n");
2017-08-05 21:02:09 +02:00
printf("Supported IO formats:\n");
2018-05-12 13:56:12 +02:00
plugin_dump(PLUGIN_TYPE_FORMAT);
2017-08-05 21:02:09 +02:00
printf("\n");
printf("Example:");
printf(" villas-signal random | villas-hook skip_first seconds=10\n");
printf("\n");
print_copyright();
}
int main(int argc, char *argv[])
{
2018-03-28 14:17:44 +02:00
int ret, recv, sent;
char *format = "villas.human";
struct format_type *ft;
struct hook_type *ht;
/* Default values */
cnt = 1;
json_t *cfg_cli = json_object();
char c, *endptr;
while ((c = getopt(argc, argv, "Vhv:d:f:o:")) != -1) {
switch (c) {
case 'V':
print_version();
exit(EXIT_SUCCESS);
2017-08-05 21:02:09 +02:00
case 'f':
format = optarg;
break;
case 'v':
cnt = strtoul(optarg, &endptr, 0);
goto check;
case 'd':
l.level = strtoul(optarg, &endptr, 0);
goto check;
case 'o':
ret = json_object_extend_str(cfg_cli, optarg);
if (ret)
error("Invalid option: %s", optarg);
break;
case '?':
case 'h':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
error("Failed to parse parse option argument '-%c %s'", c, optarg);
}
if (argc < optind + 1) {
usage();
exit(EXIT_FAILURE);
}
2017-08-05 21:02:09 +02:00
char *hook = argv[optind];
ret = log_init(&l, l.level, LOG_ALL);
if (ret)
error("Failed to initialize log");
ret = log_open(&l);
2017-08-05 21:02:09 +02:00
if (ret)
error("Failed to start log");
ret = signals_init(quit);
if (ret)
error("Failed to intialize signals");
if (cnt < 1)
error("Vectorize option must be greater than 0");
2017-08-05 21:02:09 +02:00
ret = memory_init(DEFAULT_NR_HUGEPAGES);
if (ret)
error("Failed to initialize memory");
2017-08-05 21:02:09 +02:00
smps = alloc(cnt * sizeof(struct sample *));
ret = pool_init(&q, 10 * cnt, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
if (ret)
error("Failed to initilize memory pool");
2017-08-05 21:02:09 +02:00
/* Initialize IO */
ft = format_type_lookup(format);
if (!ft)
2017-08-05 21:02:09 +02:00
error("Unknown IO format '%s'", format);
ret = io_init(&io, ft, NULL, SAMPLE_HAS_ALL);
2017-08-05 21:02:09 +02:00
if (ret)
error("Failed to initialize IO");
ret = io_open(&io, NULL);
2017-08-05 21:02:09 +02:00
if (ret)
error("Failed to open IO");
/* Initialize hook */
ht = hook_type_lookup(hook);
if (!ht)
2017-08-05 21:02:09 +02:00
error("Unknown hook function '%s'", hook);
ret = hook_init(&h, ht, NULL, NULL);
2017-03-27 12:26:11 +02:00
if (ret)
error("Failed to initialize hook");
ret = hook_parse(&h, cfg_cli);
2017-03-27 12:26:11 +02:00
if (ret)
error("Failed to parse hook config");
2017-07-09 14:38:58 +02:00
ret = hook_start(&h);
if (ret)
error("Failed to start hook");
for (;;) {
ret = sample_alloc_many(&q, smps, cnt);
if (ret != cnt)
2017-08-05 21:02:09 +02:00
error("Failed to allocate %d smps from pool", cnt);
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) {
if (io_eof(&io)) {
killme(SIGTERM);
pause();
}
error("Failed to read from stdin");
}
2017-03-20 09:04:23 -03:00
debug(15, "Read %u smps from stdin", recv);
2018-03-28 14:17:44 +02:00
unsigned send = recv;
hook_read(&h, smps, (unsigned *) &send);
hook_process(&h, smps, (unsigned *) &send);
hook_write(&h, smps, (unsigned *) &send);
2018-03-28 14:17:44 +02:00
sent = io_print(&io, smps, send);
if (sent < 0)
error("Failed to write to stdout");
sample_free_many(smps, cnt);
}
return 0;
}