summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2011-03-05 19:11:20 +0000
committerChristian Pointner <equinox@spreadspace.org>2011-03-05 19:11:20 +0000
commitb387243639c4294c00bfd51f6d8187653d6a032d (patch)
treee86da246c46842eb1be9c3f3a1ba966167b1104a
parentadded svn default dirs (diff)
initial checkin
-rw-r--r--Makefile20
-rw-r--r--README10
-rw-r--r--config25
-rw-r--r--src/luamq.c124
-rw-r--r--src/luamq.def2
-rwxr-xr-xtest.lua51
6 files changed, 232 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..31ff59c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+CONFIG= ./config
+
+include $(CONFIG)
+
+OBJS= src/luamq.o
+SRCS= src/luamq.h src/luamq.c
+AR= ar rcu
+RANLIB= ranlib
+
+lib: src/$(LIBNAME)
+
+src/$(LIBNAME): $(OBJS)
+ $(CC) $(CFLAGS) -o $@ $(LIB_OPTION) $(OBJS)
+
+install:
+ mkdir -p $(LUA_LIBDIR)
+ cp src/$(LIBNAME) $(LUA_LIBDIR)/
+
+clean:
+ rm -f src/$(LIBNAME) src/*.o
diff --git a/README b/README
new file mode 100644
index 0000000..0943dee
--- /dev/null
+++ b/README
@@ -0,0 +1,10 @@
+LuaMQ 0.1
+http://www.spreadspace.org/luamq/
+
+LuaMQ is a simple interface from Lua to posix message queues. It
+allows interprocess communication using posix message queues.
+
+LuaMQ is free software and uses the same license as Lua 5.1.
+
+
+Source code for LuaMQ can be downloaded from the project page.
diff --git a/config b/config
new file mode 100644
index 0000000..1f3bd01
--- /dev/null
+++ b/config
@@ -0,0 +1,25 @@
+# Installation directories
+
+# Default prefix
+PREFIX = /usr
+
+# System's libraries directory (where binary libraries are installed)
+LUA_LIBDIR= $(PREFIX)/lib/lua/5.1
+
+# System's lua directory (where Lua libraries are installed)
+LUA_DIR= $(PREFIX)/share/lua/5.1
+
+# Lua includes directory
+LUA_INC= $(PREFIX)/include/lua5.1
+
+# OS dependent
+LIB_OPTION= -lrt -shared #for Linux
+
+V= 0.1
+
+LIBNAME= luamq.so
+
+WARN= -Wall -Wmissing-prototypes -Wmissing-declarations -ansi -pedantic -fPIC
+INCS= -I$(LUA_INC)
+CFLAGS= -O2 $(WARN) $(INCS)
+CC= gcc
diff --git a/src/luamq.c b/src/luamq.c
new file mode 100644
index 0000000..c2bb03f
--- /dev/null
+++ b/src/luamq.c
@@ -0,0 +1,124 @@
+#include "lua.h"
+#include "lauxlib.h"
+
+#include <sys/stat.h>
+#include <mqueue.h>
+#include <errno.h>
+#include <string.h>
+
+
+#define MQD_TYPENAME "mqd_t"
+
+LUALIB_API int luaopen_luamq(lua_State *L);
+
+static int get_oflags(const char* flagstr)
+{
+ if(strcmp(flagstr, "ro"))
+ return O_RDONLY;
+
+ if(strcmp(flagstr, "wo"))
+ return O_WRONLY;
+
+ return O_RDWR;
+}
+
+static int l_luamq_create(lua_State *L)
+{
+ mqd_t id, *ptr;
+ int flags;
+ flags = get_oflags(luaL_optlstring(L, 2, "", NULL));
+ id = mq_open(luaL_checkstring(L, 1), flags | O_CREAT, S_IRWXU | S_IRWXG, NULL);
+ if(id == (mqd_t)-1) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno)); /* TODO: thread safety */
+ return 2;
+ }
+ ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t));
+ *ptr = id;
+ luaL_newmetatable(L, MQD_TYPENAME);
+ lua_setmetatable(L, -2);
+ return 1;
+}
+
+static int l_luamq_open(lua_State *L)
+{
+ mqd_t id, *ptr;
+ int flags;
+ flags = get_oflags(luaL_optlstring(L, 2, "", NULL));
+ id = mq_open(luaL_checkstring(L, 1), flags);
+ if(id == (mqd_t)-1) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno)); /* TODO: thread safety */
+ return 2;
+ }
+ ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t));
+ *ptr = id;
+ luaL_newmetatable(L, MQD_TYPENAME);
+ lua_setmetatable(L, -2);
+ return 1;
+}
+
+static int l_luamq_send(lua_State *L)
+{
+ mqd_t* id;
+ unsigned int prio;
+ size_t len;
+ const char* str;
+
+ id = luaL_checkudata(L, 1, MQD_TYPENAME);
+ str = luaL_checklstring(L, 2, &len);
+ prio = luaL_optint(L, 3, 0);
+ if(mq_send(*id, str, len, prio)) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno)); /* TODO: thread safety */
+ return 2;
+ }
+ return 0;
+}
+
+static int l_luamq_receive(lua_State *L)
+{
+ return 0;
+}
+
+static int l_luamq_close(lua_State *L)
+{
+ mqd_t* id;
+ id = luaL_checkudata(L, 1, MQD_TYPENAME);
+ if(mq_close(*id)) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno)); /* TODO: thread safety */
+ return 2;
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+static int l_luamq_unlink(lua_State *L)
+{
+ if(mq_unlink(luaL_checkstring(L, 1))) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno)); /* TODO: thread safety */
+ return 2;
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+static const struct luaL_reg luamq_funcs [] = {
+ { "create", l_luamq_create },
+ { "open", l_luamq_open },
+ { "send", l_luamq_send },
+ { "receive", l_luamq_receive },
+ { "close", l_luamq_close },
+ { "unlink", l_luamq_unlink },
+ { NULL, NULL }
+};
+
+LUALIB_API int luaopen_luamq(lua_State *L)
+{
+ luaL_register(L, "luamq", luamq_funcs);
+ return 1;
+}
diff --git a/src/luamq.def b/src/luamq.def
new file mode 100644
index 0000000..20b82ea
--- /dev/null
+++ b/src/luamq.def
@@ -0,0 +1,2 @@
+EXPORTS
+ luaopen_luamq
diff --git a/test.lua b/test.lua
new file mode 100755
index 0000000..b1961c8
--- /dev/null
+++ b/test.lua
@@ -0,0 +1,51 @@
+#!/usr/bin/lua
+
+mq = require "luamq"
+
+q, err = mq.create("/nownext", "wo")
+if q == nil then
+ print(err)
+ return 1
+end
+print("q created successfully")
+
+
+result, err = mq.close(q)
+if result == nil then
+ print(err)
+ return 1
+end
+print("q closed successfully")
+
+
+
+
+q, err = mq.open("/nownext", "ro")
+if q == nil then
+ print(err)
+ return 1
+end
+print("q opened successfully")
+
+result, err = mq.close(q)
+if result == nil then
+ print(err)
+ return 1
+end
+print("q closed successfully")
+
+
+
+
+
+result, err = mq.unlink("/nownext")
+if result == nil then
+ print(err)
+ return 1
+end
+print("q deleted successfully")
+
+
+return 0
+
+