Add LibNetbox

This commit is contained in:
Kirigaya Kazuto 2018-01-07 02:58:06 +08:00
parent eabe7757d5
commit 4fd6b54552
2 changed files with 76 additions and 0 deletions

View File

@ -117,6 +117,16 @@ local function doEventTranslate(raw_event)
-- libevent events
elseif(name=="EventBus_NewEvent") then
t["id"]=a
-- libnetbox events
elseif(name=="net_message") then
t["receiverAddress"]=a
t["senderAddress"]=b
t["port"]=c
local dtb={}
for i=5,raw_event.n,1 do
table.insert(dtb,raw_event[i])
end
t["data"]=dtb
-- Unknown Event
else
if(canEventTranslate(name)) then -- Try Translate

66
libnetbox.lua Normal file
View File

@ -0,0 +1,66 @@
local component = require("component")
local event = require("event")
require("util")
require("libevent")
local netbox_router_port = 9999
local netbox_client_port = 9998
--- Auto Configure
local modem=proxy("modem")
local tunnel=proxy("tunnel")
local is_router=false
-- APIs
function SendData(target_address,port,...)
if(not is_router) then
modem.broadcast(netbox_router_port,"NetBox","Direct",target_address,port,...)
end
end
function BroadcastData(port,...)
if(not is_router) then
modem.broadcast(netbox_router_port,"NetBox","Broadcast",port,...)
end
end
function routerMain()
modem.open(netbox_router_port)
local bus=CreateEventBus()
EventBusListen(bus,"modem_message")
while true do
local e=GetNextEvent(bus,-1)
if(e.receiverAddress==tunnel.address) then
if(e.data[1]=="NetBoxAir") then
if(e.data[2]=="Direct") then
print("Debug: Sending from " .. e.data[3] .. " to " .. e.data[4] .. " at port " .. netbox_client_port)
modem.send(e.data[4],netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,5))
elseif(e.data[2]=="Broadcast") then
print("Debug: Broadcasting at port " .. netbox_client_port .. " from " .. e.data[3])
modem.broadcast(netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,4))
end
end
elseif(e.receiverAddress==modem.address) then
if(e.port==netbox_router_port and e.data[1]=="NetBox") then
if(e.data[2]=="Direct") then
print("Debug: Tunnel Sending from " .. e.senderAddress .. " to " .. e.data[3])
tunnel.send("NetBoxAir","Direct",e.senderAddress,e.data[3],table.unpack(e.data,4))
elseif(e.data[2]=="Broadcast") then
print("Debug: Tunnel Broadcast from " .. e.senderAddress .. " at port " .. e.port)
tunnel.send("NetBoxAir","Broadcast",e.senderAddress,e.data[3],table.unpack(e.data,4))
end
end
end
end
end
function clientServiceStart(redirect)
modem.open(netbox_client_port)
AddEventListener("modem_message",function(ev)
if(ev.data[1]=="NetBox") then
event.push("net_message",ev.receiverAddress,ev.data[2],ev.data[3],table.unpack(ev.data,4))
end
end)
end