/* Copyright (c) 2013-2014, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "context.hpp" #include "lex.hpp" #include "network.hpp" #include "node.hpp" #include "protocol.hpp" #include #include #include #include #include namespace MMSS { 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 proto = protocol_t::load(mmss, module); if (!proto) return false; if (def) default_proto = proto; 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; } current_net = &networks.insert(std::make_pair(name, std::make_shared(mmss, name, rand_seed++))).first->second; return true; } bool config_t::set_packet_loss(float etx) { if (etx < 1) { mmss->logf(LOG_ERR, "config error: network `%s': ETX must be at least 1.0", (*current_net)->get_name().c_str()); return false; } (*current_net)->set_packet_loss(etx); return true; } bool config_t::set_packet_loss(float etx_min, float etx_max, float period, float phase) { if (etx_min < 1) { mmss->logf(LOG_ERR, "config error: network `%s': ETX must be at least 1.0", (*current_net)->get_name().c_str()); return false; } if (etx_max < etx_min) { mmss->logf(LOG_ERR, "config error: network `%s': max ETX smaller than min ETX", (*current_net)->get_name().c_str()); return false; } if (period <= 0) { mmss->logf(LOG_ERR, "config error: network `%s': zero or negative period", (*current_net)->get_name().c_str()); return false; } (*current_net)->set_packet_loss(etx_min, etx_max, period, phase); 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 *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 config_t::read(context_t *mmss, const char *filename) { std::shared_ptr conf = std::shared_ptr(new config_t(mmss)); char *oldcwd = get_current_dir_name(); char *filename2 = NULL; char *dir = NULL; FILE *file; mmss_config_pstate *ps; int token; YYSTYPE token_val; YYLTYPE loc = {1, 0, 1, 0}; int parse_ret; std::stack strings; if (!filename) { file = stdin; } else { file = fopen(filename, "r"); if (!file) { mmss->logf(LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno)); return std::shared_ptr(); } } lex_t scanner(file); ps = mmss_config_pstate_new(); if (filename) { filename2 = strdup(filename); dir = dirname(filename2); if (chdir(dir)) { mmss->logf(LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir); conf.reset(); goto end_free; } } do { token = scanner.lex(&token_val, &loc); if (token < 0) { mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); conf.reset(); goto end_free; } if (token == TOK_STRING) strings.push(token_val.str); parse_ret = mmss_config_push_parse(ps, token, &token_val, &loc, conf, filename); } while (parse_ret == YYPUSH_MORE); if (parse_ret) conf.reset(); end_free: while (!strings.empty()) { std::free(strings.top()); strings.pop(); } mmss_config_pstate_delete(ps); if(chdir(oldcwd)) mmss->logf(LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno)); std::free(filename2); std::free(oldcwd); if (filename && file) std::fclose(file); return conf; } }