summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-07-27 13:38:55 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-07-27 13:38:55 +0200
commit1880d6addc275da4827a240e188bebd2d4cc3306 (patch)
treea6c1b47f1d78016ddd9a3f62ac85745005e8f04b
parentcb5d5a9397928b0c1c844f35e5cae14a52955526 (diff)
downloadgmrf-1880d6addc275da4827a240e188bebd2d4cc3306.tar
gmrf-1880d6addc275da4827a240e188bebd2d4cc3306.zip
Some minor API improvements
-rw-r--r--mmss/gmrf.cpp4
-rw-r--r--mmss/iface.cpp4
-rw-r--r--mmss/log.cpp2
-rw-r--r--mmss/mmss.cpp11
-rw-r--r--mmss/mmss.hpp27
-rw-r--r--mmss/now.hpp47
-rw-r--r--mmss/queue.hpp16
-rw-r--r--mmss/types.hpp8
-rw-r--r--mmss/util.hpp5
9 files changed, 69 insertions, 55 deletions
diff --git a/mmss/gmrf.cpp b/mmss/gmrf.cpp
index c71f681..08db8e6 100644
--- a/mmss/gmrf.cpp
+++ b/mmss/gmrf.cpp
@@ -92,11 +92,11 @@ void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned dela
scheduled->f = f;
scheduled->arg = arg;
- gmrf->mmss->scheduled_queue.put(std::move(scheduled), gmrf->mmss->now+delay);
+ gmrf->mmss->scheduled_queue.put(std::move(scheduled), gmrf->mmss->now()+delay);
}
gmrf_time_t gmrf_now(gmrf_t *gmrf) {
- return gmrf->mmss->now;
+ return gmrf->mmss->now();
}
void gmrf_random_bytes(gmrf_t *gmrf, void *buffer, size_t len) {
diff --git a/mmss/iface.cpp b/mmss/iface.cpp
index e4ddec7..f54c1ea 100644
--- a/mmss/iface.cpp
+++ b/mmss/iface.cpp
@@ -62,7 +62,7 @@ void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<networ
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->sent = mmss->now();
packet->source = source;
packet->dest = dest;
packet->len = len;
@@ -70,7 +70,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->packet_queue.put(std::move(packet), mmss->now+1);
+ mmss->packet_queue.put(std::move(packet), mmss->now()+1);
}
}
diff --git a/mmss/log.cpp b/mmss/log.cpp
index 4e57f12..79f4ef7 100644
--- a/mmss/log.cpp
+++ b/mmss/log.cpp
@@ -33,7 +33,7 @@ void logf(context_t *mmss, int priority, const char *format, ...) {
char buf[1024];
size_t pos = 0;
- pos += snprintf_safe(buf, sizeof(buf), "[% 5u.%03u] ", mmss->now/1000, mmss->now%1000);
+ pos += snprintf_safe(buf, sizeof(buf), "[% 5u.%03u] ", mmss->now()/1000, mmss->now()%1000);
std::va_list ap;
va_start(ap, format);
diff --git a/mmss/mmss.cpp b/mmss/mmss.cpp
index c3a4bc2..0e3b230 100644
--- a/mmss/mmss.cpp
+++ b/mmss/mmss.cpp
@@ -26,6 +26,7 @@
#include "mmss.hpp"
+#include <algorithm>
#include <cassert>
#include <cstdio>
@@ -44,7 +45,7 @@ static inline int timeout_min(int a, int b) {
else if (b < 0)
return a;
else
- return min(a, b);
+ return std::min(a, b);
}
@@ -52,10 +53,6 @@ static int get_queue_timeout(const context_t *mmss) {
return timeout_min(mmss->packet_queue.timeout(), mmss->scheduled_queue.timeout());
}
-uint64_t now(const context_t *mmss) {
- return mmss->now;
-}
-
void main(int argc, char *argv[]) {
if (argc != 2) {
std::fprintf(stderr, "usage: %s protocol_module\n", argv[0]);
@@ -96,7 +93,7 @@ void main(int argc, char *argv[]) {
add_iface(node3, net1, "mmss1", &addr4);
while (true) {
- if (mmss.now > 1000000000)
+ if (mmss.now() > 1000000000)
break;
int timeout = get_queue_timeout(&mmss);
@@ -110,7 +107,7 @@ void main(int argc, char *argv[]) {
assert(!mmss.packet_queue.get());
assert(!mmss.scheduled_queue.get());
- mmss.now += timeout;
+ mmss.time += timeout;
timeout = get_queue_timeout(&mmss);
}
diff --git a/mmss/mmss.hpp b/mmss/mmss.hpp
index f131ec9..28f3ddd 100644
--- a/mmss/mmss.hpp
+++ b/mmss/mmss.hpp
@@ -24,8 +24,7 @@
*/
-#ifndef _GMRF_MMSS_MMSS_HPP_
-#define _GMRF_MMSS_MMSS_HPP_
+#pragma once
#include "queue.hpp"
@@ -92,13 +91,12 @@ public:
namespace MMSS {
-class context_t {
+class context_t : public now_t {
public:
- uint64_t now;
timeout_queue_t<packet_t> packet_queue;
timeout_queue_t<scheduled_t> scheduled_queue;
- context_t() : now(0), packet_queue(this), scheduled_queue(this) {}
+ context_t() : packet_queue(this), scheduled_queue(this) {}
};
class config_t {
@@ -140,8 +138,6 @@ 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);
-uint64_t now(const context_t *mmss);
-
void dispatch(const std::shared_ptr<packet_t> &packet);
void run_scheduled(const std::shared_ptr<scheduled_t> &scheduled);
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);
@@ -149,20 +145,6 @@ void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std:
void logf(context_t *mmss, int priority, const char *format, ...);
-static inline int max(int a, int b) {
- return (a > b) ? a : b;
-}
-
-static inline int min(int a, int b) {
- return (a < b) ? a : b;
-}
-
-
-static inline size_t alignto(size_t l, size_t a) {
- return ((l+a-1)/a)*a;
-}
-
-
namespace Config {
void add_network(context_t *mmss, config_t *conf, const char *name);
@@ -170,6 +152,3 @@ void add_network(context_t *mmss, config_t *conf, const char *name);
}
}
-
-
-#endif /* _GMRF_MMSS_MMSS_HPP_ */
diff --git a/mmss/now.hpp b/mmss/now.hpp
new file mode 100644
index 0000000..0f2e2fe
--- /dev/null
+++ b/mmss/now.hpp
@@ -0,0 +1,47 @@
+/*
+ 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 <cstdint>
+
+
+namespace MMSS {
+
+class now_t {
+public:
+ uint64_t time;
+
+ now_t() : time(0) {}
+
+ uint64_t now() const {
+ return time;
+ }
+};
+
+}
diff --git a/mmss/queue.hpp b/mmss/queue.hpp
index 8efaa97..92c2199 100644
--- a/mmss/queue.hpp
+++ b/mmss/queue.hpp
@@ -24,14 +24,14 @@
*/
-#ifndef _GMRF_MMSS_QUEUE_HPP_
-#define _GMRF_MMSS_QUEUE_HPP_
+#pragma once
-#include "types.hpp"
+#include "now.hpp"
#include <list>
#include <memory>
+
namespace MMSS {
template<typename T> class timeout_queue_t {
@@ -42,12 +42,12 @@ template<typename T> class timeout_queue_t {
element_t(std::shared_ptr<T> &&data0, uint64_t timeout0) : data(std::move(data0)), timeout(timeout0) {}
};
- context_t *mmss;
+ const now_t *clock;
std::list<element_t> queue;
public:
- timeout_queue_t(context_t *mmss0) : mmss(mmss0) {}
+ timeout_queue_t(const now_t *clock0) : clock(clock0) {}
void put(std::shared_ptr<T> &&data, uint64_t timeout) {
auto it = queue.begin(), end = queue.end();
@@ -64,7 +64,7 @@ public:
element_t &el = queue.front();
- if (el.timeout > now(mmss))
+ if (el.timeout > clock->now())
return std::shared_ptr<T>();
std::shared_ptr<T> ret(std::move(el.data));
@@ -76,7 +76,7 @@ public:
if (queue.empty())
return -1;
- int diff = queue.front().timeout - now(mmss);
+ int diff = queue.front().timeout - clock->now();
if (diff < 0)
return 0;
@@ -86,5 +86,3 @@ public:
};
}
-
-#endif /* _GMRF_MMSS_QUEUE_HPP_ */
diff --git a/mmss/types.hpp b/mmss/types.hpp
index 4a145ae..ca1d290 100644
--- a/mmss/types.hpp
+++ b/mmss/types.hpp
@@ -24,8 +24,7 @@
*/
-#ifndef _GMRF_MMSS_TYPES_HPP_
-#define _GMRF_MMSS_TYPES_HPP_
+#pragma once
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
@@ -43,6 +42,7 @@ namespace MMSS {
class context_t;
class config_t;
class network_t;
+class now_t;
class packet_t;
class scheduled_t;
@@ -52,8 +52,4 @@ typedef ::gmrf_t node_t;
typedef ::gmrf_iface_t iface_t;
-uint64_t now(const context_t *mmss);
-
}
-
-#endif /* _GMRF_MMSS_TYPES_HPP_ */
diff --git a/mmss/util.hpp b/mmss/util.hpp
index 147a9aa..d4a9351 100644
--- a/mmss/util.hpp
+++ b/mmss/util.hpp
@@ -24,8 +24,7 @@
*/
-#ifndef _GMRF_MMSS_UTIL_HPP_
-#define _GMRF_MMSS_UTIL_HPP_
+#pragma once
#include <cstdarg>
#include <cstdio>
@@ -43,5 +42,3 @@ static inline int snprintf_safe(char *buffer, size_t size, const char *format, .
}
}
-
-#endif /* _GMRF_MMSS_UTIL_HPP_ */