1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/src/pipe.c

216 lines
4.7 KiB
C
Raw Normal View History

/** Receive messages from server snd print them on stdout.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2016-02-09 05:33:19 +01:00
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
2016-06-08 23:21:42 +02:00
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* @addtogroup tools Test and debug tools
* @{
*********************************************************************************/
#include <stdlib.h>
2016-06-08 22:38:21 +02:00
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include "config.h"
#include "cfg.h"
#include "utils.h"
#include "node.h"
#include "msg.h"
#include "timing.h"
#include "pool.h"
struct list nodes; /**< List of all nodes */
struct settings settings; /**< The global configuration */
struct pool recv_pool, send_pool;
pthread_t recv_thread, send_thread;
struct node *node;
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
pthread_cancel(recv_thread);
pthread_cancel(send_thread);
pthread_join(recv_thread, NULL);
pthread_join(send_thread, NULL);
node_stop(node);
node_deinit(node->_vt);
pool_destroy(&recv_pool);
pool_destroy(&send_pool);
list_destroy(&nodes, (dtor_cb_t) node_destroy, false);
info(GRN("Goodbye!"));
exit(EXIT_SUCCESS);
}
static void usage(char *name)
{
printf("Usage: %s CONFIG [-r] NODE\n", name);
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-08 22:38:21 +02:00
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");
print_copyright();
exit(EXIT_FAILURE);
}
void * send_loop(void *ctx)
2016-06-08 22:38:21 +02:00
{
int ret;
struct sample *smps[node->vectorize];
/* Initialize memory */
ret = pool_init_mmap(&send_pool, SAMPLE_LEN(DEFAULT_VALUES), node->vectorize);
if (ret < 0)
error("Failed to allocate memory for receive pool.");
ret = sample_get_many(&send_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);
for (;;) {
for (int i = 0; i < node->vectorize; i++) {
2016-06-08 22:38:21 +02:00
struct sample *s = smps[i];
int reason, retry;
do {
retry = 0;
reason = sample_fscan(stdin, s, NULL);
if (reason < 0) {
if (feof(stdin))
return NULL;
else {
warn("Skipped invalid message message: reason=%d", reason);
retry = 1;
}
}
} while (retry);
}
2016-06-08 22:38:21 +02:00
node_write(node, smps, node->vectorize);
}
return NULL;
}
void * recv_loop(void *ctx)
{
2016-06-08 22:38:21 +02:00
int ret;
struct sample *smps[node->vectorize];
/* Initialize memory */
ret = pool_init_mmap(&recv_pool, SAMPLE_LEN(DEFAULT_VALUES), node->vectorize);
if (ret < 0)
error("Failed to allocate memory for receive pool.");
ret = sample_get_many(&recv_pool, smps, node->vectorize);
2016-06-08 22:38:21 +02:00
if (ret < 0)
error("Failed to get %u samples out of receive pool (%d).", node->vectorize, ret);
/* Print header */
2016-06-08 22:38:21 +02:00
fprintf(stdout, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]");
for (;;) {
2016-06-08 22:38:21 +02:00
int recv = node_read(node, smps, node->vectorize);
for (int i = 0; i < recv; i++) {
2016-06-08 22:38:21 +02:00
struct sample *s = smps[i];
sample_fprint(stdout, s, SAMPLE_ALL);
2015-12-04 01:54:33 +01:00
fflush(stdout);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
2016-06-08 22:38:21 +02:00
bool send = true, recv = true, reverse = false;
/* Parse command line arguments */
if (argc < 3)
usage(argv[0]);
2016-06-08 22:38:21 +02:00
log_init();
char c;
2016-06-08 22:38:21 +02:00
while ((c = getopt(argc-2, argv+2, "hxrsd:")) != -1) {
switch (c) {
2016-06-08 22:38:21 +02:00
case 'x':
reverse = true;
break;
case 's':
recv = false;
break;
case 'r':
2016-06-08 22:38:21 +02:00
send = false;
break;
case 'd':
log_setlevel(atoi(optarg), -1);
break;
case 'h':
case '?':
usage(argv[0]);
}
}
/* Setup signals */
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGTERM, &sa_quit, NULL);
sigaction(SIGINT, &sa_quit, NULL);
/* Initialize log, configuration.. */
config_t config;
/* Create lists */
list_init(&nodes);
config_init(&config);
config_parse(argv[1], &config, &settings, &nodes, NULL);
/* Initialize node */
node = list_lookup(&nodes, argv[2]);
if (!node)
error("Node '%s' does not exist!", argv[2]);
2016-06-08 22:38:21 +02:00
if (reverse)
node_reverse(node);
2016-06-08 22:38:21 +02:00
node_init(node->_vt, argc-optind, argv+optind, config_root_setting(&config));
node_start(node);
2016-06-08 22:38:21 +02:00
/* Start threads */
2016-06-08 22:38:21 +02:00
if (recv)
pthread_create(&recv_thread, NULL, recv_loop, NULL);
if (send)
pthread_create(&send_thread, NULL, send_loop, NULL);
2016-06-08 22:38:21 +02:00
for (;;)
pause();
return 0;
}
/** @} */