/** Receive messages from server snd print them on stdout. * * @file * @author Steffen Vogel * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC * @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. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * @addtogroup tools Test and debug tools * @{ *********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace villas; using namespace villas::node; static SuperNode sn; /**< The global configuration */ static Logger logger = logging.get("pipe"); class Direction { public: Direction(struct io *i) : io(i), enabled(true), limit(-1) { } struct pool pool; struct node *node; struct io *io; pthread_t thread; bool enabled; int limit; }; static std::atomic stop(false); static void quit(int signal, siginfo_t *sinfo, void *ctx) { if (signal == SIGALRM) logger->info("Reached timeout. Terminating..."); stop = true; } static 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 << " -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; utils::print_copyright(); } static void * send_loop(void *ctx) { Direction *d = static_cast(ctx); unsigned last_sequenceno = 0, release; int ret, scanned, sent, allocated, cnt = 0; struct sample *smps[d->node->out.vectorize]; /* Initialize memory */ unsigned pool_size = node_type(d->node)->pool_size ? node_type(d->node)->pool_size : LOG2_CEIL(d->node->out.vectorize); ret = pool_init(&d->pool, pool_size, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), node_memory_type(d->node, &memory_hugepage)); if (ret < 0) throw new RuntimeError("Failed to allocate memory for receive pool."); while (!io_eof(d->io)) { allocated = sample_alloc_many(&d->pool, smps, d->node->out.vectorize); if (ret < 0) 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"); scanned = io_scan(d->io, smps, allocated); if (scanned < 0) { 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++) { if (smps[i]->flags & SAMPLE_HAS_SEQUENCE) last_sequenceno = smps[i]->sequence; else smps[i]->sequence = last_sequenceno++; } release = allocated; sent = node_write(d->node, smps, scanned, &release); if (sent < 0) logger->warn("Failed to sent samples to node {}: reason={}", node_name(d->node), sent); else if (sent < scanned) logger->warn("Failed to sent {} out of {} samples to node {}", scanned-sent, scanned, node_name(d->node)); sample_decref_many(smps, release); cnt += sent; if (d->limit > 0 && cnt >= d->limit) goto leave; pthread_testcancel(); } leave: if (io_eof(d->io)) { if (d->limit < 0) { logger->info("Reached end-of-file. Terminating..."); killme(SIGTERM); } else logger->info("Reached end-of-file. Wait for receive side..."); } else { logger->info("Reached send limit. Terminating..."); killme(SIGTERM); } return nullptr; } static void * recv_loop(void *ctx) { Direction *d = static_cast(ctx); int recv, ret, cnt = 0, allocated = 0; unsigned release; struct sample *smps[d->node->in.vectorize]; /* Initialize memory */ unsigned pool_size = node_type(d->node)->pool_size ? node_type(d->node)->pool_size : LOG2_CEIL(d->node->in.vectorize); ret = pool_init(&d->pool, pool_size, SAMPLE_LENGTH(list_length(&d->node->signals)), node_memory_type(d->node, &memory_hugepage)); if (ret < 0) throw new RuntimeError("Failed to allocate memory for receive pool."); for (;;) { allocated = sample_alloc_many(&d->pool, smps, d->node->in.vectorize); if (allocated < 0) 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; recv = node_read(d->node, smps, allocated, &release); if (recv < 0) logger->warn("Failed to receive samples from node {}: reason={}", node_name(d->node), recv); else { io_print(d->io, smps, recv); cnt += recv; if (d->limit > 0 && cnt >= d->limit) goto leave; } sample_decref_many(smps, release); pthread_testcancel(); } leave: logger->info("Reached receive limit. Terminating..."); killme(SIGTERM); return nullptr; } int main(int argc, char *argv[]) { int ret, timeout = 0; bool reverse = false; const char *format = "villas.human"; struct node *node; static struct io io = { .state = STATE_DESTROYED }; Direction sendd(&io); Direction recvv(&io); json_t *cfg_cli = json_object(); int c; char *endptr; while ((c = getopt(argc, argv, "Vhxrsd:l:L:t:f:o:")) != -1) { switch (c) { case 'V': utils::print_version(); exit(EXIT_SUCCESS); case 'f': format = optarg; break; case 'x': reverse = true; break; case 's': recvv.enabled = false; // send only break; case 'r': sendd.enabled = false; // receive only break; case 'l': recvv.limit = strtoul(optarg, &endptr, 10); goto check; case 'L': sendd.limit = strtoul(optarg, &endptr, 10); goto check; case 't': timeout = strtoul(optarg, &endptr, 10); goto check; case 'o': ret = json_object_extend_str(cfg_cli, optarg); if (ret) throw new RuntimeError("Invalid option: {}", optarg); break; case 'd': logging.setLevel(optarg); break; case 'h': case '?': usage(); exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS); } continue; check: if (optarg == endptr) throw new RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg); } if (argc != optind + 2) { usage(); exit(EXIT_FAILURE); } char *uri = argv[optind]; char *nodestr = argv[optind+1]; struct format_type *fmt; ret = utils::signals_init(quit); if (ret) throw new RuntimeError("Failed to initialize signals"); 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."); ret = sn.init(); if (ret) throw new RuntimeError("Failed to initialize super-node"); fmt = format_type_lookup(format); if (!fmt) throw new RuntimeError("Invalid format: {}", format); ret = io_init_auto(&io, fmt, DEFAULT_SAMPLE_LENGTH, SAMPLE_HAS_ALL); if (ret) throw new RuntimeError("Failed to initialize IO"); ret = io_check(&io); if (ret) throw new RuntimeError("Failed to validate IO configuration"); ret = io_open(&io, nullptr); if (ret) throw new RuntimeError("Failed to open IO"); node = sn.getNode(nodestr); if (!node) throw new RuntimeError("Node {} does not exist!", nodestr); <<<<<<< HEAD #ifdef LIBWEBSOCKETS_FOUND ======= /** @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 */ if (node_type(node)->start == websocket_start) { Web *w = sn.getWeb(); Api *a = sn.getApi(); w->start(); a->start(); } #endif /* LIBWEBSOCKETS_FOUND */ if (reverse) node_reverse(node); ret = node_type_start(node->_vt);//, &sn); // @todo: port to C++ if (ret) throw new RuntimeError("Failed to intialize node type {}: reason={}", node_type_name(node->_vt), ret); ret = node_check(node); if (ret) throw new RuntimeError("Invalid node configuration"); ret = node_init2(node); if (ret) throw new RuntimeError("Failed to start node {}: reason={}", node_name(node), ret); ret = node_start(node); if (ret) throw new RuntimeError("Failed to start node {}: reason={}", node_name(node), ret); /* Start threads */ if (recvv.enabled) { recvv.node = node; pthread_create(&recvv.thread, nullptr, recv_loop, &recvv); } if (sendd.enabled) { sendd.node = node; pthread_create(&sendd.thread, nullptr, send_loop, &sendd); } alarm(timeout); while (!stop) pause(); 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; } /** @} */