diff options
Diffstat (limited to 'mmss/config.cpp')
-rw-r--r-- | mmss/config.cpp | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/mmss/config.cpp b/mmss/config.cpp index 8e7cea3..0692d5f 100644 --- a/mmss/config.cpp +++ b/mmss/config.cpp @@ -26,6 +26,8 @@ #include "context.hpp" #include "network.hpp" +#include "node.hpp" +#include "protocol.hpp" #include <config.ll.hpp> #include <config.yy.hpp> @@ -38,12 +40,83 @@ namespace MMSS { -void config_t::add_network(const char *name) { - mmss->logf(LOG_NOTICE, "adding network `%s'", name); +bool config_t::load_proto(const char *name, const char *module, bool def) { + if (protos.find(name) != protos.end()) { + mmss->logf(LOG_ERR, "config error: duplicate protocol `%s'", name); + return false; + } + + + std::shared_ptr<const protocol_t> proto = protocol_t::load(mmss, module); + + if (!proto) + return false; + + if (def) + default_proto = proto; - network.push_back(std::make_shared<network_t>(name)); + protos.insert(std::make_pair(name, std::move(proto))); + + return true; } +bool config_t::add_network(const char *name) { + if (networks.find(name) != networks.end()) { + mmss->logf(LOG_ERR, "config error: duplicate network `%s'", name); + return false; + } + + + networks.insert(std::make_pair(name, std::make_shared<network_t>(name))); + + return true; +} + +bool config_t::add_node(const char *name, const char *proto) { + if (nodes.find(name) != nodes.end()) { + mmss->logf(LOG_ERR, "config error: duplicate node `%s'", name); + return false; + } + + const std::shared_ptr<const protocol_t> *proto_ptr = &default_proto; + + if (proto) { + auto it = protos.find(proto); + + if (it == protos.end()) { + mmss->logf(LOG_ERR, "config error: no protocol `%s' found", proto); + return false; + } + + proto_ptr = &it->second; + } + + if (!*proto_ptr) { + mmss->logf(LOG_ERR, "config error: node `%s': no protocol given and no default protocol defined", name); + return false; + } + + current_node = &nodes.insert(std::make_pair(name, node_t::create(mmss, name, rand_seed++, *proto_ptr))).first->second; + + return true; +} + +bool config_t::add_iface(const char *name, const char *net, const gmrf_addr_t *addr) { + // TODO duplicate name, address check + + auto it = networks.find(net); + + if (it == networks.end()) { + mmss->logf(LOG_ERR, "config error: no network `%s' found", net); + return false; + } + + iface_t::add(*current_node, it->second, name, addr); + + return true; +} + + std::shared_ptr<const config_t> config_t::read(context_t *mmss, const char *filename) { std::shared_ptr<config_t> conf = std::shared_ptr<config_t>(new config_t(mmss)); char *oldcwd = get_current_dir_name(); |