Finish data encapsulation

This commit is contained in:
Matthias Schiffer 2013-07-28 00:39:43 +02:00
parent aced19e374
commit d5c57ad133
5 changed files with 28 additions and 24 deletions

View file

@ -114,10 +114,10 @@ void context_t::run(int argc, char *argv[]) {
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);
iface_t::add(node1, net0, "mmss0", &addr1);
iface_t::add(node2, net0, "mmss0", &addr2);
iface_t::add(node2, net1, "mmss1", &addr3);
iface_t::add(node3, net1, "mmss1", &addr4);
while (true) {
if (now() > 10000000)

View file

@ -34,13 +34,13 @@
namespace MMSS {
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) {
std::shared_ptr<iface_t> iface = std::make_shared<iface_t>(node.get(), net.get(), name, address);
node->interfaces.push_back(iface);
net->interfaces.push_back(iface);
std::shared_ptr<iface_t> iface_t::add(const std::shared_ptr<node_t> &node, const std::shared_ptr<network_t> &net, const std::string &name, const gmrf_addr_t *address) {
std::shared_ptr<iface_t> iface = std::shared_ptr<iface_t>(new iface_t(node.get(), net.get(), name, address));
net->add_iface(iface);
node->add_iface(iface);
return iface;
}
}

View file

@ -44,10 +44,10 @@ private:
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) {}
public:
node_t* get_node() const {
return node;
}
@ -71,8 +71,8 @@ public:
void send_bc(const void *data, size_t len) {
net->send_bc(data, len, this);
}
static std::shared_ptr<iface_t> add(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);
}

View file

@ -28,8 +28,8 @@
#include "types.hpp"
#include <list>
#include <memory>
#include <unordered_set>
namespace MMSS {
@ -37,16 +37,19 @@ namespace MMSS {
class network_t : public nocopy_t {
private:
std::string name;
size_t mtu;
std::unordered_set<std::shared_ptr<iface_t>> interfaces;
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;
network_t(const std::string &name0) : name(name0), mtu(1500) {}
void add_iface(const std::shared_ptr<iface_t> &iface) {
interfaces.insert(iface);
}
const std::string& get_name() const {
return name;
}
@ -57,7 +60,6 @@ public:
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

@ -29,9 +29,9 @@
#include "iface.hpp"
#include "protocol.hpp"
#include <list>
#include <memory>
#include <string>
#include <unordered_set>
namespace MMSS {
@ -49,18 +49,24 @@ private:
unsigned rand_seed;
std::shared_ptr<const protocol_t> proto;
std::unordered_set<std::shared_ptr<iface_t>> interfaces;
node_t(context_t *mmss0, const std::string &name0, unsigned rand_seed0, const std::shared_ptr<const protocol_t> &proto0) :
mmss(mmss0), name(name0), gmrf_ctx(nullptr), rand_seed(rand_seed0), proto(proto0) {
}
public:
std::list<std::shared_ptr<iface_t>> interfaces;
public:
static std::shared_ptr<node_t> create(context_t *mmss, const std::string &name, unsigned rand_seed, const std::shared_ptr<const protocol_t> &proto) {
auto node = std::shared_ptr<node_t>(new node_t(mmss, name, rand_seed, proto));
node->gmrf_ctx = proto->init(node.get());
return node;
}
void add_iface(const std::shared_ptr<iface_t> &iface) {
interfaces.insert(iface);
proto->add_iface(this, gmrf_ctx, iface.get());
}
context_t* get_context() const {
return mmss;
}
@ -73,10 +79,6 @@ public:
return rand_r(&rand_seed);
}
void add_iface(const std::shared_ptr<iface_t> &iface) {
proto->add_iface(this, gmrf_ctx, iface.get());
}
void handle_packet(const std::shared_ptr<iface_t> &iface, const gmrf_addr_t *source, const void *data, size_t len) {
proto->handle_packet(this, gmrf_ctx, iface.get(), source, data, len);
}