Skip to content

Commit

Permalink
first working generic wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscoffier committed Sep 2, 2011
1 parent 8354d41 commit d1b902f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
72 changes: 72 additions & 0 deletions generic/zmq.c
@@ -0,0 +1,72 @@
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/zmq.c"
#else

static int Lzmq_(sendStorage)(lua_State *L)
{
zmq_ptr *s = luaL_checkudata(L, 1, MT_ZMQ_SOCKET);
size_t msg_size;
const char *data = luaL_checklstring(L, 2, &msg_size);
int flags = luaL_optint(L, 3, 0);

zmq_msg_t msg;
if(zmq_msg_init_size(&msg, msg_size) != 0) {
return Lzmq_push_error(L);
}
memcpy(zmq_msg_data(&msg), data, msg_size);

int rc = zmq_send(s->ptr, &msg, flags);

if(zmq_msg_close(&msg) != 0) {
return Lzmq_push_error(L);
}

if (rc != 0) {
return Lzmq_push_error(L);
}

lua_pushboolean(L, 1);

return 1;
}

static int Lzmq_(recvStorage)(lua_State *L)
{
zmq_ptr *s = luaL_checkudata(L, 1, MT_ZMQ_SOCKET);
int flags = luaL_optint(L, 2, 0);

zmq_msg_t msg;
if(zmq_msg_init(&msg) != 0) {
return Lzmq_push_error(L);
}

if(zmq_recv(s->ptr, &msg, flags) != 0) {
// Best we can do in this case is try to close and hope for the best.
zmq_msg_close(&msg);
return Lzmq_push_error(L);
}

lua_pushlstring(L, zmq_msg_data(&msg), zmq_msg_size(&msg));

if(zmq_msg_close(&msg) != 0) {
// Above string will be poped from the stack by the normalising code
// upon sucessful return.
return Lzmq_push_error(L);
}

return 1;
}

static const struct luaL_reg Lzmq_(methods)[] = {
{"sendStorage", Lzmq_(sendStorage)},
{"recvStorage", Lzmq_(recvStorage)},
{NULL, NULL}
};

static void Lzmq_(Init)(lua_State *L)
{
luaT_pushmetaclass(L, torch_(Storage_id));
luaT_registeratname(L, Lzmq_(methods), "zmq");
}

#endif
61 changes: 59 additions & 2 deletions zmq.c
Expand Up @@ -22,14 +22,49 @@

#define LUA_LIB

#include "lua.h"
#include "TH.h"
#include "luaT.h"
//#include "lua.h"
#include "lauxlib.h"

#include <zmq.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>

#ifdef min
#undef min
#endif
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )

#ifdef max
#undef max
#endif
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )

#define torch_(NAME) TH_CONCAT_3(torch_, Real, NAME)
#define torch_string_(NAME) TH_CONCAT_STRING_3(torch., Real, NAME)
#define Lzmq_(NAME) TH_CONCAT_3(Lzmq_, Real, NAME)

static const void* torch_CharStorage_id = NULL;
static const void* torch_ByteStorage_id = NULL;
static const void* torch_ShortStorage_id = NULL;
static const void* torch_IntStorage_id = NULL;
static const void* torch_LongStorage_id = NULL;
static const void* torch_FloatStorage_id = NULL;
static const void* torch_DoubleStorage_id = NULL;

/* detect zmq version >= 2.1.0 */
#define VERSION_2_1 0
#if defined(ZMQ_VERSION)
Expand Down Expand Up @@ -462,6 +497,8 @@ static int Lzmq_recv(lua_State *L)
return 1;
}



static const luaL_reg zmqlib[] = {
{"version", Lzmq_version},
{"init", Lzmq_init},
Expand Down Expand Up @@ -490,7 +527,10 @@ static const luaL_reg sockmethods[] = {

#define set_zmq_const(s) lua_pushinteger(L,ZMQ_##s); lua_setfield(L, -2, #s);

LUALIB_API int luaopen_libluazmq(lua_State *L)
#include "generic/zmq.c"
#include "THGenerateAllTypes.h"

DLL_EXPORT int luaopen_libluazmq(lua_State *L)
{
/* context metatable. */
luaL_newmetatable(L, MT_ZMQ_CONTEXT);
Expand Down Expand Up @@ -548,5 +588,22 @@ LUALIB_API int luaopen_libluazmq(lua_State *L)
set_zmq_const(NOBLOCK);
set_zmq_const(SNDMORE);

/* torch stuff */
torch_CharStorage_id = luaT_checktypename2id(L, "torch.CharStorage");
torch_ByteStorage_id = luaT_checktypename2id(L, "torch.ByteStorage");
torch_ShortStorage_id = luaT_checktypename2id(L, "torch.ShortStorage");
torch_IntStorage_id = luaT_checktypename2id(L, "torch.IntStorage");
torch_LongStorage_id = luaT_checktypename2id(L, "torch.LongStorage");
torch_FloatStorage_id = luaT_checktypename2id(L, "torch.FloatStorage");
torch_DoubleStorage_id = luaT_checktypename2id(L, "torch.DoubleStorage");

zmq_CharInit(L);
zmq_ByteInit(L);
zmq_ShortInit(L);
zmq_IntInit(L);
zmq_LongInit(L);
zmq_FloatInit(L);
zmq_DoubleInit(L);

return 1;
}

0 comments on commit d1b902f

Please sign in to comment.