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

310 lines
7.2 KiB
C
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* 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 12:56:43 +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 12:56:43 +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
* @{
*********************************************************************************/
#include <stdlib.h>
2016-06-08 22:38:21 +02:00
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <villas/super_node.h>
2016-06-26 15:33:04 +02:00
#include <villas/utils.h>
#include <villas/node.h>
#include <villas/msg.h>
#include <villas/timing.h>
#include <villas/pool.h>
#include <villas/sample_io.h>
2016-06-26 15:33:04 +02:00
#include <villas/kernel/rt.h>
#include <villas/nodes/websocket.h>
2016-06-26 15:33:04 +02:00
#include "config.h"
2017-04-15 20:16:51 +02:00
static struct super_node sn = { .state = STATE_DESTROYED }; /**< The global configuration */
2016-06-26 15:33:04 +02:00
struct dir {
struct pool pool;
pthread_t thread;
bool enabled;
int limit;
2016-06-26 15:33:04 +02:00
} sendd, recvv;
struct node *node;
2016-06-26 15:33:04 +02:00
pthread_t ptid; /**< Parent thread id */
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
if (signal == SIGALRM)
info("Reached timeout. Terminating...");
if (recvv.enabled) {
2016-06-26 15:33:04 +02:00
pthread_cancel(recvv.thread);
pthread_join(recvv.thread, NULL);
pool_destroy(&recvv.pool);
2016-06-26 15:33:04 +02:00
}
if (sendd.enabled) {
2016-06-26 15:33:04 +02:00
pthread_cancel(sendd.thread);
pthread_join(sendd.thread, NULL);
pool_destroy(&sendd.pool);
2016-06-26 15:33:04 +02:00
}
2017-07-02 23:53:48 +02:00
super_node_stop(&sn);
super_node_destroy(&sn);
2016-06-26 15:33:04 +02:00
info(GRN("Goodbye!"));
exit(EXIT_SUCCESS);
}
2016-10-22 20:37:02 -04:00
static void usage()
{
printf("Usage: villas-pipe [OPTIONS] CONFIG NODE\n");
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");
2017-07-02 22:15:07 +02:00
printf(" -t NUM terminate after NUM seconds\n");
printf(" -L NUM terminate after NUM samples sent\n");
printf(" -l NUM terminate after NUM samples received\n\n");
print_copyright();
}
2016-06-26 15:33:04 +02:00
static void * send_loop(void *ctx)
2016-06-08 22:38:21 +02:00
{
int ret, cnt = 0;
2016-06-08 22:38:21 +02:00
struct sample *smps[node->vectorize];
2016-06-26 15:33:04 +02:00
2016-06-08 22:38:21 +02:00
/* Initialize memory */
ret = pool_init(&sendd.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_SAMPLELEN), &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);
2017-04-15 20:16:51 +02:00
while (!feof(stdin)) {
int len;
for (len = 0; len < node->vectorize; len++) {
struct sample *s = smps[len];
int reason;
if (sendd.limit > 0 && cnt >= sendd.limit)
break;
retry: reason = sample_io_villas_fscan(stdin, s, NULL);
if (reason < 0) {
2017-04-15 20:16:51 +02:00
if (feof(stdin))
goto leave;
else {
warn("Skipped invalid message message: reason=%d", reason);
goto retry;
}
}
}
cnt += node_write(node, smps, len);
if (sendd.limit > 0 && cnt >= sendd.limit)
goto leave2;
pthread_testcancel();
}
leave2: info("Reached send limit. Terminating...");
2017-04-15 20:16:51 +02:00
pthread_kill(ptid, SIGINT);
2016-06-26 15:33:04 +02:00
return NULL;
/* We reached EOF on stdin here. Lets kill the process */
leave: if (recvv.limit < 0) {
info("Reached end-of-file. Terminating...");
pthread_kill(ptid, SIGINT);
}
return NULL;
}
2016-06-26 15:33:04 +02:00
static void * recv_loop(void *ctx)
{
int ret, cnt = 0;
2016-06-08 22:38:21 +02:00
struct sample *smps[node->vectorize];
2016-06-08 22:38:21 +02:00
/* Initialize memory */
ret = pool_init(&recvv.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_SAMPLELEN), &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
/* 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);
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();
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;
sample_io_villas_fprint(stdout, s, SAMPLE_IO_ALL);
2015-12-04 01:54:33 +01:00
fflush(stdout);
}
cnt += recv;
if (recvv.limit > 0 && cnt >= recvv.limit)
goto leave;
pthread_testcancel();
}
2016-09-10 22:19:07 -04:00
leave: info("Reached receive limit. Terminating...");
pthread_kill(ptid, SIGINT);
return NULL;
return NULL;
}
int main(int argc, char *argv[])
{
2017-07-02 22:15:07 +02:00
int ret, level = V, timeout = 0;
bool reverse = false;
2016-06-26 15:33:04 +02:00
sendd = recvv = (struct dir) {
.enabled = true,
.limit = -1
};
2016-06-26 15:33:04 +02:00
ptid = pthread_self();
char c, *endptr;
while ((c = getopt(argc, argv, "hxrsd:l:L:t:")) != -1) {
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;
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':
level = strtoul(optarg, &endptr, 10);
goto check;
case 'l':
recvv.limit = strtoul(optarg, &endptr, 10);
goto check;
case 'L':
sendd.limit = strtoul(optarg, &endptr, 10);
goto check;
case 't':
2017-07-02 22:15:07 +02:00
timeout = strtoul(optarg, &endptr, 10);
goto check;
case 'h':
case '?':
2016-10-22 20:37:02 -04:00
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 + 2) {
usage();
exit(EXIT_FAILURE);
}
char *configfile = argv[optind];
char *nodestr = argv[optind+1];
log_init(&sn.log, level, LOG_ALL);
log_start(&sn.log);
super_node_init(&sn);
super_node_parse_uri(&sn, configfile);
memory_init(sn.hugepages);
2017-03-06 19:09:44 -04:00
signals_init(quit);
rt_init(sn.priority, sn.affinity);
/* Initialize node */
node = list_lookup(&sn.nodes, nodestr);
if (!node)
error("Node '%s' does not exist!", nodestr);
2016-09-10 22:19:07 -04:00
if (node->_vt->start == websocket_start) {
web_start(&sn.web);
2017-04-07 17:35:55 +02:00
api_start(&sn.api);
}
if (reverse)
node_reverse(node);
2016-06-08 22:38:21 +02:00
ret = node_type_start(node->_vt, &sn);
2016-06-26 15:33:04 +02:00
if (ret)
2017-05-22 20:10:12 +02:00
error("Failed to intialize node type: %s", node_type_name(node->_vt));
ret = node_check(node);
if (ret)
error("Invalid node configuration");
2016-06-26 15:33:04 +02:00
ret = node_start(node);
if (ret)
error("Failed to start node: %s", node_name(node));
2016-06-08 22:38:21 +02:00
/* Start threads */
if (recvv.enabled)
pthread_create(&recvv.thread, NULL, recv_loop, NULL);
if (sendd.enabled)
pthread_create(&sendd.thread, NULL, send_loop, NULL);
2016-06-26 15:33:04 +02:00
2017-07-02 22:15:07 +02:00
alarm(timeout);
2017-04-07 17:39:37 +02:00
for (;;)
pause();
return 0;
}
/** @} */