2015-11-29 21:27:26 +01:00
|
|
|
/** Receive messages from server snd print them on stdout.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2016-06-08 23:21:42 +02:00
|
|
|
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
|
2015-11-29 21:27:26 +01:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*
|
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <stdbool.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
#include <villas/cfg.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/msg.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/kernel/rt.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
#include "config.h"
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-07-14 09:47:00 +02:00
|
|
|
static struct list nodes; /**< List of all nodes */
|
|
|
|
static struct settings settings; /**< The global configuration */
|
|
|
|
static config_t config;
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
struct dir {
|
|
|
|
struct pool pool;
|
|
|
|
pthread_t thread;
|
|
|
|
bool enabled;
|
|
|
|
bool started;
|
|
|
|
} sendd, recvv;
|
|
|
|
|
|
|
|
bool reverse = false;
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
struct node *node;
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
pthread_t ptid; /**< Parent thread id */
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
2016-06-26 15:33:04 +02:00
|
|
|
if (recvv.started) {
|
|
|
|
pthread_cancel(recvv.thread);
|
|
|
|
pthread_join(recvv.thread, NULL);
|
|
|
|
}
|
2016-01-15 15:34:28 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
if (sendd.started) {
|
|
|
|
pthread_cancel(sendd.thread);
|
|
|
|
pthread_join(sendd.thread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
pool_destroy(&recvv.pool);
|
|
|
|
pool_destroy(&sendd.pool);
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
node_stop(node);
|
2015-12-13 00:45:20 +01:00
|
|
|
node_deinit(node->_vt);
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2016-04-16 19:54:26 +02:00
|
|
|
list_destroy(&nodes, (dtor_cb_t) node_destroy, false);
|
2016-07-14 09:47:00 +02:00
|
|
|
cfg_destroy(&config);
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
info(GRN("Goodbye!"));
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
static void usage()
|
2015-11-29 21:27:26 +01:00
|
|
|
{
|
2016-10-22 20:37:02 -04:00
|
|
|
printf("Usage: villas-pipe CONFIG NODE [OPTIONS]\n");
|
2015-11-29 21:27:26 +01:00
|
|
|
printf(" CONFIG path to a configuration file\n");
|
|
|
|
printf(" NODE the name of the node to which samples are sent and received from\n");
|
2016-06-26 15:33:04 +02:00
|
|
|
printf(" OPTIONS are:\n");
|
|
|
|
printf(" -d LVL set debug log level to LVL\n");
|
|
|
|
printf(" -x swap read / write endpoints\n");
|
|
|
|
printf(" -s only read data from stdin and send it to node\n");
|
|
|
|
printf(" -r only read data from node and write it to stdout\n\n");
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-01-15 15:25:22 +01:00
|
|
|
print_copyright();
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * send_loop(void *ctx)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sample *smps[node->vectorize];
|
2016-06-26 15:33:04 +02:00
|
|
|
|
|
|
|
if (!sendd.enabled)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sendd.started = true;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
/* Initialize memory */
|
2016-09-22 21:20:21 -04:00
|
|
|
ret = pool_init(&sendd.pool, SAMPLE_LEN(DEFAULT_VALUES), node->vectorize, &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
ret = sample_alloc(&sendd.pool, smps, node->vectorize);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to get %u samples out of send pool (%d).", node->vectorize, ret);
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
for (;;) {
|
2015-12-19 16:48:20 +01:00
|
|
|
for (int i = 0; i < node->vectorize; i++) {
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *s = smps[i];
|
2015-11-29 21:27:26 +01:00
|
|
|
int reason;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
retry: reason = sample_fscan(stdin, s, NULL);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reason < 0) {
|
|
|
|
if (feof(stdin))
|
2016-06-26 15:33:04 +02:00
|
|
|
goto killme;
|
2015-11-29 21:27:26 +01:00
|
|
|
else {
|
|
|
|
warn("Skipped invalid message message: reason=%d", reason);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
node_write(node, smps, node->vectorize);
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
killme: pthread_kill(ptid, SIGINT);
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * recv_loop(void *ctx)
|
2015-11-29 21:27:26 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
|
|
|
struct sample *smps[node->vectorize];
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
if (!recvv.enabled)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
recvv.started = true;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Initialize memory */
|
2016-09-22 21:20:21 -04:00
|
|
|
ret = pool_init(&recvv.pool, SAMPLE_LEN(DEFAULT_VALUES), node->vectorize, &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
ret = sample_alloc(&recvv.pool, smps, node->vectorize);
|
2016-09-10 22:19:07 -04:00
|
|
|
if (ret < 0)
|
2016-06-08 22:38:21 +02:00
|
|
|
error("Failed to get %u samples out of receive pool (%d).", node->vectorize, ret);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Print header */
|
2016-06-08 22:38:21 +02:00
|
|
|
fprintf(stdout, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]");
|
2016-06-26 15:33:04 +02:00
|
|
|
fflush(stdout);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2016-06-08 22:38:21 +02:00
|
|
|
int recv = node_read(node, smps, node->vectorize);
|
2016-09-14 03:32:31 +02:00
|
|
|
struct timespec now = time_now();
|
2015-11-29 21:27:26 +01:00
|
|
|
for (int i = 0; i < recv; i++) {
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *s = smps[i];
|
|
|
|
|
2016-09-14 03:32:31 +02:00
|
|
|
if (s->ts.received.tv_sec == -1 || s->ts.received.tv_sec == 0)
|
|
|
|
s->ts.received = now;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
sample_fprint(stdout, s, SAMPLE_ALL);
|
2015-12-04 01:54:33 +01:00
|
|
|
fflush(stdout);
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2016-06-26 15:33:04 +02:00
|
|
|
int ret;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
ptid = pthread_self();
|
|
|
|
log_init();
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Parse command line arguments */
|
2015-12-04 17:34:57 +01:00
|
|
|
if (argc < 3)
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2016-06-26 15:33:04 +02:00
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
sendd.enabled = true;
|
|
|
|
recvv.enabled = true;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
while ((c = getopt(argc-2, argv+2, "hxrsd:")) != -1) {
|
2015-11-29 21:27:26 +01:00
|
|
|
switch (c) {
|
2016-06-08 22:38:21 +02:00
|
|
|
case 'x':
|
|
|
|
reverse = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-06-26 15:33:04 +02:00
|
|
|
recvv.enabled = false; // send only
|
2016-06-08 22:38:21 +02:00
|
|
|
break;
|
2015-11-29 21:27:26 +01:00
|
|
|
case 'r':
|
2016-06-26 15:33:04 +02:00
|
|
|
sendd.enabled = false; // receive only
|
2016-06-08 22:38:21 +02:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
log_setlevel(atoi(optarg), -1);
|
2015-11-29 21:27:26 +01:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Setup signals */
|
|
|
|
struct sigaction sa_quit = {
|
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
.sa_sigaction = quit
|
|
|
|
};
|
|
|
|
|
|
|
|
sigemptyset(&sa_quit.sa_mask);
|
|
|
|
sigaction(SIGTERM, &sa_quit, NULL);
|
|
|
|
sigaction(SIGINT, &sa_quit, NULL);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Initialize log, configuration.. */
|
2016-04-16 19:54:26 +02:00
|
|
|
list_init(&nodes);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
info("Parsing configuration");
|
2016-07-14 09:47:00 +02:00
|
|
|
cfg_parse(argv[1], &config, &settings, &nodes, NULL);
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
info("Initialize real-time system");
|
|
|
|
rt_init(settings.affinity, settings.priority);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
/* Initialize node */
|
|
|
|
node = list_lookup(&nodes, argv[2]);
|
|
|
|
if (!node)
|
|
|
|
error("Node '%s' does not exist!", argv[2]);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reverse)
|
|
|
|
node_reverse(node);
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
ret = node_init(node->_vt, argc-optind, argv+optind, config_root_setting(&config));
|
|
|
|
if (ret)
|
|
|
|
error("Failed to intialize node: %s", node_name(node));
|
|
|
|
|
|
|
|
ret = node_start(node);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to start node: %s", node_name(node));
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Start threads */
|
2016-06-26 15:33:04 +02:00
|
|
|
pthread_create(&recvv.thread, NULL, recv_loop, NULL);
|
|
|
|
pthread_create(&sendd.thread, NULL, send_loop, NULL);
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
for (;;)
|
|
|
|
pause();
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|