2015-11-29 21:27:26 +01:00
|
|
|
/** 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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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/>.
|
2015-11-29 21:27:26 +01:00
|
|
|
*
|
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <stdbool.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
#include <unistd.h>
|
2018-08-20 18:31:27 +02:00
|
|
|
#include <string.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
#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>
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2018-08-23 17:31:01 +02:00
|
|
|
#include <villas/node/config.h>
|
2018-02-06 21:26:12 +01:00
|
|
|
#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>
|
2017-04-02 04:31:34 +02:00
|
|
|
#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:
|
2018-12-02 03:09:09 +01:00
|
|
|
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),
|
2018-12-02 03:09:09 +01:00
|
|
|
enabled(en),
|
|
|
|
limit(lim)
|
|
|
|
{
|
|
|
|
pool.state = STATE_DESTROYED;
|
|
|
|
pool.queue.state = STATE_DESTROYED;
|
|
|
|
|
|
|
|
/* Initialize memory */
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize memory */
|
|
|
|
unsigned pool_size = node_type(node)->pool_size ? node_type(node)->pool_size : LOG2_CEIL(node->out.vectorize);
|
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
Direction(const Direction &c)
|
|
|
|
{
|
|
|
|
io = c.io;
|
|
|
|
}
|
|
|
|
|
2018-12-02 03:09:09 +01:00
|
|
|
~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;
|
2017-07-01 21:12:44 +02:00
|
|
|
int limit;
|
2018-10-20 14:20:51 +02:00
|
|
|
};
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
struct Directions {
|
|
|
|
Direction send;
|
|
|
|
Direction recv;
|
|
|
|
};
|
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
static std::atomic<bool> stop(false);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
2018-10-21 22:28:38 +01:00
|
|
|
Logger logger = logging.get("pipe");
|
|
|
|
|
2017-07-02 00:13:06 +02:00
|
|
|
if (signal == SIGALRM)
|
2018-10-20 14:20:51 +02:00
|
|
|
logger->info("Reached timeout. Terminating...");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
stop = true;
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
static void usage()
|
2015-11-29 21:27:26 +01:00
|
|
|
{
|
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;
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
utils::print_copyright();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * send_loop(void *ctx)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
2018-10-28 13:24:23 +01:00
|
|
|
Directions *dirs = static_cast<Directions*>(ctx);
|
2018-10-21 22:28:38 +01:00
|
|
|
Logger logger = logging.get("pipe");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
unsigned last_sequenceno = 0, release;
|
|
|
|
int ret, scanned, sent, allocated, cnt = 0;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
struct sample *smps[dirs->send.node->out.vectorize];
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
while (!io_eof(dirs->send.io)) {
|
|
|
|
allocated = sample_alloc_many(&dirs->send.pool, smps, dirs->send.node->out.vectorize);
|
2018-12-04 00:30:58 +01:00
|
|
|
if (allocated < 0)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to get {} samples out of send pool ({}).", dirs->send.node->out.vectorize, ret);
|
2018-10-28 13:24:23 +01:00
|
|
|
else if (allocated < dirs->send.node->out.vectorize)
|
2018-10-20 14:20:51 +02:00
|
|
|
logger->warn("Send pool underrun");
|
2017-09-02 21:08:10 +02:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
scanned = io_scan(dirs->send.io, smps, allocated);
|
2017-09-02 21:08:10 +02:00
|
|
|
if (scanned < 0) {
|
2018-10-20 14:20:51 +02:00
|
|
|
logger->warn("Failed to read samples from stdin");
|
2017-09-02 21:08:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (scanned == 0)
|
2017-08-05 21:02:09 +02:00
|
|
|
continue;
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2017-10-16 23:08:27 +02:00
|
|
|
/* 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++;
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
release = allocated;
|
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
sent = node_write(dirs->send.node, smps, scanned, &release);
|
2018-05-26 02:33:41 +02:00
|
|
|
if (sent < 0)
|
2018-10-28 13:24:23 +01:00
|
|
|
logger->warn("Failed to sent samples to node {}: reason={}", node_name(dirs->send.node), sent);
|
2018-05-26 02:33:41 +02:00
|
|
|
else if (sent < scanned)
|
2018-10-28 13:24:23 +01:00
|
|
|
logger->warn("Failed to sent {} out of {} samples to node {}", scanned-sent, scanned, node_name(dirs->send.node));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref_many(smps, release);
|
2017-09-02 21:08:10 +02:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
cnt += sent;
|
2018-10-28 13:24:23 +01:00
|
|
|
if (dirs->send.limit > 0 && cnt >= dirs->send.limit)
|
2017-08-05 21:02:09 +02:00
|
|
|
goto leave;
|
2017-07-01 21:12:44 +02:00
|
|
|
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
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...");
|
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...");
|
2017-07-06 21:14:42 +02:00
|
|
|
killme(SIGTERM);
|
2017-07-01 21:12:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
return nullptr;
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * recv_loop(void *ctx)
|
2015-11-29 21:27:26 +01:00
|
|
|
{
|
2018-10-28 13:24:23 +01:00
|
|
|
Directions *dirs = static_cast<Directions*>(ctx);
|
2018-10-21 22:28:38 +01:00
|
|
|
Logger logger = logging.get("pipe");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2018-12-02 03:09:09 +01:00
|
|
|
int recv, cnt = 0, allocated = 0;
|
2018-07-11 18:14:29 +02:00
|
|
|
unsigned release;
|
2018-10-28 13:24:23 +01:00
|
|
|
struct sample *smps[dirs->recv.node->in.vectorize];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
for (;;) {
|
2018-10-28 13:24:23 +01:00
|
|
|
allocated = sample_alloc_many(&dirs->recv.pool, smps, dirs->recv.node->in.vectorize);
|
2018-07-11 18:14:29 +02:00
|
|
|
if (allocated < 0)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to allocate {} samples from receive pool.", dirs->recv.node->in.vectorize);
|
2018-10-28 13:24:23 +01:00
|
|
|
else if (allocated < dirs->recv.node->in.vectorize)
|
|
|
|
logger->warn("Receive pool underrun: allocated only {} of {} samples", allocated, dirs->recv.node->in.vectorize);
|
2017-09-02 21:08:10 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
release = allocated;
|
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
recv = node_read(dirs->recv.node, smps, allocated, &release);
|
2017-12-06 16:57:02 +08:00
|
|
|
if (recv < 0)
|
2018-10-28 13:24:23 +01:00
|
|
|
logger->warn("Failed to receive samples from node {}: reason={}", node_name(dirs->recv.node), recv);
|
2018-08-20 18:31:27 +02:00
|
|
|
else {
|
2018-10-28 13:24:23 +01:00
|
|
|
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;
|
2018-10-28 13:24:23 +01:00
|
|
|
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
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref_many(smps, release);
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
leave: logger->info("Reached receive limit. Terminating...");
|
2017-07-06 21:14:42 +02:00
|
|
|
killme(SIGTERM);
|
2017-07-01 21:12:44 +02:00
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
return nullptr;
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-07-03 21:51:48 +02:00
|
|
|
int ret, timeout = 0;
|
2017-07-01 21:12:44 +02:00
|
|
|
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-07-07 17:07:45 +02:00
|
|
|
|
2018-10-21 22:28:38 +01:00
|
|
|
SuperNode sn; /**< The global configuration */
|
|
|
|
Logger logger = logging.get("pipe");
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
json_t *cfg_cli = json_object();
|
|
|
|
|
2018-12-02 03:09:09 +01:00
|
|
|
bool enable_send = true, enable_recv = true;
|
|
|
|
int limit_send = -1, limit_recv = -1;
|
|
|
|
|
2018-09-26 22:47:58 +00:00
|
|
|
int c;
|
|
|
|
char *endptr;
|
2017-09-16 11:52:30 +02:00
|
|
|
while ((c = getopt(argc, argv, "Vhxrsd:l:L:t:f:o:")) != -1) {
|
2015-11-29 21:27:26 +01:00
|
|
|
switch (c) {
|
2017-09-16 11:52:30 +02:00
|
|
|
case 'V':
|
2018-10-20 14:20:51 +02:00
|
|
|
utils::print_version();
|
2017-09-16 11:52:30 +02:00
|
|
|
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':
|
2018-12-02 03:09:09 +01:00
|
|
|
enable_recv = false; // send only
|
2016-06-08 22:38:21 +02:00
|
|
|
break;
|
2018-08-27 11:23:21 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
case 'r':
|
2018-12-02 03:09:09 +01:00
|
|
|
enable_send = false; // receive only
|
2016-06-08 22:38:21 +02:00
|
|
|
break;
|
2018-08-27 11:23:21 +02:00
|
|
|
|
2017-07-01 21:12:44 +02:00
|
|
|
case 'l':
|
2018-12-02 03:09:09 +01:00
|
|
|
limit_recv = strtoul(optarg, &endptr, 10);
|
2017-07-01 21:12:44 +02:00
|
|
|
goto check;
|
2018-08-27 11:23:21 +02:00
|
|
|
|
2017-07-01 21:12:44 +02:00
|
|
|
case 'L':
|
2018-12-02 03:09:09 +01:00
|
|
|
limit_send = strtoul(optarg, &endptr, 10);
|
2017-07-01 21:12:44 +02:00
|
|
|
goto check;
|
2018-08-27 11:23:21 +02:00
|
|
|
|
2017-07-02 00:13:06 +02:00
|
|
|
case 't':
|
2017-07-02 22:15:07 +02:00
|
|
|
timeout = strtoul(optarg, &endptr, 10);
|
2017-07-02 00:13:06 +02:00
|
|
|
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-12-02 03:01:13 +01:00
|
|
|
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;
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2016-10-30 22:05:29 -04:00
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
2017-07-01 20:46:47 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-03 19:26:58 +02:00
|
|
|
if (argc != optind + 2) {
|
2017-03-12 17:01:24 -03:00
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-12-02 03:09:09 +01:00
|
|
|
logger->info("Logging level: {}", logging.getLevelName());
|
2018-10-28 13:24:23 +01:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
char *uri = argv[optind];
|
|
|
|
char *nodestr = argv[optind+1];
|
2018-05-13 13:52:02 +02:00
|
|
|
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-12-02 03:01:13 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
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
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
fmt = format_type_lookup(format);
|
|
|
|
if (!fmt)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Invalid format: {}", format);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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-12-02 03:01:13 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
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);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (!node)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Node {} does not exist!", nodestr);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2018-11-23 20:53:53 +02:00
|
|
|
#ifdef LIBWEBSOCKETS_FOUND
|
2017-08-22 12:31:12 +02:00
|
|
|
/* 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();
|
2017-08-30 23:28:40 +02:00
|
|
|
}
|
2018-11-23 20:53:53 +02:00
|
|
|
#endif /* LIBWEBSOCKETS_FOUND */
|
2017-04-02 04:31:34 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reverse)
|
|
|
|
node_reverse(node);
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2018-12-02 02:56:52 +01:00
|
|
|
ret = node_type_start(node->_vt, reinterpret_cast<super_node *>(&sn));
|
2016-06-26 15:33:04 +02:00
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to intialize node type {}: reason={}", node_type_name(node->_vt), ret);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
ret = node_check(node);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Start threads */
|
2018-12-02 03:09:09 +01:00
|
|
|
Directions dirs = {
|
|
|
|
.send = Direction(node, &io, enable_send, limit_send),
|
|
|
|
.recv = Direction(node, &io, enable_recv, limit_recv)
|
|
|
|
};
|
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
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
|
|
|
|
2018-10-28 13:24:23 +01: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);
|
2017-07-02 00:13:06 +02:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
while (!stop)
|
2017-07-02 00:13:06 +02:00
|
|
|
pause();
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2018-10-28 13:24:23 +01:00
|
|
|
if (dirs.recv.enabled) {
|
|
|
|
pthread_cancel(dirs.recv.thread);
|
|
|
|
pthread_join(dirs.recv.thread, nullptr);
|
2018-10-20 14:20:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 13:24:23 +01: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)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to stop node {}: reason={}", node_name(node), ret);
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2018-12-02 02:56:52 +01:00
|
|
|
ret = node_type_stop(node->_vt);
|
2018-10-20 14:20:51 +02:00
|
|
|
if (ret)
|
2018-12-02 03:09:09 +01:00
|
|
|
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)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to close IO");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
|
|
|
ret = io_destroy(&io);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to destroy IO");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
|
|
|
logger->info(CLR_GRN("Goodbye!"));
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|