Skip to content

Commit

Permalink
Moved Lua Example from Mesh to Fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
James Pascoe committed Mar 23, 2023
1 parent 06d5bdb commit d5c15f4
Show file tree
Hide file tree
Showing 579 changed files with 114 additions and 397 deletions.
4 changes: 2 additions & 2 deletions lua_mesh/CMakeLists.txt → lua_fiber/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Feedback: [email protected]
]]

cmake_minimum_required (VERSION 3.12)
project(LUA_MESH DESCRIPTION
"Lua Mesh - A Lua Based Tool for Simulating Mobile Mesh Networks")
project(LUA_FIBER DESCRIPTION
"Lua Fiber - A Lua Example of Fibers")

# Look for required packages
if (POLICY CMP0078)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,54 +1,73 @@
--[[
lua_mesh.lua
lua_fiber.lua
This behaviour simulates a Mobile Mesh. In particular, the actions
allow the connection management behaviour to be arbitraily complex,
whilst mocking out the low-level interactions with the hardware.
This behaviour provides a simple example of fibers. The C++
actions allow the behaviour to be arbitraily complex, whilst
mocking out the low-level interactions with the hardware.
Copyright © Blu Wireless. All Rights Reserved.
Licensed under the MIT license. See LICENSE file in the project.
Feedback: [email protected]
]]

function connector_fiber (connector, remote_port)
function ping_fiber (connector, remote_port)

-- Scan for a Mesh node and connect to it. Whilst part of the mesh,
-- L3 (IP) traffic can flow.
while true do
Actions.Log.info(
"ping_fiber: connecting to port: " .. remote_port
)

scanner_fiber()
local timer = Actions.Timer()

Actions.Log.warn("In connector " .. remote_port)
-- Connect to a node and send a 'ping' message
while true do

connector:Send("localhost", remote_port, "PING")

repeat
coroutine.yield()
until (connector:IsMessageAvailable())

Actions.Log.critical("Received message " .. connector:GetNextMessage())
Actions.Log.info(
"ping_fiber: received: " .. connector:GetNextMessage()
)

timer(Actions.Timer.WaitType_NOBLOCK, 1, "s", 1)
while timer:IsWaiting() do
coroutine.yield()
end

end

end

function event_monitor_fiber (connector, remote_port)
function pong_fiber (connector, remote_port)

Actions.Log.info(
"pong_fiber: connecting to port: " .. remote_port
)

local timer = Actions.Timer()

-- Monitor the status of the connection. If the node becomes
-- disconnected then signal the connector coroutine.
-- Connect to a node and send a 'pong' message
while true do
Actions.Log.warn("In event_monitor")

repeat
coroutine.yield()
until (connector:IsMessageAvailable())

Actions.Log.critical("Received message " .. connector:GetNextMessage())
Actions.Log.info(
"pong_fiber: received: " .. connector:GetNextMessage()
)

connector:Send("localhost", remote_port, "PONG")

timer(Actions.Timer.WaitType_NOBLOCK, 1, "s", 2)
while timer:IsWaiting() do
coroutine.yield()
end

end

end
Expand Down Expand Up @@ -79,11 +98,9 @@ function dispatcher (coroutines)
end
end

-- Run the dispatcher every 1 ms. Note, that this is required to prevent
-- LuaChat from consuming 100% of a core. Note also that the notification
-- value is set to the maximum value of a uint32_t to prevent conflicts
-- with user timer ids (i.e. user timers can start from 0 and count up).
timer(Actions.Timer.WaitType_BLOCK, 1, "ms", 0xffffffff)
-- Run the dispatcher every 1 ms. Note, that a blocking timer is required
-- to prevent lua_mesh from consuming 100% of a core.
timer(Actions.Timer.WaitType_BLOCK, 1, "ms", 3)

end
end
Expand All @@ -103,30 +120,24 @@ local function main(args)
)

local connector = Actions.Connector(args["port"])

local remote_port
if (args["port"] == 7777) then
remote_port = "8888"
else
remote_port = "7777"
end
local remote_port = args["port"] == 7777 and "8888" or "7777"

-- Create co-routines
local coroutines = {}
coroutines["connector"] = coroutine.create(connector_fiber)
coroutine.resume(coroutines["connector"], connector, remote_port)
coroutines["ping"] = coroutine.create(ping_fiber)
coroutine.resume(coroutines["ping"], connector, remote_port)

