More encapsulation
This commit is contained in:
parent
0b696dafb3
commit
aced19e374
14 changed files with 111 additions and 53 deletions
|
@ -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}
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
|
||||
namespace MMSS {
|
||||
|
||||
class config_t {
|
||||
class config_t : public nocopy_t {
|
||||
public:
|
||||
std::list<std::shared_ptr<network_t>> network;
|
||||
std::list<std::shared_ptr<node_t>> nodes;
|
||||
|
|
|
@ -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_t> event_queue;
|
||||
|
||||
|
|
|
@ -28,12 +28,13 @@
|
|||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
|
||||
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<iface_t> dest;
|
||||
|
||||
size_t len;
|
||||
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);
|
||||
};
|
||||
|
||||
class scheduled_t : public event_t {
|
||||
public:
|
||||
private:
|
||||
std::weak_ptr<node_t> node;
|
||||
gmrf_scheduled_func f;
|
||||
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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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_t*>(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<node_t*>(gmrf)->get_context(), src_iface, dest_iface, data, len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
static_cast<iface_t*>(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_t*>(iface)->shared_from_this();
|
||||
|
||||
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);
|
||||
}
|
||||
static_cast<iface_t*>(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<node_t*>(gmrf);
|
||||
|
||||
std::shared_ptr<scheduled_t> scheduled = std::make_shared<scheduled_t>();
|
||||
|
||||
scheduled->node = node->shared_from_this();
|
||||
scheduled->f = f;
|
||||
scheduled->arg = arg;
|
||||
|
||||
std::shared_ptr<scheduled_t> scheduled = std::make_shared<scheduled_t>(node->shared_from_this(), f, arg);
|
||||
node->get_context()->queue_event(std::move(scheduled), node->get_context()->now()+delay);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,18 +43,4 @@ void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<networ
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
#include "network.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
@ -34,17 +34,16 @@
|
|||
|
||||
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:
|
||||
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<iface_t> &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_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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
57
mmss/network.cpp
Normal file
57
mmss/network.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<iface_t> &dest_iface);
|
||||
|
||||
public:
|
||||
std::list<std::shared_ptr<iface_t>> 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);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
|
||||
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:
|
||||
node_t(node_t const&) = delete;
|
||||
node_t& operator=(node_t const&) = delete;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
|
||||
namespace MMSS {
|
||||
|
||||
template<typename T> class timeout_queue_t {
|
||||
template<typename T> class timeout_queue_t : public nocopy_t {
|
||||
struct element_t {
|
||||
std::shared_ptr<T> data;
|
||||
uint64_t timeout;
|
||||
|
|
|
@ -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;
|
||||
|
|
Reference in a new issue