diff options
author | Christian Pointner <equinox@helsinki.at> | 2011-03-16 00:54:52 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2011-03-16 00:54:52 (GMT) |
commit | fba23d62b11f361af19eae4cf545f946c17b97a5 (patch) | |
tree | c02ac491b450ac3bd0f2261f8b5f593a7e8e964d | |
parent | 2edacdcb0ac14a1e3ab8d6f6bc2a56f81783a88a (diff) |
added rddb lookup
-rwxr-xr-x | nopsyncd/qlistener.lua | 29 | ||||
-rw-r--r-- | nopsyncd/rddb.lua | 71 | ||||
-rwxr-xr-x | nopsyncd/tcpserver.lua | 9 | ||||
-rw-r--r-- | nopsyncd/tempstorage.lua (renamed from nopsyncd/db.lua) | 16 |
4 files changed, 111 insertions, 14 deletions
diff --git a/nopsyncd/qlistener.lua b/nopsyncd/qlistener.lua index 9498fbb..b7c74d8 100755 --- a/nopsyncd/qlistener.lua +++ b/nopsyncd/qlistener.lua @@ -22,7 +22,8 @@ local queue_name = "/rhnop" mq = require "luamq" -db = require "db" +tempstorage = require "tempstorage" +rddb = require "rddb" function main_loop() local q, err = mq.create(queue_name, "ro") @@ -31,9 +32,16 @@ function main_loop() os.exit(1) end - local ret, err = db:init() + local ret, err = tempstorage:init() if ret == nil then - io.stderr:write("creation of db failed: " .. err .. "\n") + io.stderr:write("creation of tempstorage failed: " .. err .. "\n") + os.exit(1) + end + + local ret, err = rddb:init() + if ret == nil then + io.stderr:write("opening rivendell db failed: " .. err .. "\n") + tempstorage:close() os.exit(1) end @@ -41,6 +49,8 @@ function main_loop() local msg, prio = mq.receive(q) if msg == nil then io.stderr:write("recv error: " .. prio .. "\n") + rddb:close() + tempstorage:close() os.exit(2) end @@ -48,9 +58,14 @@ function main_loop() if not timestamp or not nowcart or not nowlen or not nextcart or not nextlen then io.stderr:write("ignoring malformed message\n") else - -- TODO: lookup info in Rivendell DB and insert into temporary storage - print("received message:\ntimestamp = " .. timestamp .. "\nnowcart = " .. nowcart .. "\nnowlen = " .. nowlen .. "\nnextcart = " .. nextcart .. "\nnextlen = " .. nextlen) - pipe.signal() + local results, err = rddb:getCartInfo(nowcart); + if results == nil then + io.stderr:write("can't fetch cart info: " .. err .. "\n") + else + -- TODO: insert into tempstorage + print(timestamp .. " Info: '" .. results.TITLE .. "' von '" .. results.ARTIST .. "' aus '" .. results.ALBUM .. "'") + pipe.signal() + end end end -end
\ No newline at end of file +end
\ No newline at end of file diff --git a/nopsyncd/rddb.lua b/nopsyncd/rddb.lua new file mode 100644 index 0000000..1a16a4d --- /dev/null +++ b/nopsyncd/rddb.lua @@ -0,0 +1,71 @@ +-- +-- rhnop +-- +-- Copyright (C) 2011 Christian Pointner <equinox@helsinki.at> +-- +-- This file is part of rhnop. +-- +-- rhnop is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- any later version. +-- +-- rhnop is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with rhnop. If not, see <http://www.gnu.org/licenses/>. +-- + +require "luasql.mysql" + +local rddb = {} + +function rddb:init() + local err + + self.env, err = luasql.mysql() + if self.env == nil then + return nil, err + end + + -- TODO: read /etc/rd.conf for connection info + self.con, err = self.env:connect("rivendell", "rivendellro", "lldrivenro", "127.0.0.1") + if self.con == nil then + return nil, err + end + + return true +end + +function rddb:getCartInfo(cartnum) + -- TODO: SQL Injections!!! + local cur, err = self.con:execute("select TITLE,ARTIST,ALBUM from CART where NUMBER = " .. cartnum); + if cur == nil then + return nil, err + end + + if cur:numrows() ~= 1 then + return nil, "nothing found in rivendell db" + end + + local results = {} + results, err = cur:fetch(results, "a") + cur:close() + + return results, err +end + +function rddb:close() + if self.con then + self.con:close() + end + + if self.env then + self.env:close() + end +end + +return rddb diff --git a/nopsyncd/tcpserver.lua b/nopsyncd/tcpserver.lua index fc19b9c..3f7c70a 100755 --- a/nopsyncd/tcpserver.lua +++ b/nopsyncd/tcpserver.lua @@ -20,7 +20,7 @@ -- require "socket" -db = require "db" +tempstorage = require "tempstorage" function init_server() local server = assert(socket.tcp()) @@ -86,9 +86,9 @@ end function main_loop() local pipefd = pipe.getreadfd() - local ret, err = db:init() + local ret, err = tempstorage:init() if ret == nil then - io.stderr:write("creation of db failed: " .. err .. "\n") + io.stderr:write("creation of tempstorage failed: " .. err .. "\n") os.exit(1) end @@ -133,8 +133,9 @@ function main_loop() end end + tempstorage:close() server:close() - cleanup_clients() + cleanup_clients() return 0 end diff --git a/nopsyncd/db.lua b/nopsyncd/tempstorage.lua index ffdefed..f034647 100644 --- a/nopsyncd/db.lua +++ b/nopsyncd/tempstorage.lua @@ -21,9 +21,9 @@ require "luasql.sqlite3" -local db = {} +local tempstorage = {} -function db:init() +function tempstorage:init() local err self.env, err = luasql.sqlite3() @@ -49,4 +49,14 @@ function db:init() return true end -return db +function tempstorage:close() + if self.con then + self.con:close() + end + + if self.env then + self.env:close() + end +end + +return tempstorage |