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

528 lines
12 KiB
C++
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, 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 <cstdlib>
#include <cstring>
#include <unistd.h>
#include <signal.h>
#include <thread>
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>
2019-03-26 07:01:10 +01:00
#include <villas/config_helper.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/super_node.hpp>
#include <villas/utils.hpp>
#include <villas/utils.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/log.hpp>
2019-04-23 13:11:08 +02:00
#include <villas/colors.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.hpp>
#include <villas/tool.hpp>
namespace villas {
namespace node {
namespace tools {
2018-07-03 21:51:48 +02:00
class PipeDirection {
2018-10-20 14:20:51 +02:00
protected:
struct pool pool;
2020-08-25 21:00:52 +02:00
struct vnode *node;
struct io *io;
std::thread thread;
2019-06-05 19:00:09 +02:00
bool stop;
bool enabled;
int limit;
2018-10-20 14:20:51 +02:00
public:
2020-08-25 21:00:52 +02:00
PipeDirection(struct vnode *n, struct io *i, bool en = true, int lim = -1) :
node(n),
2018-10-20 14:20:51 +02:00
io(i),
2019-06-05 19:00:09 +02:00
stop(false),
enabled(en),
limit(lim)
{
/* Initialize memory */
2019-02-09 21:21:31 +00:00
unsigned vec = LOG2_CEIL(MAX(node->out.vectorize, node->in.vectorize));
unsigned pool_size = node_type(node)->pool_size ? node_type(node)->pool_size : vec;
int ret = pool_init(&pool, pool_size, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), node_memory_type(node));
if (ret < 0)
throw RuntimeError("Failed to allocate memory for pool.");
}
~PipeDirection()
{
int ret __attribute__((unused));
ret = pool_destroy(&pool);
}
virtual void run()
{
2018-10-20 14:20:51 +02:00
}
2018-10-20 14:20:51 +02:00
void startThread()
{
2019-06-05 19:00:09 +02:00
stop = false;
if (enabled)
thread = std::thread(&villas::node::tools::PipeDirection::run, this);
}
2016-06-26 15:33:04 +02:00
void stopThread()
{
2019-06-05 19:00:09 +02:00
stop = true;
/* We send a signal to the thread in order to interrupt blocking system calls */
pthread_kill(thread.native_handle(), SIGUSR1);
thread.join();
}
};
class PipeSendDirection : public PipeDirection {
public:
2020-08-25 21:00:52 +02:00
PipeSendDirection(struct vnode *n, struct io *i, bool en = true, int lim = -1) :
PipeDirection(n, i, en, lim)
{ }
virtual void run()
{
Logger logger = logging.get("pipe:send");
2019-02-11 16:39:30 +01:00
unsigned last_sequenceno = 0, release;
int scanned, sent, allocated, cnt = 0;
struct sample *smps[node->out.vectorize];
2019-06-05 19:00:09 +02:00
while (!stop && !io_eof(io)) {
allocated = sample_alloc_many(&pool, smps, node->out.vectorize);
if (allocated < 0)
throw RuntimeError("Failed to get {} samples out of send pool.", node->out.vectorize);
else if (allocated < (int) node->out.vectorize)
logger->warn("Send pool underrun");
scanned = io_scan(io, smps, allocated);
if (scanned < 0) {
2019-06-05 19:00:09 +02:00
if (stop)
goto leave2;
logger->warn("Failed to read samples from stdin");
continue;
}
else if (scanned == 0)
continue;
/* Fill in missing sequence numbers */
for (int i = 0; i < scanned; i++) {
2019-06-23 16:13:23 +02:00
if (smps[i]->flags & (int) SampleFlags::HAS_SEQUENCE)
last_sequenceno = smps[i]->sequence;
else
smps[i]->sequence = last_sequenceno++;
}
2018-10-20 14:20:51 +02:00
release = allocated;
2018-10-20 14:20:51 +02:00
sent = node_write(node, smps, scanned, &release);
2016-06-26 15:33:04 +02:00
sample_decref_many(smps, release);
cnt += sent;
if (limit > 0 && cnt >= limit)
goto leave;
}
leave2:
logger->info("Send thread stopped");
return;
2019-06-05 19:00:09 +02:00
leave: if (io_eof(io)) {
if (limit < 0) {
logger->info("Reached end-of-file. Terminating...");
raise(SIGINT);
}
else
logger->info("Reached end-of-file. Wait for receive side...");
}
else {
logger->info("Reached send limit. Terminating...");
raise(SIGINT);
}
}
};
class PipeReceiveDirection : public PipeDirection {
2017-07-24 19:33:35 +02:00
public:
2020-08-25 21:00:52 +02:00
PipeReceiveDirection(struct vnode *n, struct io *i, bool en = true, int lim = -1) :
PipeDirection(n, i, en, lim)
{ }
virtual void run()
{
Logger logger = logging.get("pipe:recv");
int recv, cnt = 0, allocated = 0;
unsigned release;
struct sample *smps[node->in.vectorize];
2019-06-05 19:00:09 +02:00
while (!stop) {
allocated = sample_alloc_many(&pool, smps, node->in.vectorize);
if (allocated < 0)
throw RuntimeError("Failed to allocate {} samples from receive pool.", node->in.vectorize);
else if (allocated < (int) node->in.vectorize)
logger->warn("Receive pool underrun: allocated only {} of {} samples", allocated, node->in.vectorize);
release = allocated;
recv = node_read(node, smps, allocated, &release);
if (recv < 0) {
2019-06-23 16:13:23 +02:00
if (node->state == State::STOPPING || stop)
goto leave2;
else
logger->warn("Failed to receive samples from node {}: reason={}", node_name(node), recv);
}
else {
io_print(io, smps, recv);
cnt += recv;
if (limit > 0 && cnt >= limit)
goto leave;
}
sample_decref_many(smps, release);
2017-08-05 21:02:09 +02:00
}
2019-06-05 19:00:09 +02:00
return;
leave:
logger->info("Reached receive limit. Terminating...");
leave2:
logger->info("Receive thread stopped");
raise(SIGINT);
}
};
class Pipe : public Tool {
public:
Pipe(int argc, char *argv[]) :
Tool(argc, argv, "pipe"),
stop(false),
io(),
timeout(0),
reverse(false),
format("villas.human"),
dtypes("64f"),
cfg_cli(json_object()),
enable_send(true),
enable_recv(true),
limit_send(-1),
limit_recv(-1)
{
int ret;
2017-07-24 19:33:35 +02:00
ret = memory_init(DEFAULT_NR_HUGEPAGES);
if (ret)
throw RuntimeError("Failed to initialize memory");
}
2016-09-10 22:19:07 -04:00
~Pipe()
{
json_decref(cfg_cli);
}
protected:
std::atomic<bool> stop;
SuperNode sn; /**< The global configuration */
struct io io;
2016-06-26 15:33:04 +02:00
int timeout;
bool reverse;
std::string format;
std::string dtypes;
std::string uri;
std::string nodestr;
json_t *cfg_cli;
2020-01-09 10:35:21 +01:00
bool enable_send;
bool enable_recv;
int limit_send;
int limit_recv;
void handler(int signal, siginfo_t *sinfo, void *ctx)
{
switch (signal) {
2019-06-05 19:00:09 +02:00
case SIGALRM:
logger->info("Reached timeout. Terminating...");
break;
2019-06-05 19:00:09 +02:00
case SIGUSR1:
break; /* ignore silently */
default:
logger->info("Received {} signal. Terminating...", strsignal(signal));
break;
}
stop = true;
}
void usage()
{
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
<< " -t DT the data-type format string" << 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
<< " -d set logging level" << std::endl
<< " -V show the version of the tool" << std::endl << std::endl;
printCopyright();
}
void parse()
{
int c, ret;
2019-04-17 18:46:18 +02:00
char *endptr;
while ((c = getopt(argc, argv, "Vhxrsd:l:L:T:f:t:o:")) != -1) {
switch (c) {
case 'V':
printVersion();
2019-04-17 18:46:18 +02:00
exit(EXIT_SUCCESS);
case 'f':
format = optarg;
break;
case 't':
dtypes = optarg;
break;
2019-04-17 18:46:18 +02:00
case 'x':
reverse = true;
break;
2019-04-17 18:46:18 +02:00
case 's':
enable_recv = false; // send only
break;
2017-08-05 21:02:09 +02:00
2019-04-17 18:46:18 +02:00
case 'r':
enable_send = false; // receive only
break;
2019-04-17 18:46:18 +02:00
case 'l':
limit_recv = strtoul(optarg, &endptr, 10);
goto check;
2017-08-05 21:02:09 +02:00
2019-04-17 18:46:18 +02:00
case 'L':
limit_send = strtoul(optarg, &endptr, 10);
goto check;
2017-10-18 12:49:52 +02:00
2019-04-17 18:46:18 +02:00
case 'T':
timeout = strtoul(optarg, &endptr, 10);
goto check;
2019-04-17 18:46:18 +02:00
case 'o':
ret = json_object_extend_str(cfg_cli, optarg);
if (ret)
throw RuntimeError("Invalid option: {}", optarg);
break;
2017-08-05 21:02:09 +02:00
2019-04-17 18:46:18 +02:00
case 'd':
logging.setLevel(optarg);
break;
2018-08-20 18:31:27 +02:00
2019-04-17 18:46:18 +02:00
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
2017-08-05 21:02:09 +02:00
2019-04-17 18:46:18 +02:00
check: if (optarg == endptr)
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc != optind + 2) {
usage();
exit(EXIT_FAILURE);
}
uri = argv[optind];
nodestr = argv[optind+1];
}
2019-04-17 18:46:18 +02:00
int main()
{
int ret;
2019-04-17 18:46:18 +02:00
2020-08-25 21:00:52 +02:00
struct vnode *node;
struct format_type *ft;
2019-04-17 18:46:18 +02:00
logger->info("Logging level: {}", logging.getLevelName());
2019-04-17 18:46:18 +02:00
if (!uri.empty())
2019-04-17 18:46:18 +02:00
sn.parse(uri);
else
logger->warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
ft = format_type_lookup(format.c_str());
2019-04-17 18:46:18 +02:00
if (!ft)
throw RuntimeError("Invalid format: {}", format);
2019-06-23 16:13:23 +02:00
ret = io_init2(&io, ft, dtypes.c_str(), (int) SampleFlags::HAS_ALL);
2019-04-17 18:46:18 +02:00
if (ret)
throw RuntimeError("Failed to initialize IO");
ret = io_check(&io);
if (ret)
throw RuntimeError("Failed to validate IO configuration");
ret = io_open(&io, nullptr);
if (ret)
throw RuntimeError("Failed to open IO");
node = sn.getNode(nodestr);
if (!node)
throw RuntimeError("Node {} does not exist!", nodestr);
2016-09-10 22:19:07 -04:00
2020-01-09 10:35:21 +01:00
if (!node->_vt->read && enable_recv)
throw RuntimeError("Node {} can not receive data. Consider using send-only mode by using '-s' option", nodestr);
if (!node->_vt->write && enable_send)
throw RuntimeError("Node {} can not send data. Consider using receive-only mode by using '-r' option", nodestr);
#if defined(WITH_NODE_WEBSOCKET) && defined(WITH_WEB)
2019-04-17 18:46:18 +02:00
/* Only start web subsystem if villas-pipe is used with a websocket node */
if (node_type(node)->start == websocket_start) {
Web *w = sn.getWeb();
w->start();
}
#endif /* WITH_NODE_WEBSOCKET */
2019-04-17 18:46:18 +02:00
if (reverse)
node_reverse(node);
2016-06-08 22:38:21 +02:00
2019-04-23 13:14:47 +02:00
ret = node_type_start(node_type(node), &sn);
2019-04-17 18:46:18 +02:00
if (ret)
throw RuntimeError("Failed to intialize node type {}: reason={}", node_type_name(node_type(node)), ret);
2019-04-17 18:46:18 +02:00
sn.startInterfaces();
2019-01-21 22:59:20 +01:00
2019-04-17 18:46:18 +02:00
ret = node_check(node);
if (ret)
throw RuntimeError("Invalid node configuration");
2016-06-26 15:33:04 +02:00
2019-04-17 18:46:18 +02:00
ret = node_prepare(node);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2018-08-17 12:40:03 +02:00
2019-04-17 18:46:18 +02:00
ret = node_start(node);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2016-06-08 22:38:21 +02:00
PipeReceiveDirection recv_dir(node, &io, enable_recv, limit_recv);
2020-01-09 10:35:21 +01:00
PipeSendDirection send_dir(node, &io, enable_send, limit_send);
recv_dir.startThread();
send_dir.startThread();
2016-06-26 15:33:04 +02:00
2019-04-17 18:46:18 +02:00
alarm(timeout);
2019-04-17 18:46:18 +02:00
while (!stop)
sleep(1);
recv_dir.stopThread();
send_dir.stopThread();
2019-04-17 18:46:18 +02:00
ret = node_stop(node);
if (ret)
throw RuntimeError("Failed to stop node {}: reason={}", node_name(node), ret);
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
sn.stopInterfaces();
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
ret = node_type_stop(node->_vt);
if (ret)
throw RuntimeError("Failed to stop node type {}: reason={}", node_type_name(node->_vt), ret);
2019-01-21 22:59:20 +01:00
#if defined(WITH_NODE_WEBSOCKET) && defined(WITH_WEB)
/* Only start web subsystem if villas-pipe is used with a websocket node */
if (node_type(node)->stop == websocket_stop) {
Web *w = sn.getWeb();
w->stop();
}
#endif /* WITH_NODE_WEBSOCKET */
2019-04-17 18:46:18 +02:00
ret = io_close(&io);
if (ret)
throw RuntimeError("Failed to close IO");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
ret = io_destroy(&io);
if (ret)
throw RuntimeError("Failed to destroy IO");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
return 0;
}
};
2018-10-20 14:20:51 +02:00
2020-06-15 22:16:38 +02:00
} /* namespace tools */
} /* namespace node */
} /* namespace villas */
int main(int argc, char *argv[])
{
2019-06-18 17:52:54 +02:00
villas::node::tools::Pipe t(argc, argv);
return t.run();
}
/** @} */