From 8b43d80f6a3b8ea2759174c6556c164574b3f8fb Mon Sep 17 00:00:00 2001 From: mike castleman Date: Wed, 8 Sep 2010 13:22:06 -0400 Subject: [PATCH 1/2] port some of the examples to Ruby --- examples/Ruby/hwclient.rb | 21 ++++++++++++++++----- examples/Ruby/hwserver.rb | 24 +++++++++++++++++++----- examples/Ruby/tasksink.rb | 33 ++++++++++++++++++++++++++++----- examples/Ruby/taskvent.rb | 37 ++++++++++++++++++++++++++++++++----- examples/Ruby/taskwork.rb | 39 ++++++++++++++++++++++++++++++++++----- examples/Ruby/wuclient.rb | 34 +++++++++++++++++++++++++++++----- examples/Ruby/wuserver.rb | 27 ++++++++++++++++++++++----- 7 files changed, 180 insertions(+), 35 deletions(-) diff --git a/examples/Ruby/hwclient.rb b/examples/Ruby/hwclient.rb index 97728b5fd..b634cd728 100644 --- a/examples/Ruby/hwclient.rb +++ b/examples/Ruby/hwclient.rb @@ -1,6 +1,17 @@ -No-one has translated the hwclient example into Ruby yet. Be the first to create -hwclient in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +require 'rubygems' +require 'ffi-rzmq' -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +context = ZMQ::Context.new(1) + +# Socket to talk to server +puts "Connecting to hello world server..." +requester = context.socket(ZMQ::REQ) +requester.connect("tcp://localhost:5555") + +0.upto(9) do |request_nbr| + puts "Sending request #{request_nbr}..." + requester.send_string "Hello" + + reply = requester.recv_string + puts "Received reply #{request_nbr}: [#{reply}]" +end diff --git a/examples/Ruby/hwserver.rb b/examples/Ruby/hwserver.rb index cb7347494..91f7aa9ef 100644 --- a/examples/Ruby/hwserver.rb +++ b/examples/Ruby/hwserver.rb @@ -1,6 +1,20 @@ -No-one has translated the hwserver example into Ruby yet. Be the first to create -hwserver in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +require 'rubygems' +require 'ffi-rzmq' -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +context = ZMQ::Context.new(1) + +# Socket to talk to clients +responder = context.socket(ZMQ::REP) +responder.bind('tcp://*:5555') + +while true + # Wait for next request from client + message = responder.recv_string + print "Received request: #{message}\n" + + # Do some 'work' + sleep 1 + + # Send reply back to client + responder.send_string("World") +end diff --git a/examples/Ruby/tasksink.rb b/examples/Ruby/tasksink.rb index 92c6a985b..f1f623572 100644 --- a/examples/Ruby/tasksink.rb +++ b/examples/Ruby/tasksink.rb @@ -1,6 +1,29 @@ -No-one has translated the tasksink example into Ruby yet. Be the first to create -tasksink in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +# +# Task sink in C +# Binds PULL socket to tcp://localhost:5558 +# Collects results from workers via that socket +# -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +require 'rubygems' +require 'ffi-rzmq' + +# Prepare our context and socket +context = ZMQ::Context.new(1) +receiver = context.socket(ZMQ::PULL) +receiver.bind("tcp://*:5558") + +# Wait for start of batch +receiver.recv_string +tstart = Time.now + +# Process 100 confirmations +100.times do |task_nbr| + receiver.recv_string + $stdout << ((task_nbr % 10 == 0) ? ':' : '.') + $stdout.flush +end + +# Calculate and report duration of batch +tend = Time.now +total_msec = (tend-tstart) * 1000 +puts "Total elapsed time: #{total_msec} msec" diff --git a/examples/Ruby/taskvent.rb b/examples/Ruby/taskvent.rb index ad35f12d7..ee2dd0a6a 100644 --- a/examples/Ruby/taskvent.rb +++ b/examples/Ruby/taskvent.rb @@ -1,6 +1,33 @@ -No-one has translated the taskvent example into Ruby yet. Be the first to create -taskvent in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +# +# Task ventilator in Ruby +# Binds PUSH socket to tcp://localhost:5557 +# Sends batch of tasks to workers via that socket +# -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +require 'rubygems' +require 'ffi-rzmq' + +context = ZMQ::Context.new(1) + +# Socket to send messages on +sender = context.socket(ZMQ::PUSH) +sender.bind("tcp://*:5557") + +puts "Press enter when the workers are ready..." +$stdin.read(1) +puts "Sending tasks to workers..." + +# The first message is "0" and signals start of batch +sender.send_string('0') + +# Send 100 tasks +total_msec = 0 # Total expected cost in msecs +100.times do + workload = rand(100) + 1 + total_msec += workload + $stdout << "#{workload}." + sender.send_string(workload.to_s) +end + +puts "Total expected cost: #{total_msec} msec" +Kernel.sleep(1) # Give 0MQ time to deliver diff --git a/examples/Ruby/taskwork.rb b/examples/Ruby/taskwork.rb index 7e4425482..f1750e52a 100644 --- a/examples/Ruby/taskwork.rb +++ b/examples/Ruby/taskwork.rb @@ -1,6 +1,35 @@ -No-one has translated the taskwork example into Ruby yet. Be the first to create -taskwork in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +# +# Task worker in Ruby +# Connects PULL socket to tcp://localhost:5557 +# Collects workloads from ventilator via that socket +# Connects PUSH socket to tcp://localhost:5558 +# Sends results to sink via that socket +# -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +require 'rubygems' +require 'ffi-rzmq' + +context = ZMQ::Context.new(1) + +# Socket to receive messages on +receiver = context.socket(ZMQ::PULL) +receiver.connect("tcp://localhost:5557") + +# Socket to send messages to +sender = context.socket(ZMQ::PUSH) +sender.connect("tcp://localhost:5558") + +# Process tasks forever +while true + msec = receiver.recv_string + + # Simple progress indicator for the viewer + $stdout << "#{msec}." + $stdout.flush + + # Do the work + sleep(msec.to_f / 1000) + + # Send results to sink + sender.send_string("") +end diff --git a/examples/Ruby/wuclient.rb b/examples/Ruby/wuclient.rb index 1ace083b0..b10966d7d 100644 --- a/examples/Ruby/wuclient.rb +++ b/examples/Ruby/wuclient.rb @@ -1,6 +1,30 @@ -No-one has translated the wuclient example into Ruby yet. Be the first to create -wuclient in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +# +# Weather update client in Ruby +# Connects SUB socket to tcp://localhost:5556 +# Collects weather updates and finds avg temp in zipcode +# -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +require 'rubygems' +require 'ffi-rzmq' + +COUNT = 100 + +context = ZMQ::Context.new(1) + +# Socket to talk to server +puts "Collecting updates from weather server..." +subscriber = context.socket(ZMQ::SUB) +subscriber.connect("tcp://localhost:5556") + +# Subscribe to zipcode, default is NYC, 10001 +filter = ARGV.size > 0 ? argv[0] : "10001 " +subscriber.setsockopt(ZMQ::SUBSCRIBE, filter) + +# Process 100 updates +total_temp = 0 +1.upto(COUNT) do |update_nbr| + zipcode, temperature, relhumidity = subscriber.recv_string.split.map(&:to_i) + total_temp += temperature +end + +puts "Average temperature for zipcode '#{filter}' was #{total_temp / COUNT}F" diff --git a/examples/Ruby/wuserver.rb b/examples/Ruby/wuserver.rb index 896c6a16d..7b75c3865 100644 --- a/examples/Ruby/wuserver.rb +++ b/examples/Ruby/wuserver.rb @@ -1,6 +1,23 @@ -No-one has translated the wuserver example into Ruby yet. Be the first to create -wuserver in Ruby and get one free Internet! If you're the author of the Ruby -binding, this is a great way to get people to use 0MQ in Ruby. +# +# Weather update server in Ruby +# Binds PUB socket to tcp://*:5556 +# Publishes random weather updates +# -To submit a translation, just email it to zeromq-dev.zeromq.org. -Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. +require 'rubygems' +require 'ffi-rzmq' + +context = ZMQ::Context.new(1) +publisher = context.socket(ZMQ::PUB) +publisher.bind("tcp://*:5556") +publisher.bind("ipc://weather") + +while true + # Get values that will fool the boss + zipcode = rand(100000) + temperature = rand(215) - 80 + relhumidity = rand(50) + 10 + + update = "%05d %d %d" % [zipcode, temperature, relhumidity] + publisher.send_string(update) +end From 2a1a021c1873ea784995cbcb0d52d15b0974e464 Mon Sep 17 00:00:00 2001 From: mike castleman Date: Wed, 8 Sep 2010 13:26:38 -0400 Subject: [PATCH 2/2] add myself to contributors list --- CONTRIBUTORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7757dced6..ff0dcd967 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -4,4 +4,4 @@ Pieter Hintjens (C examples) Olivier Chamoux (C++ examples) Naveen Chawla (Java examples) Nicola Peduzzi (Java examples) - +Mike Castleman (Ruby examples)