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

277 lines
6.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;
bool started;
} sendd, recvv;
bool reverse = false;
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)
{
2017-04-07 17:35:55 +02:00
int ret;
2016-06-26 15:33:04 +02:00
if (recvv.started) {
pthread_cancel(recvv.thread);
pthread_join(recvv.thread, NULL);
pool_destroy(&recvv.pool);
2016-06-26 15:33:04 +02:00
}
2016-06-26 15:33:04 +02:00
if (sendd.started) {
pthread_cancel(sendd.thread);
pthread_join(sendd.thread, NULL);
pool_destroy(&sendd.pool);
2016-06-26 15:33:04 +02:00
}
2017-04-07 17:35:55 +02:00
ret = super_node_stop(&sn);
if (ret)
error("Failed to stop super-node");
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\n");
print_copyright();
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;
2016-06-26 15:33:04 +02:00
sendd.started = true;
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;
retry: reason = sample_io_villas_fscan(stdin, s, NULL);
if (reason < 0) {
2017-04-15 20:16:51 +02:00
if (feof(stdin))
goto exit;
else {
warn("Skipped invalid message message: reason=%d", reason);
goto retry;
}
}
}
2017-04-15 20:16:51 +02:00
node_write(node, smps, len);
pthread_testcancel();
}
2017-04-15 20:16:51 +02:00
/* We reached EOF on stdin here. Lets kill the process */
exit: info("Reached end-of-file. Terminating...");
2017-04-15 20:16:51 +02:00
pthread_kill(ptid, SIGINT);
2016-06-26 15:33:04 +02:00
return NULL;
}
2016-06-26 15:33:04 +02:00
static void * recv_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 (!recvv.enabled)
return NULL;
recvv.started = true;
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);
}
pthread_testcancel();
}
2016-09-10 22:19:07 -04:00
return NULL;
}
int main(int argc, char *argv[])
{
int ret, level = V;
2016-06-26 15:33:04 +02:00
char c;
ptid = pthread_self();
/* Default values */
sendd.enabled = true;
recvv.enabled = true;
while ((c = getopt(argc, argv, "hxrsd:")) != -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 = atoi(optarg);
break;
case 'h':
case '?':
2016-10-22 20:37:02 -04:00
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
}
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-03-07 08:05:04 -04:00
error("Failed to intialize node type: %s", node_name(node));
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 */
2016-06-26 15:33:04 +02:00
pthread_create(&recvv.thread, NULL, recv_loop, NULL);
pthread_create(&sendd.thread, NULL, send_loop, NULL);
2017-04-07 17:39:37 +02:00
for (;;)
sleep(1);
return 0;
}
/** @} */