Skip to content

Commit

Permalink
Lightly refactored require-ing to be more inline with Lua's style and…
Browse files Browse the repository at this point in the history
… also to be more cross-platform friendly (no slashes).
  • Loading branch information
Andrew Starks committed Feb 2, 2014
1 parent 1df60f7 commit ce54d21
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 112 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

#vim swap
*.swp
*.sublime-workspace
*.sublime-project
*.sublime-*
22 changes: 4 additions & 18 deletions lib/idle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,8 @@

--[[
https://github.com/andrewstarks/lsleep
--]]
local success, lsleep= pcall(require, 'lsleep')


function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end



--]]


local function unix_idle (t)
Expand All @@ -37,5 +22,6 @@ local function windows_idle (t)
os.execute( ('ping 1.1.1.1 -n 1 -w %d > nul'):format(t * 1000) )
end

return success and lsleep.sleep or
os.getenv('OS'):match("^Windows-.") and windows_idle or unix_idle
-- return success and lsleep.sleep or
-- os.getenv('OS'):match("^Windows-.") and windows_idle or unix_idle
return os.getenv('OS'):match("^Windows-.") and windows_idle or unix_idle
2 changes: 1 addition & 1 deletion pipe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- no message can be missed by task doing something else when the signal occurs:
-- writers get blocked when the pipe is full
-- @module pipes
-- @usage local pipe = require 'pipe'
-- @usage local pipe = require 'lumen.pipe'
-- @alias M

local sched = require 'lumen.sched'
Expand Down
6 changes: 3 additions & 3 deletions sched.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
-- end)
-- @alias M

local log=require 'log'
local queue3 = require 'lib/queue3'
local log=require 'lumen.log'
local queue3 = require 'lumen.lib.queue3'
local weak_key = {__mode='k'}
local weak_value = {__mode='v'}
local weak_keyvalue = {__mode='kv'}
Expand Down Expand Up @@ -534,7 +534,7 @@ end
--- Wait for the scheduler to finish.
-- This call will block until there is no more task activity, i.e. there's no active task,
-- and none of the waiting tasks has a timeout set.
-- @usage local sched = require 'sched'
-- @usage local sched = require 'lumen.sched'
-- sched.run(function()
-- --start at least one task
-- end)
Expand Down
6 changes: 3 additions & 3 deletions stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
-- @usage local stream = require 'stream'
-- @alias M

local sched = require 'sched'
local log=require 'log'
local queue=require 'lib/queue'
local sched = require 'lumen.sched'
local log=require 'lumen.log'
local queue=require 'lumen.lib.queue'

--get locals for some useful things
local setmetatable, tostring = setmetatable, tostring
Expand Down
14 changes: 8 additions & 6 deletions tasks/http-server.lua → tasks/http-server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
-- Handlers for serving static files from disk are provided.
-- @module http-server
-- @alias M
local lumen = require'lumen'

local log=require 'log'
local log = lumen.log
local sched = lumen.sched
local stream = lumen.stream

local sched = require 'sched'
local selector = require 'tasks/selector'
local http_util = require 'tasks/http-server/http-util'
local stream = require 'stream'
local selector = require 'lumen.tasks.selector'
local http_util = require 'lumen.tasks.http-server.http-util'
local websocket = require 'lumen.tasks.http-server.websocket'

local function backup_response(code_out, header_out)
local httpstatus = tostring(code_out).." "..http_util.http_error_code[code_out]
Expand Down Expand Up @@ -72,7 +74,7 @@ M.set_request_handler = function ( method, pattern, callback )
}
end

local websocket = require 'tasks/http-server/websocket'


--- Register a websocket protocol.
-- The configuration flag _ws_enable_ must be set (see @{conf})
Expand Down
7 changes: 4 additions & 3 deletions tasks/http-server/websocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
-- depends on
-- - luabitop or nixio (if not using Lua 5.2 nor luajit)

local http_util = require 'tasks/http-server/http-util'
local handshake = require 'tasks/http-server/websocket/handshake'
local sync = require 'tasks/http-server/websocket/sync'
local http_util = require 'lumen.tasks.http-server.http-util'
local handshake = require 'lumen.tasks.http-server.websocket.handshake'
local sync = require 'lumen.tasks.http-server.websocket.sync'


