Get rid of mmss.hpp
This commit is contained in:
parent
3756620213
commit
0b696dafb3
13 changed files with 209 additions and 123 deletions
|
@ -24,7 +24,8 @@
|
|||
*/
|
||||
|
||||
|
||||
#include "mmss.hpp"
|
||||
#include "context.hpp"
|
||||
#include "network.hpp"
|
||||
#include <config.ll.hpp>
|
||||
#include <config.yy.hpp>
|
||||
|
||||
|
@ -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<network_t> net = std::make_shared<network_t>();
|
||||
|
||||
net->name = name;
|
||||
net->mtu = 1500;
|
||||
|
||||
conf->network.push_back(net);
|
||||
conf->network.push_back(std::make_shared<network_t>(name));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
51
mmss/config.hpp
Normal file
51
mmss/config.hpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace MMSS {
|
||||
|
||||
class config_t {
|
||||
public:
|
||||
std::list<std::shared_ptr<network_t>> network;
|
||||
std::list<std::shared_ptr<node_t>> 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);
|
||||
|
||||
}
|
|
@ -34,10 +34,10 @@
|
|||
|
||||
|
||||
%code requires {
|
||||
#include <mmss.hpp>
|
||||
#include <config.hpp>
|
||||
#include <context.hpp>
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -24,9 +24,15 @@
|
|||
*/
|
||||
|
||||
|
||||
#include "mmss.hpp"
|
||||
#include "context.hpp"
|
||||
#include "config.hpp"
|
||||
#include "network.hpp"
|
||||
#include "node.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
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<const protocol_t> 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<network_t> net0 = std::make_shared<network_t>("net0");
|
||||
std::shared_ptr<network_t> net1 = std::make_shared<network_t>("net1");
|
||||
|
||||
std::shared_ptr<node_t> node1 = node_t::create(this, "node1", 1, proto);
|
||||
std::shared_ptr<node_t> node2 = node_t::create(this, "node2", 2, proto);
|
||||
std::shared_ptr<node_t> node3 = node_t::create(this, "node3", 3, proto);
|
||||
|
||||
std::list<std::shared_ptr<node_t>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,41 +37,20 @@
|
|||
namespace MMSS {
|
||||
|
||||
class context_t : public now_t {
|
||||
public:
|
||||
private:
|
||||
timeout_queue_t<event_t> event_queue;
|
||||
|
||||
public:
|
||||
context_t() : event_queue(this) {}
|
||||
|
||||
void queue_event(std::shared_ptr<event_t> &&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[]);
|
||||
};
|
||||
|
||||
class config_t {
|
||||
public:
|
||||
std::list<std::shared_ptr<network_t>> network;
|
||||
std::list<std::shared_ptr<node_t>> nodes;
|
||||
};
|
||||
|
||||
class network_t {
|
||||
public:
|
||||
std::string name;
|
||||
|
||||
std::list<std::shared_ptr<iface_t>> interfaces;
|
||||
size_t mtu;
|
||||
};
|
||||
|
||||
|
||||
bool read_config(context_t *mmss, config_t *conf, const char *filename);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
namespace Config {
|
||||
|
||||
void add_network(context_t *mmss, config_t *conf, const char *name);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include "event.hpp"
|
||||
#include "iface.hpp"
|
||||
#include "mmss.hpp"
|
||||
#include "node.hpp"
|
||||
|
||||
|
||||
|
|
|
@ -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_t*>(iface)->get_network()->mtu;
|
||||
return static_cast<iface_t*>(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) {
|
||||
|
|
|
@ -25,8 +25,9 @@
|
|||
|
||||
|
||||
#include "iface.hpp"
|
||||
#include "context.hpp"
|
||||
#include "network.hpp"
|
||||
#include "node.hpp"
|
||||
#include "mmss.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
|
@ -53,7 +54,7 @@ void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -66,4 +66,7 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
|
|
@ -24,82 +24,11 @@
|
|||
*/
|
||||
|
||||
|
||||
#include "mmss.hpp"
|
||||
#include "node.hpp"
|
||||
#include "context.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
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<const protocol_t> 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<network_t> net0 = std::make_shared<network_t>();
|
||||
std::shared_ptr<network_t> net1 = std::make_shared<network_t>();
|
||||
|
||||
net0->mtu = 1500;
|
||||
net1->mtu = 1500;
|
||||
|
||||
std::shared_ptr<node_t> node1 = node_t::create(&mmss, "node1", 1, proto);
|
||||
std::shared_ptr<node_t> node2 = node_t::create(&mmss, "node2", 2, proto);
|
||||
std::shared_ptr<node_t> node3 = node_t::create(&mmss, "node3", 3, proto);
|
||||
|
||||
std::list<std::shared_ptr<node_t>> 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;
|
||||
}
|
||||
|
|
57
mmss/network.hpp
Normal file
57
mmss/network.hpp
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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace MMSS {
|
||||
|
||||
class network_t {
|
||||
private:
|
||||
std::string name;
|
||||
|
||||
size_t mtu;
|
||||
|
||||
public:
|
||||
std::list<std::shared_ptr<iface_t>> 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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
|
||||
#include "protocol.hpp"
|
||||
#include "mmss.hpp"
|
||||
#include "context.hpp"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
|
|
Reference in a new issue