Skip to content

Commit

Permalink
Merge pull request #54 from jesstr/telnetsrv
Browse files Browse the repository at this point in the history
Issue #44
  • Loading branch information
4refr0nt authored Jul 20, 2017
2 parents 8409ed7 + fd7cc8f commit dfb05c0
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions luatool/telnet_srv.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
--
-- setup a telnet server that hooks the sockets input
--
function setupTelnetServer()
inUse = false
function listenFun(sock)
if inUse then
sock:send("Already in use.\n")
sock:close()
return
end
inUse = true
-- a simple telnet server

-- restart server if needed
if telnet_srv ~= nil then
telnet_srv:close()
end
telnet_srv = net.createServer(net.TCP, 180)

function s_output(str)
if(sock ~=nil) then
sock:send(str)
end
telnet_srv:listen(23, function(socket)
local fifo = {}
local fifo_drained = true

local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end

node.output(s_output, 0)
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end

sock:on("receive",function(sock, input)
node.input(input)
end)
node.output(s_output, 0) -- re-direct output to function s_ouput.

sock:on("disconnection",function(sock)
node.output(nil)
inUse = false
end)
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)

sock:send("Welcome to NodeMCU world.\n> ")
end
print("Welcome to NodeMCU world.")
end)

telnetServer = net.createServer(net.TCP, 180)
telnetServer:listen(23, listenFun)
end
print("Telnet server running...")

0 comments on commit dfb05c0

Please sign in to comment.