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/node.c

175 lines
4.1 KiB
C
Raw Normal View History

/** Main routine.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
2015-08-22 17:42:38 +02:00
#include "config.h"
2016-06-14 01:19:17 +02:00
#include <villas/utils.h>
#include <villas/cfg.h>
#include <villas/path.h>
#include <villas/node.h>
2017-02-18 10:58:17 -05:00
#include <villas/api.h>
#include <villas/web.h>
#include <villas/timing.h>
2016-06-14 01:19:17 +02:00
#include <villas/kernel/kernel.h>
#include <villas/kernel/rt.h>
2017-02-18 10:58:17 -05:00
/* Forward declarations */
void hook_stats_header();
#ifdef ENABLE_OPAL_ASYNC
2015-08-21 10:19:32 +02:00
#include "opal.h"
#endif
2017-02-18 10:31:42 -05:00
struct cfg config;
static void quit()
{
2015-03-31 18:13:43 +02:00
info("Stopping paths");
2017-02-18 10:31:42 -05:00
list_foreach(struct path *p, &config.paths) { INDENT
path_stop(p);
2015-12-11 17:56:14 +01:00
}
2015-03-31 18:13:43 +02:00
info("Stopping nodes");
2017-02-18 10:31:42 -05:00
list_foreach(struct node *n, &config.nodes) { INDENT
node_stop(n);
2015-12-11 17:56:14 +01:00
}
2017-02-18 10:31:42 -05:00
cfg_deinit(&config);
cfg_destroy(&config);
2015-08-07 01:11:43 +02:00
info(GRN("Goodbye!"));
2015-03-21 18:03:55 +01:00
_exit(EXIT_SUCCESS);
}
/* Setup exit handler */
2015-06-02 22:04:03 +02:00
static void signals_init()
{ INDENT
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGINT, &sa_quit, NULL);
2015-06-02 22:24:44 +02:00
sigaction(SIGTERM, &sa_quit, NULL);
}
2015-06-02 22:04:03 +02:00
static void usage(const char *name)
{
2017-02-18 10:58:17 -05:00
printf("Usage: %s [CONFIG]\n", name);
printf(" CONFIG is the path to an optional configuration file\n");
printf(" if omitted, VILLASnode will start without a configuration\n");
printf(" and wait for provisioning over the web interface.\n\n");
#ifdef ENABLE_OPAL_ASYNC
printf("Usage: %s OPAL_ASYNC_SHMEM_NAME OPAL_ASYNC_SHMEM_SIZE OPAL_PRINT_SHMEM_NAME\n", name);
printf(" This type of invocation is used by OPAL-RT Asynchronous processes.\n");
printf(" See in the RT-LAB User Guide for more information.\n\n");
#endif
printf("Supported node types:\n");
list_foreach(struct node_type *vt, &node_types)
printf(" - %s: %s\n", vt->name, vt->description);
2015-08-22 17:42:38 +02:00
printf("\n");
2016-06-08 22:38:21 +02:00
printf("Supported hooks:\n");
list_foreach(struct hook *h, &hooks)
printf(" - %s: %s\n", h->name, h->description);
printf("\n");
2017-02-18 10:58:17 -05:00
printf("Supported API commands:\n");
list_foreach(struct api_ressource *r, &apis)
printf(" - %s: %s\n", r->name, r->description);
printf("\n");
print_copyright();
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
/* Check arguments */
#ifdef ENABLE_OPAL_ASYNC
2017-02-18 10:58:17 -05:00
if (argc != 4)
usage(argv[0]);
char *uri = "opal-shmem.conf";
#else
2017-02-18 10:58:17 -05:00
if (argc > 2)
usage(argv[0]);
2017-02-18 10:58:17 -05:00
char *uri = (argc == 2) ? argv[1] : NULL;
#endif
2015-08-07 01:11:43 +02:00
2016-06-08 23:21:42 +02:00
info("This is VILLASnode %s (built on %s, %s)", BLD(YEL(VERSION)),
2015-09-17 00:58:04 +02:00
BLD(MAG(__DATE__)), BLD(MAG(__TIME__)));
2015-09-17 00:55:20 +02:00
/* Checks system requirements*/
if (kernel_has_version(KERNEL_VERSION_MAJ, KERNEL_VERSION_MIN))
2015-09-17 00:55:20 +02:00
error("Your kernel version is to old: required >= %u.%u", KERNEL_VERSION_MAJ, KERNEL_VERSION_MIN);
info("Initialize signals");
signals_init();
2017-02-18 10:31:42 -05:00
info("Parsing configuration");
cfg_init_pre(&config);
cfg_parse(&config, uri);
cfg_init_post(&config);
2015-08-07 01:11:43 +02:00
info("Initialize node types");
list_foreach(struct node_type *vt, &node_types) { INDENT
2016-06-08 22:38:21 +02:00
int refs = list_length(&vt->instances);
if (refs > 0)
2017-02-18 10:31:42 -05:00
node_init(vt, argc, argv, config_root_setting(config.cfg));
2015-12-11 17:56:14 +01:00
}
2015-03-31 18:13:43 +02:00
info("Starting nodes");
2017-02-18 10:31:42 -05:00
list_foreach(struct node *n, &config.nodes) { INDENT
int refs = list_count(&config.paths, (cmp_cb_t) path_uses_node, n);
2015-12-04 01:54:33 +01:00
if (refs > 0)
node_start(n);
else
2015-12-11 17:56:14 +01:00
warn("No path is using the node %s. Skipping...", node_name(n));
}
2015-03-31 18:13:43 +02:00
info("Starting paths");
2017-02-18 10:31:42 -05:00
list_foreach(struct path *p, &config.paths) { INDENT
2016-06-08 22:38:21 +02:00
if (p->enabled) {
path_prepare(p);
path_start(p);
2016-06-08 22:38:21 +02:00
}
else
warn("Path %s is disabled. Skipping...", path_name(p));
}
2017-02-18 10:31:42 -05:00
if (config.stats > 0)
2015-10-12 16:16:25 +02:00
hook_stats_header();
2015-08-07 01:11:43 +02:00
2017-02-18 10:58:17 -05:00
struct timespec now, last = time_now();
/* Run! Until signal handler is invoked */
while (1) {
now = time_now();
if (config.stats > 0 && time_delta(&last, &now) > config.stats) {
list_foreach(struct path *p, &config.paths) {
2016-06-08 22:38:21 +02:00
hook_run(p, NULL, 0, HOOK_PERIODIC);
2017-02-18 10:58:17 -05:00
}
last = time_now();
2015-10-14 14:52:29 +02:00
}
2017-02-18 10:58:17 -05:00
web_service(&config.web); /** @todo Maybe we should move this to another thread */
}
return 0;
}