Commit 8d091c47 authored by Marcel Stör's avatar Marcel Stör
Browse files

Make the telnet example an Lua module (#3133)

Also update ftp server
parent f20591a8
...@@ -2,21 +2,15 @@ ...@@ -2,21 +2,15 @@
| Since | Origin / Contributor | Maintainer | Source | | Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ | | :----- | :-------------------- | :---------- | :------ |
| 2014-12-22 | [Zeroday](https://github.com/funshine) | [Terry Ellison](https://github.com/TerryE) | [simple_telnet.lua](./simple_telnet.lua) | | 2018-05-24 | [Terry Ellison](https://github.com/TerryE) | [Terry Ellison](https://github.com/TerryE) | [telnet.lua](../../lua_modules/telnet/telnet.lua) |
| 2018-05-24 | [Terry Ellison](https://github.com/TerryE) | [Terry Ellison](https://github.com/TerryE) | [telnet.lua](./telnet.lua) |
The current version of this module exploits the stdin / stdout pipe functionality and
This README discusses the packet marshalling versions of telnet. The first (fifosock) task integration that is now build into the NodeNMCU Lua core.
version was written for SDK 2 implementations, with all of the marshalling imlemented
in Lua; the second (pipe) version uses the latest features added to the SDK 3 version
that have been added to prepare for the `lua53` implementation. These exploit the
stdin / stdout pipe functionality and task integration that is now build into the
NodeNMCU Lua core.
There are two nice advantages of this core implementation: There are two nice advantages of this core implementation:
- Errors are now written to stdout in a spearate task execution. - Errors are now written to stdout in a separate task execution.
- The pipes pretty much eliminate uart and telnet overrun. - The pipes pretty much eliminate UART and telnet overrun.
Both have the same interface if required into the variable `telnet` Both have the same interface if required into the variable `telnet`
...@@ -31,13 +25,13 @@ Open a telnet server based on the provided parameters. ...@@ -31,13 +25,13 @@ Open a telnet server based on the provided parameters.
#### Parameters #### Parameters
`ssid` and `password`. Strings. SSID and Password for the Wifi network. If these are `ssid` and `password`. Strings. SSID and Password for the Wifi network. If these are
`nil` then the wifi is assumed to be configured or autoconfigured. `nil` then the wifi is assumed to be configured or auto-configured.
`port`. Integer TCP listenting port for the Telnet service. The default is 2323 `port`. Integer TCP listening port for the Telnet service. The default is 2323
#### Returns #### Returns
Nothing returned (this is evaluted as `nil` in a scalar context). Nothing returned (this is evaluated as `nil` in a scalar context).
## telnet:close() ## telnet:close()
...@@ -53,4 +47,4 @@ None ...@@ -53,4 +47,4 @@ None
#### Returns #### Returns
Nothing returned (this is evaluted as `nil` in a scalar context). Nothing returned (this is evaluated as `nil` in a scalar context).
--[[ A telnet server T. Ellison, May 2018
This version is more complex than the simple Lua example previously provided in our
distro. The main reason is that a single Lua command can produce a LOT of output,
and the server has to work within four constraints:
- The SDK rules are that you can only issue one send per task invocation, so any
overflow must be buffered, and the buffer emptied using an on:sent cb
- Since the interpeter invokes a node.output cb per field, you have a double
whammy that these fields are typically small, so using a simple array FIFO
would rapidly exhaust RAM.
- For network efficiency, you want to aggregate any FIFO buffered into sensible
sized packet, say 1024 bytes, but you also need to handle the case when larger
string span multiple packets. However, you must flush the buffer if necessary.
- The overall buffering strategy needs to be reasonably memory efficient and avoid
hitting the GC too hard, so where practical avoid aggregating small strings to
more than 256 chars (as NodeMCU handles <256 using stack buffers), and avoid
serial aggregation such as buf = buf .. str as this hammers the GC.
So this server adopts a simple buffering scheme using a two level FIFO. The node.output
cb adds cb records to the 1st level FIFO until the #recs is > 32 or the total size
would exceed 256 bytes. Once over this threashold, the contents of the FIFO are
concatenated into a 2nd level FIFO entry of upto 256 bytes, and the 1st level FIFO
cleared down to any residue.
]]
--luacheck: no unused args
local node, tmr, wifi, uwrite = node, tmr, wifi, uart.write
local function telnet_listener(socket)
local queueLine = (require "fifosock").wrap(socket)
local function receiveLine(s, line)
node.input(line)
end
local function disconnect(s)
socket:on("disconnection", nil)
socket:on("reconnection", nil)
socket:on("connection", nil)
socket:on("receive", nil)
socket:on("sent", nil)
node.output(nil)
end
socket:on("receive", receiveLine)
socket:on("disconnection", disconnect)
node.output(queueLine, 0)
print(("Welcome to NodeMCU world (%d mem free, %s)"):format(node.heap(), wifi.sta.getip()))
end
local listenerSocket
return {
open = function(this, ssid, pwd, port)
if ssid then
wifi.setmode(wifi.STATION, false)
wifi.sta.config { ssid = ssid, pwd = pwd, save = false }
end
local t = tmr.create()
t:alarm(500, tmr.ALARM_AUTO, function()
if (wifi.sta.status() == wifi.STA_GOTIP) then
t:unregister()
t=nil
print(("Telnet server started (%d mem free, %s)"):format(node.heap(), wifi.sta.getip()))
net.createServer(net.TCP, 180):listen(port or 23, telnet_listener)
else
uwrite(0,".")
end
end)
end,
close = function(this)
if listenerSocket then
listenerSocket:close()
package.loaded.telnet = nil
listenerSocket = nil
collectgarbage()
end
end,
}
This diff is collapsed.
# Telnet Module
Documentation for this Lua module is available in the [telnet.md](../../docs/lua-modules/telnet.md) file and in the [Official NodeMCU Documentation](https://nodemcu.readthedocs.io/) in `Lua Modules` section.
--[[SPLIT MODULE telnet]]
--[[ A telnet server T. Ellison, June 2019 --[[ A telnet server T. Ellison, June 2019
This version of the telnet server demonstrates the use of the new stdin and stout This version of the telnet server demonstrates the use of the new stdin and stout
...@@ -65,5 +67,4 @@ function M.close(this) ...@@ -65,5 +67,4 @@ function M.close(this)
end end
return M return M
--[[SPLIT HERE]]
...@@ -58,6 +58,7 @@ pages: ...@@ -58,6 +58,7 @@ pages:
- 'lm92': 'lua-modules/lm92.md' - 'lm92': 'lua-modules/lm92.md'
- 'mcp23008': 'lua-modules/mcp23008.md' - 'mcp23008': 'lua-modules/mcp23008.md'
- 'redis': 'lua-modules/redis.md' - 'redis': 'lua-modules/redis.md'
- 'telnet': 'lua-modules/telnet.md'
- 'yeelink': 'lua-modules/yeelink.md' - 'yeelink': 'lua-modules/yeelink.md'
- C Modules: - C Modules:
- 'adc': 'modules/adc.md' - 'adc': 'modules/adc.md'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment