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

461 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>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, 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/copyright.hpp>
2018-10-20 14:20:51 +02:00
#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;
2018-10-20 14:20:51 +02:00
class Direction {
public:
Direction(struct node *n, struct io *i, bool en = true, int lim = -1) :
node(n),
2018-10-20 14:20:51 +02:00
io(i),
enabled(en),
limit(lim)
{
pool.state = STATE_DESTROYED;
pool.queue.state = STATE_DESTROYED;
/* Initialize memory */
/* 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, &memory_hugepage));
if (ret < 0)
throw RuntimeError("Failed to allocate memory for pool.");
}
Direction(const Direction &c)
{
io = c.io;
}
~Direction()
{
pool_destroy(&pool);
}
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
struct Directions {
Direction send;
Direction recv;
};
2018-10-20 14:20:51 +02:00
static std::atomic<bool> stop(false);
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
Logger logger = logging.get("pipe");
2019-02-11 16:39:30 +01:00
switch (signal) {
case SIGALRM:
logger->info("Reached timeout. Terminating...");
break;
default:
logger->info("Received {} signal. Terminating...", strsignal(signal));
break;
}
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;
print_copyright();
}
2016-06-26 15:33:04 +02:00
static void * send_loop(void *ctx)
2016-06-08 22:38:21 +02:00
{
Directions *dirs = static_cast<Directions*>(ctx);
Logger logger = logging.get("pipe");
2018-10-20 14:20:51 +02:00
unsigned last_sequenceno = 0, release;
2019-01-06 02:13:02 +01:00
int scanned, sent, allocated, cnt = 0;
2018-10-20 14:20:51 +02:00
2019-02-06 15:12:02 +01:00
struct node *node = dirs->send.node;
struct sample *smps[node->out.vectorize];
2016-06-26 15:33:04 +02:00
2019-02-11 16:39:30 +01:00
while (node->state == STATE_STARTED && !io_eof(dirs->send.io)) {
2019-02-06 15:12:02 +01:00
allocated = sample_alloc_many(&dirs->send.pool, smps, node->out.vectorize);
2018-12-04 00:30:58 +01:00
if (allocated < 0)
2019-02-06 15:12:02 +01:00
throw RuntimeError("Failed to get {} samples out of send pool.", node->out.vectorize);
else if (allocated < node->out.vectorize)
2018-10-20 14:20:51 +02:00
logger->warn("Send pool underrun");
scanned = io_scan(dirs->send.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;
2019-02-06 15:12:02 +01:00
sent = node_write(node, smps, scanned, &release);
2017-07-24 19:33:35 +02:00
sample_decref_many(smps, release);
2017-08-05 21:02:09 +02:00
cnt += sent;
if (dirs->send.limit > 0 && cnt >= dirs->send.limit)
2017-08-05 21:02:09 +02:00
goto leave;
pthread_testcancel();
}
leave: if (io_eof(dirs->send.io)) {
if (dirs->recv.limit < 0) {
2018-10-20 14:20:51 +02:00
logger->info("Reached end-of-file. Terminating...");
2019-02-11 16:39:30 +01:00
stop = true;
2017-08-05 21:02:09 +02:00
}
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...");
2019-02-11 16:39:30 +01:00
stop = true;
}
2018-08-27 11:21:57 +02:00
return nullptr;
}
2016-06-26 15:33:04 +02:00
static void * recv_loop(void *ctx)
{
Directions *dirs = static_cast<Directions*>(ctx);
Logger logger = logging.get("pipe");
2018-10-20 14:20:51 +02:00
int recv, cnt = 0, allocated = 0;
unsigned release;
2019-02-06 15:12:02 +01:00
struct node *node = dirs->recv.node;
struct sample *smps[node->in.vectorize];
2019-02-11 16:39:30 +01:00
while (node->state == STATE_STARTED) {
2019-02-06 15:12:02 +01:00
allocated = sample_alloc_many(&dirs->recv.pool, smps, node->in.vectorize);
if (allocated < 0)
2019-02-06 15:12:02 +01:00
throw RuntimeError("Failed to allocate {} samples from receive pool.", node->in.vectorize);
else if (allocated < node->in.vectorize)
logger->warn("Receive pool underrun: allocated only {} of {} samples", allocated, node->in.vectorize);
release = allocated;
2019-02-06 15:12:02 +01:00
recv = node_read(node, smps, allocated, &release);
2019-02-11 16:39:30 +01:00
if (recv < 0) {
if (node->state == STATE_STOPPING)
goto leave2;
else
logger->warn("Failed to receive samples from node {}: reason={}", node_name(node), recv);
}
2018-08-20 18:31:27 +02:00
else {
io_print(dirs->recv.io, smps, recv);
2017-07-24 19:33:35 +02:00
2018-08-20 18:31:27 +02:00
cnt += recv;
if (dirs->recv.limit > 0 && cnt >= dirs->recv.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...");
2019-02-11 16:39:30 +01:00
leave2: stop = true;
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 };
SuperNode sn; /**< The global configuration */
Logger logger = logging.get("pipe");
2017-08-05 21:02:09 +02:00
json_t *cfg_cli = json_object();
bool enable_send = true, enable_recv = true;
int limit_send = -1, limit_recv = -1;
2018-10-04 02:41:01 +02:00
/* Parse optional command line arguments */
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':
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':
enable_recv = false; // send only
2016-06-08 22:38:21 +02:00
break;
2018-08-27 11:23:21 +02:00
case 'r':
enable_send = false; // receive only
2016-06-08 22:38:21 +02:00
break;
2018-08-27 11:23:21 +02:00
case 'l':
limit_recv = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 'L':
limit_send = 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)
throw 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)
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc != optind + 2) {
usage();
exit(EXIT_FAILURE);
}
logger->info("Logging level: {}", logging.getLevelName());
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
ret = memory_init(0);
if (ret)
throw RuntimeError("Failed to intialize memory");
2018-10-20 14:20:51 +02:00
ret = utils::signals_init(quit);
2017-08-05 21:02:09 +02:00
if (ret)
throw 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 RuntimeError("Failed to parse configuration");
2018-10-20 14:20:51 +02:00
}
else
logger->warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
2017-10-18 12:49:52 +02:00
fmt = format_type_lookup(format);
if (!fmt)
throw 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)
throw 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)
throw 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)
throw 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)
throw RuntimeError("Node {} does not exist!", nodestr);
2016-09-10 22:19:07 -04:00
#ifdef LIBWEBSOCKETS_FOUND
/* 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
ret = node_type_start(node_type(node), reinterpret_cast<super_node *>(&sn));
2016-06-26 15:33:04 +02:00
if (ret)
throw RuntimeError("Failed to intialize node type {}: reason={}", node_type_name(node_type(node)), ret);
2019-01-21 22:59:20 +01:00
sn.startInterfaces();
ret = node_check(node);
if (ret)
throw 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)
throw 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)
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2016-06-08 22:38:21 +02:00
/* Start threads */
Directions dirs = {
.send = Direction(node, &io, enable_send, limit_send),
.recv = Direction(node, &io, enable_recv, limit_recv)
};
if (dirs.recv.enabled) {
dirs.recv.node = node;
pthread_create(&dirs.recv.thread, nullptr, recv_loop, &dirs);
2018-10-20 14:20:51 +02:00
}
2017-07-24 19:33:35 +02:00
if (dirs.send.enabled) {
dirs.send.node = node;
pthread_create(&dirs.send.thread, nullptr, send_loop, &dirs);
2018-10-20 14:20:51 +02:00
}
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)
2019-02-11 16:39:30 +01:00
sleep(1);
if (dirs.recv.enabled) {
pthread_cancel(dirs.recv.thread);
pthread_join(dirs.recv.thread, nullptr);
2018-10-20 14:20:51 +02:00
}
if (dirs.send.enabled) {
pthread_cancel(dirs.send.thread);
pthread_join(dirs.send.thread, nullptr);
2018-10-20 14:20:51 +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-01-21 22:59:20 +01:00
sn.stopInterfaces();
2018-12-02 02:56:52 +01:00
ret = node_type_stop(node->_vt);
2018-10-20 14:20:51 +02:00
if (ret)
throw RuntimeError("Failed to stop node type {}: reason={}", node_type_name(node->_vt), ret);
2018-10-20 14:20:51 +02:00
ret = io_close(&io);
if (ret)
throw RuntimeError("Failed to close IO");
2018-10-20 14:20:51 +02:00
ret = io_destroy(&io);
if (ret)
throw RuntimeError("Failed to destroy IO");
2018-10-20 14:20:51 +02:00
logger->info(CLR_GRN("Goodbye!"));
return 0;
}
/** @} */