Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruby examples #1

Merged
3 commits merged into from
Sep 10, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Pieter Hintjens <ph@imatix.com> (C examples)
Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> (C++ examples)
Naveen Chawla <naveen.chwl@gmail.com> (Java examples)
Nicola Peduzzi <thenikso@gmail.com> (Java examples)

Mike Castleman <m@mlcastle.net> (Ruby examples)
21 changes: 16 additions & 5 deletions examples/Ruby/hwclient.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 19 additions & 5 deletions examples/Ruby/hwserver.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 28 additions & 5 deletions examples/Ruby/tasksink.rb
Original file line number Diff line number Diff line change
@@ -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"
37 changes: 32 additions & 5 deletions examples/Ruby/taskvent.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 34 additions & 5 deletions examples/Ruby/taskwork.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 29 additions & 5 deletions examples/Ruby/wuclient.rb
Original file line number Diff line number Diff line change
@@ -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"
27 changes: 22 additions & 5 deletions examples/Ruby/wuserver.rb
Original file line number Diff line number Diff line change
@@ -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