From aced19e374789be28eace052a4bf078afa41315f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 27 Jul 2013 23:38:38 +0200 Subject: More encapsulation --- mmss/CMakeLists.txt | 1 + mmss/config.hpp | 2 +- mmss/context.hpp | 2 +- mmss/event.hpp | 18 ++++++++++++----- mmss/gmrf.cpp | 24 +++------------------- mmss/iface.cpp | 14 ------------- mmss/iface.hpp | 18 +++++++++++------ mmss/mmss.cpp | 5 ++++- mmss/network.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mmss/network.hpp | 8 +++++++- mmss/node.hpp | 2 +- mmss/protocol.hpp | 2 +- mmss/queue.hpp | 2 +- mmss/types.hpp | 9 +++++++++ 14 files changed, 111 insertions(+), 53 deletions(-) create mode 100644 mmss/network.cpp diff --git a/mmss/CMakeLists.txt b/mmss/CMakeLists.txt index bbd6f5b..7e942f5 100644 --- a/mmss/CMakeLists.txt +++ b/mmss/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(mmss gmrf.cpp iface.cpp mmss.cpp + network.cpp protocol.cpp ${FLEX_mmss_config_lex_OUTPUTS} ${BISON_mmss_config_parse_OUTPUTS} diff --git a/mmss/config.hpp b/mmss/config.hpp index 3f7f272..48f0634 100644 --- a/mmss/config.hpp +++ b/mmss/config.hpp @@ -34,7 +34,7 @@ namespace MMSS { -class config_t { +class config_t : public nocopy_t { public: std::list> network; std::list> nodes; diff --git a/mmss/context.hpp b/mmss/context.hpp index e5e15f1..8282c77 100644 --- a/mmss/context.hpp +++ b/mmss/context.hpp @@ -36,7 +36,7 @@ namespace MMSS { -class context_t : public now_t { +class context_t : public now_t, public nocopy_t { private: timeout_queue_t event_queue; diff --git a/mmss/event.hpp b/mmss/event.hpp index e531c7c..c346781 100644 --- a/mmss/event.hpp +++ b/mmss/event.hpp @@ -28,12 +28,13 @@ #include "types.hpp" +#include #include namespace MMSS { -class event_t { +class event_t : public nocopy_t { public: virtual ~event_t() {} @@ -41,24 +42,31 @@ public: }; class packet_t : public event_t { -public: - uint64_t sent; - +private: gmrf_addr_t source_addr; std::weak_ptr dest; size_t len; std::unique_ptr data; +public: + packet_t(const gmrf_addr_t *source_addr0, const std::shared_ptr &dest0, const void *data0, size_t len0) + : source_addr(*source_addr0), dest(dest0), len(len0), data(new uint8_t[len0]) { + std::memcpy(data.get(), data0, len0); + } + virtual void handle(context_t *mmss); }; class scheduled_t : public event_t { -public: +private: std::weak_ptr node; gmrf_scheduled_func f; void *arg; +public: + scheduled_t(const std::shared_ptr &node0, gmrf_scheduled_func f0, void *arg0) : node(node0), f(f0), arg(arg0) {} + virtual void handle(context_t *mmss); }; diff --git a/mmss/gmrf.cpp b/mmss/gmrf.cpp index c83b184..b97f1e5 100644 --- a/mmss/gmrf.cpp +++ b/mmss/gmrf.cpp @@ -27,7 +27,6 @@ #include "context.hpp" #include "event.hpp" #include "iface.hpp" -#include "network.hpp" #include "node.hpp" @@ -48,25 +47,13 @@ size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { } bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) { - auto src_iface = static_cast(iface)->shared_from_this(); - - for (auto dest_iface : src_iface->get_network()->interfaces) { - if (gmrf_addr_equal(dest_iface->get_address(), dest)) { - enqueue(static_cast(gmrf)->get_context(), src_iface, dest_iface, data, len); - break; - } - } + static_cast(iface)->send(data, len, dest); return true; } bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) { - auto src_iface = static_cast(iface)->shared_from_this(); - - for (auto dest_iface : src_iface->get_network()->interfaces) { - if (dest_iface != src_iface) - enqueue(static_cast(gmrf)->get_context(), src_iface, dest_iface, data, len); - } + static_cast(iface)->send_bc(data, len); return true; } @@ -74,12 +61,7 @@ bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, siz void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned delay) { node_t *node = static_cast(gmrf); - std::shared_ptr scheduled = std::make_shared(); - - scheduled->node = node->shared_from_this(); - scheduled->f = f; - scheduled->arg = arg; - + std::shared_ptr scheduled = std::make_shared(node->shared_from_this(), f, arg); node->get_context()->queue_event(std::move(scheduled), node->get_context()->now()+delay); } diff --git a/mmss/iface.cpp b/mmss/iface.cpp index 85079b6..31befbb 100644 --- a/mmss/iface.cpp +++ b/mmss/iface.cpp @@ -43,18 +43,4 @@ void add_iface(const std::shared_ptr &node, const std::shared_ptradd_iface(iface); } -void enqueue(context_t *mmss, const std::shared_ptr &source, const std::shared_ptr &dest, const void *data, size_t len) { - std::shared_ptr packet = std::make_shared(); - - packet->sent = mmss->now(); - packet->source_addr = *source->get_address(); - packet->dest = dest; - packet->len = len; - - packet->data.reset(new uint8_t[len]); - std::memcpy(packet->data.get(), data, len); - - mmss->queue_event(std::move(packet), mmss->now()+1); -} - } diff --git a/mmss/iface.hpp b/mmss/iface.hpp index c922815..45e9d6a 100644 --- a/mmss/iface.hpp +++ b/mmss/iface.hpp @@ -26,7 +26,7 @@ #pragma once -#include "types.hpp" +#include "network.hpp" #include #include @@ -34,17 +34,16 @@ namespace MMSS { -class iface_t : public ::gmrf_iface, public std::enable_shared_from_this { +class iface_t : public ::gmrf_iface, public std::enable_shared_from_this, public nocopy_t { private: - iface_t(iface_t const&) = delete; - iface_t& operator=(iface_t const&) = delete; - node_t *node; network_t *net; std::string name; gmrf_addr_t address; + void enqueue(context_t *mmss, const std::shared_ptr &dest, const void *data, size_t len); + public: iface_t(node_t *node0, network_t *net0, const std::string &name0, const gmrf_addr_t *address0) : node(node0), net(net0), name(name0), address(*address0) {} @@ -64,9 +63,16 @@ public: const gmrf_addr_t* get_address() const { return &address; } + + void send(const void *data, size_t len, const gmrf_addr_t *dest) { + net->send(data, len, this, dest); + } + + void send_bc(const void *data, size_t len) { + net->send_bc(data, len, this); + } }; 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 42d7dea..846d79b 100644 --- a/mmss/mmss.cpp +++ b/mmss/mmss.cpp @@ -27,8 +27,11 @@ #include "context.hpp" +using namespace MMSS; + + int main(int argc, char *argv[]) { - MMSS::context_t().run(argc, argv); + context_t().run(argc, argv); return 0; } diff --git a/mmss/network.cpp b/mmss/network.cpp new file mode 100644 index 0000000..863f10b --- /dev/null +++ b/mmss/network.cpp @@ -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. +*/ + + +#include "network.hpp" +#include "context.hpp" +#include "node.hpp" + + +namespace MMSS { + +void network_t::enqueue(const void *data, size_t len, const iface_t *src_iface, const std::shared_ptr &dest_iface) { + context_t *mmss = dest_iface->get_node()->get_context(); + + std::shared_ptr packet = std::make_shared(src_iface->get_address(), dest_iface, data, len); + mmss->queue_event(std::move(packet), mmss->now()+1); +} + +void network_t::send(const void *data, size_t len, const iface_t *src_iface, const gmrf_addr_t *dest) { + for (auto dest_iface : interfaces) { + if (gmrf_addr_equal(dest_iface->get_address(), dest)) { + enqueue(data, len, src_iface, dest_iface); + return; + } + } +} + +void network_t::send_bc(const void *data, size_t len, const iface_t *src_iface) { + for (auto dest_iface : interfaces) { + if (dest_iface.get() != src_iface) + enqueue(data, len, src_iface, dest_iface); + } +} + +} diff --git a/mmss/network.hpp b/mmss/network.hpp index ecd5caa..0beb6ae 100644 --- a/mmss/network.hpp +++ b/mmss/network.hpp @@ -34,12 +34,14 @@ namespace MMSS { -class network_t { +class network_t : public nocopy_t { private: std::string name; size_t mtu; + void enqueue(const void *data, size_t len, const iface_t *src_iface, const std::shared_ptr &dest_iface); + public: std::list> interfaces; @@ -52,6 +54,10 @@ public: size_t get_mtu() const { return mtu; } + + void send(const void *data, size_t len, const iface_t *src_iface, const gmrf_addr_t *dest); + void send_bc(const void *data, size_t len, const iface_t *src_iface); + }; } diff --git a/mmss/node.hpp b/mmss/node.hpp index 7daf57c..04b9ae7 100644 --- a/mmss/node.hpp +++ b/mmss/node.hpp @@ -36,7 +36,7 @@ namespace MMSS { -class node_t : public ::gmrf, public std::enable_shared_from_this { +class node_t : public ::gmrf, public std::enable_shared_from_this, public nocopy_t { private: node_t(node_t const&) = delete; node_t& operator=(node_t const&) = delete; diff --git a/mmss/protocol.hpp b/mmss/protocol.hpp index 551f54e..fcf54ae 100644 --- a/mmss/protocol.hpp +++ b/mmss/protocol.hpp @@ -39,7 +39,7 @@ extern "C" { namespace MMSS { -class protocol_t { +class protocol_t : public nocopy_t { private: void *handle; const mmss_protocol_t *proto; diff --git a/mmss/queue.hpp b/mmss/queue.hpp index 92c2199..16051ee 100644 --- a/mmss/queue.hpp +++ b/mmss/queue.hpp @@ -34,7 +34,7 @@ namespace MMSS { -template class timeout_queue_t { +template class timeout_queue_t : public nocopy_t { struct element_t { std::shared_ptr data; uint64_t timeout; diff --git a/mmss/types.hpp b/mmss/types.hpp index a976823..36ad77b 100644 --- a/mmss/types.hpp +++ b/mmss/types.hpp @@ -42,6 +42,15 @@ struct gmrf_iface {}; namespace MMSS { +class nocopy_t { +protected: + nocopy_t() = default; + +private: + nocopy_t(nocopy_t const&) = delete; + nocopy_t& operator=(nocopy_t const&) = delete; +}; + class context_t; class config_t; class iface_t; -- cgit v1.2.3