Move function definition to inside the socket listen argument list. This is harder to follow, but allows the coroutine to be unique since it is now part of the closure. Multiple files can be served simultaneously this way.

This commit is contained in:
Marcos Kirsch 2015-03-10 22:15:52 -05:00
parent 65538efc1b
commit d5fcc71c23

View File

@ -54,12 +54,20 @@ local function parseUri(uri)
return r return r
end end
-- This variable holds the thread used for sending data back to the user. -- Starts web server in the specified port.
-- We do it in a separate thread because we need to yield when sending lots return function (port)
-- of data in order to avoid overflowing the mcu's buffer.
local connectionThread
local function onGet(connection, uri) local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(
port,
function (connection)
-- This variable holds the thread used for sending data back to the user.
-- We do it in a separate thread because we need to yield when sending lots
-- of data in order to avoid overflowing the mcu's buffer.
local connectionThread
local function onGet(connection, uri)
local uri = parseUri(uri) local uri = parseUri(uri)
local fileExists = file.open(uri.file, "r") local fileExists = file.open(uri.file, "r")
file.close() file.close()
@ -76,10 +84,11 @@ local function onGet(connection, uri)
fileServeFunction = dofile("httpserver-static.lc") fileServeFunction = dofile("httpserver-static.lc")
end end
connectionThread = coroutine.create(fileServeFunction) connectionThread = coroutine.create(fileServeFunction)
--print("Thread created", connectionThread)
coroutine.resume(connectionThread, connection, uri.args) coroutine.resume(connectionThread, connection, uri.args)
end end
local function onReceive(connection, payload) local function onReceive(connection, payload)
--print(payload) -- for debugging --print(payload) -- for debugging
-- parse payload and decide what to serve. -- parse payload and decide what to serve.
local req = parseRequest(payload) local req = parseRequest(payload)
@ -88,31 +97,32 @@ local function onReceive(connection, payload)
if req.method == "GET" then onGet(connection, req.uri) if req.method == "GET" then onGet(connection, req.uri)
elseif req.method == nil then dofile("httpserver-static.lc")(conection, {code=400}) elseif req.method == nil then dofile("httpserver-static.lc")(conection, {code=400})
else dofile("httpserver-static.lc")(conection, {code=501}) end else dofile("httpserver-static.lc")(conection, {code=501}) end
end end
local function onSent(connection, payload) local function onSent(connection, payload)
if coroutine.status(connectionThread) == "dead" then local connectionThreadStatus = coroutine.status(connectionThread)
--print (connectionThread, "status is", connectionThreadStatus)
if connectionThreadStatus == "dead" then
-- We're done sending file. -- We're done sending file.
--print("Done sending file", connectionThread)
connection:close() connection:close()
connectionThread = nil connectionThread = nil
elseif coroutine.status(connectionThread) == "suspended" then elseif connectionThreadStatus == "suspended" then
-- Not finished sending file, resume. -- Not finished sending file, resume.
--print("Resume thread", connectionThread)
coroutine.resume(connectionThread) coroutine.resume(connectionThread)
else else
print ("Fatal error! I did not expect to hit this codepath") print ("Fatal error! I did not expect to hit this codepath")
connection:close() connection:close()
end end
end end
local function handleRequest(connection)
connection:on("receive", onReceive) connection:on("receive", onReceive)
connection:on("sent", onSent) connection:on("sent", onSent)
end
-- Starts web server in the specified port. end
return function (port) )
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(port, handleRequest)
print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port) print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
return s return s
end end