2017-08-31 22:42:56 +10:00
|
|
|
local tA = {...}
|
|
|
|
local nport = tonumber(tA[1])
|
|
|
|
local fpath = tostring(tA[2])
|
|
|
|
for m in component.list("modem") do
|
2017-09-23 14:26:37 +10:00
|
|
|
log("[nbsrv] opening port on "..m)
|
2017-08-31 22:42:56 +10:00
|
|
|
component.invoke(m,"open",nport)
|
|
|
|
end
|
|
|
|
local f=io.open(fpath,"rb")
|
|
|
|
local nbdata = f:read("*a")
|
|
|
|
f:close()
|
2017-09-23 14:26:37 +10:00
|
|
|
spawn("nbsrv: "..tostring(nport)..","..fpath,function() log(xpcall(function()
|
2017-08-31 22:42:56 +10:00
|
|
|
while true do
|
|
|
|
local _, laddress, raddress, port, _, payload = event.pull("modem_message")
|
|
|
|
if port == nport and type(payload) == "string" then
|
|
|
|
if payload:len() == 36 then
|
|
|
|
local sraddress,spayload = raddress:sub(1,8),payload:sub(1,8)
|
2017-09-23 14:26:37 +10:00
|
|
|
log("[nbsrv] request from "..sraddress.."/"..spayload.." on port "..tostring(port))
|
2017-08-31 22:42:56 +10:00
|
|
|
for i = 1, nbdata:len(), 2048 do
|
|
|
|
component.invoke(laddress,"send",raddress,port,nbdata:sub(i,i+2047))
|
|
|
|
end
|
|
|
|
component.invoke(laddress,"send",raddress,port,".")
|
2017-09-23 14:26:37 +10:00
|
|
|
log("[nbsrv] served request from "..sraddress.."/"..spayload)
|
2017-08-31 22:42:56 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-04 02:04:41 +10:00
|
|
|
end)) end)
|
2017-09-23 14:26:37 +10:00
|
|
|
log("[nbsrv] nbsrv started.")
|