|
|
|
@ -18,65 +18,65 @@
|
|
|
|
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
-- THE SOFTWARE.
|
|
|
|
-- THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
require("zmq")
|
|
|
|
local zmq = require"zmq"
|
|
|
|
local ev = require'ev'
|
|
|
|
local ev = require'ev'
|
|
|
|
local loop = ev.Loop.default
|
|
|
|
local loop = ev.Loop.default
|
|
|
|
|
|
|
|
|
|
|
|
-- define a sub_worker class
|
|
|
|
-- define a sub_worker class
|
|
|
|
local sub_worker_mt = {}
|
|
|
|
local sub_worker_mt = {}
|
|
|
|
function sub_worker_mt:close(...)
|
|
|
|
function sub_worker_mt:close(...)
|
|
|
|
self.s_io_idle:stop(self.loop)
|
|
|
|
self.s_io_idle:stop(self.loop)
|
|
|
|
self.s_io_read:stop(self.loop)
|
|
|
|
self.s_io_read:stop(self.loop)
|
|
|
|
return self.socket:close(...)
|
|
|
|
return self.socket:close(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function sub_worker_mt:bind(...)
|
|
|
|
function sub_worker_mt:bind(...)
|
|
|
|
return self.socket:bind(...)
|
|
|
|
return self.socket:bind(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function sub_worker_mt:connect(...)
|
|
|
|
function sub_worker_mt:connect(...)
|
|
|
|
return self.socket:connect(...)
|
|
|
|
return self.socket:connect(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function sub_worker_mt:sub(topic)
|
|
|
|
function sub_worker_mt:sub(topic)
|
|
|
|
return self.socket:setopt(zmq.SUBSCRIBE, topic)
|
|
|
|
return self.socket:setopt(zmq.SUBSCRIBE, topic)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function sub_worker_mt:unsub(topic)
|
|
|
|
function sub_worker_mt:unsub(topic)
|
|
|
|
return self.socket:setopt(zmq.UNSUBSCRIBE, topic)
|
|
|
|
return self.socket:setopt(zmq.UNSUBSCRIBE, topic)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
sub_worker_mt.__index = sub_worker_mt
|
|
|
|
sub_worker_mt.__index = sub_worker_mt
|
|
|
|
|
|
|
|
|
|
|
|
local function sub_worker(loop, ctx, msg_cb)
|
|
|
|
local function sub_worker(loop, ctx, msg_cb)
|
|
|
|
local s = ctx:socket(zmq.SUB)
|
|
|
|
local s = ctx:socket(zmq.SUB)
|
|
|
|
local self = { loop = loop, socket = s, msg_cb = msg_cb }
|
|
|
|
local self = { loop = loop, socket = s, msg_cb = msg_cb }
|
|
|
|
setmetatable(self, sub_worker_mt)
|
|
|
|
setmetatable(self, sub_worker_mt)
|
|
|
|
-- create ev callbacks for recving data.
|
|
|
|
-- create ev callbacks for recving data.
|
|
|
|
-- need idle watcher since ZeroMQ sockets are edge-triggered instead of level-triggered
|
|
|
|
-- need idle watcher since ZeroMQ sockets are edge-triggered instead of level-triggered
|
|
|
|
local s_io_idle
|
|
|
|
local s_io_idle
|
|
|
|
local s_io_read
|
|
|
|
local s_io_read
|
|
|
|
s_io_idle = ev.Idle.new(function()
|
|
|
|
s_io_idle = ev.Idle.new(function()
|
|
|
|
local msg, err = s:recv(zmq.NOBLOCK)
|
|
|
|
local msg, err = s:recv(zmq.NOBLOCK)
|
|
|
|
if err == 'timeout' then
|
|
|
|
if err == 'timeout' then
|
|
|
|
-- need to block on read IO
|
|
|
|
-- need to block on read IO
|
|
|
|
s_io_idle:stop(loop)
|
|
|
|
s_io_idle:stop(loop)
|
|
|
|
s_io_read:start(loop)
|
|
|
|
s_io_read:start(loop)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self:msg_cb(msg)
|
|
|
|
self:msg_cb(msg)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
s_io_idle:start(loop)
|
|
|
|
s_io_idle:start(loop)
|
|
|
|
s_io_read = ev.IO.new(function()
|
|
|
|
s_io_read = ev.IO.new(function()
|
|
|
|
s_io_idle:start(loop)
|
|
|
|
s_io_idle:start(loop)
|
|
|
|
s_io_read:stop(loop)
|
|
|
|
s_io_read:stop(loop)
|
|
|
|
end, s:getopt(zmq.FD), ev.READ)
|
|
|
|
end, s:getopt(zmq.FD), ev.READ)
|
|
|
|
self.s_io_idle = s_io_idle
|
|
|
|
self.s_io_idle = s_io_idle
|
|
|
|
self.s_io_read = s_io_read
|
|
|
|
self.s_io_read = s_io_read
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local ctx = zmq.init(1)
|
|
|
|
local ctx = zmq.init(1)
|
|
|
|
|
|
|
|
|
|
|
|
-- message handling function.
|
|
|
|
-- message handling function.
|
|
|
|
local function handle_msg(worker, msg)
|
|
|
|
local function handle_msg(worker, msg)
|
|
|
|
local msg_id = tonumber(msg)
|
|
|
|
local msg_id = tonumber(msg)
|
|
|
|
if math.mod(msg_id, 10000) == 0 then print(worker.id, msg_id) end
|
|
|
|
if math.mod(msg_id, 10000) == 0 then print(worker.id, msg_id) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local sub1 = sub_worker(loop, ctx, handle_msg)
|
|
|
|
local sub1 = sub_worker(loop, ctx, handle_msg)
|
|
|
|
|