diff options
Diffstat (limited to 'nopsyncd')
-rw-r--r-- | nopsyncd/db.lua | 52 | ||||
-rw-r--r-- | nopsyncd/nopsyncd.c | 2 | ||||
-rwxr-xr-x | nopsyncd/qlistener.lua | 19 | ||||
-rwxr-xr-x | nopsyncd/tcpserver.lua | 7 |
4 files changed, 76 insertions, 4 deletions
diff --git a/nopsyncd/db.lua b/nopsyncd/db.lua new file mode 100644 index 0000000..ffdefed --- /dev/null +++ b/nopsyncd/db.lua @@ -0,0 +1,52 @@ +-- +-- 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.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/nopsyncd.c b/nopsyncd/nopsyncd.c index f216d1e..63bef7f 100644 --- a/nopsyncd/nopsyncd.c +++ b/nopsyncd/nopsyncd.c @@ -39,6 +39,8 @@ static const luaL_Reg nopsyncd_lualibs[] = { {LUA_TABLIBNAME, luaopen_table}, {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, + {LUA_MATHLIBNAME, luaopen_io}, + {LUA_MATHLIBNAME, luaopen_os}, {LUA_MATHLIBNAME, luaopen_pipe}, {NULL, NULL} }; diff --git a/nopsyncd/qlistener.lua b/nopsyncd/qlistener.lua index 2ebf5be..9498fbb 100755 --- a/nopsyncd/qlistener.lua +++ b/nopsyncd/qlistener.lua @@ -22,6 +22,7 @@ local queue_name = "/rhnop" mq = require "luamq" +db = require "db" function main_loop() local q, err = mq.create(queue_name, "ro") @@ -30,6 +31,12 @@ function main_loop() os.exit(1) end + local ret, err = db:init() + if ret == nil then + io.stderr:write("creation of db failed: " .. err .. "\n") + os.exit(1) + end + while true do local msg, prio = mq.receive(q) if msg == nil then @@ -37,9 +44,13 @@ function main_loop() os.exit(2) end - -- TODO: lookup info in Rivendell DB and insert into temporary storage - print("received message '" .. msg .. "' with prio: " .. prio) - - pipe.signal() + local timestamp, nowcart, nowlen, nextcart, nextlen = string.match(msg, "^(%d+) (%d+) (%d+) (%d+) (%d+)$"); + 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() + end end end
\ No newline at end of file diff --git a/nopsyncd/tcpserver.lua b/nopsyncd/tcpserver.lua index 972603d..fc19b9c 100755 --- a/nopsyncd/tcpserver.lua +++ b/nopsyncd/tcpserver.lua @@ -20,6 +20,7 @@ -- require "socket" +db = require "db" function init_server() local server = assert(socket.tcp()) @@ -85,6 +86,12 @@ end function main_loop() local pipefd = pipe.getreadfd() + local ret, err = db:init() + if ret == nil then + io.stderr:write("creation of db failed: " .. err .. "\n") + os.exit(1) + end + server = init_server() while true do |