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>
|
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*
|
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/hooks.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/log.h>
|
2016-11-20 02:45:39 -05:00
|
|
|
|
|
|
|
#include "config.h"
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
static void usage()
|
|
|
|
{
|
|
|
|
printf("Usage: villas-hook [OPTIONS] NAME [PARAMETER] \n");
|
|
|
|
printf(" NAME the name of the hook function to run\n");
|
|
|
|
printf(" PARAM the name of the node to which samples are sent and received from\n\n");
|
|
|
|
printf(" OPTIONS are:\n");
|
|
|
|
printf(" -h show this help\n");
|
|
|
|
printf(" -d lvl set debug level\n");
|
|
|
|
printf(" -v process multiple samples at once\n\n");
|
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2016-11-20 12:59:37 -05:00
|
|
|
int j, ret, cnt = 1, level = -1;
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
char *name, *parameter;
|
|
|
|
|
|
|
|
struct hook *h;
|
|
|
|
struct hook_info *i = alloc(sizeof(struct hook_info));
|
|
|
|
|
|
|
|
char c;
|
|
|
|
while ((c = getopt(argc, argv, "hv:d:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
cnt = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2016-11-20 12:59:37 -05:00
|
|
|
level = atoi(optarg);
|
2016-10-22 20:43:36 -04:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-30 22:05:29 -04:00
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc <= optind)
|
|
|
|
|
|
|
|
name = argc > optind ? argv[optind ] : NULL;
|
|
|
|
parameter = argc > optind + 1 ? argv[optind+1] : NULL;
|
|
|
|
|
|
|
|
if (argc > optind)
|
|
|
|
name = argv[optind];
|
|
|
|
else {
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > optind + 1)
|
|
|
|
parameter = argv[optind + 1];
|
|
|
|
|
|
|
|
h = list_lookup(&hooks, name);
|
|
|
|
if (!h)
|
|
|
|
error("Unknown hook function '%s'", argv[optind]);
|
|
|
|
|
|
|
|
if (cnt < 1)
|
|
|
|
error("Vectorize option must be greater than 0");
|
|
|
|
|
|
|
|
struct pool pool;
|
|
|
|
struct sample *smps[cnt];
|
2016-11-20 12:59:37 -05:00
|
|
|
|
|
|
|
info("Initialize logging system");
|
|
|
|
log_init(level, -1, NULL);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
ret = pool_init(&pool, 10 * cnt, SAMPLE_LEN(DEFAULT_VALUES), &memtype_hugepage);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initilize memory pool");
|
|
|
|
|
|
|
|
ret = sample_alloc(&pool, smps, cnt);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to allocate %u samples from pool", cnt);
|
|
|
|
|
|
|
|
h->parameter = parameter;
|
|
|
|
i->smps = smps;
|
|
|
|
|
|
|
|
h->cb(h, HOOK_INIT, i);
|
|
|
|
h->cb(h, HOOK_PARSE, i);
|
|
|
|
h->cb(h, HOOK_PATH_START, i);
|
|
|
|
|
|
|
|
while (!feof(stdin)) {
|
|
|
|
for (j = 0; j < cnt && !feof(stdin); j++)
|
|
|
|
sample_fscan(stdin, i->smps[j], NULL);
|
|
|
|
|
|
|
|
i->cnt = j;
|
|
|
|
i->cnt = h->cb(h, HOOK_READ, i);
|
|
|
|
i->cnt = h->cb(h, HOOK_WRITE, i);
|
|
|
|
|
|
|
|
for (j = 0; j < i->cnt; j++)
|
|
|
|
sample_fprint(stdout, i->smps[j], SAMPLE_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
h->cb(h, HOOK_PATH_STOP, i);
|
|
|
|
h->cb(h, HOOK_DESTROY, i);
|
|
|
|
|
|
|
|
sample_free(smps, cnt);
|
|
|
|
|
|
|
|
pool_destroy(&pool);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|