dofile("diag.lc")
dofile("nwfnet-go.lc")
tcpserv = net.createServer(net.TCP, 180)
- tcpserv:listen(23,function(k)
+ tcpserv:listen(23,function(k)
local telnetd = dofile "telnetd.lc"
- telnetd.on["conn"] = function(k)
- tmr.unregister(6)
- k:send(string.format("NODE-%06X RECOVERY (auto reboot cancelled)",node.chipid())) end
+ telnetd.on["conn"] = function(s)
+ tmr.unregister(6)
+ s(string.format("NODE-%06X RECOVERY (auto reboot cancelled)",node.chipid()))
+ end
telnetd.server(k)
end)
end
--- STATELESS
---
--- Wrap a socket object so that it queues sends. Must call :fini after
--- done to avoid a memory leak (that I don't understand in full)
---
--- Ideally, import this once, wrap all the sockets you need, and then forget
--- it. If one needs new sockets periodically, it is unclear to me whether
--- it's better to load this once and hold it in RAM or to load it every
--- time.
+-- Wrap a fifo around a socket's send
return function(fifo,sock)
local function dosend(s) sock:send(s) end
sock:on("sent", function() fifo:dequeue(dosend) end)
- local nsock = {}
- function nsock.send(_,s)
+ return function(s)
if s == nil or s == "" then return end
fifo:queue(s,dosend)
end
- function nsock.fini(_) sock = nil end
- local sockit = getmetatable(sock)["__index"]
- setmetatable(nsock,{ __index = function(_,k)
- local fn = sockit[k]
- return function(a,...) if a == nsock then fn(sock,...) else fn(a,...) end end
- end })
- return nsock
end
if rt ~= nil
then self.tryin(r,rt(),function(c2) tx(c.." "..c2.."?") end, function() tx(c.." ??") end,tx)
else tx(c.."?")
- end end end,
- function(_) tx("?") end,tx)
- k(true)
+ end
+ k(true)
+ end
+ end,
+ function(_) tx("?") k(true) end,tx)
end
function self.server(sock_)
- local sock = (dofile("fifosock.lc"))((require "fifo")(), sock_)
- local dosend = function(...) sock:send(...) end
- local k = function(c) if c then sock:send("\n$ ") else sock:close() end end
- sock:on("receive",function(s_,input) self.rx(dosend,input,k) end)
- sock:on("disconnection",function(s_) tryon("disconn",sock) ; sock:fini() end)
- tryon("conn",sock)
- sock:send("\n$ ")
+ local fsend = (dofile("fifosock.lc"))((require "fifo")(), sock_)
+ local function teardown(rawsock)
+ rawsock:on("sent", nil)
+ rawsock:on("receive", nil)
+ rawsock:on("disconnection", nil)
+ tryon("disconn",fsend)
+ end
+ sock_:on("receive",function(s_,input) self.rx(fsend,input,function(c) if c then fsend("\n$ ") else s_:close() teardown(s_) end end) end)
+ sock_:on("disconnection",function(s_, x) teardown(s_) end)
+ tryon("conn",fsend)
+ fsend("\n$ ")
end
return self