diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-27 22:28:34 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-27 22:28:34 +0200 |
commit | 0b696dafb3c8a6efebeaceab64fbdebdb399f7b4 (patch) | |
tree | 77b572850e3628d8a36de62e7b327542dfc70cb0 | |
parent | 37566202133915d76179e0fd0c7fad654fc4dda0 (diff) | |
download | gmrf-0b696dafb3c8a6efebeaceab64fbdebdb399f7b4.tar gmrf-0b696dafb3c8a6efebeaceab64fbdebdb399f7b4.zip |
Get rid of mmss.hpp
-rw-r--r-- | mmss/config.cpp | 10 | ||||
-rw-r--r-- | mmss/config.hpp | 51 | ||||
-rw-r--r-- | mmss/config.y | 10 | ||||
-rw-r--r-- | mmss/context.cpp | 65 | ||||
-rw-r--r-- | mmss/context.hpp (renamed from mmss/mmss.hpp) | 35 | ||||
-rw-r--r-- | mmss/event.cpp | 1 | ||||
-rw-r--r-- | mmss/gmrf.cpp | 8 | ||||
-rw-r--r-- | mmss/iface.cpp | 5 | ||||
-rw-r--r-- | mmss/iface.hpp | 3 | ||||
-rw-r--r-- | mmss/mmss.cpp | 75 | ||||
-rw-r--r-- | mmss/network.hpp | 57 | ||||
-rw-r--r-- | mmss/now.hpp | 8 | ||||
-rw-r--r-- | mmss/protocol.cpp | 2 |
13 files changed, 208 insertions, 122 deletions
diff --git a/mmss/config.cpp b/mmss/config.cpp index 5897720..89f1658 100644 --- a/mmss/config.cpp +++ b/mmss/config.cpp @@ -24,7 +24,8 @@ */ -#include "mmss.hpp" +#include "context.hpp" +#include "network.hpp" #include <config.ll.hpp> #include <config.yy.hpp> @@ -40,12 +41,7 @@ namespace Config { void add_network(context_t *mmss, config_t *conf, const char *name) { mmss->logf(LOG_NOTICE, "adding network `%s'", name); - std::shared_ptr<network_t> net = std::make_shared<network_t>(); - - net->name = name; - net->mtu = 1500; - - conf->network.push_back(net); + conf->network.push_back(std::make_shared<network_t>(name)); } } diff --git a/mmss/config.hpp b/mmss/config.hpp new file mode 100644 index 0000000..3f7f272 --- /dev/null +++ b/mmss/config.hpp @@ -0,0 +1,51 @@ +/* + Copyright (c) 2013, Matthias Schiffer <mschiffer@universe-factory.net> + 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. +*/ + + +#pragma once + +#include "types.hpp" + +#include <list> +#include <memory> + + +namespace MMSS { + +class config_t { +public: + std::list<std::shared_ptr<network_t>> network; + std::list<std::shared_ptr<node_t>> nodes; +}; + +namespace Config { + +void add_network(context_t *mmss, config_t *conf, const char *name); + +} + +bool read_config(context_t *mmss, config_t *conf, const char *filename); + +} diff --git a/mmss/config.y b/mmss/config.y index ca14690..452e383 100644 --- a/mmss/config.y +++ b/mmss/config.y @@ -34,10 +34,10 @@ %code requires { - #include <mmss.hpp> + #include <config.hpp> + #include <context.hpp> - #include <cstdio> - #include <string> + using namespace MMSS; } %union { @@ -57,7 +57,7 @@ %code { - void mmss_config_error(YYLTYPE *loc, MMSS::context_t *mmss, MMSS::config_t *conf, const char *filename, const char *s); + void mmss_config_error(YYLTYPE *loc, context_t *mmss, config_t *conf, const char *filename, const char *s); } @@ -89,6 +89,6 @@ boolean: TOK_YES { $$ = true; } %% -void mmss_config_error(YYLTYPE *loc, MMSS::context_t *mmss, MMSS::config_t *conf, const char *filename, const char *s) { +void mmss_config_error(YYLTYPE *loc, context_t *mmss, config_t *conf, const char *filename, const char *s) { mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); } diff --git a/mmss/context.cpp b/mmss/context.cpp index 36f2ea5..c398d3c 100644 --- a/mmss/context.cpp +++ b/mmss/context.cpp @@ -24,9 +24,15 @@ */ -#include "mmss.hpp" +#include "context.hpp" +#include "config.hpp" +#include "network.hpp" #include "node.hpp" +#include <algorithm> +#include <cassert> +#include <cstdio> + namespace MMSS { @@ -80,4 +86,61 @@ void context_t::logf(int priority, const char *format, ...) { va_end(ap); } +void context_t::run(int argc, char *argv[]) { + if (argc != 2) { + std::fprintf(stderr, "usage: %s protocol_module\n", argv[0]); + std::exit(1); + } + + std::shared_ptr<const protocol_t> proto = protocol_t::load(this, argv[1]); + if (!proto) + std::exit(1); + + config_t conf = {}; + + //read_config(this, &conf, "babel_test.mmss"); + + std::shared_ptr<network_t> net0 = std::make_shared<network_t>("net0"); + std::shared_ptr<network_t> net1 = std::make_shared<network_t>("net1"); + + std::shared_ptr<node_t> node1 = node_t::create(this, "node1", 1, proto); + std::shared_ptr<node_t> node2 = node_t::create(this, "node2", 2, proto); + std::shared_ptr<node_t> node3 = node_t::create(this, "node3", 3, proto); + + std::list<std::shared_ptr<node_t>> nodes; + + nodes.push_back(node1); + nodes.push_back(node2); + nodes.push_back(node3); + + gmrf_addr_t addr1 = {{1}}, addr2 = {{2}}, addr3 = {{3}}, addr4 = {{4}}; + add_iface(node1, net0, "mmss0", &addr1); + add_iface(node2, net0, "mmss0", &addr2); + add_iface(node2, net1, "mmss1", &addr3); + add_iface(node3, net1, "mmss1", &addr4); + + while (true) { + if (now() > 10000000) + break; + + int timeout = event_queue.timeout(); + + if (timeout < 0) { + fprintf(stderr, "nothing queued, deadlock occured.\n"); + break; + } + + if (timeout > 0) { + assert(!event_queue.get()); + + advance(timeout); + timeout = event_queue.timeout(); + } + + assert(timeout == 0); + + event_queue.get()->handle(this); + } +} + } diff --git a/mmss/mmss.hpp b/mmss/context.hpp index f9284aa..e5e15f1 100644 --- a/mmss/mmss.hpp +++ b/mmss/context.hpp @@ -37,41 +37,20 @@ namespace MMSS { class context_t : public now_t { -public: +private: timeout_queue_t<event_t> event_queue; +public: context_t() : event_queue(this) {} + void queue_event(std::shared_ptr<event_t> &&data, uint64_t timeout) { + event_queue.put(std::move(data), timeout); + } + void vlogf_orig(const node_t *orig, int priority, const char *format, std::va_list ap); void logf(int priority, const char *format, ...); -}; - -class config_t { -public: - std::list<std::shared_ptr<network_t>> network; - std::list<std::shared_ptr<node_t>> nodes; -}; - -class network_t { -public: - std::string name; - std::list<std::shared_ptr<iface_t>> interfaces; - size_t mtu; + void run(int argc, char *argv[]); }; - -bool read_config(context_t *mmss, config_t *conf, const char *filename); - -void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<network_t> &net, const std::string &name, const gmrf_addr_t *address); - -void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std::shared_ptr<iface_t> &dest, const void *data, size_t len); - - -namespace Config { - -void add_network(context_t *mmss, config_t *conf, const char *name); - -} - } diff --git a/mmss/event.cpp b/mmss/event.cpp index 5246e87..3e32605 100644 --- a/mmss/event.cpp +++ b/mmss/event.cpp @@ -26,7 +26,6 @@ #include "event.hpp" #include "iface.hpp" -#include "mmss.hpp" #include "node.hpp" diff --git a/mmss/gmrf.cpp b/mmss/gmrf.cpp index 0fc8527..c83b184 100644 --- a/mmss/gmrf.cpp +++ b/mmss/gmrf.cpp @@ -24,8 +24,10 @@ */ +#include "context.hpp" +#include "event.hpp" #include "iface.hpp" -#include "mmss.hpp" +#include "network.hpp" #include "node.hpp" @@ -42,7 +44,7 @@ const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface) { } size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { - return static_cast<iface_t*>(iface)->get_network()->mtu; + return static_cast<iface_t*>(iface)->get_network()->get_mtu(); } bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) { @@ -78,7 +80,7 @@ void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned dela scheduled->f = f; scheduled->arg = arg; - node->get_context()->event_queue.put(std::move(scheduled), node->get_context()->now()+delay); + node->get_context()->queue_event(std::move(scheduled), node->get_context()->now()+delay); } gmrf_time_t gmrf_now(gmrf_t *gmrf) { diff --git a/mmss/iface.cpp b/mmss/iface.cpp index 7de4789..85079b6 100644 --- a/mmss/iface.cpp +++ b/mmss/iface.cpp @@ -25,8 +25,9 @@ #include "iface.hpp" +#include "context.hpp" +#include "network.hpp" #include "node.hpp" -#include "mmss.hpp" #include <cstring> @@ -53,7 +54,7 @@ void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std: packet->data.reset(new uint8_t[len]); std::memcpy(packet->data.get(), data, len); - mmss->event_queue.put(std::move(packet), mmss->now()+1); + mmss->queue_event(std::move(packet), mmss->now()+1); } } diff --git a/mmss/iface.hpp b/mmss/iface.hpp index 322fcc1..c922815 100644 --- a/mmss/iface.hpp +++ b/mmss/iface.hpp @@ -66,4 +66,7 @@ public: } }; +void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<network_t> &net, const std::string &name, const gmrf_addr_t *address); +void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std::shared_ptr<iface_t> &dest, const void *data, size_t len); + } diff --git a/mmss/mmss.cpp b/mmss/mmss.cpp index 0358063..42d7dea 100644 --- a/mmss/mmss.cpp +++ b/mmss/mmss.cpp @@ -24,82 +24,11 @@ */ -#include "mmss.hpp" -#include "node.hpp" +#include "context.hpp" -#include <algorithm> -#include <cassert> -#include <cstdio> - - -namespace MMSS { - -void main(int argc, char *argv[]) { - if (argc != 2) { - std::fprintf(stderr, "usage: %s protocol_module\n", argv[0]); - std::exit(1); - } - - context_t mmss; - - std::shared_ptr<const protocol_t> proto = protocol_t::load(&mmss, argv[1]); - if (!proto) - std::exit(1); - - config_t conf = {}; - - //read_config(&mmss, &conf, "babel_test.mmss"); - - std::shared_ptr<network_t> net0 = std::make_shared<network_t>(); - std::shared_ptr<network_t> net1 = std::make_shared<network_t>(); - - net0->mtu = 1500; - net1->mtu = 1500; - - std::shared_ptr<node_t> node1 = node_t::create(&mmss, "node1", 1, proto); - std::shared_ptr<node_t> node2 = node_t::create(&mmss, "node2", 2, proto); - std::shared_ptr<node_t> node3 = node_t::create(&mmss, "node3", 3, proto); - - std::list<std::shared_ptr<node_t>> nodes; - - nodes.push_back(node1); - nodes.push_back(node2); - nodes.push_back(node3); - - gmrf_addr_t addr1 = {{1}}, addr2 = {{2}}, addr3 = {{3}}, addr4 = {{4}}; - add_iface(node1, net0, "mmss0", &addr1); - add_iface(node2, net0, "mmss0", &addr2); - add_iface(node2, net1, "mmss1", &addr3); - add_iface(node3, net1, "mmss1", &addr4); - - while (true) { - if (mmss.now() > 10000000) - break; - - int timeout = mmss.event_queue.timeout(); - - if (timeout < 0) { - fprintf(stderr, "nothing queued, deadlock occured.\n"); - break; - } - - if (timeout > 0) { - assert(!mmss.event_queue.get()); - - mmss.time += timeout; - timeout = mmss.event_queue.timeout(); - } - - assert(timeout == 0); - - mmss.event_queue.get()->handle(&mmss); - } -} - -} int main(int argc, char *argv[]) { - MMSS::main(argc, argv); + MMSS::context_t().run(argc, argv); return 0; } diff --git a/mmss/network.hpp b/mmss/network.hpp new file mode 100644 index 0000000..ecd5caa --- /dev/null +++ b/mmss/network.hpp @@ -0,0 +1,57 @@ +/* + Copyright (c) 2013, Matthias Schiffer <mschiffer@universe-factory.net> + 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. +*/ + + +#pragma once + +#include "types.hpp" + +#include <list> +#include <memory> + + +namespace MMSS { + +class network_t { +private: + std::string name; + + size_t mtu; + +public: + std::list<std::shared_ptr<iface_t>> interfaces; + + network_t(const std::string &name0) : name(name0), mtu(1500) {} + + const std::string& get_name() const { + return name; + } + + size_t get_mtu() const { + return mtu; + } +}; + +} diff --git a/mmss/now.hpp b/mmss/now.hpp index 0f2e2fe..9b14127 100644 --- a/mmss/now.hpp +++ b/mmss/now.hpp @@ -34,11 +34,17 @@ namespace MMSS { class now_t { -public: +private: uint64_t time; +protected: now_t() : time(0) {} + void advance(uint64_t d) { + time += d; + } + +public: uint64_t now() const { return time; } diff --git a/mmss/protocol.cpp b/mmss/protocol.cpp index 5688525..8dcc49d 100644 --- a/mmss/protocol.cpp +++ b/mmss/protocol.cpp @@ -25,7 +25,7 @@ #include "protocol.hpp" -#include "mmss.hpp" +#include "context.hpp" #include <dlfcn.h> |