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>
|
2016-11-20 13:11:37 -05:00
|
|
|
#include <villas/hook.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/log.h>
|
2017-03-05 10:06:32 -04:00
|
|
|
#include <villas/plugin.h>
|
|
|
|
|
2016-11-20 13:00:23 -05:00
|
|
|
#include <villas/kernel/rt.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");
|
2017-03-12 17:10:04 -03:00
|
|
|
printf(" -d LVL set debug level to LVL\n");
|
|
|
|
printf(" -v CNT process CNT samples at once\n\n");
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-03-14 11:19:59 -03:00
|
|
|
int j, ret, level, cnt;
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
level = V;
|
|
|
|
cnt = 1;
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
char *name, *parameter;
|
|
|
|
|
2017-03-14 11:18:49 -03:00
|
|
|
struct sample *samples[cnt];
|
2017-03-05 10:06:32 -04:00
|
|
|
struct log log;
|
2017-03-12 17:10:04 -03:00
|
|
|
struct pool pool = { .state = STATE_DESTROYED };
|
2017-03-14 11:18:49 -03:00
|
|
|
struct hook_info hi = { .smps = samples };
|
2017-03-05 10:06:32 -04:00
|
|
|
struct plugin *p;
|
2017-03-12 17:10:04 -03:00
|
|
|
struct hook *h;
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
char c;
|
|
|
|
while ((c = getopt(argc, argv, "hv:d:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
cnt = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2017-03-14 11:19:59 -03: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 11:19:59 -03:00
|
|
|
log_init(&log, level, LOG_ALL);
|
2017-03-12 17:10:04 -03:00
|
|
|
memory_init(DEFAULT_NR_HUGEPAGES);
|
|
|
|
|
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-03-12 17:10:04 -03:00
|
|
|
name = argv[optind];
|
2017-03-14 11:18:12 -03:00
|
|
|
parameter = argc >= optind + 2 ? argv[optind + 1] : NULL;
|
2017-03-12 17:10:04 -03:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
p = plugin_lookup(PLUGIN_TYPE_HOOK, name);
|
|
|
|
if (!p)
|
2016-10-22 20:43:36 -04:00
|
|
|
error("Unknown hook function '%s'", argv[optind]);
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
h = &p->hook;
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
if (cnt < 1)
|
|
|
|
error("Vectorize option must be greater than 0");
|
|
|
|
|
|
|
|
ret = pool_init(&pool, 10 * cnt, SAMPLE_LEN(DEFAULT_VALUES), &memtype_hugepage);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initilize memory pool");
|
|
|
|
|
|
|
|
h->parameter = parameter;
|
|
|
|
|
2017-03-14 11:17:40 -03:00
|
|
|
if (h->type & HOOK_INIT)
|
|
|
|
h->cb(h, HOOK_INIT, &hi);
|
|
|
|
if (h->type & HOOK_PARSE)
|
|
|
|
h->cb(h, HOOK_PARSE, &hi);
|
|
|
|
if (h->type & HOOK_PATH_START)
|
|
|
|
h->cb(h, HOOK_PATH_START, &hi);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
while (!feof(stdin)) {
|
2017-03-14 11:21:05 -03:00
|
|
|
ret = sample_alloc(&pool, samples, cnt);
|
|
|
|
if (ret != cnt)
|
|
|
|
error("Failed to allocate %u samples from pool", cnt);
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
for (j = 0; j < cnt && !feof(stdin); j++)
|
2017-03-12 17:10:04 -03:00
|
|
|
sample_fscan(stdin, hi.smps[j], NULL);
|
|
|
|
|
|
|
|
debug(15, "Read %d samples from stdin", cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-12 17:10:04 -03:00
|
|
|
hi.cnt = j;
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-12 17:10:04 -03:00
|
|
|
if (h->type & HOOK_READ)
|
|
|
|
hi.cnt = h->cb(h, HOOK_READ, &hi);
|
|
|
|
if (h->type & HOOK_WRITE)
|
|
|
|
hi.cnt = h->cb(h, HOOK_WRITE, &hi);
|
|
|
|
|
|
|
|
for (j = 0; j < hi.cnt; j++)
|
|
|
|
sample_fprint(stdout, hi.smps[j], SAMPLE_ALL);
|
2017-03-14 11:21:05 -03:00
|
|
|
|
|
|
|
sample_free(samples, cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
|
|
|
|
2017-03-14 11:17:40 -03:00
|
|
|
if (h->type & HOOK_PATH_STOP)
|
|
|
|
h->cb(h, HOOK_PATH_STOP, &hi);
|
|
|
|
if (h->type & HOOK_DESTROY)
|
|
|
|
h->cb(h, HOOK_DESTROY, &hi);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-12 17:10:04 -03:00
|
|
|
sample_free(samples, cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
pool_destroy(&pool);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|