Cleanup examples code.

pull/47/merge
Robert G. Jakabosky 14 years ago
parent caba791908
commit 0697debefb

@ -18,15 +18,24 @@
-- 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 ctx = zmq.init(1) local N=tonumber(arg[1] or 100)
local ctx = zmq.init()
local s = ctx:socket(zmq.REQ) local s = ctx:socket(zmq.REQ)
s:connect("tcp://localhost:5555") s:connect("tcp://localhost:5555")
s:send("SELECT * FROM mytable") for i=1,N do
print(s:recv()) s:send("SELECT * FROM mytable")
local data, err = s:recv()
if data then
print(data)
else
print("s:recv() error:", err)
end
end
s:close() s:close()
ctx:term() ctx:term()

@ -18,17 +18,26 @@
-- 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 ctx = zmq.init(1) local N=tonumber(arg[1] or 100)
local ctx = zmq.init()
local s = ctx:socket(zmq.REQ) local s = ctx:socket(zmq.REQ)
s:connect("tcp://localhost:5555") s:connect("tcp://localhost:5555")
s:send("SELECT * FROM mytable ", zmq.SNDMORE) for i=1,N do
s:send("WHERE library = 'zmq'") s:send("SELECT * FROM mytable ", zmq.SNDMORE)
s:send("WHERE library = 'zmq'")
print(s:recv()) local data, err = s:recv()
if data then
print(data)
else
print("s:recv() error:", err)
end
end
s:close() s:close()
ctx:term() ctx:term()

@ -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)

@ -18,15 +18,15 @@
-- 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 ctx = zmq.init(1) local ctx = zmq.init()
local s = ctx:socket(zmq.PUB) local s = ctx:socket(zmq.PUB)
s:bind("tcp://lo:5555") s:bind("tcp://lo:5555")
local msg_id = 1 local msg_id = 1
while true do while true do
s:send(tostring(msg_id)) s:send(tostring(msg_id))
msg_id = msg_id + 1 msg_id = msg_id + 1
end end

@ -18,14 +18,14 @@
-- 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 ctx = zmq.init(1) local ctx = zmq.init()
local s = ctx:socket(zmq.REP) local s = ctx:socket(zmq.REP)
s:bind("tcp://lo:5555") s:bind("tcp://lo:5555")
while true do while true do
print(string.format("Received query: '%s'", s:recv())) print(string.format("Received query: '%s'", s:recv()))
s:send("OK") s:send("OK")
end end

@ -18,18 +18,18 @@
-- 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 ctx = zmq.init(1) local ctx = zmq.init()
local s = ctx:socket(zmq.REP) local s = ctx:socket(zmq.REP)
s:bind("tcp://lo:5555") s:bind("tcp://lo:5555")
while true do while true do
local query = s:recv() local query = s:recv()
while s:getopt(zmq.RCVMORE) == 1 do while s:getopt(zmq.RCVMORE) == 1 do
query = query .. s:recv() query = query .. s:recv()
end end
print(string.format("Received query: '%s'", query)) print(string.format("Received query: '%s'", query))
s:send("OK") s:send("OK")
end end

@ -18,14 +18,14 @@
-- 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 ctx = zmq.init(1) local ctx = zmq.init()
local s = ctx:socket(zmq.SUB) local s = ctx:socket(zmq.SUB)
s:setopt(zmq.SUBSCRIBE, "") s:setopt(zmq.SUBSCRIBE, "")
s:connect("tcp://localhost:5555") s:connect("tcp://localhost:5555")
while true do while true do
local msg = s:recv() local msg = s:recv()
local msg_id = tonumber(msg) local msg_id = tonumber(msg)
if math.mod(msg_id, 10000) == 0 then print(msg_id) end if math.mod(msg_id, 10000) == 0 then print(msg_id) end
end end

Loading…
Cancel
Save