Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #132 from MitinPavel/master

Custom routing Router to Dealer in Ruby
  • Loading branch information...
commit 45e9f86fd9ce6e51da10ad88008b542229efecc6 2 parents 4c231d1 + d429677
@hintjens hintjens authored
Showing with 50 additions and 0 deletions.
  1. +50 −0 examples/Ruby/rtdealer.rb
View
50 examples/Ruby/rtdealer.rb
@@ -0,0 +1,50 @@
+# Custom routing Router to Dealer. (ROUTER to DEALER)
+# Ruby version, based on the C version from
+# http://zguide.zeromq.org/chapter:all#toc45
+#
+# libzmq: 2.1.10
+# ruby: 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
+# ffi-rzmq: 0.9.0
+#
+# @author Pavel Mitin
+# @email mitin.pavel@gmail.com
+
+require 'rubygems'
+require 'ffi-rzmq'
+
+def task(name, context)
+ worker = context.socket ZMQ::DEALER
+ worker.setsockopt ZMQ::IDENTITY, name
+ worker.connect 'ipc://routing.ipc'
+
+ total = 0
+ loop do
+ data = ''
+ worker.recv_string data
+ p "#{name} received: #{total}" and break if data == 'END'
+ total += 1
+ end
+end
+
+context = ZMQ::Context.new 1
+client = context.socket ZMQ::ROUTER
+client.bind 'ipc://routing.ipc'
+
+worker_1 = Thread.new { task 'A', context }
+worker_2 = Thread.new { task 'B', context }
+
+sleep 1
+
+10.times do
+ address = rand(3) % 3 == 0 ? 'A' : 'B'
+ client.send_string address, ZMQ::SNDMORE
+ client.send_string "This is the workload"
+end
+
+%w(A B).each do |address|
+ client.send_string address, ZMQ::SNDMORE
+ client.send_string 'END'
+end
+
+worker_1.join
+worker_2.join
Please sign in to comment.
Something went wrong with that request. Please try again.