Few tweaks in installation, some missing files, etc.

This commit is contained in:
Diego Nehab 2005-11-22 08:33:29 +00:00
parent a2b780bf7a
commit d55a5826e8
26 changed files with 232 additions and 238 deletions

View file

@ -37,7 +37,7 @@ function getstatus(link)
handler:start(function()
local r, c, h, s = http.request{
method = "HEAD",
url = link,
url = link,
create = handler.tcp
}
if r and c == 200 then io.write('\t', link, '\n')
@ -107,6 +107,6 @@ for _, address in ipairs(arg) do
checklinks(url.absolute("file:", address))
end
while nthreads > 0 do
while nthreads > 0 do
handler:step()
end
end

View file

@ -17,7 +17,7 @@ local tp = require("socket.tp")
module("socket.dict")
-----------------------------------------------------------------------------
-- Globals
-- Globals
-----------------------------------------------------------------------------
HOST = "dict.org"
PORT = 2628
@ -39,7 +39,7 @@ end
function metat.__index:check(ok)
local code, status = socket.try(self.tp:check(ok))
return code,
return code,
base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
end
@ -101,7 +101,7 @@ local default = {
}
local function there(f)
if f == "" then return nil
if f == "" then return nil
else return f end
end
@ -116,7 +116,7 @@ local function parse(u)
arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end)
socket.try(t.word, "need at least <word> in URL")
arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end)
if cmd == "m" then
if cmd == "m" then
arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
end
string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
@ -130,7 +130,7 @@ local function tget(gett)
local def = con:define(gett.database, gett.word)
con:quit()
con:close()
if gett.n then return def[gett.n]
if gett.n then return def[gett.n]
else return def end
elseif gett.command == "m" then
local mat = con:match(gett.database, gett.strat, gett.word)

View file

@ -14,7 +14,7 @@ module("dispatch")
TIMEOUT = 60
-----------------------------------------------------------------------------
-- We implement 3 types of dispatchers:
-- We implement 3 types of dispatchers:
-- sequential
-- coroutine
-- threaded
@ -34,7 +34,7 @@ end
-- sequential handler simply calls the functions and doesn't wrap I/O
function handlert.sequential()
return {
return {
tcp = socket.tcp,
start = seqstart
}
@ -55,7 +55,7 @@ function socket.protect(f)
if not status then
if type(results[1]) == 'table' then
return nil, results[1][1]
else error(results[1]) end
else error(results[1]) end
end
if coroutine.status(co) == "suspended" then
arg = {coroutine.yield(unpack(results))}
@ -84,17 +84,17 @@ local function newset()
if index then
reverse[value] = nil
local top = table.remove(set)
if top ~= value then
if top ~= value then
reverse[top] = index
set[index] = top
end
end
end
end
}})
}})
end
-----------------------------------------------------------------------------
-- socket.tcp() wrapper for the coroutine dispatcher
-- socket.tcp() wrapper for the coroutine dispatcher
-----------------------------------------------------------------------------
local function cowrap(dispatcher, tcp, error)
if not tcp then return nil, error end
@ -102,7 +102,7 @@ local function cowrap(dispatcher, tcp, error)
tcp:settimeout(0)
-- metatable for wrap produces new methods on demand for those that we
-- don't override explicitly.
local metat = { __index = function(table, key)
local metat = { __index = function(table, key)
table[key] = function(...)
arg[1] = tcp
return tcp[key](unpack(arg))
@ -112,7 +112,7 @@ local function cowrap(dispatcher, tcp, error)
-- does our user want to do his own non-blocking I/O?
local zero = false
-- create a wrap object that will behave just like a real socket object
local wrap = { }
local wrap = { }
-- we ignore settimeout to preserve our 0 timeout, but record whether
-- the user wants to do his own non-blocking I/O
function wrap:settimeout(value, mode)
@ -121,19 +121,19 @@ local function cowrap(dispatcher, tcp, error)
return 1
end
-- send in non-blocking mode and yield on timeout
function wrap:send(data, first, last)
function wrap:send(data, first, last)
first = (first or 1) - 1
local result, error
while true do
-- return control to dispatcher and tell it we want to send
-- if upon return the dispatcher tells us we timed out,
-- return an error to whoever called us
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
return nil, "timeout"
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
return nil, "timeout"
end
-- try sending
result, error, first = tcp:send(data, first+1, last)
-- if we are done, or there was an unexpected error,
-- if we are done, or there was an unexpected error,
-- break away from loop
if error ~= "timeout" then return result, error, first end
end
@ -143,20 +143,20 @@ local function cowrap(dispatcher, tcp, error)
function wrap:receive(pattern, partial)
local error = "timeout"
local value
while true do
while true do
-- return control to dispatcher and tell it we want to receive
-- if upon return the dispatcher tells us we timed out,
-- return an error to whoever called us
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
return nil, "timeout"
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
return nil, "timeout"
end
-- try receiving
value, error, partial = tcp:receive(pattern, partial)
-- if we are done, or there was an unexpected error,
-- if we are done, or there was an unexpected error,
-- break away from loop. also, if the user requested
-- zero timeout, return all we got
if (error ~= "timeout") or zero then
return value, error, partial
if (error ~= "timeout") or zero then
return value, error, partial
end
end
end
@ -168,8 +168,8 @@ local function cowrap(dispatcher, tcp, error)
-- connection succeeds.
-- if upon return the dispatcher tells us we have a
-- timeout, just abort
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
return nil, "timeout"
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
return nil, "timeout"
end
-- when we come back, check if connection was successful
result, error = tcp:connect(host, port)
@ -179,27 +179,27 @@ local function cowrap(dispatcher, tcp, error)
end
-- accept in non-blocking mode and yield on timeout
function wrap:accept()
while 1 do
while 1 do
-- return control to dispatcher. we will be readable when a
-- connection arrives.
-- if upon return the dispatcher tells us we have a
-- timeout, just abort
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
return nil, "timeout"
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
return nil, "timeout"
end
local client, error = tcp:accept()
if error ~= "timeout" then
return cowrap(dispatcher, client, error)
if error ~= "timeout" then
return cowrap(dispatcher, client, error)
end
end
end
end
-- remove cortn from context
function wrap:close()
dispatcher.stamp[tcp] = nil
dispatcher.sending.set:remove(tcp)
dispatcher.sending.cortn[tcp] = nil
dispatcher.sending.cortn[tcp] = nil
dispatcher.receiving.set:remove(tcp)
dispatcher.receiving.cortn[tcp] = nil
dispatcher.receiving.cortn[tcp] = nil
return tcp:close()
end
return setmetatable(wrap, metat)
@ -207,12 +207,12 @@ end
-----------------------------------------------------------------------------
-- Our coroutine dispatcher
-- Our coroutine dispatcher
-----------------------------------------------------------------------------
local cometat = { __index = {} }
function schedule(cortn, status, operation, tcp)
if status then
if status then
if cortn and operation then
operation.set:insert(tcp)
operation.cortn[tcp] = cortn
@ -233,7 +233,7 @@ function wakeup(operation, tcp)
kick(operation, tcp)
return cortn, coroutine.resume(cortn)
-- othrewise, just get scheduler not to do anything
else
else
return nil, true
end
end
@ -249,7 +249,7 @@ end
-- step through all active cortns
function cometat.__index:step()
-- check which sockets are interesting and act on them
local readable, writable = socket.select(self.receiving.set,
local readable, writable = socket.select(self.receiving.set,
self.sending.set, 1)
-- for all readable connections, resume their cortns and reschedule
-- when they yield back to us
@ -260,7 +260,7 @@ function cometat.__index:step()
for _, tcp in ipairs(writable) do
schedule(wakeup(self.sending, tcp))
end
-- politely ask replacement I/O functions in idle cortns to
-- politely ask replacement I/O functions in idle cortns to
-- return reporting a timeout
local now = socket.gettime()
for tcp, stamp in pairs(self.stamp) do
@ -271,25 +271,25 @@ function cometat.__index:step()
end
end
function cometat.__index:start(func)
function cometat.__index:start(func)
local cortn = coroutine.create(func)
schedule(cortn, coroutine.resume(cortn))
end
function handlert.coroutine()
local stamp = {}
local dispatcher = {
local dispatcher = {
stamp = stamp,
sending = {
name = "sending",
set = newset(),
cortn = {},
name = "sending",
set = newset(),
cortn = {},
stamp = stamp
},
receiving = {
name = "receiving",
set = newset(),
cortn = {},
set = newset(),
cortn = {},
stamp = stamp
},
}

View file

@ -1,5 +1,5 @@
-----------------------------------------------------------------------------
-- Little program to adjust end of line markers.
-- Little program to adjust end of line markers.
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id$

View file

@ -17,10 +17,10 @@ local function move(foo, bar)
live = data or error == "timeout"
data = data or partial
local result, error = bar:send(data)
if not live or not result then
foo:close()
if not live or not result then
foo:close()
bar:close()
break
break
end
end
end
@ -51,7 +51,7 @@ for i, v in ipairs(arg) do
handler:start(function()
move(client, peer)
end)
-- afte starting new handler, enter in loop sending data from
-- afte starting new handler, enter in loop sending data from
-- peer to client
move(peer, client)
end)

View file

@ -53,10 +53,10 @@ local elapsed_s = "%s received, %s/s throughput, %s elapsed "
function gauge(got, delta, size)
local rate = got / delta
if size and size >= 1 then
return string.format(remaining_s, nicesize(got), nicesize(rate),
return string.format(remaining_s, nicesize(got), nicesize(rate),
100*got/size, nicetime((size-got)/rate))
else
return string.format(elapsed_s, nicesize(got),
else
return string.format(elapsed_s, nicesize(got),
nicesize(rate), nicetime(delta))
end
end
@ -68,18 +68,18 @@ function stats(size)
local got = 0
return function(chunk)
-- elapsed time since start
local delta = socket.gettime() - start
if chunk then
local delta = socket.gettime() - start
if chunk then
-- total bytes received
got = got + string.len(chunk)
got = got + string.len(chunk)
-- not enough time for estimate
if delta > 0.1 then
io.stderr:write("\r", gauge(got, delta, size))
if delta > 0.1 then
io.stderr:write("\r", gauge(got, delta, size))
io.stderr:flush()
end
else
else
-- close up
io.stderr:write("\r", gauge(got, delta), "\n")
io.stderr:write("\r", gauge(got, delta), "\n")
end
return chunk
end
@ -111,11 +111,11 @@ function getbyftp(u, file)
local gett = url.parse(u)
gett.sink = save
gett.type = "i"
local ret, err = ftp.get(gett)
local ret, err = ftp.get(gett)
if err then print(err) end
end
-- determines the scheme
-- determines the scheme
function getscheme(u)
-- this is an heuristic to solve a common invalid url poblem
if not string.find(u, "//") then u = "//" .. u end
@ -134,7 +134,7 @@ end
-- main program
arg = arg or {}
if table.getn(arg) < 1 then
if table.getn(arg) < 1 then
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
os.exit(1)
else get(arg[1], arg[2]) end

View file

@ -46,7 +46,7 @@ local function ACK(block)
local low, high
low = math.mod(block, 256)
high = (block - low)/256
return char(0, OP_ACK, high, low)
return char(0, OP_ACK, high, low)
end
local function get_OP(dgram)
@ -71,7 +71,7 @@ local function get_ERROR(dgram)
end
-----------------------------------------------------------------------------
-- The real work
-- The real work
-----------------------------------------------------------------------------
local function tget(gett)
local retries, dgram, sent, datahost, dataport, code
@ -83,15 +83,15 @@ local function tget(gett)
gett.host = try(socket.dns.toip(gett.host))
con:settimeout(1)
-- first packet gives data host/port to be used for data transfers
local path = string.gsub(gett.path or "", "^/", "")
local path = string.gsub(gett.path or "", "^/", "")
path = url.unescape(path)
retries = 0
repeat
repeat
sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port))
dgram, datahost, dataport = con:receivefrom()
retries = retries + 1
until dgram or datahost ~= "timeout" or retries > 5
try(dgram, datahost)
try(dgram, datahost)
-- associate socket with data host/port
try(con:setpeername(datahost, dataport))
-- default sink
@ -110,7 +110,7 @@ local function tget(gett)
last = block
end
-- last packet brings less than 512 bytes of data
if string.len(data) < 512 then
if string.len(data) < 512 then
try(con:send(ACK(block)))
try(con:close())
try(sink(nil))
@ -118,7 +118,7 @@ local function tget(gett)
end
-- get the next packet
retries = 0
repeat
repeat
sent = try(con:send(ACK(last)))
dgram, err = con:receive()
retries = retries + 1
@ -141,7 +141,7 @@ local function parse(u)
end
local function sget(u)
local gett = parse(u)
local gett = parse(u)
local t = {}
gett.sink = ltn12.sink.table(t)
tget(gett)