From 87a905c4419d6b83dde4209a84c4726f532b1845 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 8 May 2015 19:35:26 +0200 Subject: conf refactoring for nopyncd diff --git a/src/conf.lua b/src/conf.lua index a0af5d3..44546cc 100644 --- a/src/conf.lua +++ b/src/conf.lua @@ -21,13 +21,19 @@ local conf = {} -local file = assert(io.open(conffile, "r")) -for line in file:lines() do - local k,v = string.match(line, "^([^=#]+)=(.*)$") - if k and v and v ~= "" then - conf[k] = v +function conf.load(conffile) + local cnf = {} + + local file = assert(io.open(conffile, "r")) + for line in file:lines() do + local k,v = string.match(line, "^([^=#]+)=(.*)$") + if k and v and v ~= "" then + cnf[k] = v + end end + file:close() + + return cnf end -file:close() return conf diff --git a/src/configure b/src/configure index 4551aba..6cebe01 100755 --- a/src/configure +++ b/src/configure @@ -149,7 +149,7 @@ if [ -z "$LUA_DIR" ]; then break fi else - for dir in `ls -r -d $prefix/include/lua* 2> /dev/null`; do + for dir in `ls -d $prefix/include/lua* 2> /dev/null`; do if [ -e $dir/lua.h ]; then test_lua_version $dir/lua.h if [ $? -eq 1 ]; then diff --git a/src/playlog.lua b/src/playlog.lua index 33095c4..e692154 100644 --- a/src/playlog.lua +++ b/src/playlog.lua @@ -28,11 +28,9 @@ luasql = require "luasql.mysql" -- CREATE TABLE IF NOT EXISTS now (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); -- ALTER TABLE now CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -conf = require "conf" - local playlog = {} -function playlog:init() +function playlog:init(cnf) local err self.env, err = luasql.mysql() @@ -40,7 +38,7 @@ function playlog:init() return nil, err end - self.con, err = self.env:connect(conf.playlog_db, conf.playlog_user, conf.playlog_pwd, conf.playlog_host, conf.playlog_port) + self.con, err = self.env:connect(cnf.playlog_db, cnf.playlog_user, cnf.playlog_pwd, cnf.playlog_host, cnf.playlog_port) if self.con == nil then return nil, err end diff --git a/src/qlistener.lua b/src/qlistener.lua index e3b774b..2a44019 100644 --- a/src/qlistener.lua +++ b/src/qlistener.lua @@ -21,25 +21,28 @@ local last_cart = nil -require "posix" +posix = require "posix" mq = require "mq" package.path = package.path .. ";" .. rhnoplibdir .. "/?.lua" playlog = require "playlog" rddb = require "rddb" - -conffile = "nopsyncd.conf" conf = require "conf" -if conf.music_carts_lo == nil then - conf.music_carts_lo = 400000 -end -if conf.music_carts_hi == nil then - conf.music_carts_hi = 499999 -end -conf.music_carts_lo = tonumber(conf.music_carts_lo) -conf.music_carts_hi = tonumber(conf.music_carts_hi) +local cnf = {} +function init(conffile) + cnf = conf.load(conffile) + if cnf.music_carts_lo == nil then + cnf.music_carts_lo = 400000 + end + if cnf.music_carts_hi == nil then + cnf.music_carts_hi = 499999 + end + cnf.music_carts_lo = tonumber(cnf.music_carts_lo) + cnf.music_carts_hi = tonumber(cnf.music_carts_hi) +end + function handle_now(timestamp, nowcart, nowlen) local results, err = rddb:getCartInfo(nowcart); if results == nil then @@ -63,7 +66,7 @@ function handle_message(msg) else nowcart = tonumber(nowcart) nowlen = tonumber(nowlen) - if last_cart ~= nowcart and nowcart >= conf.music_carts_lo and nowcart <= conf.music_carts_hi and nowlen > 0 then + if last_cart ~= nowcart and nowcart >= cnf.music_carts_lo and nowcart <= cnf.music_carts_hi and nowlen > 0 then last_cart = nowcart handle_now(timestamp, nowcart, nowlen) end @@ -72,24 +75,26 @@ function handle_message(msg) end function main_loop() + init("nopsyncd.conf") + posix.umask("rwxrwxr-x") - local q, err = mq.create(conf.queue_name, "ro", "rw-rw----") + local q, err = mq.create(cnf.queue_name, "ro", "rw-rw----") if q == nil then - q, err = mq.open(conf.queue_name, "wo") + q, err = mq.open(cnf.queue_name, "wo") if q == nil then io.stderr:write("creation of message queue failed: " .. err .. "\n") os.exit(1) end end - local ret, err = playlog:init() + local ret, err = playlog:init(cnf) if ret == nil then io.stderr:write("creation of playlog failed: " .. err .. "\n") os.exit(1) end last_cart = assert(playlog:getLastCart()) - local ret, err = rddb:init() + local ret, err = rddb:init(cnf) if ret == nil then io.stderr:write("opening rivendell db failed: " .. err .. "\n") playlog:close() diff --git a/src/rddb.lua b/src/rddb.lua index 52ecf1b..4eb7bc8 100644 --- a/src/rddb.lua +++ b/src/rddb.lua @@ -23,7 +23,7 @@ luasql = require "luasql.mysql" local rddb = {} -function rddb:init(conf) +function rddb:init(cnf) local err self.env, err = luasql.mysql() @@ -31,7 +31,7 @@ function rddb:init(conf) return nil, err end - self.con, err = self.env:connect(conf.rddb_db, conf.rddb_user, conf.rddb_pwd, conf.rddb_host, conf.rddb_port) + self.con, err = self.env:connect(cnf.rddb_db, cnf.rddb_user, cnf.rddb_pwd, cnf.rddb_host, cnf.rddb_port) if self.con == nil then return nil, err end diff --git a/src/tcpserver.lua b/src/tcpserver.lua index 85992c3..837bf4e 100644 --- a/src/tcpserver.lua +++ b/src/tcpserver.lua @@ -19,16 +19,17 @@ -- along with rhnop. If not, see . -- -require "socket" +socket = require "socket" package.path = package.path .. ";" .. rhnoplibdir .. "/?.lua" conf = require "conf" function init_server() local server = assert(socket.tcp()) - + + cnf = conf.load("nopsyncd.conf") assert(server:setoption('reuseaddr', true)) - assert(server:bind(conf.tcp_host, conf.tcp_port)) + assert(server:bind(cnf.tcp_host, cnf.tcp_port)) assert(server:listen(5)) return server @@ -67,7 +68,7 @@ end function clients_get_writeables() local fds = {} - + for _, client in ipairs(clients) do if client.buffer ~= "" then table.insert(fds, client) @@ -127,7 +128,7 @@ function main_loop() end server:close() - cleanup_clients() + cleanup_clients() return 0 end -- cgit v0.10.2