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>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
#include <villas/super_node.h>
|
2016-06-26 15:33:04 +02:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/msg.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/pool.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
#include <villas/sample_io.h>
|
2016-06-26 15:33:04 +02:00
|
|
|
#include <villas/kernel/rt.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2017-04-02 04:31:34 +02:00
|
|
|
#include <villas/nodes/websocket.h>
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
#include "config.h"
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2017-04-15 20:16:51 +02:00
|
|
|
static struct super_node sn = { .state = STATE_DESTROYED }; /**< The global configuration */
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
struct dir {
|
|
|
|
struct pool pool;
|
|
|
|
pthread_t thread;
|
|
|
|
bool enabled;
|
|
|
|
bool started;
|
|
|
|
} sendd, recvv;
|
|
|
|
|
|
|
|
bool reverse = false;
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
struct node *node;
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
pthread_t ptid; /**< Parent thread id */
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
2016-06-26 15:33:04 +02:00
|
|
|
if (recvv.started) {
|
|
|
|
pthread_cancel(recvv.thread);
|
|
|
|
pthread_join(recvv.thread, NULL);
|
2016-10-30 22:57:05 -04:00
|
|
|
pool_destroy(&recvv.pool);
|
2016-06-26 15:33:04 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
if (sendd.started) {
|
|
|
|
pthread_cancel(sendd.thread);
|
|
|
|
pthread_join(sendd.thread, NULL);
|
2016-10-30 22:57:05 -04:00
|
|
|
pool_destroy(&sendd.pool);
|
2016-06-26 15:33:04 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-06-14 13:01:51 +02:00
|
|
|
node_stop(node);
|
|
|
|
node_destroy(node);
|
2017-06-15 15:29:33 +02:00
|
|
|
|
|
|
|
if (node->_vt->start == websocket_start) {
|
|
|
|
web_stop(&sn.web);
|
|
|
|
api_stop(&sn.api);
|
|
|
|
}
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
info(GRN("Goodbye!"));
|
2017-04-24 18:59:12 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
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
|
|
|
{
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("Usage: villas-pipe [OPTIONS] CONFIG NODE\n");
|
2015-11-29 21:27:26 +01:00
|
|
|
printf(" CONFIG path to a configuration file\n");
|
|
|
|
printf(" NODE the name of the node to which samples are sent and received from\n");
|
2016-06-26 15:33:04 +02:00
|
|
|
printf(" OPTIONS are:\n");
|
|
|
|
printf(" -d LVL set debug log level to LVL\n");
|
|
|
|
printf(" -x swap read / write endpoints\n");
|
|
|
|
printf(" -s only read data from stdin and send it to node\n");
|
|
|
|
printf(" -r only read data from node and write it to stdout\n\n");
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-01-15 15:25:22 +01:00
|
|
|
print_copyright();
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * send_loop(void *ctx)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sample *smps[node->vectorize];
|
2016-06-26 15:33:04 +02:00
|
|
|
|
|
|
|
if (!sendd.enabled)
|
|
|
|
return NULL;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
sendd.started = true;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Initialize memory */
|
2017-04-15 22:42:24 +02:00
|
|
|
ret = pool_init(&sendd.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
ret = sample_alloc(&sendd.pool, smps, node->vectorize);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to get %u samples out of send pool (%d).", node->vectorize, ret);
|
|
|
|
|
2017-04-15 20:16:51 +02:00
|
|
|
while (!feof(stdin)) {
|
|
|
|
int len;
|
|
|
|
for (len = 0; len < node->vectorize; len++) {
|
|
|
|
struct sample *s = smps[len];
|
2015-11-29 21:27:26 +01:00
|
|
|
int reason;
|
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
retry: reason = sample_io_villas_fscan(stdin, s, NULL);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reason < 0) {
|
2017-04-15 20:16:51 +02:00
|
|
|
if (feof(stdin))
|
2017-04-27 15:16:35 +00:00
|
|
|
goto exit;
|
2015-11-29 21:27:26 +01:00
|
|
|
else {
|
|
|
|
warn("Skipped invalid message message: reason=%d", reason);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 20:16:51 +02:00
|
|
|
node_write(node, smps, len);
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-15 20:16:51 +02:00
|
|
|
/* We reached EOF on stdin here. Lets kill the process */
|
2017-04-27 15:16:35 +00:00
|
|
|
exit: info("Reached end-of-file. Terminating...");
|
2017-04-15 20:16:51 +02:00
|
|
|
pthread_kill(ptid, SIGINT);
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
static void * recv_loop(void *ctx)
|
2015-11-29 21:27:26 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
|
|
|
struct sample *smps[node->vectorize];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
if (!recvv.enabled)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
recvv.started = true;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Initialize memory */
|
2017-04-15 22:42:24 +02:00
|
|
|
ret = pool_init(&recvv.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
ret = sample_alloc(&recvv.pool, smps, node->vectorize);
|
2016-09-10 22:19:07 -04:00
|
|
|
if (ret < 0)
|
2016-06-08 22:38:21 +02:00
|
|
|
error("Failed to get %u samples out of receive pool (%d).", node->vectorize, ret);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Print header */
|
2016-06-08 22:38:21 +02:00
|
|
|
fprintf(stdout, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]");
|
2016-06-26 15:33:04 +02:00
|
|
|
fflush(stdout);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2016-06-08 22:38:21 +02:00
|
|
|
int recv = node_read(node, smps, node->vectorize);
|
2016-09-14 03:32:31 +02:00
|
|
|
struct timespec now = time_now();
|
2015-11-29 21:27:26 +01:00
|
|
|
for (int i = 0; i < recv; i++) {
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *s = smps[i];
|
|
|
|
|
2016-09-14 03:32:31 +02:00
|
|
|
if (s->ts.received.tv_sec == -1 || s->ts.received.tv_sec == 0)
|
|
|
|
s->ts.received = now;
|
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
sample_io_villas_fprint(stdout, s, SAMPLE_IO_ALL);
|
2015-12-04 01:54:33 +01:00
|
|
|
fflush(stdout);
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
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
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-03-06 19:14:24 -04:00
|
|
|
int ret, level = V;
|
2016-06-26 15:33:04 +02:00
|
|
|
char c;
|
|
|
|
|
|
|
|
ptid = pthread_self();
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
sendd.enabled = true;
|
|
|
|
recvv.enabled = true;
|
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
while ((c = getopt(argc, argv, "hxrsd:")) != -1) {
|
2015-11-29 21:27:26 +01:00
|
|
|
switch (c) {
|
2016-06-08 22:38:21 +02:00
|
|
|
case 'x':
|
|
|
|
reverse = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-06-26 15:33:04 +02:00
|
|
|
recvv.enabled = false; // send only
|
2016-06-08 22:38:21 +02:00
|
|
|
break;
|
2015-11-29 21:27:26 +01: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;
|
|
|
|
case 'd':
|
2017-03-06 19:14:24 -04:00
|
|
|
level = atoi(optarg);
|
2015-11-29 21:27:26 +01:00
|
|
|
break;
|
|
|
|
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-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
|
|
|
|
2017-05-03 19:26:58 +02:00
|
|
|
char *configfile = argv[optind];
|
|
|
|
char *nodestr = argv[optind+1];
|
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
log_init(&sn.log, level, LOG_ALL);
|
|
|
|
log_start(&sn.log);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
super_node_init(&sn);
|
2017-05-03 19:26:58 +02:00
|
|
|
super_node_parse_uri(&sn, configfile);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
memory_init(sn.hugepages);
|
2017-03-06 19:09:44 -04:00
|
|
|
signals_init(quit);
|
2017-03-12 17:01:24 -03:00
|
|
|
rt_init(sn.priority, sn.affinity);
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Initialize node */
|
2017-05-03 19:26:58 +02:00
|
|
|
node = list_lookup(&sn.nodes, nodestr);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (!node)
|
2017-05-03 19:26:58 +02:00
|
|
|
error("Node '%s' does not exist!", nodestr);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2017-04-02 04:31:34 +02:00
|
|
|
if (node->_vt->start == websocket_start) {
|
|
|
|
web_start(&sn.web);
|
2017-04-07 17:35:55 +02:00
|
|
|
api_start(&sn.api);
|
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
|
|
|
|
2017-04-07 17:32:36 +02:00
|
|
|
ret = node_type_start(node->_vt, &sn);
|
2016-06-26 15:33:04 +02:00
|
|
|
if (ret)
|
2017-05-22 20:10:12 +02:00
|
|
|
error("Failed to intialize node type: %s", node_type_name(node->_vt));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
ret = node_check(node);
|
|
|
|
if (ret)
|
|
|
|
error("Invalid node configuration");
|
2016-06-26 15:33:04 +02:00
|
|
|
|
|
|
|
ret = node_start(node);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to start node: %s", node_name(node));
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Start threads */
|
2016-06-26 15:33:04 +02:00
|
|
|
pthread_create(&recvv.thread, NULL, recv_loop, NULL);
|
|
|
|
pthread_create(&sendd.thread, NULL, send_loop, NULL);
|
|
|
|
|
2017-04-07 17:39:37 +02:00
|
|
|
for (;;)
|
|
|
|
sleep(1);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|