From b387243639c4294c00bfd51f6d8187653d6a032d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 5 Mar 2011 19:11:20 +0000 Subject: initial checkin --- Makefile | 20 ++++++++++ README | 10 +++++ config | 25 ++++++++++++ src/luamq.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/luamq.def | 2 + test.lua | 51 ++++++++++++++++++++++++ 6 files changed, 232 insertions(+) create mode 100644 Makefile create mode 100644 README create mode 100644 config create mode 100644 src/luamq.c create mode 100644 src/luamq.def create mode 100755 test.lua 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 +#include +#include +#include + + +#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 + + -- cgit v1.2.3