diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-22 08:33:29 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-22 08:33:29 +0000 |
| commit | d55a5826e81136a9ecf65c4cd407152a56684dc2 (patch) | |
| tree | 109ad44c75cee890ad5e98583e12b15b5e65a18e /etc | |
| parent | a2b780bf7a78c66d54a248fa99b5fc862c12a127 (diff) | |
| download | luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.gz luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.bz2 luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.zip | |
Few tweaks in installation, some missing files, etc.
Diffstat (limited to 'etc')
| -rw-r--r-- | etc/check-links.lua | 6 | ||||
| -rw-r--r-- | etc/dict.lua | 10 | ||||
| -rw-r--r-- | etc/dispatch.lua | 82 | ||||
| -rw-r--r-- | etc/eol.lua | 2 | ||||
| -rw-r--r-- | etc/forward.lua | 8 | ||||
| -rw-r--r-- | etc/get.lua | 26 | ||||
| -rw-r--r-- | etc/tftp.lua | 16 |
7 files changed, 75 insertions, 75 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index 725cd2a..a989f8c 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
| @@ -37,7 +37,7 @@ function getstatus(link) | |||
| 37 | handler:start(function() | 37 | handler:start(function() |
| 38 | local r, c, h, s = http.request{ | 38 | local r, c, h, s = http.request{ |
| 39 | method = "HEAD", | 39 | method = "HEAD", |
| 40 | url = link, | 40 | url = link, |
| 41 | create = handler.tcp | 41 | create = handler.tcp |
| 42 | } | 42 | } |
| 43 | if r and c == 200 then io.write('\t', link, '\n') | 43 | if r and c == 200 then io.write('\t', link, '\n') |
| @@ -107,6 +107,6 @@ for _, address in ipairs(arg) do | |||
| 107 | checklinks(url.absolute("file:", address)) | 107 | checklinks(url.absolute("file:", address)) |
| 108 | end | 108 | end |
| 109 | 109 | ||
| 110 | while nthreads > 0 do | 110 | while nthreads > 0 do |
| 111 | handler:step() | 111 | handler:step() |
| 112 | end | 112 | end |
diff --git a/etc/dict.lua b/etc/dict.lua index 2d035a7..5c85aae 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
| @@ -17,7 +17,7 @@ local tp = require("socket.tp") | |||
| 17 | module("socket.dict") | 17 | module("socket.dict") |
| 18 | 18 | ||
| 19 | ----------------------------------------------------------------------------- | 19 | ----------------------------------------------------------------------------- |
| 20 | -- Globals | 20 | -- Globals |
| 21 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
| 22 | HOST = "dict.org" | 22 | HOST = "dict.org" |
| 23 | PORT = 2628 | 23 | PORT = 2628 |
| @@ -39,7 +39,7 @@ end | |||
| 39 | 39 | ||
| 40 | function metat.__index:check(ok) | 40 | function metat.__index:check(ok) |
| 41 | local code, status = socket.try(self.tp:check(ok)) | 41 | local code, status = socket.try(self.tp:check(ok)) |
| 42 | return code, | 42 | return code, |
| 43 | base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) | 43 | base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) |
| 44 | end | 44 | end |
| 45 | 45 | ||
| @@ -101,7 +101,7 @@ local default = { | |||
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | local function there(f) | 103 | local function there(f) |
| 104 | if f == "" then return nil | 104 | if f == "" then return nil |
| 105 | else return f end | 105 | else return f end |
| 106 | end | 106 | end |
| 107 | 107 | ||
| @@ -116,7 +116,7 @@ local function parse(u) | |||
| 116 | arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end) | 116 | arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end) |
| 117 | socket.try(t.word, "need at least <word> in URL") | 117 | socket.try(t.word, "need at least <word> in URL") |
| 118 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end) | 118 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end) |
| 119 | if cmd == "m" then | 119 | if cmd == "m" then |
| 120 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) | 120 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) |
| 121 | end | 121 | end |
| 122 | string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end) | 122 | string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end) |
| @@ -130,7 +130,7 @@ local function tget(gett) | |||
| 130 | local def = con:define(gett.database, gett.word) | 130 | local def = con:define(gett.database, gett.word) |
| 131 | con:quit() | 131 | con:quit() |
| 132 | con:close() | 132 | con:close() |
| 133 | if gett.n then return def[gett.n] | 133 | if gett.n then return def[gett.n] |
| 134 | else return def end | 134 | else return def end |
| 135 | elseif gett.command == "m" then | 135 | elseif gett.command == "m" then |
| 136 | local mat = con:match(gett.database, gett.strat, gett.word) | 136 | local mat = con:match(gett.database, gett.strat, gett.word) |
diff --git a/etc/dispatch.lua b/etc/dispatch.lua index 98fa8a8..dd76d6d 100644 --- a/etc/dispatch.lua +++ b/etc/dispatch.lua | |||
| @@ -14,7 +14,7 @@ module("dispatch") | |||
| 14 | TIMEOUT = 60 | 14 | TIMEOUT = 60 |
| 15 | 15 | ||
| 16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
| 17 | -- We implement 3 types of dispatchers: | 17 | -- We implement 3 types of dispatchers: |
| 18 | -- sequential | 18 | -- sequential |
| 19 | -- coroutine | 19 | -- coroutine |
| 20 | -- threaded | 20 | -- threaded |
| @@ -34,7 +34,7 @@ end | |||
| 34 | 34 | ||
| 35 | -- sequential handler simply calls the functions and doesn't wrap I/O | 35 | -- sequential handler simply calls the functions and doesn't wrap I/O |
| 36 | function handlert.sequential() | 36 | function handlert.sequential() |
| 37 | return { | 37 | return { |
| 38 | tcp = socket.tcp, | 38 | tcp = socket.tcp, |
| 39 | start = seqstart | 39 | start = seqstart |
| 40 | } | 40 | } |
| @@ -55,7 +55,7 @@ function socket.protect(f) | |||
| 55 | if not status then | 55 | if not status then |
| 56 | if type(results[1]) == 'table' then | 56 | if type(results[1]) == 'table' then |
| 57 | return nil, results[1][1] | 57 | return nil, results[1][1] |
| 58 | else error(results[1]) end | 58 | else error(results[1]) end |
| 59 | end | 59 | end |
| 60 | if coroutine.status(co) == "suspended" then | 60 | if coroutine.status(co) == "suspended" then |
| 61 | arg = {coroutine.yield(unpack(results))} | 61 | arg = {coroutine.yield(unpack(results))} |
| @@ -84,17 +84,17 @@ local function newset() | |||
| 84 | if index then | 84 | if index then |
| 85 | reverse[value] = nil | 85 | reverse[value] = nil |
| 86 | local top = table.remove(set) | 86 | local top = table.remove(set) |
| 87 | if top ~= value then | 87 | if top ~= value then |
| 88 | reverse[top] = index | 88 | reverse[top] = index |
| 89 | set[index] = top | 89 | set[index] = top |
| 90 | end | 90 | end |
| 91 | end | 91 | end |
| 92 | end | 92 | end |
| 93 | }}) | 93 | }}) |
| 94 | end | 94 | end |
| 95 | 95 | ||
| 96 | ----------------------------------------------------------------------------- | 96 | ----------------------------------------------------------------------------- |
| 97 | -- socket.tcp() wrapper for the coroutine dispatcher | 97 | -- socket.tcp() wrapper for the coroutine dispatcher |
| 98 | ----------------------------------------------------------------------------- | 98 | ----------------------------------------------------------------------------- |
| 99 | local function cowrap(dispatcher, tcp, error) | 99 | local function cowrap(dispatcher, tcp, error) |
| 100 | if not tcp then return nil, error end | 100 | if not tcp then return nil, error end |
| @@ -102,7 +102,7 @@ local function cowrap(dispatcher, tcp, error) | |||
| 102 | tcp:settimeout(0) | 102 | tcp:settimeout(0) |
| 103 | -- metatable for wrap produces new methods on demand for those that we | 103 | -- metatable for wrap produces new methods on demand for those that we |
| 104 | -- don't override explicitly. | 104 | -- don't override explicitly. |
| 105 | local metat = { __index = function(table, key) | 105 | local metat = { __index = function(table, key) |
| 106 | table[key] = function(...) | 106 | table[key] = function(...) |
| 107 | arg[1] = tcp | 107 | arg[1] = tcp |
| 108 | return tcp[key](unpack(arg)) | 108 | return tcp[key](unpack(arg)) |
| @@ -112,7 +112,7 @@ local function cowrap(dispatcher, tcp, error) | |||
| 112 | -- does our user want to do his own non-blocking I/O? | 112 | -- does our user want to do his own non-blocking I/O? |
| 113 | local zero = false | 113 | local zero = false |
| 114 | -- create a wrap object that will behave just like a real socket object | 114 | -- create a wrap object that will behave just like a real socket object |
| 115 | local wrap = { } | 115 | local wrap = { } |
| 116 | -- we ignore settimeout to preserve our 0 timeout, but record whether | 116 | -- we ignore settimeout to preserve our 0 timeout, but record whether |
| 117 | -- the user wants to do his own non-blocking I/O | 117 | -- the user wants to do his own non-blocking I/O |
| 118 | function wrap:settimeout(value, mode) | 118 | function wrap:settimeout(value, mode) |
| @@ -121,19 +121,19 @@ local function cowrap(dispatcher, tcp, error) | |||
| 121 | return 1 | 121 | return 1 |
| 122 | end | 122 | end |
| 123 | -- send in non-blocking mode and yield on timeout | 123 | -- send in non-blocking mode and yield on timeout |
| 124 | function wrap:send(data, first, last) | 124 | function wrap:send(data, first, last) |
| 125 | first = (first or 1) - 1 | 125 | first = (first or 1) - 1 |
| 126 | local result, error | 126 | local result, error |
| 127 | while true do | 127 | while true do |
| 128 | -- return control to dispatcher and tell it we want to send | 128 | -- return control to dispatcher and tell it we want to send |
| 129 | -- if upon return the dispatcher tells us we timed out, | 129 | -- if upon return the dispatcher tells us we timed out, |
| 130 | -- return an error to whoever called us | 130 | -- return an error to whoever called us |
| 131 | if coroutine.yield(dispatcher.sending, tcp) == "timeout" then | 131 | if coroutine.yield(dispatcher.sending, tcp) == "timeout" then |
| 132 | return nil, "timeout" | 132 | return nil, "timeout" |
| 133 | end | 133 | end |
| 134 | -- try sending | 134 | -- try sending |
| 135 | result, error, first = tcp:send(data, first+1, last) | 135 | result, error, first = tcp:send(data, first+1, last) |
| 136 | -- if we are done, or there was an unexpected error, | 136 | -- if we are done, or there was an unexpected error, |
| 137 | -- break away from loop | 137 | -- break away from loop |
| 138 | if error ~= "timeout" then return result, error, first end | 138 | if error ~= "timeout" then return result, error, first end |
| 139 | end | 139 | end |
| @@ -143,20 +143,20 @@ local function cowrap(dispatcher, tcp, error) | |||
| 143 | function wrap:receive(pattern, partial) | 143 | function wrap:receive(pattern, partial) |
| 144 | local error = "timeout" | 144 | local error = "timeout" |
| 145 | local value | 145 | local value |
| 146 | while true do | 146 | while true do |
| 147 | -- return control to dispatcher and tell it we want to receive | 147 | -- return control to dispatcher and tell it we want to receive |
| 148 | -- if upon return the dispatcher tells us we timed out, | 148 | -- if upon return the dispatcher tells us we timed out, |
| 149 | -- return an error to whoever called us | 149 | -- return an error to whoever called us |
| 150 | if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then | 150 | if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then |
| 151 | return nil, "timeout" | 151 | return nil, "timeout" |
| 152 | end | 152 | end |
| 153 | -- try receiving | 153 | -- try receiving |
| 154 | value, error, partial = tcp:receive(pattern, partial) | 154 | value, error, partial = tcp:receive(pattern, partial) |
| 155 | -- if we are done, or there was an unexpected error, | 155 | -- if we are done, or there was an unexpected error, |
| 156 | -- break away from loop. also, if the user requested | 156 | -- break away from loop. also, if the user requested |
| 157 | -- zero timeout, return all we got | 157 | -- zero timeout, return all we got |
| 158 | if (error ~= "timeout") or zero then | 158 | if (error ~= "timeout") or zero then |
| 159 | return value, error, partial | 159 | return value, error, partial |
| 160 | end | 160 | end |
| 161 | end | 161 | end |
| 162 | end | 162 | end |
| @@ -168,8 +168,8 @@ local function cowrap(dispatcher, tcp, error) | |||
| 168 | -- connection succeeds. | 168 | -- connection succeeds. |
| 169 | -- if upon return the dispatcher tells us we have a | 169 | -- if upon return the dispatcher tells us we have a |
| 170 | -- timeout, just abort | 170 | -- timeout, just abort |
| 171 | if coroutine.yield(dispatcher.sending, tcp) == "timeout" then | 171 | if coroutine.yield(dispatcher.sending, tcp) == "timeout" then |
| 172 | return nil, "timeout" | 172 | return nil, "timeout" |
| 173 | end | 173 | end |
| 174 | -- when we come back, check if connection was successful | 174 | -- when we come back, check if connection was successful |
| 175 | result, error = tcp:connect(host, port) | 175 | result, error = tcp:connect(host, port) |
| @@ -179,27 +179,27 @@ local function cowrap(dispatcher, tcp, error) | |||
| 179 | end | 179 | end |
| 180 | -- accept in non-blocking mode and yield on timeout | 180 | -- accept in non-blocking mode and yield on timeout |
| 181 | function wrap:accept() | 181 | function wrap:accept() |
| 182 | while 1 do | 182 | while 1 do |
| 183 | -- return control to dispatcher. we will be readable when a | 183 | -- return control to dispatcher. we will be readable when a |
| 184 | -- connection arrives. | 184 | -- connection arrives. |
| 185 | -- if upon return the dispatcher tells us we have a | 185 | -- if upon return the dispatcher tells us we have a |
| 186 | -- timeout, just abort | 186 | -- timeout, just abort |
| 187 | if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then | 187 | if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then |
| 188 | return nil, "timeout" | 188 | return nil, "timeout" |
| 189 | end | 189 | end |
| 190 | local client, error = tcp:accept() | 190 | local client, error = tcp:accept() |
| 191 | if error ~= "timeout" then | 191 | if error ~= "timeout" then |
| 192 | return cowrap(dispatcher, client, error) | 192 | return cowrap(dispatcher, client, error) |
| 193 | end | 193 | end |
| 194 | end | 194 | end |
| 195 | end | 195 | end |
| 196 | -- remove cortn from context | 196 | -- remove cortn from context |
| 197 | function wrap:close() | 197 | function wrap:close() |
| 198 | dispatcher.stamp[tcp] = nil | 198 | dispatcher.stamp[tcp] = nil |
| 199 | dispatcher.sending.set:remove(tcp) | 199 | dispatcher.sending.set:remove(tcp) |
| 200 | dispatcher.sending.cortn[tcp] = nil | 200 | dispatcher.sending.cortn[tcp] = nil |
| 201 | dispatcher.receiving.set:remove(tcp) | 201 | dispatcher.receiving.set:remove(tcp) |
| 202 | dispatcher.receiving.cortn[tcp] = nil | 202 | dispatcher.receiving.cortn[tcp] = nil |
| 203 | return tcp:close() | 203 | return tcp:close() |
| 204 | end | 204 | end |
| 205 | return setmetatable(wrap, metat) | 205 | return setmetatable(wrap, metat) |
| @@ -207,12 +207,12 @@ end | |||
| 207 | 207 | ||
| 208 | 208 | ||
| 209 | ----------------------------------------------------------------------------- | 209 | ----------------------------------------------------------------------------- |
| 210 | -- Our coroutine dispatcher | 210 | -- Our coroutine dispatcher |
| 211 | ----------------------------------------------------------------------------- | 211 | ----------------------------------------------------------------------------- |
| 212 | local cometat = { __index = {} } | 212 | local cometat = { __index = {} } |
| 213 | 213 | ||
| 214 | function schedule(cortn, status, operation, tcp) | 214 | function schedule(cortn, status, operation, tcp) |
| 215 | if status then | 215 | if status then |
| 216 | if cortn and operation then | 216 | if cortn and operation then |
| 217 | operation.set:insert(tcp) | 217 | operation.set:insert(tcp) |
| 218 | operation.cortn[tcp] = cortn | 218 | operation.cortn[tcp] = cortn |
| @@ -233,7 +233,7 @@ function wakeup(operation, tcp) | |||
| 233 | kick(operation, tcp) | 233 | kick(operation, tcp) |
| 234 | return cortn, coroutine.resume(cortn) | 234 | return cortn, coroutine.resume(cortn) |
| 235 | -- othrewise, just get scheduler not to do anything | 235 | -- othrewise, just get scheduler not to do anything |
| 236 | else | 236 | else |
| 237 | return nil, true | 237 | return nil, true |
| 238 | end | 238 | end |
| 239 | end | 239 | end |
| @@ -249,7 +249,7 @@ end | |||
| 249 | -- step through all active cortns | 249 | -- step through all active cortns |
| 250 | function cometat.__index:step() | 250 | function cometat.__index:step() |
| 251 | -- check which sockets are interesting and act on them | 251 | -- check which sockets are interesting and act on them |
| 252 | local readable, writable = socket.select(self.receiving.set, | 252 | local readable, writable = socket.select(self.receiving.set, |
| 253 | self.sending.set, 1) | 253 | self.sending.set, 1) |
| 254 | -- for all readable connections, resume their cortns and reschedule | 254 | -- for all readable connections, resume their cortns and reschedule |
| 255 | -- when they yield back to us | 255 | -- when they yield back to us |
| @@ -260,7 +260,7 @@ function cometat.__index:step() | |||
| 260 | for _, tcp in ipairs(writable) do | 260 | for _, tcp in ipairs(writable) do |
| 261 | schedule(wakeup(self.sending, tcp)) | 261 | schedule(wakeup(self.sending, tcp)) |
| 262 | end | 262 | end |
| 263 | -- politely ask replacement I/O functions in idle cortns to | 263 | -- politely ask replacement I/O functions in idle cortns to |
| 264 | -- return reporting a timeout | 264 | -- return reporting a timeout |
| 265 | local now = socket.gettime() | 265 | local now = socket.gettime() |
| 266 | for tcp, stamp in pairs(self.stamp) do | 266 | for tcp, stamp in pairs(self.stamp) do |
| @@ -271,25 +271,25 @@ function cometat.__index:step() | |||
| 271 | end | 271 | end |
| 272 | end | 272 | end |
| 273 | 273 | ||
| 274 | function cometat.__index:start(func) | 274 | function cometat.__index:start(func) |
| 275 | local cortn = coroutine.create(func) | 275 | local cortn = coroutine.create(func) |
| 276 | schedule(cortn, coroutine.resume(cortn)) | 276 | schedule(cortn, coroutine.resume(cortn)) |
| 277 | end | 277 | end |
| 278 | 278 | ||
| 279 | function handlert.coroutine() | 279 | function handlert.coroutine() |
| 280 | local stamp = {} | 280 | local stamp = {} |
| 281 | local dispatcher = { | 281 | local dispatcher = { |
| 282 | stamp = stamp, | 282 | stamp = stamp, |
| 283 | sending = { | 283 | sending = { |
| 284 | name = "sending", | 284 | name = "sending", |
| 285 | set = newset(), | 285 | set = newset(), |
| 286 | cortn = {}, | 286 | cortn = {}, |
| 287 | stamp = stamp | 287 | stamp = stamp |
| 288 | }, | 288 | }, |
| 289 | receiving = { | 289 | receiving = { |
| 290 | name = "receiving", | 290 | name = "receiving", |
| 291 | set = newset(), | 291 | set = newset(), |
| 292 | cortn = {}, | 292 | cortn = {}, |
| 293 | stamp = stamp | 293 | stamp = stamp |
| 294 | }, | 294 | }, |
| 295 | } | 295 | } |
diff --git a/etc/eol.lua b/etc/eol.lua index 01dba67..d7c5db6 100644 --- a/etc/eol.lua +++ b/etc/eol.lua | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
| 2 | -- Little program to adjust end of line markers. | 2 | -- Little program to adjust end of line markers. |
| 3 | -- LuaSocket sample files | 3 | -- LuaSocket sample files |
| 4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
| 5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
diff --git a/etc/forward.lua b/etc/forward.lua index eac98ae..9073ac4 100644 --- a/etc/forward.lua +++ b/etc/forward.lua | |||
| @@ -17,10 +17,10 @@ local function move(foo, bar) | |||
| 17 | live = data or error == "timeout" | 17 | live = data or error == "timeout" |
| 18 | data = data or partial | 18 | data = data or partial |
| 19 | local result, error = bar:send(data) | 19 | local result, error = bar:send(data) |
| 20 | if not live or not result then | 20 | if not live or not result then |
| 21 | foo:close() | 21 | foo:close() |
| 22 | bar:close() | 22 | bar:close() |
| 23 | break | 23 | break |
| 24 | end | 24 | end |
| 25 | end | 25 | end |
| 26 | end | 26 | end |
| @@ -51,7 +51,7 @@ for i, v in ipairs(arg) do | |||
| 51 | handler:start(function() | 51 | handler:start(function() |
| 52 | move(client, peer) | 52 | move(client, peer) |
| 53 | end) | 53 | end) |
| 54 | -- afte starting new handler, enter in loop sending data from | 54 | -- afte starting new handler, enter in loop sending data from |
| 55 | -- peer to client | 55 | -- peer to client |
| 56 | move(peer, client) | 56 | move(peer, client) |
| 57 | end) | 57 | end) |
diff --git a/etc/get.lua b/etc/get.lua index 0c95d54..ebf3e75 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
| @@ -53,10 +53,10 @@ local elapsed_s = "%s received, %s/s throughput, %s elapsed " | |||
| 53 | function gauge(got, delta, size) | 53 | function gauge(got, delta, size) |
| 54 | local rate = got / delta | 54 | local rate = got / delta |
| 55 | if size and size >= 1 then | 55 | if size and size >= 1 then |
| 56 | return string.format(remaining_s, nicesize(got), nicesize(rate), | 56 | return string.format(remaining_s, nicesize(got), nicesize(rate), |
| 57 | 100*got/size, nicetime((size-got)/rate)) | 57 | 100*got/size, nicetime((size-got)/rate)) |
| 58 | else | 58 | else |
| 59 | return string.format(elapsed_s, nicesize(got), | 59 | return string.format(elapsed_s, nicesize(got), |
| 60 | nicesize(rate), nicetime(delta)) | 60 | nicesize(rate), nicetime(delta)) |
| 61 | end | 61 | end |
| 62 | end | 62 | end |
| @@ -68,18 +68,18 @@ function stats(size) | |||
| 68 | local got = 0 | 68 | local got = 0 |
| 69 | return function(chunk) | 69 | return function(chunk) |
| 70 | -- elapsed time since start | 70 | -- elapsed time since start |
| 71 | local delta = socket.gettime() - start | 71 | local delta = socket.gettime() - start |
| 72 | if chunk then | 72 | if chunk then |
| 73 | -- total bytes received | 73 | -- total bytes received |
| 74 | got = got + string.len(chunk) | 74 | got = got + string.len(chunk) |
| 75 | -- not enough time for estimate | 75 | -- not enough time for estimate |
| 76 | if delta > 0.1 then | 76 | if delta > 0.1 then |
| 77 | io.stderr:write("\r", gauge(got, delta, size)) | 77 | io.stderr:write("\r", gauge(got, delta, size)) |
| 78 | io.stderr:flush() | 78 | io.stderr:flush() |
| 79 | end | 79 | end |
| 80 | else | 80 | else |
| 81 | -- close up | 81 | -- close up |
| 82 | io.stderr:write("\r", gauge(got, delta), "\n") | 82 | io.stderr:write("\r", gauge(got, delta), "\n") |
| 83 | end | 83 | end |
| 84 | return chunk | 84 | return chunk |
| 85 | end | 85 | end |
| @@ -111,11 +111,11 @@ function getbyftp(u, file) | |||
| 111 | local gett = url.parse(u) | 111 | local gett = url.parse(u) |
| 112 | gett.sink = save | 112 | gett.sink = save |
| 113 | gett.type = "i" | 113 | gett.type = "i" |
| 114 | local ret, err = ftp.get(gett) | 114 | local ret, err = ftp.get(gett) |
| 115 | if err then print(err) end | 115 | if err then print(err) end |
| 116 | end | 116 | end |
| 117 | 117 | ||
| 118 | -- determines the scheme | 118 | -- determines the scheme |
| 119 | function getscheme(u) | 119 | function getscheme(u) |
| 120 | -- this is an heuristic to solve a common invalid url poblem | 120 | -- this is an heuristic to solve a common invalid url poblem |
| 121 | if not string.find(u, "//") then u = "//" .. u end | 121 | if not string.find(u, "//") then u = "//" .. u end |
| @@ -134,7 +134,7 @@ end | |||
| 134 | 134 | ||
| 135 | -- main program | 135 | -- main program |
| 136 | arg = arg or {} | 136 | arg = arg or {} |
| 137 | if table.getn(arg) < 1 then | 137 | if table.getn(arg) < 1 then |
| 138 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") | 138 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") |
| 139 | os.exit(1) | 139 | os.exit(1) |
| 140 | else get(arg[1], arg[2]) end | 140 | else get(arg[1], arg[2]) end |
diff --git a/etc/tftp.lua b/etc/tftp.lua index 1b589fb..c028b20 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua | |||
| @@ -46,7 +46,7 @@ local function ACK(block) | |||
| 46 | local low, high | 46 | local low, high |
| 47 | low = math.mod(block, 256) | 47 | low = math.mod(block, 256) |
| 48 | high = (block - low)/256 | 48 | high = (block - low)/256 |
| 49 | return char(0, OP_ACK, high, low) | 49 | return char(0, OP_ACK, high, low) |
| 50 | end | 50 | end |
| 51 | 51 | ||
| 52 | local function get_OP(dgram) | 52 | local function get_OP(dgram) |
| @@ -71,7 +71,7 @@ local function get_ERROR(dgram) | |||
| 71 | end | 71 | end |
| 72 | 72 | ||
| 73 | ----------------------------------------------------------------------------- | 73 | ----------------------------------------------------------------------------- |
| 74 | -- The real work | 74 | -- The real work |
| 75 | ----------------------------------------------------------------------------- | 75 | ----------------------------------------------------------------------------- |
| 76 | local function tget(gett) | 76 | local function tget(gett) |
| 77 | local retries, dgram, sent, datahost, dataport, code | 77 | local retries, dgram, sent, datahost, dataport, code |
| @@ -83,15 +83,15 @@ local function tget(gett) | |||
| 83 | gett.host = try(socket.dns.toip(gett.host)) | 83 | gett.host = try(socket.dns.toip(gett.host)) |
| 84 | con:settimeout(1) | 84 | con:settimeout(1) |
| 85 | -- first packet gives data host/port to be used for data transfers | 85 | -- first packet gives data host/port to be used for data transfers |
| 86 | local path = string.gsub(gett.path or "", "^/", "") | 86 | local path = string.gsub(gett.path or "", "^/", "") |
| 87 | path = url.unescape(path) | 87 | path = url.unescape(path) |
| 88 | retries = 0 | 88 | retries = 0 |
| 89 | repeat | 89 | repeat |
| 90 | sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) | 90 | sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) |
| 91 | dgram, datahost, dataport = con:receivefrom() | 91 | dgram, datahost, dataport = con:receivefrom() |
| 92 | retries = retries + 1 | 92 | retries = retries + 1 |
| 93 | until dgram or datahost ~= "timeout" or retries > 5 | 93 | until dgram or datahost ~= "timeout" or retries > 5 |
| 94 | try(dgram, datahost) | 94 | try(dgram, datahost) |
| 95 | -- associate socket with data host/port | 95 | -- associate socket with data host/port |
| 96 | try(con:setpeername(datahost, dataport)) | 96 | try(con:setpeername(datahost, dataport)) |
| 97 | -- default sink | 97 | -- default sink |
| @@ -110,7 +110,7 @@ local function tget(gett) | |||
| 110 | last = block | 110 | last = block |
| 111 | end | 111 | end |
| 112 | -- last packet brings less than 512 bytes of data | 112 | -- last packet brings less than 512 bytes of data |
| 113 | if string.len(data) < 512 then | 113 | if string.len(data) < 512 then |
| 114 | try(con:send(ACK(block))) | 114 | try(con:send(ACK(block))) |
| 115 | try(con:close()) | 115 | try(con:close()) |
| 116 | try(sink(nil)) | 116 | try(sink(nil)) |
| @@ -118,7 +118,7 @@ local function tget(gett) | |||
| 118 | end | 118 | end |
| 119 | -- get the next packet | 119 | -- get the next packet |
| 120 | retries = 0 | 120 | retries = 0 |
| 121 | repeat | 121 | repeat |
| 122 | sent = try(con:send(ACK(last))) | 122 | sent = try(con:send(ACK(last))) |
| 123 | dgram, err = con:receive() | 123 | dgram, err = con:receive() |
| 124 | retries = retries + 1 | 124 | retries = retries + 1 |
| @@ -141,7 +141,7 @@ local function parse(u) | |||
| 141 | end | 141 | end |
| 142 | 142 | ||
| 143 | local function sget(u) | 143 | local function sget(u) |
| 144 | local gett = parse(u) | 144 | local gett = parse(u) |
| 145 | local t = {} | 145 | local t = {} |
| 146 | gett.sink = ltn12.sink.table(t) | 146 | gett.sink = ltn12.sink.table(t) |
| 147 | tget(gett) | 147 | tget(gett) |
