--- a/examples/mpd.lua Fri Mar 27 09:48:17 2009 +0200
+++ b/examples/mpd.lua Fri Mar 27 12:06:19 2009 +0200
@@ -5,23 +5,23 @@
-- library
-require 'socket'
+local socket = require 'socket'
--- public
+--
-local settings = {
+local O = {
hostname = "localhost",
password = nil,
port = 6600,
}
-mpd = { }
+local F = { }
-- separator allows split output into records, that are started by any of present in separator keys
-- returns table of field (lowercase) - value records
-- command status is returned in STATUS field
-- on unexpected errors returns just false, dropping any available data
-function mpd.receive_message ( tcp, separator )
+function F.receive_message ( tcp, separator )
local ret = {}
local line = tcp:receive ( '*l' )
while line and not ( line:match ( '^OK' ) or line:match ( '^ACK' ) ) do
@@ -54,12 +54,12 @@
-- on one command returns just results of that command, on multi - array of results
-- on errors returns nil
-- noret can terminate socket too early, thus, do not use it with lists of commands
-function mpd.call_command ( opts )
+function F.call_command ( opts )
local tcp = socket.tcp ()
if not tcp then
print ( 'mpd: cannot get master tcp object' )
return nil
- elseif not tcp:connect ( settings.hostname, settings.port ) then
+ elseif not tcp:connect ( O.hostname, O.port ) then
tcp:close ()
print ( 'mpd: cannot connect to server' )
return nil
@@ -67,7 +67,7 @@
local ret = {}
if not opts.noret then
- ret = mpd.receive_message ( tcp )
+ ret = F.receive_message ( tcp )
if not ret then
tcp:close ()
print ( 'mpd: error getting greeting from server' )
@@ -84,7 +84,7 @@
return nil
end
if not opts.noret then
- ret[num] = mpd.receive_message ( tcp, opts.list )
+ ret[num] = F.receive_message ( tcp, opts.list )
if not ret[num] then
print ( 'mpd: error getting result' )
end
@@ -102,4 +102,12 @@
end
end
+function F.server ( host, port, password )
+ O.hostname = host
+ O.port = port
+ O.password = password
+end
+
+return F
+
-- vim: se ts=4: --