2014-06-05 09:34:29 +00:00
|
|
|
/**
|
|
|
|
* Main routine
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <error.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2014-06-05 09:35:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "msg.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "node.h"
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/** Linked list of nodes */
|
|
|
|
static struct node *nodes;
|
|
|
|
|
|
|
|
/** Linked list of paths */
|
|
|
|
static struct path *paths;
|
|
|
|
|
2014-06-05 09:35:41 +00:00
|
|
|
/** Default settings */
|
2014-06-10 16:57:40 +00:00
|
|
|
static struct settings settings = {
|
2014-06-05 09:35:35 +00:00
|
|
|
.priority = 0,
|
|
|
|
.affinity = 0xC0,
|
|
|
|
.protocol = 0
|
|
|
|
};
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
static config_t config;
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void start()
|
2014-06-05 09:35:12 +00:00
|
|
|
{
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Connect and bind nodes to their sockets, set socket options */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct node *n = nodes; n; n = n->next) {
|
2014-06-05 09:35:12 +00:00
|
|
|
node_connect(n);
|
2014-06-05 09:35:18 +00:00
|
|
|
|
|
|
|
debug(1, " We listen for node '%s' at %s:%u", n->name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port));
|
|
|
|
debug(1, " We sent to node '%s' at %s:%u", n->name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Start on thread per path for asynchronous processing */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct path *p = paths; p; p = p->next) {
|
2014-06-05 09:35:12 +00:00
|
|
|
path_start(p);
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
info("Starting path: %12s => %s => %-12s", p->in->name, settings.name, p->out->name);
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void stop()
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Join all threads and print statistics */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct path *p = paths; p; p = p->next) {
|
2014-06-05 09:34:50 +00:00
|
|
|
path_stop(p);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
info("Stopping path: %12s => %s => %-12s", p->in->name, settings.name, p->out->name);
|
2014-06-05 09:34:57 +00:00
|
|
|
info(" %u messages received", p->received);
|
|
|
|
info(" %u messages duplicated", p->duplicated);
|
|
|
|
info(" %u messages delayed", p->delayed);
|
2014-06-05 09:34:50 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Close all sockets we listing on */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct node *n = nodes; n; n = n->next) {
|
2014-06-05 09:35:12 +00:00
|
|
|
node_disconnect(n);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void quit()
|
2014-06-05 09:35:12 +00:00
|
|
|
{
|
|
|
|
stop();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
// TODO: free nodes and paths
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
config_destroy(&config);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-05 09:34:45 +00:00
|
|
|
_exit(EXIT_SUCCESS);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-05 09:35:35 +00:00
|
|
|
/* 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);
|
2014-06-05 09:34:40 +00:00
|
|
|
atexit(&quit);
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Check arguments */
|
2014-06-05 09:34:50 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage: %s CONFIG\n", argv[0]);
|
|
|
|
printf(" CONFIG is a required path to a configuration file\n\n");
|
2014-06-05 09:34:45 +00:00
|
|
|
printf("Simulator2Simulator Server %s (%s %s)\n", VERSION, __DATE__, __TIME__);
|
|
|
|
printf(" Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
|
|
|
|
printf(" Steffen Vogel <stvogel@eonerc.rwth-aachen.de>\n\n");
|
2014-06-05 09:34:29 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
info("This is s2ss %s", VERSION);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Parse configuration file */
|
2014-06-10 16:57:40 +00:00
|
|
|
config_init(&config);
|
2014-06-10 18:47:25 +00:00
|
|
|
config_parse(argv[1], &config, &settings, &nodes, &paths);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
if (!paths)
|
2014-06-05 09:34:50 +00:00
|
|
|
error("No paths found. Terminating...");
|
|
|
|
|
2014-06-05 09:35:33 +00:00
|
|
|
/* Setup various realtime related things */
|
2014-06-10 16:57:40 +00:00
|
|
|
init_realtime(&settings);
|
2014-06-05 09:35:12 +00:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/* Connect all nodes and start one thread per path */
|
2014-06-05 09:35:35 +00:00
|
|
|
start();
|
2014-06-05 09:35:10 +00:00
|
|
|
|
|
|
|
/* Main thread is sleeping */
|
2014-06-10 18:47:25 +00:00
|
|
|
pause();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|