summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2011-03-05 19:51:37 +0000
committerChristian Pointner <equinox@spreadspace.org>2011-03-05 19:51:37 +0000
commitf6db683bf7a88e850c4bdf2a428efa464b62b3fb (patch)
tree62ed18fad519cae8f591c311727153400966e935
parentinitial checkin (diff)
implemented send and receive
-rw-r--r--src/luamq.c44
-rwxr-xr-xtest.lua38
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 <mqueue.h>
#include <errno.h>
#include <string.h>
-
+#include <stdlib.h>
#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")