coroutines["event_monitor"] = coroutine.create(event_monitor_fiber)
coroutine.resume(coroutines["event_monitor"], connector, remote_port)
coroutines["pong"] = coroutine.create(pong_fiber)
coroutine.resume(coroutines["pong"], connector, remote_port)

-- Run the main loop
dispatcher(coroutines)

end

local behaviour = {
name = "LuaMesh",
description = "A Lua behaviour to simulate a Mobile Mesh",
name = "lua_mesh",
description = "A Lua behaviour to demonstrate fibers",
entry_point = main
}

Expand Down
16 changes: 8 additions & 8 deletions lua_mesh/src/CMakeLists.txt → lua_fiber/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[[
CMakeLists.txt - 'Luamesh' src subdirectory build generation file.
CMakeLists.txt - 'lua_fiber' src subdirectory build generation file.
Copyright © Blu Wireless. All Rights Reserved.
Licensed under the MIT license. See LICENSE file in the project.
Expand All @@ -10,20 +10,20 @@ Feedback: [email protected]

add_subdirectory(actions)

add_executable(lua_mesh
lua_mesh_main.cpp
lua_mesh_log_manager.cpp
lua_mesh_lua_manager.cpp)
add_executable(lua_fiber
lua_fiber_main.cpp
lua_fiber_log_manager.cpp
lua_fiber_lua_manager.cpp)

target_link_libraries(lua_mesh
target_link_libraries(lua_fiber
PRIVATE
actions
${LUA_LIBRARIES}
std::filesystem
Threads::Threads)

target_include_directories(lua_mesh
target_include_directories(lua_fiber
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${LUA_INCLUDE_DIR}
${LUA_MESH_SOURCE_DIR}/third_party)
${LUA_FIBER_SOURCE_DIR}/third_party)
31 changes: 31 additions & 0 deletions lua_fiber/src/actions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[[
CMakeLists.txt - 'lua_fiber' actions subdirectory build generation file.
Copyright © Blu Wireless. All Rights Reserved.
Licensed under the MIT license. See LICENSE file in the project.
Feedback: [email protected]
]]

set(LUA_FIBER_SWIG_SRCS
lua_fiber_actions.i
lua_fiber_action_log.cpp
lua_fiber_action_connector.cpp
lua_fiber_action_timer.cpp)

set_source_files_properties(${LUA_FIBER_SWIG_SRCS} PROPERTIES CPLUSPLUS ON)
swig_add_library(actions
TYPE USE_BUILD_SHARED_LIBS
LANGUAGE lua
SOURCES ${LUA_FIBER_SWIG_SRCS})

target_include_directories(actions PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
${LUA_FIBER_SOURCE_DIR}/src
${LUA_FIBER_SOURCE_DIR}/third_party
${LUA_FIBER_SOURCE_DIR}/third_party/asio
${LUA_INCLUDE_DIR})

target_compile_definitions(actions PRIVATE ASIO_STANDALONE)

target_link_libraries(actions PRIVATE std::filesystem)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* lua_mesh_action_Connector.cpp
* lua_fiber_action_connector.cpp
*
* This action allows Lua behaviours to send messages to other Lua behaviours.
* The primary use-case for this action is for Lua behaviours to implement
* algorithms that require distributed co-ordination e.g. 'best 2of4'.
* algorithms that require distributed co-ordination.
*
* Copyright © Blu Wireless. All Rights Reserved.
* Licensed under the MIT license. See LICENSE file in the project.
* Feedback: [email protected]
*/

#include "lua_mesh_action_connector.hpp"
#include "lua_fiber_action_connector.hpp"

#include "lua_mesh_log_manager.hpp"
#include "lua_fiber_log_manager.hpp"

Connector::Connector(unsigned short port)
: m_acceptor(m_io_context, tcp::endpoint(tcp::v4(), port)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* lua_chat_action_Connector.hpp
* lua_chat_action_connector.hpp
*
* This action allows Lua behaviours to send messages to other Lua behaviours.
* The primary use-case for this action is for Lua behaviours to implement
* algorithms that require distributed co-ordination e.g. 'best 2of4'.
* algorithms that require distributed co-ordination.
*
* Copyright © Blu Wireless. All Rights Reserved.
* Licensed under the MIT license. See LICENSE file in the project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Feedback: [email protected]
*/

#include "lua_mesh_log_manager.hpp"
#include "lua_fiber_log_manager.hpp"

#include "lua_mesh_action_log.hpp"
#include "lua_fiber_action_log.hpp"

void Log::trace(std::string const& message) { log_trace(message); }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* lua_mesh_action_timer.cpp
* lua_fiber_action_timer.cpp
*
* The Timer action allows the user to wait for a given duration. Lua simply
* polls the object to discover whether the timer has fired. The action allows
Expand All @@ -9,9 +9,9 @@
* Licensed under the MIT license. See LICENSE file in the project.
* Feedback: [email protected]
*/
#include "lua_mesh_action_timer.hpp"
#include "lua_fiber_action_timer.hpp"

#include "lua_mesh_log_manager.hpp"
#include "lua_fiber_log_manager.hpp"

#include <functional>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

// Definitions required by the SWIG wrapper to compile
%{
#include "lua_mesh_log_manager.hpp"
#include "lua_mesh_action_log.hpp"
#include "lua_mesh_action_scan.hpp"
#include "lua_mesh_action_connector.hpp"
#include "lua_mesh_action_timer.hpp"
#include "lua_fiber_log_manager.hpp"
#include "lua_fiber_action_log.hpp"
#include "lua_fiber_action_connector.hpp"
#include "lua_fiber_action_timer.hpp"
%}

// Files to be wrapped by SWIG
%include "lua_mesh_action_log.hpp"
%include "lua_fiber_action_log.hpp"

%define CTOR_ERROR
{
Expand All @@ -39,11 +38,8 @@
%enddef

// Include the actions and define exception handlers
%exception Scan::Scan CTOR_ERROR;
%include "lua_mesh_action_scan.hpp"

%exception Connector::Connector CTOR_ERROR;
%include "lua_mesh_action_connector.hpp"
%include "lua_fiber_action_connector.hpp"

%exception Timer::Timer CTOR_ERROR;
%include "lua_mesh_action_timer.hpp"
%include "lua_fiber_action_timer.hpp"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <filesystem>

#include "lua_mesh_log_manager.hpp"
#include "lua_fiber_log_manager.hpp"

#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LogManager {
// Default logging constants
inline static const std::string def_log_lvl = "warning";
inline static const std::string def_file_lvl = "info";
inline static const std::string def_log_name = "logs/lua-mesh.log";
inline static const std::string def_log_name = "logs/lua-fiber.log";

private:

Expand All @@ -47,7 +47,7 @@ class LogManager {
LogManager& operator=(LogManager&& rhs) = delete;

// Constants relating to the logger.
inline static const std::string logger_name = "LUA-MESH";
inline static const std::string logger_name = "LUA-FIBER";

// Constants relating to the rotating file sink. Note that max_file_size
// is expressed in bytes files are rotated when they reach 1 MB in size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#include <lua.hpp>

#include "lua_mesh_log_manager.hpp"
#include "lua_mesh_lua_manager.hpp"
#include "lua_fiber_log_manager.hpp"
#include "lua_fiber_lua_manager.hpp"

// Declare the prototype to import the actions library.
extern "C" {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* lua_mesh_main.cpp
* lua_fiber_main.cpp
*
* This file defines the entry point for LuaMesh. Command line arguments are
* This file defines the entry point for lua_fiber. Command line arguments are
* parsed using the excellent cxxopts (https://github.com/jarro2783/cxxopts/).
*
* Copyright © Blu Wireless. All Rights Reserved.
Expand All @@ -14,14 +14,14 @@

#include "cxxopts/cxxopts.hpp"

#include "lua_mesh_log_manager.hpp"
#include "lua_mesh_lua_manager.hpp"
#include "lua_fiber_log_manager.hpp"
#include "lua_fiber_lua_manager.hpp"

// Parse command line arguments (see cxxopts.hpp for details)
static cxxopts::ParseResult parse(int argc, char *argv[])
{
cxxopts::Options options(argv[0],
"Lua Mesh - A Lua Based Tool for Simulating Mobile Mesh Networks");
"Lua Fiber - A Lua Example of Fibers");

options.positional_help("[Lua behaviour to run]").show_positional_help();

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d5c15f4

Please sign in to comment.