Permalink
Browse files

Port to Lua 5.2 and the new module system.

  • Loading branch information...
lefcha committed Feb 12, 2012
1 parent a4af8a6 commit 7ce9acd70e4f570a626effbe1b6b01421ac88a8f
View
7 README
@@ -27,11 +27,12 @@ Changes
Installation
The LuaSocket and Copas modules are runtime dependencies.
Lua version 5.1 or 5.2 is compile-time requirement.
The new package system for Lua 5.1 is used and a makefile is supplied to
build and install:
The LuaSocket, Copas and Coxpcall modules are runtime dependencies.
Compile and install the system:
make all
make install
View
@@ -2,15 +2,8 @@ DESTDIR =
PREFIX = /usr/local
LIBDIR = $(PREFIX)/lib/lua/5.1
INCDIRS = -I/usr/local/include
LIBDIRS = -L/usr/local/lib
MYCFLAGS = -Wall -g -fpic
MYLDFLAGS = -shared
CFLAGS = $(MYCFLAGS) $(INCDIRS)
LDFLAGS = $(MYLDFLAGS) $(LIBDIRS)
CFLAGS = -Wall -g -fpic
LDFLAGS = -shared
LIBS = -llua
LIB = cldaemon.so
View
@@ -24,45 +24,40 @@ cldaemon_daemon(lua_State *lua)
switch (fork()) {
case -1:
fprintf(stderr, "forking; %s\n", strerror(errno));
exit(1);
lua_pushboolean(lua, 0);
return 1;
/* NOTREACHED */
break;
case 0:
break;
default:
exit(0);
/* NOTREACHED */
break;
}
if (setsid() == -1) {
fprintf(stderr, "creating session; %s\n", strerror(errno));
exit(1);
}
switch (fork()) {
case -1:
fprintf(stderr, "creating session; %s\n", strerror(errno));
exit(1);
break;
case 0:
break;
default:
exit(0);
break;
}
chdir("/");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (open("/dev/null", O_RDWR) != -1) {
dup(STDIN_FILENO);
dup(STDIN_FILENO);
}
if (open("/dev/null", O_RDWR) == -1 ||
dup(STDIN_FILENO) == -1 ||
dup(STDIN_FILENO) == -1)
fprintf(stderr, "duplicating file descriptors; %s\n",
strerror(errno));
#endif
return 0;
}
/* The cldaemon library. */
static const luaL_reg lib[] = {
static const luaL_Reg lib[] = {
{ "daemon", cldaemon_daemon },
{ NULL, NULL }
};
@@ -74,7 +69,11 @@ LUALIB_API int
luaopen_cldaemon(lua_State *lua)
{
luaL_openlib(lua, "cldaemon", lib, 0);
#if LUA_VERSION_NUM < 502
luaL_register(lua, "cldaemon", lib);
#else
luaL_newlib(lua, lib);
lua_setglobal(lua, "cldaemon");
#endif
return 1;
}
View
@@ -2,15 +2,8 @@ DESTDIR =
PREFIX = /usr/local
LIBDIR = $(PREFIX)/lib/lua/5.1
INCDIRS = -I/usr/local/include
LIBDIRS = -L/usr/local/lib
MYCFLAGS = -Wall -g -fpic
MYLDFLAGS = -shared
CFLAGS = $(MYCFLAGS) $(INCDIRS)
LDFLAGS = $(MYLDFLAGS) $(LIBDIRS)
CFLAGS = -Wall -g -fpic
LDFLAGS = -shared
LIBS = -llua
LIB = cltime.so
View
@@ -69,7 +69,7 @@ cltime_sleep(lua_State *L)
}
/* The cltime library. */
static const luaL_reg lib[] = {
static const luaL_Reg lib[] = {
{ "time", cltime_time },
{ "sleep", cltime_sleep },
{ NULL, NULL }
@@ -82,7 +82,11 @@ LUALIB_API int
luaopen_cltime(lua_State *lua)
{
luaL_openlib(lua, "cltime", lib, 0);
#if LUA_VERSION_NUM < 502
luaL_register(lua, "cltime", lib);
#else
luaL_newlib(lua, lib);
lua_setglobal(lua, "cldaemon");
#endif
return 1;
}
@@ -3,8 +3,7 @@ PREFIX = /usr/local
SHAREDIR = $(PREFIX)/share/lua/5.1
SHAREMODDIR = $(SHAREDIR)/concurrent/distributed
SHARE = init.lua \
network.lua \
SHARE = network.lua \
node.lua \
cookie.lua \
process.lua \
@@ -1,19 +1,21 @@
-- Submodule for setting the magic cookie.
module('concurrent._distributed._cookie', package.seeall)
local _cookie = {}
cookie = nil -- The magic cookie used for authentication.
_cookie.cookie = nil -- The magic cookie used for authentication.
-- Sets the magic cookie.
function setcookie(c)
function _cookie.setcookie(c)
if concurrent.node() then
cookie = c
_cookie.cookie = c
end
end
-- Returns the set magic cookie.
function getcookie()
return cookie
function _cookie.getcookie()
return _cookie.cookie
end
concurrent.setcookie = setcookie
concurrent.getcookie = getcookie
concurrent.setcookie = _cookie.setcookie
concurrent.getcookie = _cookie.getcookie
return _cookie

This file was deleted.

Oops, something went wrong.
@@ -1,18 +1,19 @@
-- Submodule for linking between distributed processes.
module('concurrent._distributed._link', package.seeall)
local _link = {}
-- The existing versions of the linking related functions are renamed.
_link = concurrent._link.link
_unlink = concurrent._link.unlink
_signal = concurrent._link.signal
_link._link = concurrent._link.link
_link._spawnlink = concurrent._link.spawnlink
_link._unlink = concurrent._link.unlink
_link._signal = concurrent._link.signal
-- Links the calling process with the specified process. If the destination
-- process is local the old renamed version of the function is called, otherwise
-- a linking request is sent to the node where the destination process is
-- executing under.
function link(dest)
function _link.link(dest)
if type(dest) ~= 'table' then
return _link(concurrent.whereis(dest))
return _link._link(concurrent.whereis(dest))
end
local links = concurrent._link.links
@@ -32,7 +33,7 @@ function link(dest)
end
-- Handles linking requests from a remote process.
function controller_link(msg)
function _link.controller_link(msg)
local links = concurrent._link.links
local pid = concurrent.whereis(msg.to.pid)
if not pid then
@@ -52,7 +53,7 @@ end
-- Creates a process either local or remote which is also linked to the calling
-- process.
function spawnlink(...)
function _link.spawnlink(...)
local pid, errmsg = concurrent.spawn(...)
if not pid then
return nil, errmsg
@@ -65,9 +66,9 @@ end
-- process is local the old renamed version of the function is called, otherwise
-- an unlinking request is sent to the node where the destination process is
-- executing under.
function unlink(dest)
function _link.unlink(dest)
if type(dest) ~= 'table' then
return _unlink(concurrent.whereis(dest))
return _link._unlink(concurrent.whereis(dest))
end
local links = concurrent._link.links
@@ -86,7 +87,7 @@ function unlink(dest)
end
-- Handles unlinking requests from a remote process.
function controller_unlink(msg)
function _link.controller_unlink(msg)
local links = concurrent._link.links
local pid = concurrent.whereis(msg.to.pid)
if not pid then
@@ -105,19 +106,19 @@ end
-- Signals all processes that are linked to processes in and node to which the
-- connection is lost.
function signal_all(deadnode)
function _link.signal_all(deadnode)
for k, v in pairs(concurrent._link.links) do
if v[2] == deadnode then
signal(k, v, 'noconnection')
_link.signal(k, v, 'noconnection')
end
end
end
-- Signals a single process that is linked to processes in a node to which the
-- connection is lost.
function signal(dest, dead, reason)
function _link.signal(dest, dead, reason)
if type(dest) ~= 'table' then
return _signal(concurrent.whereis(dest), dead, reason)
return _link._signal(concurrent.whereis(dest), dead, reason)
end
local pid, node = unpack(dest)
@@ -126,7 +127,7 @@ function signal(dest, dead, reason)
end
-- Handles exit requests from distributed processes.
function controller_exit(msg)
function _link.controller_exit(msg)
if not concurrent.getoption('trapexit') then
concurrent._process.kill(concurrent.whereis(msg.to.pid), msg.reason)
else
@@ -136,14 +137,17 @@ function controller_exit(msg)
end
-- Controllers to handle link, unlink and exit requests.
concurrent._distributed._network.controllers['LINK'] = controller_link
concurrent._distributed._network.controllers['UNLINK'] = controller_unlink
concurrent._distributed._network.controllers['EXIT'] = controller_exit
concurrent._distributed._network.controllers['LINK'] = _link.controller_link
concurrent._distributed._network.controllers['UNLINK'] = _link.controller_unlink
concurrent._distributed._network.controllers['EXIT'] = _link.controller_exit
-- Signals all processes linked to processes in a node to which the connection
-- is lost.
table.insert(concurrent._distributed._network.onfailure, signal_all)
table.insert(concurrent._distributed._network.onfailure, _link.signal_all)
concurrent.link = link
concurrent.unlink = unlink
concurrent._link.signal = signal
concurrent.link = _link.link
concurrent.spawnlink = _link.spawnlink
concurrent.unlink = _link.unlink
concurrent._link.signal = _link.signal
return _link
@@ -1,18 +1,18 @@
-- Submodule for sending messages to remote processes.
module('concurrent._distributed._message', package.seeall)
require 'mime'
local _message = {}
-- The existing version of this function for message sending is renamed.
_send = concurrent._message.send
_message._send = concurrent._message.send
-- Sends a message to local or remote processes. If the process is local the
-- old renamed version of this function is used, otherwise the message is send
-- through the network. The message is serialized and the magic cookie is also
-- attached before sent. Returns true for success and false otherwise.
function send(dest, mesg)
function _message.send(dest, mesg)
if type(dest) ~= 'table' then
return _send(concurrent.whereis(dest), mesg)
return _message._send(concurrent.whereis(dest), mesg)
end
local pid, node = unpack(dest)
@@ -24,9 +24,9 @@ function send(dest, mesg)
local data
if concurrent.getcookie() then
data = concurrent.getcookie() .. ' ' .. tostring(pid) .. ' ' ..
serialize(mesg) .. '\r\n'
_message.serialize(mesg) .. '\r\n'
else
data = tostring(pid) .. ' ' .. serialize(mesg) .. '\r\n'
data = tostring(pid) .. ' ' .. _message.serialize(mesg) .. '\r\n'
end
local total = #data
repeat
@@ -45,7 +45,7 @@ end
-- Serializes an object that can be any of: nil, boolean, number, string, table,
-- function. Returns the serialized object.
function serialize(obj)
function _message.serialize(obj)
local t = type(obj)
if t == 'nil' or t == 'boolean' or t == 'number' then
return tostring(obj)
@@ -58,9 +58,11 @@ function serialize(obj)
local t = '{'
for k, v in pairs(obj) do
if type(k) == 'number' or type(k) == 'boolean' then
t = t .. ' [' .. tostring(k) .. '] = ' .. serialize(v) .. ','
t = t .. ' [' .. tostring(k) .. '] = ' ..
_message.serialize(v) .. ','
else
t = t .. ' ["' .. tostring(k) .. '"] = ' .. serialize(v) .. ','
t = t .. ' ["' .. tostring(k) .. '"] = ' ..
_message.serialize(v) .. ','
end
end
t = t .. ' }'
@@ -70,4 +72,6 @@ function serialize(obj)
end
end
concurrent.send = send
concurrent.send = _message.send
return _message
Oops, something went wrong.

0 comments on commit 7ce9acd

Please sign in to comment.