From f6db683bf7a88e850c4bdf2a428efa464b62b3fb Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 5 Mar 2011 19:51:37 +0000 Subject: implemented send and receive --- src/luamq.c | 44 +++++++++++++++++++++++++++++++++++++++----- test.lua | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/luamq.c b/src/luamq.c index c2bb03f..c286b6c 100644 --- a/src/luamq.c +++ b/src/luamq.c @@ -5,7 +5,7 @@ #include #include #include - +#include #define MQD_TYPENAME "mqd_t" @@ -13,10 +13,10 @@ LUALIB_API int luaopen_luamq(lua_State *L); static int get_oflags(const char* flagstr) { - if(strcmp(flagstr, "ro")) + if(!strcmp(flagstr, "ro")) return O_RDONLY; - if(strcmp(flagstr, "wo")) + if(!strcmp(flagstr, "wo")) return O_WRONLY; return O_RDWR; @@ -73,12 +73,46 @@ static int l_luamq_send(lua_State *L) lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ return 2; } - return 0; + + lua_pushboolean(L, 1); + return 1; } static int l_luamq_receive(lua_State *L) { - return 0; + mqd_t* id; + unsigned int prio; + struct mq_attr attr; + ssize_t len; + char* str; + + id = luaL_checkudata(L, 1, MQD_TYPENAME); + if(mq_getattr(*id, &attr)) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + return 2; + } + + len = attr.mq_msgsize; + str = malloc(len); + if(!str) { + lua_pushnil(L); + lua_pushstring(L, "bad alloc"); + return 2; + } + + len = mq_receive(*id, str, len, &prio); + if(len < 0) { + free(str); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + return 2; + } + + lua_pushlstring(L, str, len); + free(str); + lua_pushinteger(L, prio); + return 2; } static int l_luamq_close(lua_State *L) diff --git a/test.lua b/test.lua index b1961c8..8861fea 100755 --- a/test.lua +++ b/test.lua @@ -10,6 +10,14 @@ end print("q created successfully") +result, err = mq.send(q, "hello world1") +if result == nil then + print(err) + return 1 +end +print("message sent successfully") + + result, err = mq.close(q) if result == nil then print(err) @@ -20,13 +28,29 @@ print("q closed successfully") -q, err = mq.open("/nownext", "ro") +q, err = mq.open("/nownext", "rw") if q == nil then print(err) return 1 end print("q opened successfully") + +result, err = mq.send(q, "hello world2", 1) +if result == nil then + print(err) + return 1 +end +print("message sent successfully") + +msg, prio = mq.receive(q) +if msg == nil then + print(prio) + return 1 +end +print("message '" .. msg .. "' received with prio " .. prio) + + result, err = mq.close(q) if result == nil then print(err) @@ -35,7 +59,19 @@ end print("q closed successfully") +q, err = mq.open("/nownext", "ro") +if q == nil then + print(err) + return 1 +end +print("q opened successfully") +msg, prio = mq.receive(q) +if msg == nil then + print(prio) + return 1 +end +print("message '" .. msg .. "' received with prio " .. prio) result, err = mq.unlink("/nownext") -- cgit v1.2.3