From 0b696dafb3c8a6efebeaceab64fbdebdb399f7b4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 27 Jul 2013 22:28:34 +0200 Subject: Get rid of mmss.hpp --- mmss/config.cpp | 10 +++----- mmss/config.hpp | 51 ++++++++++++++++++++++++++++++++++++ mmss/config.y | 10 ++++---- mmss/context.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++- mmss/context.hpp | 56 ++++++++++++++++++++++++++++++++++++++++ mmss/event.cpp | 1 - mmss/gmrf.cpp | 8 +++--- mmss/iface.cpp | 5 ++-- mmss/iface.hpp | 3 +++ mmss/mmss.cpp | 75 ++--------------------------------------------------- mmss/mmss.hpp | 77 ------------------------------------------------------- mmss/network.hpp | 57 ++++++++++++++++++++++++++++++++++++++++ mmss/now.hpp | 8 +++++- mmss/protocol.cpp | 2 +- 14 files changed, 257 insertions(+), 171 deletions(-) create mode 100644 mmss/config.hpp create mode 100644 mmss/context.hpp delete mode 100644 mmss/mmss.hpp create mode 100644 mmss/network.hpp 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 #include @@ -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 net = std::make_shared(); - - net->name = name; - net->mtu = 1500; - - conf->network.push_back(net); + conf->network.push_back(std::make_shared(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 + 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 +#include + + +namespace MMSS { + +class config_t { +public: + std::list> network; + std::list> 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 + #include + #include - #include - #include + 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 +#include +#include + 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 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 net0 = std::make_shared("net0"); + std::shared_ptr net1 = std::make_shared("net1"); + + std::shared_ptr node1 = node_t::create(this, "node1", 1, proto); + std::shared_ptr node2 = node_t::create(this, "node2", 2, proto); + std::shared_ptr node3 = node_t::create(this, "node3", 3, proto); + + std::list> 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/context.hpp b/mmss/context.hpp new file mode 100644 index 0000000..e5e15f1 --- /dev/null +++ b/mmss/context.hpp @@ -0,0 +1,56 @@ +/* + Copyright (c) 2013, 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. +*/ + + +#pragma once + +#include "event.hpp" +#include "queue.hpp" + +#include +#include +#include + + +namespace MMSS { + +class context_t : public now_t { +private: + timeout_queue_t event_queue; + +public: + context_t() : event_queue(this) {} + + void queue_event(std::shared_ptr &&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, ...); + + void run(int argc, char *argv[]); +}; + +} 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)->get_network()->mtu; + return static_cast(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 @@ -53,7 +54,7 @@ void enqueue(context_t *mmss, const std::shared_ptr &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, const std::shared_ptr &net, const std::string &name, const gmrf_addr_t *address); +void enqueue(context_t *mmss, const std::shared_ptr &source, const std::shared_ptr &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 -#include -#include - - -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 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 net0 = std::make_shared(); - std::shared_ptr net1 = std::make_shared(); - - net0->mtu = 1500; - net1->mtu = 1500; - - std::shared_ptr node1 = node_t::create(&mmss, "node1", 1, proto); - std::shared_ptr node2 = node_t::create(&mmss, "node2", 2, proto); - std::shared_ptr node3 = node_t::create(&mmss, "node3", 3, proto); - - std::list> 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/mmss.hpp b/mmss/mmss.hpp deleted file mode 100644 index f9284aa..0000000 --- a/mmss/mmss.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - Copyright (c) 2013, 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. -*/ - - -#pragma once - -#include "event.hpp" -#include "queue.hpp" - -#include -#include -#include - - -namespace MMSS { - -class context_t : public now_t { -public: - timeout_queue_t event_queue; - - context_t() : event_queue(this) {} - - 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> network; - std::list> nodes; -}; - -class network_t { -public: - std::string name; - - std::list> interfaces; - size_t mtu; -}; - - -bool read_config(context_t *mmss, config_t *conf, const char *filename); - -void add_iface(const std::shared_ptr &node, const std::shared_ptr &net, const std::string &name, const gmrf_addr_t *address); - -void enqueue(context_t *mmss, const std::shared_ptr &source, const std::shared_ptr &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/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 + 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 +#include + + +namespace MMSS { + +class network_t { +private: + std::string name; + + size_t mtu; + +public: + std::list> 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 -- cgit v1.2.3