-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Lua Example from Mesh to Fiber
- Loading branch information
James Pascoe
committed
Mar 23, 2023
1 parent
06d5bdb
commit d5c15f4
Showing
579 changed files
with
114 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
8 changes: 4 additions & 4 deletions
8
...src/actions/lua_mesh_action_connector.cpp → ...rc/actions/lua_fiber_action_connector.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
|
4 changes: 2 additions & 2 deletions
4
...src/actions/lua_mesh_action_connector.hpp → ...rc/actions/lua_fiber_action_connector.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); } | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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> | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.