From 0ffde34faac958a28842a952157852fd072834a5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 26 Jul 2013 14:22:18 +0200 Subject: Convert mmss to C++, add parser --- CMakeLists.txt | 7 ++- FindFLEX.cmake | 120 ++++++++++++++++++++++++++++++++++++ mmss/CMakeLists.txt | 21 ++++--- mmss/config.cpp | 125 ++++++++++++++++++++++++++++++++++++++ mmss/config.l | 129 +++++++++++++++++++++++++++++++++++++++ mmss/config.y | 93 ++++++++++++++++++++++++++++ mmss/iface.c | 100 ------------------------------ mmss/iface.cpp | 100 ++++++++++++++++++++++++++++++ mmss/log.c | 73 ---------------------- mmss/log.cpp | 86 ++++++++++++++++++++++++++ mmss/mmss.c | 134 ---------------------------------------- mmss/mmss.cpp | 137 +++++++++++++++++++++++++++++++++++++++++ mmss/mmss.h | 110 --------------------------------- mmss/mmss.hpp | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mmss/protocol.c | 51 ---------------- mmss/protocol.cpp | 51 ++++++++++++++++ mmss/queue.c | 78 ------------------------ mmss/queue.cpp | 78 ++++++++++++++++++++++++ mmss/queue.h | 42 ------------- mmss/queue.hpp | 42 +++++++++++++ mmss/schedule.c | 45 -------------- mmss/schedule.cpp | 43 +++++++++++++ mmss/types.h | 35 ----------- mmss/types.hpp | 42 +++++++++++++ 24 files changed, 1238 insertions(+), 676 deletions(-) create mode 100644 FindFLEX.cmake create mode 100644 mmss/config.cpp create mode 100644 mmss/config.l create mode 100644 mmss/config.y delete mode 100644 mmss/iface.c create mode 100644 mmss/iface.cpp delete mode 100644 mmss/log.c create mode 100644 mmss/log.cpp delete mode 100644 mmss/mmss.c create mode 100644 mmss/mmss.cpp delete mode 100644 mmss/mmss.h create mode 100644 mmss/mmss.hpp delete mode 100644 mmss/protocol.c create mode 100644 mmss/protocol.cpp delete mode 100644 mmss/queue.c create mode 100644 mmss/queue.cpp delete mode 100644 mmss/queue.h create mode 100644 mmss/queue.hpp delete mode 100644 mmss/schedule.c create mode 100644 mmss/schedule.cpp delete mode 100644 mmss/types.h create mode 100644 mmss/types.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 31d4066..08a9052 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,10 @@ cmake_minimum_required(VERSION 2.8.3) -project(GMRF C) +project(GMRF C CXX) + +set(CMAKE_MODULE_PATH ${GMRF_SOURCE_DIR}) + +find_package(BISON 2.5 REQUIRED) +find_package(FLEX REQUIRED) add_subdirectory(mmss) add_subdirectory(mmss-protocol) diff --git a/FindFLEX.cmake b/FindFLEX.cmake new file mode 100644 index 0000000..fe83369 --- /dev/null +++ b/FindFLEX.cmake @@ -0,0 +1,120 @@ +#============================================================================= +# Copyright 2009 Kitware, Inc. +# Copyright 2006 Tristan Carel +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * 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. +# +# * Neither the names of Kitware, Inc., the Insight Software Consortium, +# nor the names of their contributors may be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# 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. +# +# ------------------------------------------------------------------------------ +# +# The above copyright and license notice applies to distributions of +# CMake in source and binary form. Some source files contain additional +# notices of original copyright by their contributors; see each source +# for details. Third-party software packages supplied with CMake under +# compatible licenses provide their own copyright notices documented in +# corresponding subdirectories. +#============================================================================= + +FIND_PROGRAM(FLEX_EXECUTABLE flex DOC "path to the flex executable") +MARK_AS_ADVANCED(FLEX_EXECUTABLE) + +FIND_LIBRARY(FL_LIBRARY NAMES fl + DOC "Path to the fl library") + +FIND_PATH(FLEX_INCLUDE_DIR FlexLexer.h + DOC "Path to the flex headers") + +MARK_AS_ADVANCED(FL_LIBRARY FLEX_INCLUDE_DIR) + +SET(FLEX_INCLUDE_DIRS ${FLEX_INCLUDE_DIR}) +SET(FLEX_LIBRARIES ${FL_LIBRARY}) + +IF(FLEX_EXECUTABLE) + + EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version + OUTPUT_VARIABLE FLEX_version_output + ERROR_VARIABLE FLEX_version_error + RESULT_VARIABLE FLEX_version_result + OUTPUT_STRIP_TRAILING_WHITESPACE) + IF(NOT ${FLEX_version_result} EQUAL 0) + IF(FLEX_FIND_REQUIRED) + MESSAGE(SEND_ERROR "Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}") + ELSE() + MESSAGE("Command \"${FLEX_EXECUTABLE} --version\" failed with output:\n${FLEX_version_output}\n${FLEX_version_error}\nFLEX_VERSION will not be available") + ENDIF() + ELSE() + STRING(REGEX REPLACE "^flex (.*)$" "\\1" + FLEX_VERSION "${FLEX_version_output}") + ENDIF() + + #============================================================ + # FLEX_TARGET (public macro) + #============================================================ + # + MACRO(FLEX_TARGET Name Input Output) + SET(FLEX_TARGET_usage "FLEX_TARGET( [COMPILE_FLAGS ]") + IF(${ARGC} GREATER 3) + IF(${ARGC} EQUAL 5) + IF("${ARGV3}" STREQUAL "COMPILE_FLAGS") + SET(FLEX_EXECUTABLE_opts "${ARGV4}") + SEPARATE_ARGUMENTS(FLEX_EXECUTABLE_opts) + ELSE() + MESSAGE(SEND_ERROR ${FLEX_TARGET_usage}) + ENDIF() + ELSE() + MESSAGE(SEND_ERROR ${FLEX_TARGET_usage}) + ENDIF() + ENDIF() + + STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${Output}") + STRING(REPLACE "c" "h" _fileext ${_fileext}) + STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}" + OutputHeader "${Output}") + + ADD_CUSTOM_COMMAND(OUTPUT ${Output} ${OutputHeader} + COMMAND ${FLEX_EXECUTABLE} + ARGS ${FLEX_EXECUTABLE_opts} -o${Output} --header-file=${OutputHeader} ${Input} + DEPENDS ${Input} + COMMENT "[FLEX][${Name}] Building scanner with flex ${FLEX_VERSION}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + + SET(FLEX_${Name}_DEFINED TRUE) + SET(FLEX_${Name}_OUTPUTS ${Output} ${OutputHeader}) + SET(FLEX_${Name}_OUTPUT_HEADER ${OutputHeader}) + SET(FLEX_${Name}_INPUT ${Input}) + SET(FLEX_${Name}_COMPILE_FLAGS ${FLEX_EXECUTABLE_opts}) + ENDMACRO(FLEX_TARGET) + #============================================================ + + +ENDIF(FLEX_EXECUTABLE) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(FLEX REQUIRED_VARS FLEX_EXECUTABLE + VERSION_VAR FLEX_VERSION) + +# FindFLEX.cmake ends here diff --git a/mmss/CMakeLists.txt b/mmss/CMakeLists.txt index c200d7a..cd719e0 100644 --- a/mmss/CMakeLists.txt +++ b/mmss/CMakeLists.txt @@ -1,11 +1,18 @@ -include_directories(${GMRF_SOURCE_DIR}/include) +include_directories(${GMRF_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) + +FLEX_TARGET(mmss_config_lex config.l ${CMAKE_CURRENT_BINARY_DIR}/config.ll.cpp) +BISON_TARGET(mmss_config_parse config.y ${CMAKE_CURRENT_BINARY_DIR}/config.yy.cpp) add_executable(mmss - iface.c - log.c - mmss.c - protocol.c - queue.c - schedule.c + config.cpp + iface.cpp + log.cpp + mmss.cpp + protocol.cpp + queue.cpp + schedule.cpp + ${FLEX_mmss_config_lex_OUTPUTS} + ${BISON_mmss_config_parse_OUTPUTS} ) target_link_libraries(mmss dl) +set_target_properties(mmss PROPERTIES COMPILE_FLAGS -std=c++11) diff --git a/mmss/config.cpp b/mmss/config.cpp new file mode 100644 index 0000000..0be95fb --- /dev/null +++ b/mmss/config.cpp @@ -0,0 +1,125 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" +#include +#include + +#include + + +void mmss_config_add_network(mmss_t *mmss, mmss_config_t *conf, const char *name) { + mmss_logf(mmss, LOG_NOTICE, "Adding network `%s'", name); + + mmss_network_t *net = new mmss_network_t; + + net->name = strdup(name); + net->mtu = 1500; + + net->next = conf->networks; + conf->networks = net; +} + +bool mmss_read_config(mmss_t *mmss, mmss_config_t *conf, const char *filename) { + bool ret = true; + char *oldcwd = get_current_dir_name(); + char *filename2 = NULL; + char *dir = NULL; + FILE *file; + yyscan_t scanner; + mmss_config_pstate *ps; + mmss_string_stack_t *strings = NULL; + int token; + YYSTYPE token_val; + YYLTYPE loc = {1, 0, 1, 0}; + int parse_ret; + + + mmss_config_yylex_init(&scanner); + ps = mmss_config_pstate_new(); + + if (!filename) { + file = stdin; + } + else { + file = fopen(filename, "r"); + if (!file) { + mmss_logf(mmss, LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno)); + ret = false; + goto end_free; + } + } + + mmss_config_yyset_in(file, scanner); + + if (filename) { + filename2 = strdup(filename); + dir = dirname(filename2); + + if (chdir(dir)) { + mmss_logf(mmss, LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir); + ret = false; + goto end_free; + } + } + + do { + token = mmss_config_yylex(&token_val, &loc, scanner); + + if (token < 0) { + mmss_logf(mmss, LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); + ret = false; + goto end_free; + } + + if (token == TOK_STRING) { + token_val.str->next = strings; + strings = token_val.str; + } + + parse_ret = mmss_config_push_parse(ps, token, &token_val, &loc, mmss, conf, filename); + } while (parse_ret == YYPUSH_MORE); + + if (parse_ret) + ret = false; + + end_free: + mmss_string_stack_free(strings); + + mmss_config_pstate_delete(ps); + mmss_config_yylex_destroy(scanner); + + if(chdir(oldcwd)) + mmss_logf(mmss, LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + + free(filename2); + free(oldcwd); + + if (filename && file) + fclose(file); + + return ret; +} diff --git a/mmss/config.l b/mmss/config.l new file mode 100644 index 0000000..6f20423 --- /dev/null +++ b/mmss/config.l @@ -0,0 +1,129 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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. +*/ + + +%option prefix="mmss_config_yy" +%option noyywrap +%option nounput +%option noinput +%option bison-bridge +%option bison-locations +%option reentrant +%option warn + + +%top { + #include +} + +%s NEEDSPACE +%s STRING +%s COMMENT + +%% +%{ + #define UPDATE_LOCATION do { \ + yylloc->first_line = yylloc->last_line; \ + yylloc->first_column = yylloc->last_column+1; \ + yylloc->last_column += yyleng; \ + } while (0) + + #define TOKEN(tok) do { UPDATE_LOCATION; BEGIN(NEEDSPACE); return tok; } while (0) +%} + +{ +[0-9]+ { UPDATE_LOCATION; yylval->num = atoi(yytext); BEGIN(NEEDSPACE); return TOK_INTEGER; } + +yes { TOKEN(TOK_YES); } +no { TOKEN(TOK_NO); } +network { TOKEN(TOK_NETWORK); } + +[;:\{\}] { UPDATE_LOCATION; return yytext[0]; } + +[ \t] { yylloc->last_column++; } +\n { yylloc->last_column = 0; yylloc->last_line++; } +\r ; +} + +{ +[;:\{\}] { UPDATE_LOCATION; BEGIN(INITIAL); return yytext[0]; } + +[ \t] { yylloc->last_column++; BEGIN(INITIAL); } +\n { yylloc->last_column = 0; yylloc->last_line++; BEGIN(INITIAL); } +\r ; +} + +\" { UPDATE_LOCATION; BEGIN(STRING); } +[^"\\\n\r] { yylloc->last_column++; yymore(); } +\n { yylloc->last_line++; yylloc->last_column = 0; yymore(); } +\r { yymore(); } +\\. { yylloc->last_column+=2; yymore(); } +\\\n { yylloc->last_line++; yylloc->last_column = 0; yymore(); } +\" { + int i, esc = 0; + + for (i = 0; i < yyleng; i++) { + if (yytext[i] == '\\') { + i++; + if (yytext[i] == '\n') { + esc+=2; + } + else { + yytext[i-esc-1] = yytext[i]; + esc++; + } + } + else if(esc) { + yytext[i-esc] = yytext[i]; + } + } + yytext[yyleng-esc-1] = 0; + yylval->str = mmss_string_stack_dup(yytext); + BEGIN(NEEDSPACE); + yylloc->last_column++; + return TOK_STRING; + + } + +#.* { yylloc->last_column += yyleng; } +\/\/.* { yylloc->last_column += yyleng; } + +\/\* { UPDATE_LOCATION; BEGIN(COMMENT); } +\*\/ { yylloc->last_column += yyleng; BEGIN(INITIAL); } +[^\n\r] { yylloc->last_column++; } +\n { yylloc->last_line++; yylloc->last_column = 0; } +\r {} + +. { + yylloc->first_line = yylloc->last_line; + yylloc->first_column = yylloc->last_column+1; + yylval->error = "syntax error"; + return -1; + } + +<> { return 0; } +<> { yylval->error = "unterminated block comment"; return -1; } +<> { yylval->error = "unterminated string"; return -1; } +%% diff --git a/mmss/config.y b/mmss/config.y new file mode 100644 index 0000000..722ce25 --- /dev/null +++ b/mmss/config.y @@ -0,0 +1,93 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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. +*/ + + +%define api.pure +%define api.push-pull push +%name-prefix "mmss_config_" +%locations +%parse-param {mmss_t *mmss} +%parse-param {mmss_config_t *conf} +%parse-param {const char *filename} + + +%code requires { + #include + + #include +} + +%union { + int num; + mmss_string_stack_t *str; + bool boolean; + + const char *error; +} + +%token TOK_INTEGER +%token TOK_STRING + +%token TOK_NETWORK +%token TOK_YES +%token TOK_NO + + +%code { + void mmss_config_error(YYLTYPE *loc, mmss_t *mmss, mmss_config_t *conf, const char *filename, const char *s); +} + + +%type boolean + +%% + +start: config + ; + +config: config statement + | + ; + +statement: TOK_NETWORK network '{' network_config '}' + ; + +network: TOK_STRING { + mmss_config_add_network(mmss, conf, $1->str); + } + ; + +network_config: + ; + +boolean: TOK_YES { $$ = true; } + | TOK_NO { $$ = false; } + ; + +%% + +void mmss_config_error(YYLTYPE *loc, mmss_t *mmss, mmss_config_t *conf, const char *filename, const char *s) { + mmss_logf(mmss, LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); +} diff --git a/mmss/iface.c b/mmss/iface.c deleted file mode 100644 index 6d636ce..0000000 --- a/mmss/iface.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "mmss.h" - -#include - - -gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface) { - return iface->address; -} - -const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface) { - return iface->name; -} - -size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { - return iface->net->mtu; -} - -static void enqueue(mmss_t *mmss, gmrf_iface_t *source, gmrf_iface_t *dest, const void *data, size_t len) { - mmss_packet_t *packet = calloc(1, sizeof(mmss_packet_t) + len); - - packet->sent = mmss->now; - packet->source = source; - packet->dest = dest; - packet->len = len; - memcpy(packet->data, data, len); - - mmss_queue_put(mmss, &mmss->packet_queue, packet, mmss->now+1); -} - -bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) { - gmrf_iface_t *dest_iface; - for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next) { - if (gmrf_addr_equal(&dest_iface->address, dest)) { - enqueue(gmrf->mmss, iface, dest_iface, data, len); - break; - } - } - - return true; -} - -bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) { - gmrf_iface_t *dest_iface; - for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next) { - if (dest_iface != iface) - enqueue(gmrf->mmss, iface, dest_iface, data, len); - } - - return true; -} - -void mmss_dispatch(mmss_packet_t *packet) { - packet->dest->node->proto->handle_packet(packet->dest->node, packet->dest->node->ctx, packet->dest, - &packet->source->address, packet->data, packet->len); - free(packet); -} - -void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address) { - gmrf_iface_t *iface = calloc(1, sizeof(gmrf_iface_t)); - - iface->name = strdup(name); - iface->address = *address; - - iface->node = node; - iface->net = net; - - iface->node_next = node->interfaces; - node->interfaces = iface; - - iface->network_next = net->interfaces; - net->interfaces = iface; - - node->proto->add_iface(node, node->ctx, iface); -} diff --git a/mmss/iface.cpp b/mmss/iface.cpp new file mode 100644 index 0000000..441da4a --- /dev/null +++ b/mmss/iface.cpp @@ -0,0 +1,100 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" + +#include + + +gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface) { + return iface->address; +} + +const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface) { + return iface->name; +} + +size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { + return iface->net->mtu; +} + +static void enqueue(mmss_t *mmss, gmrf_iface_t *source, gmrf_iface_t *dest, const void *data, size_t len) { + mmss_packet_t *packet = reinterpret_cast(calloc(1, sizeof(mmss_packet_t) + len)); + + packet->sent = mmss->now; + packet->source = source; + packet->dest = dest; + packet->len = len; + memcpy(packet->data, data, len); + + mmss_queue_put(mmss, &mmss->packet_queue, packet, mmss->now+1); +} + +bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) { + gmrf_iface_t *dest_iface; + for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next) { + if (gmrf_addr_equal(&dest_iface->address, dest)) { + enqueue(gmrf->mmss, iface, dest_iface, data, len); + break; + } + } + + return true; +} + +bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) { + gmrf_iface_t *dest_iface; + for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next) { + if (dest_iface != iface) + enqueue(gmrf->mmss, iface, dest_iface, data, len); + } + + return true; +} + +void mmss_dispatch(mmss_packet_t *packet) { + packet->dest->node->proto->handle_packet(packet->dest->node, packet->dest->node->ctx, packet->dest, + &packet->source->address, packet->data, packet->len); + free(packet); +} + +void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address) { + gmrf_iface_t *iface = new gmrf_iface_t; + + iface->name = strdup(name); + iface->address = *address; + + iface->node = node; + iface->net = net; + + iface->node_next = node->interfaces; + node->interfaces = iface; + + iface->network_next = net->interfaces; + net->interfaces = iface; + + node->proto->add_iface(node, node->ctx, iface); +} diff --git a/mmss/log.c b/mmss/log.c deleted file mode 100644 index 5599e95..0000000 --- a/mmss/log.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "mmss.h" - -#include - - -static inline int snprintf_safe(char *buffer, size_t size, const char *format, ...) { - va_list ap; - va_start(ap, format); - int ret = vsnprintf(buffer, size, format, ap); - va_end(ap); - - return ret < 0 ? 0 : ret > size ? size : ret; -} - -static inline const char* get_log_prefix(int log_level) { - switch(log_level) { - case LOG_CRIT: - return "Fatal: "; - case LOG_ERR: - return "Error: "; - case LOG_WARNING: - return "Warning: "; - case LOG_NOTICE: - return "Info: "; - case LOG_INFO: - return "Verbose: "; - case LOG_DEBUG: - return "DEBUG: "; - default: - return ""; - } -} - - -void gmrf_logf(gmrf_t *gmrf, int priority, const char *format, ...) { - char buf[1024]; - size_t pos = 0; - - pos += snprintf_safe(buf, sizeof(buf), "[% 5u.%03u] %s: %s", gmrf->mmss->now/1000, gmrf->mmss->now%1000, gmrf->name, get_log_prefix(priority)); - - va_list ap; - va_start(ap, format); - vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); - va_end(ap); - - fprintf(stderr, "%s\n", buf); -} diff --git a/mmss/log.cpp b/mmss/log.cpp new file mode 100644 index 0000000..eb2ce03 --- /dev/null +++ b/mmss/log.cpp @@ -0,0 +1,86 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" + +#include + + +static inline int snprintf_safe(char *buffer, size_t size, const char *format, ...) { + va_list ap; + va_start(ap, format); + int ret = vsnprintf(buffer, size, format, ap); + va_end(ap); + + return ret < 0 ? 0 : ret > size ? size : ret; +} + +static inline const char* get_log_prefix(int log_level) { + switch(log_level) { + case LOG_CRIT: + return "Fatal: "; + case LOG_ERR: + return "Error: "; + case LOG_WARNING: + return "Warning: "; + case LOG_NOTICE: + return "Info: "; + case LOG_INFO: + return "Verbose: "; + case LOG_DEBUG: + return "DEBUG: "; + default: + return ""; + } +} + +void mmss_logf(mmss_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); + + va_list ap; + va_start(ap, format); + vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); + va_end(ap); + + fprintf(stderr, "%s\n", buf); +} + +void gmrf_logf(gmrf_t *gmrf, int priority, const char *format, ...) { + char buf[1024]; + size_t pos = 0; + + pos += snprintf_safe(buf, sizeof(buf), "%s: %s", gmrf->name, get_log_prefix(priority)); + + va_list ap; + va_start(ap, format); + vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); + va_end(ap); + + mmss_logf(gmrf->mmss, priority, "%s", buf); +} diff --git a/mmss/mmss.c b/mmss/mmss.c deleted file mode 100644 index a260983..0000000 --- a/mmss/mmss.c +++ /dev/null @@ -1,134 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "mmss.h" - -#include -#include -#include - - -gmrf_time_t gmrf_now(gmrf_t *gmrf) { - return gmrf->mmss->now; -} - -void gmrf_random_bytes(gmrf_t *gmrf, void *buffer, size_t len) { - uint8_t *data = buffer; - - size_t i; - for (i = 0; i < len; i++) - data[i] = rand_r(&gmrf->rand_seed); -} - - -static void init_nodes(gmrf_t *nodes) { - gmrf_t *node; - for (node = nodes; node; node = node->next) { - node->ctx = node->proto->init(node); - } -} - - -static inline int timeout_min(int a, int b) { - if (a < 0) - return b; - else if (b < 0) - return a; - else - return min(a, b); -} - - -static int get_queue_timeout(const mmss_t *mmss) { - return timeout_min(mmss_queue_timeout(mmss, &mmss->packet_queue), mmss_queue_timeout(mmss, &mmss->scheduled_queue)); -} - - -int main(int argc, char *argv[]) { - if (argc != 2) { - fprintf(stderr, "usage: %s protocol_module\n", argv[0]); - return 1; - } - - const mmss_protocol_t *proto = mmss_load_protocol(argv[1]); - if (!proto) - return 1; - - mmss_t mmss = { .now = 0 }; - - mmss_network_t net0 = { .mtu = 1500 }, net1 = { .mtu = 1500 }; - gmrf_t node1 = { .name = "node1", .mmss = &mmss, .rand_seed = 1, .proto = proto }; - gmrf_t node2 = { .name = "node2", .mmss = &mmss, .rand_seed = 2, .proto = proto }; - gmrf_t node3 = { .name = "node3", .mmss = &mmss, .rand_seed = 3, .proto = proto }; - - node2.next = &node1; - node3.next = &node2; - gmrf_t *nodes = &node3; - - init_nodes(nodes); - - gmrf_addr_t addr1 = {{1}}, addr2 = {{2}}, addr3 = {{3}}, addr4 = {{4}}; - mmss_add_iface(&node1, &net0, "mmss0", &addr1); - mmss_add_iface(&node2, &net0, "mmss0", &addr2); - mmss_add_iface(&node2, &net1, "mmss1", &addr3); - mmss_add_iface(&node3, &net1, "mmss1", &addr4); - - while (true) { - int timeout = get_queue_timeout(&mmss); - - if (timeout < 0) { - fprintf(stderr, "nothing queued, deadlock occured.\n"); - break; - } - - if (timeout > 0) { - assert(!mmss_queue_get(&mmss, &mmss.packet_queue)); - assert(!mmss_queue_get(&mmss, &mmss.scheduled_queue)); - - mmss.now += timeout; - timeout = get_queue_timeout(&mmss); - } - - assert(timeout == 0); - - while (timeout == 0) { - mmss_packet_t *packet = mmss_queue_get(&mmss, &mmss.packet_queue); - mmss_scheduled_t *scheduled = mmss_queue_get(&mmss, &mmss.scheduled_queue); - - assert(packet || scheduled); - - if(packet) - mmss_dispatch(packet); - - if (scheduled) - mmss_run_scheduled(scheduled); - - timeout = get_queue_timeout(&mmss); - } - } - - return 0; -} diff --git a/mmss/mmss.cpp b/mmss/mmss.cpp new file mode 100644 index 0000000..8447e5e --- /dev/null +++ b/mmss/mmss.cpp @@ -0,0 +1,137 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" + +#include +#include +#include + + +gmrf_time_t gmrf_now(gmrf_t *gmrf) { + return gmrf->mmss->now; +} + +void gmrf_random_bytes(gmrf_t *gmrf, void *buffer, size_t len) { + uint8_t *data = reinterpret_cast(buffer); + + size_t i; + for (i = 0; i < len; i++) + data[i] = rand_r(&gmrf->rand_seed); +} + + +static void init_nodes(gmrf_t *nodes) { + gmrf_t *node; + for (node = nodes; node; node = node->next) { + node->ctx = node->proto->init(node); + } +} + + +static inline int timeout_min(int a, int b) { + if (a < 0) + return b; + else if (b < 0) + return a; + else + return min(a, b); +} + + +static int get_queue_timeout(const mmss_t *mmss) { + return timeout_min(mmss_queue_timeout(mmss, &mmss->packet_queue), mmss_queue_timeout(mmss, &mmss->scheduled_queue)); +} + + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s protocol_module\n", argv[0]); + return 1; + } + + const mmss_protocol_t *proto = mmss_load_protocol(argv[1]); + if (!proto) + return 1; + + mmss_t mmss = { .now = 0 }; + mmss_config_t conf = {}; + + mmss_read_config(&mmss, &conf, "babel_test.mmss"); + + mmss_network_t net0 = { .mtu = 1500 }, net1 = { .mtu = 1500 }; + gmrf_t node1 = { .name = strdup("node1"), .mmss = &mmss, .rand_seed = 1, .proto = proto }; + gmrf_t node2 = { .name = strdup("node2"), .mmss = &mmss, .rand_seed = 2, .proto = proto }; + gmrf_t node3 = { .name = strdup("node3"), .mmss = &mmss, .rand_seed = 3, .proto = proto }; + + node2.next = &node1; + node3.next = &node2; + gmrf_t *nodes = &node3; + + init_nodes(nodes); + + gmrf_addr_t addr1 = {{1}}, addr2 = {{2}}, addr3 = {{3}}, addr4 = {{4}}; + mmss_add_iface(&node1, &net0, "mmss0", &addr1); + mmss_add_iface(&node2, &net0, "mmss0", &addr2); + mmss_add_iface(&node2, &net1, "mmss1", &addr3); + mmss_add_iface(&node3, &net1, "mmss1", &addr4); + + while (true) { + int timeout = get_queue_timeout(&mmss); + + if (timeout < 0) { + fprintf(stderr, "nothing queued, deadlock occured.\n"); + break; + } + + if (timeout > 0) { + assert(!mmss_queue_get(&mmss, &mmss.packet_queue)); + assert(!mmss_queue_get(&mmss, &mmss.scheduled_queue)); + + mmss.now += timeout; + timeout = get_queue_timeout(&mmss); + } + + assert(timeout == 0); + + while (timeout == 0) { + mmss_packet_t *packet = reinterpret_cast(mmss_queue_get(&mmss, &mmss.packet_queue)); + mmss_scheduled_t *scheduled = reinterpret_cast(mmss_queue_get(&mmss, &mmss.scheduled_queue)); + + assert(packet || scheduled); + + if (packet) + mmss_dispatch(packet); + + if (scheduled) + mmss_run_scheduled(scheduled); + + timeout = get_queue_timeout(&mmss); + } + } + + return 0; +} diff --git a/mmss/mmss.h b/mmss/mmss.h deleted file mode 100644 index 4b0e206..0000000 --- a/mmss/mmss.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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. -*/ - - -#ifndef _GMRF_MMSS_MMSS_H_ -#define _GMRF_MMSS_MMSS_H_ - -#include -#include - -#include "queue.h" - - -struct mmss { - uint64_t now; - mmss_queue_t packet_queue; - mmss_queue_t scheduled_queue; -}; - -struct mmss_network { - mmss_network_t *next; - - gmrf_iface_t *interfaces; - - size_t mtu; -}; - -struct mmss_packet { - uint64_t sent; - - gmrf_iface_t *source; - gmrf_iface_t *dest; - - size_t len; - uint8_t data[]; -}; - -struct mmss_scheduled { - gmrf_t *node; - gmrf_scheduled_func f; - void *arg; -}; - - -struct gmrf { - gmrf_t *next; - - const char *name; - - mmss_t *mmss; - gmrf_context_t *ctx; - gmrf_iface_t *interfaces; - - unsigned rand_seed; - - const mmss_protocol_t *proto; -}; - -struct gmrf_iface { - gmrf_iface_t *node_next; - gmrf_iface_t *network_next; - - char *name; - gmrf_addr_t address; - - gmrf_t *node; - mmss_network_t *net; -}; - - -const mmss_protocol_t* mmss_load_protocol(const char *module); - -void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address); - -void mmss_dispatch(mmss_packet_t *packet); -void mmss_run_scheduled(mmss_scheduled_t *scheduled); - - -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; -} - - -#endif /* _GMRF_MMSS_MMSS_H_ */ diff --git a/mmss/mmss.hpp b/mmss/mmss.hpp new file mode 100644 index 0000000..a2c2fe2 --- /dev/null +++ b/mmss/mmss.hpp @@ -0,0 +1,172 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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. +*/ + + +#ifndef _GMRF_MMSS_MMSS_H_ +#define _GMRF_MMSS_MMSS_H_ + +#include "queue.hpp" + +extern "C" { + +#include +#include + +} + +#include +#include + +struct mmss { + uint64_t now; + mmss_queue_t packet_queue; + mmss_queue_t scheduled_queue; +}; + +struct mmss_config { + mmss_network_t *networks; + gmrf_t *nodes; +}; + +struct mmss_network { + mmss_network_t *next; + + char *name; + + gmrf_iface_t *interfaces; + size_t mtu; +}; + +struct mmss_packet { + uint64_t sent; + + gmrf_iface_t *source; + gmrf_iface_t *dest; + + size_t len; + uint8_t data[]; +}; + +struct mmss_scheduled { + gmrf_t *node; + gmrf_scheduled_func f; + void *arg; +}; + +struct mmss_string_stack { + mmss_string_stack_t *next; + char str[]; +}; + + +struct gmrf { + gmrf_t *next; + + char *name; + + mmss_t *mmss; + gmrf_context_t *ctx; + gmrf_iface_t *interfaces; + + unsigned rand_seed; + + const mmss_protocol_t *proto; +}; + +struct gmrf_iface { + gmrf_iface_t *node_next; + gmrf_iface_t *network_next; + + char *name; + gmrf_addr_t address; + + gmrf_t *node; + mmss_network_t *net; +}; + + +const mmss_protocol_t* mmss_load_protocol(const char *module); + +void mmss_config_add_network(mmss_t *mmss, mmss_config_t *conf, const char *name); + +bool mmss_read_config(mmss_t *mmss, mmss_config_t *conf, const char *filename); + +void mmss_add_iface(gmrf_t *node, mmss_network_t *net, const char *name, const gmrf_addr_t *address); + +void mmss_dispatch(mmss_packet_t *packet); +void mmss_run_scheduled(mmss_scheduled_t *scheduled); + +void mmss_logf(mmss_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; +} + +static inline mmss_string_stack_t* mmss_string_stack_dup(const char *str) { + mmss_string_stack_t *ret = reinterpret_cast(std::malloc(alignto(sizeof(mmss_string_stack_t) + strlen(str) + 1, 8))); + ret->next = NULL; + std::strcpy(ret->str, str); + + return ret; +} + +static inline mmss_string_stack_t* mmss_string_stack_dupn(const char *str, size_t len) { + size_t str_len = strnlen(str, len); + mmss_string_stack_t *ret = reinterpret_cast(std::malloc(alignto(sizeof(mmss_string_stack_t) + str_len + 1, 8))); + ret->next = NULL; + std::strncpy(ret->str, str, str_len); + ret->str[str_len] = 0; + + return ret; +} + +static inline mmss_string_stack_t* mmss_string_stack_push(mmss_string_stack_t *stack, const char *str) { + mmss_string_stack_t *ret = reinterpret_cast(std::malloc(alignto(sizeof(mmss_string_stack_t) + strlen(str) + 1, 8))); + ret->next = stack; + std::strcpy(ret->str, str); + + return ret; +} + +static inline void mmss_string_stack_free(mmss_string_stack_t *str) { + while(str) { + mmss_string_stack_t *next = str->next; + std::free(str); + str = next; + } +} + + +#endif /* _GMRF_MMSS_MMSS_H_ */ diff --git a/mmss/protocol.c b/mmss/protocol.c deleted file mode 100644 index 046cb58..0000000 --- a/mmss/protocol.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "mmss.h" - -#include -#include - - -const mmss_protocol_t* mmss_load_protocol(const char *module) { - void *handle = dlopen(module, RTLD_NOW); - if (!handle) { - fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); - return NULL; - } - - dlerror(); - const mmss_protocol_t *proto = dlsym(handle, "mmss_protocol_info"); - if (!proto) { - fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); - dlclose(handle); - return NULL; - } - - fprintf(stderr, "loaded protocol `%s' version %s\n", proto->get_name(), proto->get_version()); - - return proto; -} diff --git a/mmss/protocol.cpp b/mmss/protocol.cpp new file mode 100644 index 0000000..ace3560 --- /dev/null +++ b/mmss/protocol.cpp @@ -0,0 +1,51 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" + +#include +#include + + +const mmss_protocol_t* mmss_load_protocol(const char *module) { + void *handle = dlopen(module, RTLD_NOW); + if (!handle) { + fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); + return NULL; + } + + dlerror(); + const mmss_protocol_t *proto = reinterpret_cast(dlsym(handle, "mmss_protocol_info")); + if (!proto) { + fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); + dlclose(handle); + return NULL; + } + + fprintf(stderr, "loaded protocol `%s' version %s\n", proto->get_name(), proto->get_version()); + + return proto; +} diff --git a/mmss/queue.c b/mmss/queue.c deleted file mode 100644 index c7c475b..0000000 --- a/mmss/queue.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "queue.h" -#include "mmss.h" - -#include - - -typedef struct mmss_queue_entry mmss_queue_entry_t; - -struct mmss_queue_entry { - mmss_queue_entry_t *next; - uint64_t timeout; - void *data; -}; - - -void mmss_queue_put(mmss_t *mmss, mmss_queue_t *queue, void *data, uint64_t timeout) { - while (*queue && timeout >= (*queue)->timeout) - queue = &(*queue)->next; - - mmss_queue_entry_t *entry = malloc(sizeof(mmss_queue_entry_t)); - - entry->timeout = timeout; - entry->data = data; - - entry->next = *queue; - *queue = entry; -} - -void* mmss_queue_get(mmss_t *mmss, mmss_queue_t *queue) { - mmss_queue_entry_t *entry = *queue; - - if (!entry || entry->timeout > mmss->now) - return NULL; - - *queue = entry->next; - void *data = entry->data; - free(entry); - - return data; -} - -int mmss_queue_timeout(const mmss_t *mmss, const mmss_queue_t *const queue) { - if (!*queue) - return -1; - - int diff = (*queue)->timeout - mmss->now; - - if (diff < 0) - return 0; - else - return diff; -} diff --git a/mmss/queue.cpp b/mmss/queue.cpp new file mode 100644 index 0000000..a5d2142 --- /dev/null +++ b/mmss/queue.cpp @@ -0,0 +1,78 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "queue.hpp" +#include "mmss.hpp" + +#include + + +typedef struct mmss_queue_entry mmss_queue_entry_t; + +struct mmss_queue_entry { + mmss_queue_entry_t *next; + uint64_t timeout; + void *data; +}; + + +void mmss_queue_put(mmss_t *mmss, mmss_queue_t *queue, void *data, uint64_t timeout) { + while (*queue && timeout >= (*queue)->timeout) + queue = &(*queue)->next; + + mmss_queue_entry_t *entry = new mmss_queue_entry_t; + + entry->timeout = timeout; + entry->data = data; + + entry->next = *queue; + *queue = entry; +} + +void* mmss_queue_get(mmss_t *mmss, mmss_queue_t *queue) { + mmss_queue_entry_t *entry = *queue; + + if (!entry || entry->timeout > mmss->now) + return NULL; + + *queue = entry->next; + void *data = entry->data; + free(entry); + + return data; +} + +int mmss_queue_timeout(const mmss_t *mmss, const mmss_queue_t *const queue) { + if (!*queue) + return -1; + + int diff = (*queue)->timeout - mmss->now; + + if (diff < 0) + return 0; + else + return diff; +} diff --git a/mmss/queue.h b/mmss/queue.h deleted file mode 100644 index 6e2648b..0000000 --- a/mmss/queue.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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. -*/ - - -#ifndef _GMRF_MMSS_QUEUE_H_ -#define _GMRF_MMSS_QUEUE_H_ - -#include - -#include "types.h" - - -typedef struct mmss_queue_entry *mmss_queue_t; - - -void mmss_queue_put(mmss_t *mmss, mmss_queue_t *queue, void *data, uint64_t timeout); -void* mmss_queue_get(mmss_t *mmss, mmss_queue_t *queue); -int mmss_queue_timeout(const mmss_t *mmss, const mmss_queue_t *const queue); - -#endif /* _GMRF_MMSS_QUEUE_H_ */ diff --git a/mmss/queue.hpp b/mmss/queue.hpp new file mode 100644 index 0000000..d24bff6 --- /dev/null +++ b/mmss/queue.hpp @@ -0,0 +1,42 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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. +*/ + + +#ifndef _GMRF_MMSS_QUEUE_H_ +#define _GMRF_MMSS_QUEUE_H_ + +#include "types.hpp" + +#include + + +typedef struct mmss_queue_entry *mmss_queue_t; + + +void mmss_queue_put(mmss_t *mmss, mmss_queue_t *queue, void *data, uint64_t timeout); +void* mmss_queue_get(mmss_t *mmss, mmss_queue_t *queue); +int mmss_queue_timeout(const mmss_t *mmss, const mmss_queue_t *const queue); + +#endif /* _GMRF_MMSS_QUEUE_H_ */ diff --git a/mmss/schedule.c b/mmss/schedule.c deleted file mode 100644 index 0474613..0000000 --- a/mmss/schedule.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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 "mmss.h" - -#include - - -void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned delay) { - mmss_scheduled_t *scheduled = calloc(1, sizeof(mmss_scheduled_t)); - - scheduled->node = gmrf; - scheduled->f = f; - scheduled->arg = arg; - - mmss_queue_put(gmrf->mmss, &gmrf->mmss->scheduled_queue, scheduled, gmrf->mmss->now+delay); -} - -void mmss_run_scheduled(mmss_scheduled_t *scheduled) { - scheduled->f(scheduled->node, scheduled->node->ctx, scheduled->arg); - free(scheduled); -} diff --git a/mmss/schedule.cpp b/mmss/schedule.cpp new file mode 100644 index 0000000..adebf19 --- /dev/null +++ b/mmss/schedule.cpp @@ -0,0 +1,43 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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 "mmss.hpp" + + +void gmrf_schedule(gmrf_t *gmrf, gmrf_scheduled_func f, void *arg, unsigned delay) { + mmss_scheduled_t *scheduled = new mmss_scheduled_t; + + scheduled->node = gmrf; + scheduled->f = f; + scheduled->arg = arg; + + mmss_queue_put(gmrf->mmss, &gmrf->mmss->scheduled_queue, scheduled, gmrf->mmss->now+delay); +} + +void mmss_run_scheduled(mmss_scheduled_t *scheduled) { + scheduled->f(scheduled->node, scheduled->node->ctx, scheduled->arg); + delete scheduled; +} diff --git a/mmss/types.h b/mmss/types.h deleted file mode 100644 index 04c77be..0000000 --- a/mmss/types.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - 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. -*/ - - -#ifndef _GMRF_MMSS_TYPES_H_ -#define _GMRF_MMSS_TYPES_H_ - -typedef struct mmss mmss_t; -typedef struct mmss_network mmss_network_t; -typedef struct mmss_packet mmss_packet_t; -typedef struct mmss_scheduled mmss_scheduled_t; - -#endif /* _GMRF_MMSS_TYPES_H_ */ diff --git a/mmss/types.hpp b/mmss/types.hpp new file mode 100644 index 0000000..52ace0f --- /dev/null +++ b/mmss/types.hpp @@ -0,0 +1,42 @@ +/* + Copyright (c) 2013, Matthias Schiffer + 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. +*/ + + +#ifndef _GMRF_MMSS_TYPES_H_ +#define _GMRF_MMSS_TYPES_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +typedef struct mmss mmss_t; +typedef struct mmss_config mmss_config_t; +typedef struct mmss_network mmss_network_t; +typedef struct mmss_packet mmss_packet_t; +typedef struct mmss_scheduled mmss_scheduled_t; + +typedef struct mmss_string_stack mmss_string_stack_t; + +#endif /* _GMRF_MMSS_TYPES_H_ */ -- cgit v1.2.3