local websocket_protocols = {} --websocket_protocols[protocol] = handler
local websocket_clients = setmetatable({}, {__mode='k'})
Expand Down
2 changes: 1 addition & 1 deletion tasks/http-server/websocket/frame.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- websocket support adaptded from lua-websocket (http://lipp.github.io/lua-websockets/)
-- Following Websocket RFC: http://tools.ietf.org/html/rfc6455
--require'pack'
local bit = require'tasks/http-server/websocket/bit'
local bit = require'lumen.tasks.http-server.websocket.bit'

local band = bit.band
local bxor = bit.bxor
Expand Down
4 changes: 2 additions & 2 deletions tasks/http-server/websocket/handshake.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- websocket support adaptded from lua-websocket (http://lipp.github.io/lua-websockets/)

local sha1 = require'tasks/http-server/sha1'.sha1_binary
local base64 = require'tasks/http-server/base64'
local sha1 = require'lumen.tasks.http-server.sha1'.sha1_binary
local base64 = require'lumen.tasks.http-server.base64'
local tinsert = table.insert

local guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
Expand Down
8 changes: 4 additions & 4 deletions tasks/http-server/websocket/sync.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local frame = require'tasks/http-server/websocket/frame'
local handshake = require'tasks/http-server/websocket/handshake'
local http_util = require'tasks/http-server/http-util'
local base64_encode = require 'tasks/http-server/base64'.encode
local frame = require 'lumen.tasks.http-server.websocket.frame'
local handshake = require 'lumen.tasks.http-server.websocket.handshake'
local http_util = require 'lumen.tasks.http-server.http-util'
local base64_encode = require 'lumen.tasks.http-server.base64'.encode


local tinsert = table.insert
Expand Down
21 changes: 15 additions & 6 deletions tasks/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-- with strings as keys are supported.
-- This module depends on the selector task, which must be started
-- separataly.
-- @usage local proxy = require 'proxy'
-- @usage local proxy = require 'lumen.proxy'
--
-- --for accepting connections
-- proxy.init({ip='*', port=1985})
Expand All @@ -20,11 +20,20 @@
-- end)
-- @module proxy
-- @alias M
local lumen = require'lumen'

local log = lumen.log
local sched = lumen.sched
local stream = lumen.stream
local events_catalog = lumen.catalog.get_catalog('events')

local selector = require 'lumen.tasks.selector'
local http_util = require 'lumen.tasks.http-server.http-util'
local websocket = require 'lumen.tasks.http-server.websocket'




local sched = require 'sched'
local selector = require 'tasks/selector'
local events_catalog = require 'catalog'.get_catalog('events')
local log=require 'log'

local encode_f
local decode_f
Expand Down Expand Up @@ -107,7 +116,7 @@ M.init = function(conf)
local encoder = conf.encoder or 'json'

if encoder =='json' then encoder = 'dkjson' end
local encoder_lib = require ('lib/'..encoder)
local encoder_lib = require ('lumen.lib.'..encoder)
encode_f = encoder_lib.encode
decode_f = encoder_lib.decode

Expand Down
11 changes: 7 additions & 4 deletions tasks/selector-luasocket.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
local log=require 'log'
local lumen = require'lumen'

local log = lumen.log
local sched = lumen.sched
local streams = lumen.stream

local sched = require 'sched'
local socket = require 'socket'
--local pipes = require 'pipes'
local streams = require 'stream'
--local pipes = require 'lumen.pipes'


--get locals for some useful things
local setmetatable, ipairs, table, type = setmetatable, ipairs, table, type
Expand Down
11 changes: 7 additions & 4 deletions tasks/selector-nixio.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
local log=require 'log'
local lumen = require'lumen'

local log = lumen.log
local sched = lumen.sched
local streams = lumen.stream

local sched = require 'sched'
local nixio = require 'nixio'
local streams = require 'stream'

--local nixiorator = require 'tasks/nixiorator'

--local nixiorator = require 'lumen.tasks/nixiorator'
require 'nixio.util'

local floor = math.floor
Expand Down
6 changes: 3 additions & 3 deletions tasks/selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- The socket can be setup to react to data arrival by either calling a handler function,
-- writing to a @{stream}, or emiting signals.
-- @module selector
-- @usage local selector = require 'selector'
-- @usage local selector = require 'lumen.selector'
--selector.init({service='luasocket'})
-- @alias M

Expand All @@ -16,7 +16,7 @@ M.init = function(conf)
conf=conf or {}
M.service=conf.service or 'luasocket'

local native = require ('tasks/selector-'..M.service)
local native = require ('lumen.tasks.selector-'..M.service)
native.init()

--- Creates a TCP server socket.
Expand Down Expand Up @@ -150,7 +150,7 @@ M.init = function(conf)
-- Must be set before first call to async send. Defaults to 1mb.
M.ASYNC_SEND_BUFFER=1024^2 --1mb

require 'catalog'.get_catalog('tasks'):register('selector', M.task)
require 'lumen.catalog'.get_catalog('tasks'):register('selector', M.task)

return M
end
Expand Down
13 changes: 7 additions & 6 deletions tasks/shell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
-- This module depends on the selector task, which must be started
-- seperataly.
-- @module shell
-- @usage local server = require 'shell'
-- @usage local server = require 'lumen.shell'
--server.init({ip='127.0.0.1', port=2012})
-- @alias M
local lumen = require'lumen'

local log=require 'log'
local log = lumen.log
local sched = lumen.sched
local pipe = lumen.pipe

local sched = require 'sched'
local selector = require "tasks/selector"
local pipe = require 'pipe'
local selector = require "lumen.tasks.selector"

local CE = require 'lib/compat_env'
local CE = require 'lumen.lib.compat_env'
local load = CE.load

local M = {}
Expand Down
10 changes: 7 additions & 3 deletions tests/test-catalog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
-- A demonstration of pipes.

--look for packages one folder up.
package.path = package.path .. ";;;../?.lua"
package.path = package.path .. ";;;../../?.lua;../../?/init.lua"

local lumen = require'lumen'
local sched = lumen.sched
local catalog = lumen.catalog



local sched=require 'sched'
--require "log".setlevel('ALL')
local catalog=require 'catalog'
local c = catalog.get_catalog('stuff')

sched.run(function()
Expand Down
27 changes: 20 additions & 7 deletions tests/test-http-server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
-- A test program for the http server.

--look for packages one folder up.
package.path = package.path .. ";;;../?.lua"
package.path = package.path .. ";;;../../?.lua;../../?/init.lua"

local lumen = require'lumen'

local log = lumen.log
local sched = lumen.sched
local stream = lumen.stream
local catalog = lumen.catalog


log.setlevel('ALL', 'HTTP')
log.setlevel('ALL')

local selector = require 'lumen.tasks.selector'

selector.init({service=service})


require "log".setlevel('ALL', 'HTTP')
require "log".setlevel('ALL')

--require "strict"

local service = _G.arg [1] or 'luasocket'

local sched = require "sched"
require "tasks/selector".init({service=service})

local http_server = require "tasks/http-server"

local http_server = require "lumen.tasks.http-server"

http_server.serve_static_content_from_ram('/', '../tasks/http-server/www')
--http_server.serve_static_content_from_stream('/', '/home/xopxe')
Expand All @@ -26,7 +39,7 @@ else
end

http_server.set_websocket_protocol('lumen-shell-protocol', function(ws)
local shell = require 'tasks/shell'
local shell = require 'lumen.tasks.shell'
local sh = shell.new_shell()

sched.run(function()
Expand Down
8 changes: 4 additions & 4 deletions tests/test-mutex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
-- to a function.

--look for packages one folder up.
package.path = package.path .. ";;;../?.lua"
package.path = package.path .. ";;;../../?.lua;../../?/init.lua"

--require "strict"

local sched = require "sched"
local mutex = require "mutex"
local lumen = require 'lumen'
local sched = lumen.sched
local mutex = lumen.mutex

local mx = mutex.new()

Expand Down
4 changes: 2 additions & 2 deletions tests/test-pause.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
-- A test program for pausing and resuming tasks

--look for packages one folder up.
package.path = package.path .. ";;;../?.lua"
package.path = package.path .. ";;;../../?.lua;../../?/init.lua"

local sched = require "sched"
local sched = require "lumen.sched"

-- task emits 5 signals and pauses itself.
local emitter_task=sched.new_task(function()
Expand Down
Loading

0 comments on commit ce54d21

Please sign in to comment.