From fba23d62b11f361af19eae4cf545f946c17b97a5 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 16 Mar 2011 00:54:52 +0000 Subject: added rddb lookup diff --git a/nopsyncd/db.lua b/nopsyncd/db.lua deleted file mode 100644 index ffdefed..0000000 --- a/nopsyncd/db.lua +++ /dev/null @@ -1,52 +0,0 @@ --- --- rhnop --- --- Copyright (C) 2011 Christian Pointner --- --- 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 . --- - -require "luasql.sqlite3" - -local db = {} - -function db:init() - local err - - self.env, err = luasql.sqlite3() - if self.env == nil then - return nil, err - end - - self.con, err = self.env:connect("nopsync.db") - if self.con == nil then - return nil, err - end - - local ret, err = self.con:setautocommit(true) - if ret == nil then - return nil, err - end - - local rows, err = self.con:execute("CREATE TABLE IF NOT EXISTS now (timestamp INT PRIMARY KEY ASC NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN)") - if rows == nil then - return nil, err - end - - return true -end - -return db 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 +-- +-- 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 . +-- + +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/tempstorage.lua b/nopsyncd/tempstorage.lua new file mode 100644 index 0000000..f034647 --- /dev/null +++ b/nopsyncd/tempstorage.lua @@ -0,0 +1,62 @@ +-- +-- rhnop +-- +-- Copyright (C) 2011 Christian Pointner +-- +-- 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 . +-- + +require "luasql.sqlite3" + +local tempstorage = {} + +function tempstorage:init() + local err + + self.env, err = luasql.sqlite3() + if self.env == nil then + return nil, err + end + + self.con, err = self.env:connect("nopsync.db") + if self.con == nil then + return nil, err + end + + local ret, err = self.con:setautocommit(true) + if ret == nil then + return nil, err + end + + local rows, err = self.con:execute("CREATE TABLE IF NOT EXISTS now (timestamp INT PRIMARY KEY ASC NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN)") + if rows == nil then + return nil, err + end + + return true +end + +function tempstorage:close() + if self.con then + self.con:close() + end + + if self.env then + self.env:close() + end +end + +return tempstorage -- cgit v0.10.2