From 2c5db5ee644277fc8816f44c02be0280e73834af Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:34:32 +0000 Subject: [PATCH] added some experiments with libconfig git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@6 8ec27952-4edc-4aab-86aa-e87bb2611832 --- etc/s2ss.conf | 26 ++++++++++++++++++++ include/cfg.h | 21 ++++++++++++++++ src/cfg.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 etc/s2ss.conf create mode 100644 include/cfg.h create mode 100644 src/cfg.c diff --git a/etc/s2ss.conf b/etc/s2ss.conf new file mode 100644 index 000000000..fec088d5a --- /dev/null +++ b/etc/s2ss.conf @@ -0,0 +1,26 @@ +# Example configuration file for the s2ss server + +# The name of this node +name = "acs"; + +# The debugging level (0 - 10) +debug = 10; + +nodes = ( + { + id = 123; + name = "acs"; + type = "opal"; + addr = "137.123.234.233"; + port = 7582; + } +); + +paths = ( + { + from = "acs"; + to = "trond"; + reverse = true; # also setup a reverse path + log = true; # log messages + } +); diff --git a/include/cfg.h b/include/cfg.h new file mode 100644 index 000000000..8ea44fcd1 --- /dev/null +++ b/include/cfg.h @@ -0,0 +1,21 @@ +/** + * Configuration parser + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#include + +#include "path.h" +#include "node.h" + +int config_parse(config_t *c, const char *filename, struct path *paths, struct node *nodes); + +int config_parse_path(config_setting_t *c, struct path *p); +int config_parse_node(config_setting_t *c, struct node *n); + +#endif /* _CONFIG_H_ */ diff --git a/src/cfg.c b/src/cfg.c new file mode 100644 index 000000000..9bb05bb2d --- /dev/null +++ b/src/cfg.c @@ -0,0 +1,66 @@ +/** + * Configuration parser + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include + +#include "config.h" +#include "utils.h" + +int config_parse(config_t *cfg, const char *filename, struct path *paths, struct node *nodes) +{ + if (!config_read_file(cfg, filename)) { + logger(ERROR, "Failed to parse configuration: %s in %s:%d", + config_error_text(cfg), config_error_file(cfg), config_error_line(cfg)); + exit(EXIT_FAILURE); + } + + config_setting_t *cfg_root = config_root_setting(cfg); + + // read global settings + int debug; + if (config_setting_lookup_int(cfg_root, "debug", &debug)) + logger(DEBUG, "Setting debug level to %u", debug); + + // read nodes + config_setting_t *cfg_nodes = config_setting_get_member(cfg_root, "nodes"); + if (cfg_nodes) { + for (int i=0; i < config_setting_length(cfg_nodes); i++) { + config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i); + config_parse_node(cfg_node, nodes); + } + } + + // read paths + config_setting_t *cfg_paths = config_setting_get_member(cfg_root, "paths"); + if (cfg_paths) { + for (int i=0; i < config_setting_length(cfg_paths); i++) { + for (int i=0; i < config_setting_length(cfg_paths); i++) { + config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i); + config_parse_path(cfg_path, &paths[i]); + } + } + } + + return CONFIG_TRUE; +} + +int config_parse_path(config_setting_t *c, struct path *p) +{ + + return CONFIG_TRUE; +} + +int config_parse_node(config_setting_t *c, struct node *n) +{ + config_setting_lookup_string(c, "name", (const char **) &n->name); + + + logger(DEBUG, "Found node: %s", n->name); + + return CONFIG_TRUE; +} +