diff options
author | Christian Pointner <equinox@helsinki.at> | 2011-03-21 16:09:14 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2011-03-21 16:09:14 (GMT) |
commit | 473634799142c6cc9f7c78778add94958f5293d2 (patch) | |
tree | be9d7906caebf3bd5532d71784f9f65bc013e7b2 | |
parent | 46348e1cd2782a701134b030be7452910ba2c46e (diff) |
nopsyncd first full version
- only timestamps through tcp connection
- info can be fetched from mysql playlog
-rw-r--r-- | nopsyncd/l_pipe.c | 38 | ||||
-rwxr-xr-x | nopsyncd/qlistener.lua | 2 | ||||
-rwxr-xr-x | nopsyncd/tcpserver.lua | 31 |
3 files changed, 35 insertions, 36 deletions
diff --git a/nopsyncd/l_pipe.c b/nopsyncd/l_pipe.c index 5a99809..6b769a5 100644 --- a/nopsyncd/l_pipe.c +++ b/nopsyncd/l_pipe.c @@ -43,16 +43,19 @@ void pipe_close() static int l_pipe_signal(lua_State *L) { - char data = 1; - int ret = 0; + size_t len = 0, written = 0; + const char* data = luaL_checklstring(L, 1, &len); + len++; // also send trailing zero + int ret = 0; for(;;) { - ret = write(pipefds_[1], &data, 1); - if(ret == 1) break; - + ret = write(pipefds_[1], &(data[written]), len - written); if(!ret) continue; if(ret == -1 && (errno == EAGAIN || errno == EINTR)) continue; + written += ret; + if(written == len) break; + break; } @@ -93,19 +96,30 @@ static int l_pipe_getreadfd(lua_State *L) static int l_pipe_consume(lua_State *L) { - char data; - int ret = 0; + char data[17]; // 17 should be sufficient + size_t len = 0; + int ret = 0; for(;;) { - ret = read(pipefds_[0], &data, 1); - if(ret == 1 || ret == 0) break; - + ret = read(pipefds_[0], &(data[len]), 1); + if(ret == 0) break; if(ret == -1 && (errno == EAGAIN || errno == EINTR)) continue; - break; + if(data[len] == 0) + break; + + len += ret; + if(len >= sizeof(data)) { + ret = 0; + break; + } } - lua_pushinteger(L, ret); + if(ret) + lua_pushstring(L, data); + else + lua_pushnil(L); + return 1; } diff --git a/nopsyncd/qlistener.lua b/nopsyncd/qlistener.lua index 753902d..c507358 100755 --- a/nopsyncd/qlistener.lua +++ b/nopsyncd/qlistener.lua @@ -36,7 +36,7 @@ function handle_now(timestamp, nowcart, nowlen) if ret == nil then io.stderr:write("can't insert music info: " .. err .. "\n") else - pipe.signal() + pipe.signal(timestamp) end end end diff --git a/nopsyncd/tcpserver.lua b/nopsyncd/tcpserver.lua index 7530ff2..dd6f18b 100755 --- a/nopsyncd/tcpserver.lua +++ b/nopsyncd/tcpserver.lua @@ -20,7 +20,6 @@ -- require "socket" -playlog = require "playlog" function init_server() local server = assert(socket.tcp()) @@ -75,25 +74,16 @@ function clients_get_writeables() return fds end -function clients_senddata() - -- TODO: fetch data from SQL and send it out to all clients - +function clients_senddata(timestamp) for _, client in ipairs(clients) do - client.buffer = client.buffer .. "hello world\n" + client.buffer = client.buffer .. timestamp .. "\n" end end function main_loop() local pipefd = pipe.getreadfd() - local ret, err = playlog:init() - if ret == nil then - io.stderr:write("creation of playlog failed: " .. err .. "\n") - os.exit(1) - end - server = init_server() - while true do local readables, writeables, err = socket.select({ pipefd , server , unpack(clients) } , clients_get_writeables()) if err then @@ -102,26 +92,22 @@ function main_loop() else for _, input in ipairs(readables) do if input == pipefd then - local ret = pipe.consume() - if ret == 1 then - print("pipe was signaled") - clients_senddata() - else - return ret + local timestamp = pipe.consume() + if timestamp == nil then + return 2 end + print("pipe was signaled with timestamp: " .. timestamp) + clients_senddata(timestamp) elseif input == server then local client = assert(server:accept()) add_client(client) else if input.hdl then -- receive is insanely stupid, therefore we must always read one byte only - local tmp, err, part = input.hdl:receive(1) + local _, err = input.hdl:receive(1) if err then remove_client(input) end - - -- TODO: receive timestamp of last received entry - -- send out (enqueue in buffer) all entrys since this timestamp end end end @@ -133,7 +119,6 @@ function main_loop() end end - playlog:close() server:close() cleanup_clients() |