More encapsulation

This commit is contained in:
Matthias Schiffer 2013-07-27 23:38:38 +02:00
parent 0b696dafb3
commit aced19e374
14 changed files with 111 additions and 53 deletions

View file

@ -10,6 +10,7 @@ add_executable(mmss
gmrf.cpp gmrf.cpp
iface.cpp iface.cpp
mmss.cpp mmss.cpp
network.cpp
protocol.cpp protocol.cpp
${FLEX_mmss_config_lex_OUTPUTS} ${FLEX_mmss_config_lex_OUTPUTS}
${BISON_mmss_config_parse_OUTPUTS} ${BISON_mmss_config_parse_OUTPUTS}

View file

@ -34,7 +34,7 @@
namespace MMSS { namespace MMSS {
class config_t { class config_t : public nocopy_t {
public: public:
std::list<std::shared_ptr<network_t>> network; std::list<std::shared_ptr<network_t>> network;
std::list<std::shared_ptr<node_t>> nodes; std::list<std::shared_ptr<node_t>> nodes;

View file

@ -36,7 +36,7 @@
namespace MMSS { namespace MMSS {
class context_t : public now_t { class context_t : public now_t, public nocopy_t {
private: private:
timeout_queue_t<event_t> event_queue; timeout_queue_t<event_t> event_queue;

View file

@ -28,12 +28,13 @@
#include "types.hpp" #include "types.hpp"
#include <cstring>
#include <memory> #include <memory>
namespace MMSS { namespace MMSS {
class event_t { class event_t : public nocopy_t {
public: public:
virtual ~event_t() {} virtual ~event_t() {}
@ -41,24 +42,31 @@ public:
}; };
class packet_t : public event_t { class packet_t : public event_t {
public: private:
uint64_t sent;
gmrf_addr_t source_addr; gmrf_addr_t source_addr;
std::weak_ptr<iface_t> dest; std::weak_ptr<iface_t> dest;
size_t len; size_t len;
std::unique_ptr<uint8_t[]> data; std::unique_ptr<uint8_t[]> data;
public:
packet_t(const gmrf_addr_t *source_addr0, const std::shared_ptr<iface_t> &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); virtual void handle(context_t *mmss);
}; };
class scheduled_t : public event_t { class scheduled_t : public event_t {
public: private:
std::weak_ptr<node_t> node; std::weak_ptr<node_t> node;
gmrf_scheduled_func f; gmrf_scheduled_func f;
void *arg; void *arg;
public:
scheduled_t(const std::shared_ptr<node_t> &node0, gmrf_scheduled_func f0, void *arg0) : node(node0), f(f0), arg(arg0) {}
virtual void handle(context_t *mmss); virtual void handle(context_t *mmss);
}; };

View file

@ -27,7 +27,6 @@
#include "context.hpp" #include "context.hpp"
#include "event.hpp" #include "event.hpp"
#include "iface.hpp" #include "iface.hpp"
#include "network.hpp"
#include "node.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) { 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_t*>(iface)->shared_from_this(); static_cast<iface_t*>(iface)->send(data, len, dest);
for (auto dest_iface : src_iface->get_network()->interfaces) {
if (gmrf_addr_equal(dest_iface->get_address(), dest)) {
enqueue(static_cast<node_t*>(gmrf)->get_context(), src_iface, dest_iface, data, len);
break;
}
}
return true; return true;
} }
bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) { bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) {
auto src_iface = static_cast<iface_t*>(iface)->shared_from_this(); static_cast<iface_t*>(iface)->send_bc(data, len);
for (auto dest_iface : src_iface->get_network()->interfaces) {
if (dest_iface != src_iface)
enqueue(static_cast<node_t*>(gmrf)->get_context(), src_iface, dest_iface, data, len);
}
return true; 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) { void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned delay) {
node_t *node = static_cast<node_t*>(gmrf); node_t *node = static_cast<node_t*>(gmrf);
std::shared_ptr<scheduled_t> scheduled = std::make_shared<scheduled_t>(); std::shared_ptr<scheduled_t> scheduled = std::make_shared<scheduled_t>(node->shared_from_this(), f, arg);
scheduled->node = node->shared_from_this();
scheduled->f = f;
scheduled->arg = arg;
node->get_context()->queue_event(std::move(scheduled), node->get_context()->now()+delay); node->get_context()->queue_event(std::move(scheduled), node->get_context()->now()+delay);
} }

View file

@ -43,18 +43,4 @@ void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<networ
node->add_iface(iface); node->add_iface(iface);
} }
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) {
std::shared_ptr<packet_t> packet = std::make_shared<packet_t>();
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);
}
} }

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include "types.hpp" #include "network.hpp"
#include <memory> #include <memory>
#include <string> #include <string>
@ -34,17 +34,16 @@
namespace MMSS { namespace MMSS {
class iface_t : public ::gmrf_iface, public std::enable_shared_from_this<iface_t> { class iface_t : public ::gmrf_iface, public std::enable_shared_from_this<iface_t>, public nocopy_t {
private: private:
iface_t(iface_t const&) = delete;
iface_t& operator=(iface_t const&) = delete;
node_t *node; node_t *node;
network_t *net; network_t *net;
std::string name; std::string name;
gmrf_addr_t address; gmrf_addr_t address;
void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &dest, const void *data, size_t len);
public: public:
iface_t(node_t *node0, network_t *net0, const std::string &name0, const gmrf_addr_t *address0) 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) {} : node(node0), net(net0), name(name0), address(*address0) {}
@ -64,9 +63,16 @@ public:
const gmrf_addr_t* get_address() const { const gmrf_addr_t* get_address() const {
return &address; 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_t> &node, const std::shared_ptr<network_t> &net, const std::string &name, const gmrf_addr_t *address); 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);
} }

View file

@ -27,8 +27,11 @@
#include "context.hpp" #include "context.hpp"
using namespace MMSS;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
MMSS::context_t().run(argc, argv); context_t().run(argc, argv);
return 0; return 0;
} }

