Skip to content

Instantly share code, notes, and snippets.

@amclain
Last active August 29, 2015 13:56
Show Gist options
  • Save amclain/9195630 to your computer and use it in GitHub Desktop.
Save amclain/9195630 to your computer and use it in GitHub Desktop.
# Works w/ zeromqrb updates.
require 'ffi-rzmq'
require 'zeromqrb'
Thread.abort_on_exception = true
class ZeroMQ::Socket
def recv_array
parts = []
str = ''
self.recv_string str
parts << str
while self.more_parts?
str = ''
self.recv_string str
parts << str
end
parts
end
end
ctx = ZeroMQ::Context.new
req = ctx.socket ZMQ::REQ
r1 = ctx.socket ZMQ::ROUTER
d1 = ctx.socket ZMQ::DEALER
r2 = ctx.socket ZMQ::ROUTER
r2.bind 'inproc://dealer-router'
d1.connect 'inproc://dealer-router'
r1.bind 'inproc://req-router'
req.connect 'inproc://req-router'
proxy_thread = Thread.new do
puts "Proxy started."
ZeroMQ::Proxy.create r1, d1
puts "Proxy exited."
end
# Req
thread = Thread.new do
req.send_string 'Hello'
puts "Req started."
end
sleep 0.1
puts "Waiting..."
# Router
received = r2.recv_array
puts "Router2 Received: #{received}"
# Return message
puts "Router2 sending return message."
received.each do |part| # Strip off addressing and send.
break if part.empty?
r2.send_string part, ZMQ::SNDMORE
end
r2.send_string '', ZMQ::SNDMORE
r2.send_string 'Goodbye'
# Receive return message
return_msg = req.recv_array
puts "Req Return Message: #{return_msg}"
# Cleanup
thread.join
r2.close
d1.close
r1.close
req.close
ctx.terminate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment