Almost ready for beta3

This commit is contained in:
Diego Nehab 2004-11-27 07:58:04 +00:00
parent eb0fc857dd
commit 7c97e8e40a
26 changed files with 310 additions and 190 deletions

View file

@ -8,11 +8,14 @@
-----------------------------------------------------------------------------
-- Load required modules
-----------------------------------------------------------------------------
local base = require("base")
local string = require("string")
local table = require("table")
local socket = require("socket")
local url = require("socket.url")
local tp = require("socket.tp")
module("socket.dict")
local dict = module("socket.dict")
-----------------------------------------------------------------------------
-- Globals
@ -28,7 +31,7 @@ local metat = { __index = {} }
function open(host, port)
local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT))
return setmetatable({tp = tp}, metat)
return base.setmetatable({tp = tp}, metat)
end
function metat.__index:greet()
@ -37,7 +40,8 @@ end
function metat.__index:check(ok)
local code, status = socket.try(self.tp:check(ok))
return code, tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
return code,
base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
end
function metat.__index:getdef()
@ -116,7 +120,7 @@ local function parse(u)
if cmd == "m" then
arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
end
string.gsub(arg, ":([^:]*)$", function(f) t.n = tonumber(f) end)
string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
return t
end
@ -143,6 +147,8 @@ local function sget(u)
end
get = socket.protect(function(gett)
if type(gett) == "string" then return sget(gett)
if base.type(gett) == "string" then return sget(gett)
else return tget(gett) end
end)
base.setmetatable(dict, nil)

View file

@ -9,9 +9,12 @@
if you have any questions: RFC 1179
]]
-- make sure LuaSocket is loaded
local io = require("io")
local base = require("base")
local string = require("string")
local socket = require("socket")
local ltn12 = require("ltn12")
local test = socket.try
local lp = module("socket.lp")
-- default port
PORT = 515
@ -28,7 +31,7 @@ local function connect(localhost, option)
local localport = 721
local done, err
repeat
skt = test(socket.tcp())
skt = socket.try(socket.tcp())
try(skt:settimeout(30))
done, err = skt:bind(localhost, localport)
if not done then
@ -37,8 +40,8 @@ local function connect(localhost, option)
skt = nil
else break end
until localport > 731
test(skt, err)
else skt = test(socket.tcp()) end
socket.try(skt, err)
else skt = socket.try(socket.tcp()) end
try(skt:connect(host, port))
return { skt = skt, try = try }
end
@ -241,9 +244,9 @@ local format_codes = {
-- lp.send
send = socket.protect(function(file, option)
test(file, "invalid file name")
test(option and type(option) == "table", "invalid options")
local fh = test(io.open(file,"rb"))
socket.try(file, "invalid file name")
socket.try(option and base.type(option) == "table", "invalid options")
local fh = socket.try(io.open(file,"rb"))
local datafile_size = fh:seek("end") -- get total size
fh:seek("set") -- go back to start of file
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
@ -270,11 +273,11 @@ send = socket.protect(function(file, option)
lpfile,
ctlfn); -- mandatory part of ctl file
if (option.banner) then cfile = cfile .. 'L'..user..'\10' end
if (option.indent) then cfile = cfile .. 'I'..tonumber(option.indent)..'\10' end
if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end
if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end
if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end
if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then
cfile = cfile .. 'W'..tonumber(option,width)..'\10'
cfile = cfile .. 'W'..base.tonumber(option,width)..'\10'
end
con.skt:settimeout(option.timeout or 65)
@ -314,3 +317,5 @@ query = socket.protect(function(p)
con.skt:close()
return data
end)
base.setmetatable(lp, nil)

View file

@ -8,11 +8,14 @@
-----------------------------------------------------------------------------
-- Load required files
-----------------------------------------------------------------------------
local base = require("base")
local table = require("table")
local math = require("math")
local string = require("string")
local socket = require("socket")
local ltn12 = require("ltn12")
local url = require("socket.url")
module("socket.tftp")
local tftp = module("socket.tftp")
-----------------------------------------------------------------------------
-- Program constants
@ -73,16 +76,18 @@ end
local function tget(gett)
local retries, dgram, sent, datahost, dataport, code
local last = 0
socket.try(gett.host, "missing host")
local con = socket.try(socket.udp())
local try = socket.newtry(function() con:close() end)
-- convert from name to ip if needed
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 "", "^/", "")
path = url.unescape(path)
retries = 0
repeat
sent = try(con:sendto(RRQ(gett.path, "octet"),
gett.host, gett.port))
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
@ -144,6 +149,8 @@ local function sget(u)
end
get = socket.protect(function(gett)
if type(gett) == "string" then return sget(gett)
if base.type(gett) == "string" then return sget(gett)
else return tget(gett) end
end)
base.setmetatable(tftp, nil)