1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00
VILLASnode/server/src/receive.c
2015-05-06 13:21:26 +02:00

133 lines
No EOL
2.8 KiB
C

/** Receive messages from server snd print them on stdout.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2015, Institute for Automation of Complex Power Systems, EONERC
* @file
* @addtogroup tools Test and debug tools
* @{
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "config.h"
#include "cfg.h"
#include "utils.h"
#include "node.h"
#include "msg.h"
#include "socket.h"
static struct settings set;
static struct msg *pool;
static struct node *node;
extern struct list nodes;
void quit(int sig, siginfo_t *si, void *ptr)
{
node_stop(node);
node_deinit();
list_destroy(&nodes);
free(pool);
exit(EXIT_SUCCESS);
}
void usage(char *name)
{
printf("Usage: %s [-r] CONFIG NODE\n", name);
printf(" -r swap local / remote address of socket based nodes)\n\n");
printf(" CONFIG path to a configuration file\n");
printf(" NODE name of the node which shoud be used\n\n");
printf("Simulator2Simulator Server %s (built on %s %s)\n",
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
printf(" Copyright 2015, Institute for Automation of Complex Power Systems, EONERC\n");
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int reverse = 0;
struct config_t config;
_mtid = pthread_self();
char c;
while ((c = getopt(argc, argv, "hr")) != -1) {
switch (c) {
case 'r': reverse = 1; break;
case 'h':
case '?': usage(argv[0]);
}
}
if (argc - optind != 2)
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);
list_init(&nodes, (dtor_cb_t) node_destroy);
config_init(&config);
config_parse(argv[optind], &config, &set, &nodes, NULL);
node = node_lookup_name(argv[optind+1], &nodes);
if (!node)
error("There's no node with the name '%s'", argv[optind+1]);
if (reverse)
node_reverse(node);
node->refcnt++;
node_init(argc-optind, argv+optind, &set);
node_start(node);
node_start_defer(node);
pool = alloc(sizeof(struct msg) * node->combine);
/* Print header */
fprintf(stderr, "# %-6s %-8s %-12s\n", "dev_id", "seq_no", "data");
while (1) {
int recv = node_read(node, pool, node->combine, 0, node->combine);
for (int i = 0; i < recv; i++) {
if (msg_verify(&pool[i]))
warn("Failed to verify message");
#if TOOLS_USE_TIMESTAMP
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
fprintf(stdout, "%17.6f\t", ts.tv_sec + ts.tv_nsec / 1e9);
#endif
msg_fprint(stdout, &pool[i]);
}
}
return 0;
}
/** @} */