Browse Source

Moved BufferedConnection into its own file.

Marcos Kirsch 8 years ago
parent
commit
e7db3dce7d
4 changed files with 76 additions and 34 deletions
  1. 11 1
      Makefile
  2. 47 0
      httpserver-connection.lua
  3. 7 32
      httpserver.lua
  4. 11 1
      init.lua

+ 11 - 1
Makefile

@@ -11,7 +11,17 @@ SPEED=9600
 # End of user config
 ######################################################################
 HTTP_FILES := $(wildcard http/*)
-LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-basicauth.lua httpserver-b64decode.lua httpserver-conf.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
+LUA_FILES := \
+   init.lua \
+   httpserver.lua \
+   httpserver-b64decode.lua \
+   httpserver-basicauth.lua \
+   httpserver-conf.lua \
+   httpserver-connection.lua \
+   httpserver-error.lua \
+   httpserver-header.lua \
+   httpserver-request.lua \
+   httpserver-static.lua \
 
 # Print usage
 usage:

+ 47 - 0
httpserver-connection.lua

@@ -0,0 +1,47 @@
+-- httpserver-connection
+-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
+-- consecutive send() calls.
+-- For this to work, it must be used from a coroutine.
+-- Author: Philip Gladstone, Marcos Kirsch
+
+BufferedConnection = {}
+
+-- parameter is the nodemcu-firmware connection
+function BufferedConnection:new(connection)
+   local newInstance = {}
+   newInstance.connection = connection
+   newInstance.size = 0
+   newInstance.data = {}
+
+   function newInstance:flush()
+      if self.size > 0 then
+         self.connection:send(table.concat(self.data, ""))
+         self.data = {}
+         self.size = 0
+         return true
+      end
+      return false
+   end
+
+   --@TODO What are the hardcoded 1000 and 800 about? Can we increase?
+   function newInstance:send(payload)
+      local l = payload:len()
+      if l + self.size > 1000 then
+         if self:flush() then
+            coroutine.yield()
+         end
+      end
+      if l > 800 then
+         self.connection:send(payload)
+         coroutine.yield()
+      else
+         table.insert(self.data, payload)
+         self.size = self.size + l
+      end
+   end
+
+   return newInstance
+end
+
+
+return BufferedConnection

+ 7 - 32
httpserver.lua

@@ -18,47 +18,21 @@ return function (port)
 
          local function startServing(fileServeFunction, connection, req, args)
 
-            local bufferedConnection = {}
-            connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args)
-               fileServeFunction(bconnection, req, args)
-               if not bconnection:flush() then
+            connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
+               fileServeFunction(bufferedConnection, req, args)
+               if not bufferedConnection:flush() then
                   connection:close()
                   connectionThread = nil
                end
             end)
 
-            function bufferedConnection:flush()
-               if self.size > 0 then
-                  connection:send(table.concat(self.data, ""))
-                  self.data = {}
-                  self.size = 0
-                  return true
-               end
-               return false
-            end
-
-            function bufferedConnection:send(payload)
-               local l = payload:len()
-               if l + self.size > 1000 then
-                  if self:flush() then
-                     coroutine.yield()
-                  end
-               end
-               if l > 800 then
-                  connection:send(payload)
-                  coroutine.yield()
-               else
-                  table.insert(self.data, payload)
-                  self.size = self.size + l
-               end
-            end
-
-            bufferedConnection.size = 0
-            bufferedConnection.data = {}
+            local BufferedConnectionClass = dofile("httpserver-connection.lc")
+            local bufferedConnection = BufferedConnectionClass:new(connection)
             local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
             if not status then
                print("Error: ", err)
             end
+
          end
 
          local function onRequest(connection, req)
@@ -185,6 +159,7 @@ return function (port)
    -- false and nil evaluate as false
    local ip = wifi.sta.getip()
    if not ip then ip = wifi.ap.getip() end
+   if not ip then ip = "unknown IP" end
    print("nodemcu-httpserver running at http://" .. ip .. ":" ..  port)
    return s
 

+ 11 - 1
init.lua

@@ -56,7 +56,17 @@ local compileAndRemoveIfNeeded = function(f)
    end
 end
 
-local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
+local serverFiles = {
+   'httpserver.lua',
+   'httpserver-b64decode.lua',
+   'httpserver-basicauth.lua',
+   'httpserver-conf.lua',
+   'httpserver-connection.lua',
+   'httpserver-error.lua',
+   'httpserver-header.lua',
+   'httpserver-request.lua',
+   'httpserver-static.lua',
+}
 for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
 
 compileAndRemoveIfNeeded = nil