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
|
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>
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
#include <villas/cfg.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/msg.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/kernel/rt.h>
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
#include "config.h"
|
2015-11-29 21:27:26 +01:00
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
static struct cfg cfg; /**< 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
|
|
|
}
|
2016-01-15 15:34:28 +01: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
|
|
|
}
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
node_stop(node);
|
2017-03-11 23:40:43 -03:00
|
|
|
node_type_stop(node->_vt);
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
cfg_destroy(&cfg);
|
2016-06-26 15:33:04 +02:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
info(GRN("Goodbye!"));
|
2017-03-07 08:05:04 -04: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
|
|
|
{
|
2016-10-22 20:37:02 -04:00
|
|
|
printf("Usage: villas-pipe CONFIG NODE [OPTIONS]\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;
|
|
|
|
|
|
|
|
sendd.started = true;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
/* Initialize memory */
|
2016-10-30 22:57:05 -04:00
|
|
|
ret = pool_init(&sendd.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_VALUES), &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
|
|
|
|
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);
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
for (;;) {
|
2015-12-19 16:48:20 +01:00
|
|
|
for (int i = 0; i < node->vectorize; i++) {
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *s = smps[i];
|
2015-11-29 21:27:26 +01:00
|
|
|
int reason;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
retry: reason = sample_fscan(stdin, s, NULL);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reason < 0) {
|
2017-03-06 19:12:31 -04:00
|
|
|
if (feof(stdin)) {
|
|
|
|
info("Reached end-of-file. Terminating...");
|
2016-06-26 15:33:04 +02:00
|
|
|
goto killme;
|
2017-03-06 19:12:31 -04:00
|
|
|
}
|
2015-11-29 21:27:26 +01:00
|
|
|
else {
|
|
|
|
warn("Skipped invalid message message: reason=%d", reason);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
node_write(node, smps, node->vectorize);
|
2016-07-11 16:02:32 +02:00
|
|
|
pthread_testcancel();
|
2015-11-29 21:27:26 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
killme: pthread_kill(ptid, SIGINT);
|
|
|
|
|
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];
|
|
|
|
|
2016-06-26 15:33:04 +02:00
|
|
|
if (!recvv.enabled)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
recvv.started = true;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Initialize memory */
|
2016-10-30 22:57:05 -04:00
|
|
|
ret = pool_init(&recvv.pool, LOG2_CEIL(node->vectorize), SAMPLE_LEN(DEFAULT_VALUES), &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
error("Failed to allocate memory for receive pool.");
|
|
|
|
|
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;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
sample_fprint(stdout, s, SAMPLE_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();
|
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
/* Parse command line arguments */
|
2015-12-04 17:34:57 +01:00
|
|
|
if (argc < 3)
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2016-06-26 15:33:04 +02:00
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
sendd.enabled = true;
|
|
|
|
recvv.enabled = true;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
while ((c = getopt(argc-2, argv+2, "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
|
|
|
}
|
|
|
|
}
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2017-03-06 19:14:24 -04:00
|
|
|
log_init(&cfg.log, level, LOG_ALL);
|
2017-03-06 19:09:44 -04:00
|
|
|
signals_init(quit);
|
2017-03-03 20:21:33 -04:00
|
|
|
cfg_parse(&cfg, argv[1]);
|
2017-03-05 10:06:32 -04:00
|
|
|
rt_init(cfg.priority, cfg.affinity);
|
2017-03-11 23:39:00 -03:00
|
|
|
memory_init(cfg.hugepages);
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
/* Initialize node */
|
2017-03-06 08:57:43 -04:00
|
|
|
node = list_lookup(&cfg.nodes, argv[2]);
|
2015-11-29 21:27:26 +01:00
|
|
|
if (!node)
|
|
|
|
error("Node '%s' does not exist!", argv[2]);
|
2016-09-10 22:19:07 -04:00
|
|
|
|
2015-11-29 21:27:26 +01:00
|
|
|
if (reverse)
|
|
|
|
node_reverse(node);
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2017-03-11 23:40:43 -03:00
|
|
|
ret = node_type_start(node->_vt, argc-optind, argv+optind, config_root_setting(&cfg.cfg));
|
2016-06-26 15:33:04 +02:00
|
|
|
if (ret)
|
2017-03-07 08:05:04 -04:00
|
|
|
error("Failed to intialize node type: %s", node_name(node));
|
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);
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
for (;;)
|
|
|
|
pause();
|
2015-11-29 21:27:26 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|