57
mmss/network.cpp Normal file
View file

@ -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.
*/
#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<iface_t> &dest_iface) {
context_t *mmss = dest_iface->get_node()->get_context();
std::shared_ptr<packet_t> packet = std::make_shared<packet_t>(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);
}
}
}

View file

@ -34,12 +34,14 @@
namespace MMSS { namespace MMSS {
class network_t { class network_t : public nocopy_t {
private: private:
std::string name; std::string name;
size_t mtu; size_t mtu;
void enqueue(const void *data, size_t len, const iface_t *src_iface, const std::shared_ptr<iface_t> &dest_iface);
public: public:
std::list<std::shared_ptr<iface_t>> interfaces; std::list<std::shared_ptr<iface_t>> interfaces;
@ -52,6 +54,10 @@ public:
size_t get_mtu() const { size_t get_mtu() const {
return mtu; 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);
}; };
} }

View file

@ -36,7 +36,7 @@
namespace MMSS { namespace MMSS {
class node_t : public ::gmrf, public std::enable_shared_from_this<node_t> { class node_t : public ::gmrf, public std::enable_shared_from_this<node_t>, public nocopy_t {
private: private:
node_t(node_t const&) = delete; node_t(node_t const&) = delete;
node_t& operator=(node_t const&) = delete; node_t& operator=(node_t const&) = delete;

View file

@ -39,7 +39,7 @@ extern "C" {
namespace MMSS { namespace MMSS {
class protocol_t { class protocol_t : public nocopy_t {
private: private:
void *handle; void *handle;
const mmss_protocol_t *proto; const mmss_protocol_t *proto;

View file

@ -34,7 +34,7 @@
namespace MMSS { namespace MMSS {
template<typename T> class timeout_queue_t { template<typename T> class timeout_queue_t : public nocopy_t {
struct element_t { struct element_t {
std::shared_ptr<T> data; std::shared_ptr<T> data;
uint64_t timeout; uint64_t timeout;

View file

@ -42,6 +42,15 @@ struct gmrf_iface {};
namespace MMSS { 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 context_t;
class config_t; class config_t;
class iface_t; class iface_t;