1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/src/villas-pipe.cpp

438 lines
11 KiB
C++
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2018-08-20 18:39:04 +02:00
* @copyright 2017-2018, 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>
2018-08-20 18:31:27 +02:00
#include <string.h>
#include <signal.h>
#include <pthread.h>
2018-07-03 21:04:28 +02:00
#include <iostream>
2018-10-20 14:20:51 +02:00
#include <atomic>
#include <villas/node/config.h>
#include <villas/config_helper.h>
2018-10-20 14:20:51 +02:00
#include <villas/super_node.hpp>
#include <villas/utils.hpp>
2016-06-26 15:33:04 +02:00
#include <villas/utils.h>
2018-10-20 14:20:51 +02:00
#include <villas/log.hpp>
2016-06-26 15:33:04 +02:00
#include <villas/node.h>
#include <villas/timing.h>
#include <villas/pool.h>
2017-07-28 18:11:52 +02:00
#include <villas/io.h>
2018-10-20 14:20:51 +02:00
#include <villas/kernel/rt.hpp>
#include <villas/exceptions.hpp>
#include <villas/format_type.h>
#include <villas/nodes/websocket.h>
2018-10-20 14:20:51 +02:00
using namespace villas;
2018-07-03 21:51:48 +02:00
using namespace villas::node;
static SuperNode sn; /**< The global configuration */
2018-10-20 14:20:51 +02:00
static Logger logger = logging.get("pipe");
class Direction {
public:
Direction(struct io *i) :
io(i),
enabled(true),
limit(-1)
{ }
2016-06-26 15:33:04 +02:00
struct pool pool;
2018-10-20 14:20:51 +02:00
struct node *node;
struct io *io;
2016-06-26 15:33:04 +02:00
pthread_t thread;
2018-10-20 14:20:51 +02:00
2016-06-26 15:33:04 +02:00
bool enabled;
int limit;
2018-10-20 14:20:51 +02:00
};
2016-06-26 15:33:04 +02:00
2018-10-20 14:20:51 +02:00
static std::atomic<bool> stop(false);
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
if (signal == SIGALRM)
2018-10-20 14:20:51 +02:00
logger->info("Reached timeout. Terminating...");
2018-10-20 14:20:51 +02:00
stop = true;
}
2016-10-22 20:37:02 -04:00
static void usage()
{
2018-08-27 11:25:56 +02:00
std::cout << "Usage: villas-pipe [OPTIONS] CONFIG NODE" << std::endl
<< " CONFIG path to a configuration file" << std::endl
<< " NODE the name of the node to which samples are sent and received from" << std::endl
<< " OPTIONS are:" << std::endl
<< " -f FMT set the format" << std::endl
<< " -o OPTION=VALUE overwrite options in config file" << std::endl
<< " -x swap read / write endpoints" << std::endl
<< " -s only read data from stdin and send it to node" << std::endl
<< " -r only read data from node and write it to stdout" << std::endl
<< " -t NUM terminate after NUM seconds" << std::endl
<< " -L NUM terminate after NUM samples sent" << std::endl
<< " -l NUM terminate after NUM samples received" << std::endl
<< " -h show this usage information" << std::endl
2018-10-20 14:20:51 +02:00
<< " -d set logging level" << std::endl
2018-08-27 11:25:56 +02:00
<< " -V show the version of the tool" << std::endl << std::endl;
2018-10-20 14:20:51 +02:00
utils::print_copyright();
}
2016-06-26 15:33:04 +02:00
static void * send_loop(void *ctx)
2016-06-08 22:38:21 +02:00
{
2018-10-20 14:20:51 +02:00
Direction *d = static_cast<Direction *>(ctx);
unsigned last_sequenceno = 0, release;
int ret, scanned, sent, allocated, cnt = 0;
2018-10-20 14:20:51 +02:00
struct sample *smps[d->node->out.vectorize];
2016-06-26 15:33:04 +02:00
2016-06-08 22:38:21 +02:00
/* Initialize memory */
2018-10-20 14:20:51 +02:00
unsigned pool_size = node_type(d->node)->pool_size ? node_type(d->node)->pool_size : LOG2_CEIL(d->node->out.vectorize);
2018-10-20 14:20:51 +02:00
ret = pool_init(&d->pool, pool_size, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), node_memory_type(d->node, &memory_hugepage));
2016-06-08 22:38:21 +02:00
if (ret < 0)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to allocate memory for receive pool.");
2018-10-20 14:20:51 +02:00
while (!io_eof(d->io)) {
allocated = sample_alloc_many(&d->pool, smps, d->node->out.vectorize);
if (ret < 0)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to get {} samples out of send pool ({}).", d->node->out.vectorize, ret);
else if (allocated < d->node->out.vectorize)
logger->warn("Send pool underrun");
2018-10-20 14:20:51 +02:00
scanned = io_scan(d->io, smps, allocated);
if (scanned < 0) {
2018-10-20 14:20:51 +02:00
logger->warn("Failed to read samples from stdin");
continue;
}
else if (scanned == 0)
2017-08-05 21:02:09 +02:00
continue;
/* Fill in missing sequence numbers */
for (int i = 0; i < scanned; i++) {
if (smps[i]->flags & SAMPLE_HAS_SEQUENCE)
last_sequenceno = smps[i]->sequence;
else
smps[i]->sequence = last_sequenceno++;
}
release = allocated;
2018-10-20 14:20:51 +02:00
sent = node_write(d->node, smps, scanned, &release);
if (sent < 0)
2018-10-20 14:20:51 +02:00
logger->warn("Failed to sent samples to node {}: reason={}", node_name(d->node), sent);
else if (sent < scanned)
2018-10-20 14:20:51 +02:00
logger->warn("Failed to sent {} out of {} samples to node {}", scanned-sent, scanned, node_name(d->node));
2017-07-24 19:33:35 +02:00
sample_decref_many(smps, release);
2017-08-05 21:02:09 +02:00
cnt += sent;
2018-10-20 14:20:51 +02:00
if (d->limit > 0 && cnt >= d->limit)
2017-08-05 21:02:09 +02:00
goto leave;
pthread_testcancel();
}
2018-10-20 14:20:51 +02:00
leave: if (io_eof(d->io)) {
if (d->limit < 0) {
logger->info("Reached end-of-file. Terminating...");
2017-08-05 21:02:09 +02:00
killme(SIGTERM);
}
else
2018-10-20 14:20:51 +02:00
logger->info("Reached end-of-file. Wait for receive side...");
2017-08-05 21:02:09 +02:00
}
else {
2018-10-20 14:20:51 +02:00
logger->info("Reached send limit. Terminating...");
killme(SIGTERM);
}
2018-08-27 11:21:57 +02:00
return nullptr;
}
2016-06-26 15:33:04 +02:00
static void * recv_loop(void *ctx)
{
2018-10-20 14:20:51 +02:00
Direction *d = static_cast<Direction *>(ctx);
int recv, ret, cnt = 0, allocated = 0;
unsigned release;
2018-10-20 14:20:51 +02:00
struct sample *smps[d->node->in.vectorize];
2016-06-08 22:38:21 +02:00
/* Initialize memory */
2018-10-20 14:20:51 +02:00
unsigned pool_size = node_type(d->node)->pool_size ? node_type(d->node)->pool_size : LOG2_CEIL(d->node->in.vectorize);
2018-10-20 14:20:51 +02:00
ret = pool_init(&d->pool, pool_size, SAMPLE_LENGTH(list_length(&d->node->signals)), node_memory_type(d->node, &memory_hugepage));
2016-06-08 22:38:21 +02:00
if (ret < 0)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to allocate memory for receive pool.");
for (;;) {
2018-10-20 14:20:51 +02:00
allocated = sample_alloc_many(&d->pool, smps, d->node->in.vectorize);
if (allocated < 0)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to allocate {} samples from receive pool.", d->node->in.vectorize);
else if (allocated < d->node->in.vectorize)
logger->warn("Receive pool underrun: allocated only {} of {} samples", allocated, d->node->in.vectorize);
release = allocated;
2018-10-20 14:20:51 +02:00
recv = node_read(d->node, smps, allocated, &release);
if (recv < 0)
2018-10-20 14:20:51 +02:00
logger->warn("Failed to receive samples from node {}: reason={}", node_name(d->node), recv);
2018-08-20 18:31:27 +02:00
else {
2018-10-20 14:20:51 +02:00
io_print(d->io, smps, recv);
2017-07-24 19:33:35 +02:00
2018-08-20 18:31:27 +02:00
cnt += recv;
2018-10-20 14:20:51 +02:00
if (d->limit > 0 && cnt >= d->limit)
2018-08-20 18:31:27 +02:00
goto leave;
}
2017-08-05 21:02:09 +02:00
sample_decref_many(smps, release);
pthread_testcancel();
}
2016-09-10 22:19:07 -04:00
2018-10-20 14:20:51 +02:00
leave: logger->info("Reached receive limit. Terminating...");
killme(SIGTERM);
2018-08-27 11:21:57 +02:00
return nullptr;
}
int main(int argc, char *argv[])
{
2018-07-03 21:51:48 +02:00
int ret, timeout = 0;
bool reverse = false;
2018-07-03 20:42:37 +02:00
const char *format = "villas.human";
2016-06-26 15:33:04 +02:00
2018-10-20 14:20:51 +02:00
struct node *node;
static struct io io = { .state = STATE_DESTROYED };
2018-10-20 14:20:51 +02:00
Direction sendd(&io);
Direction recvv(&io);
2017-07-24 19:33:35 +02:00
2017-08-05 21:02:09 +02:00
json_t *cfg_cli = json_object();
2018-09-26 22:47:58 +00:00
int c;
char *endptr;
while ((c = getopt(argc, argv, "Vhxrsd:l:L:t:f:o:")) != -1) {
switch (c) {
case 'V':
2018-10-20 14:20:51 +02:00
utils::print_version();
exit(EXIT_SUCCESS);
2018-08-27 11:23:21 +02:00
2017-08-05 21:02:09 +02:00
case 'f':
format = optarg;
break;
2018-08-27 11:23:21 +02:00
2016-06-08 22:38:21 +02:00
case 'x':
reverse = true;
break;
2018-08-27 11:23:21 +02:00
2016-06-08 22:38:21 +02:00
case 's':
2016-06-26 15:33:04 +02:00
recvv.enabled = false; // send only
2016-06-08 22:38:21 +02:00
break;
2018-08-27 11:23:21 +02: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;
2018-08-27 11:23:21 +02:00
case 'l':
recvv.limit = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 'L':
sendd.limit = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 't':
2017-07-02 22:15:07 +02:00
timeout = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
2017-08-05 21:02:09 +02:00
case 'o':
ret = json_object_extend_str(cfg_cli, optarg);
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Invalid option: {}", optarg);
2017-08-05 21:02:09 +02:00
break;
2018-10-20 14:20:51 +02:00
case 'd':
logging.setLevel(optarg);
break;
case 'h':
case '?':
2016-10-22 20:37:02 -04:00
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc != optind + 2) {
usage();
exit(EXIT_FAILURE);
}
2018-10-20 14:20:51 +02:00
char *uri = argv[optind];
char *nodestr = argv[optind+1];
struct format_type *fmt;
2017-08-05 21:02:09 +02:00
2018-10-20 14:20:51 +02:00
ret = utils::signals_init(quit);
2017-08-05 21:02:09 +02:00
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to initialize signals");
2017-08-05 21:02:09 +02:00
2018-10-20 14:20:51 +02:00
if (uri) {
ret = sn.parseUri(uri);
if (ret)
throw new RuntimeError("Failed to parse configuration");
}
else
logger->warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
2017-10-18 12:49:52 +02:00
2018-07-03 21:51:48 +02:00
ret = sn.init();
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to initialize super-node");
fmt = format_type_lookup(format);
if (!fmt)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Invalid format: {}", format);
2018-08-20 18:31:27 +02:00
ret = io_init_auto(&io, fmt, DEFAULT_SAMPLE_LENGTH, SAMPLE_HAS_ALL);
2017-08-05 21:02:09 +02:00
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to initialize IO");
2017-08-05 21:02:09 +02:00
2018-08-20 18:31:27 +02:00
ret = io_check(&io);
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to validate IO configuration");
2018-08-20 18:31:27 +02:00
2018-08-27 11:21:57 +02:00
ret = io_open(&io, nullptr);
2017-08-05 21:02:09 +02:00
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to open IO");
2017-08-05 21:02:09 +02:00
2018-07-03 21:51:48 +02:00
node = sn.getNode(nodestr);
if (!node)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Node {} does not exist!", nodestr);
2016-09-10 22:19:07 -04:00
2018-10-20 14:20:51 +02:00
<<<<<<< HEAD
#ifdef LIBWEBSOCKETS_FOUND
2018-10-20 14:20:51 +02:00
=======
/** @todo Port to C++ */
#ifdef __Libwebsockets_FOUND
>>>>>>> cpp: use new supernode class
/* Only start web subsystem if villas-pipe is used with a websocket node */
2018-07-03 21:51:48 +02:00
if (node_type(node)->start == websocket_start) {
2018-10-20 14:20:51 +02:00
Web *w = sn.getWeb();
Api *a = sn.getApi();
2018-08-20 18:31:27 +02:00
2018-10-20 14:20:51 +02:00
w->start();
a->start();
}
#endif /* LIBWEBSOCKETS_FOUND */
if (reverse)
node_reverse(node);
2016-06-08 22:38:21 +02:00
2018-07-03 21:51:48 +02:00
ret = node_type_start(node->_vt);//, &sn); // @todo: port to C++
2016-06-26 15:33:04 +02:00
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to intialize node type {}: reason={}", node_type_name(node->_vt), ret);
ret = node_check(node);
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Invalid node configuration");
2016-06-26 15:33:04 +02:00
2018-08-17 12:40:03 +02:00
ret = node_init2(node);
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2018-08-17 12:40:03 +02:00
2016-06-26 15:33:04 +02:00
ret = node_start(node);
if (ret)
2018-10-20 14:20:51 +02:00
throw new RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2016-06-08 22:38:21 +02:00
/* Start threads */
2018-10-20 14:20:51 +02:00
if (recvv.enabled) {
recvv.node = node;
pthread_create(&recvv.thread, nullptr, recv_loop, &recvv);
}
2017-07-24 19:33:35 +02:00
2018-10-20 14:20:51 +02:00
if (sendd.enabled) {
sendd.node = node;
pthread_create(&sendd.thread, nullptr, send_loop, &sendd);
}
2016-06-26 15:33:04 +02:00
2017-07-02 22:15:07 +02:00
alarm(timeout);
2018-10-20 14:20:51 +02:00
while (!stop)
pause();
2018-10-20 14:20:51 +02:00
if (recvv.enabled) {
pthread_cancel(recvv.thread);
pthread_join(recvv.thread, nullptr);
}
if (sendd.enabled) {
pthread_cancel(sendd.thread);
pthread_join(sendd.thread, nullptr);
}
ret = node_stop(node);
if (ret)
throw new RuntimeError("Failed to stop node {}: reason={}", node_name(node), ret);
ret = node_type_stop(node->_vt);//, &sn); // @todo: port to C++
if (ret)
throw new RuntimeError("Failed to stop node type {}: reason={}", node_type_name(node->_vt), ret);
if (recvv.enabled) {
ret = pool_destroy(&recvv.pool);
if (ret)
throw new RuntimeError("Failed to destroy pool");
}
if (sendd.enabled) {
ret = pool_destroy(&sendd.pool);
if (ret)
throw new RuntimeError("Failed to destroy pool");
}
ret = io_close(&io);
if (ret)
throw new RuntimeError("Failed to close IO");
ret = io_destroy(&io);
if (ret)
throw new RuntimeError("Failed to destroy IO");
logger->info(CLR_GRN("Goodbye!"));
return 0;
}
/** @} */