Created
May 13, 2010 12:41
-
-
Save wjlroe/399784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'rubygems' | |
require 'eventmachine' | |
def start_nodes(num_nodes, num_messages) | |
EM.run { | |
nodes = [] | |
num_nodes.times do | |
nodes << EM.spawn{|message| | |
case message.first | |
when :setnext | |
@nextpid = message[1] | |
puts "I got setnext with node: %s" % @nextpid | |
when :printlocal | |
puts "I am #{self} and I have this: #{@nextpid}" | |
when :sendmsg | |
msg = message[1] | |
countdown = message[2] | |
puts "I am #{self} and I got message: #{msg} (#{countdown}) and am sending it on to: #{@nextpid}" | |
if countdown > 0 | |
message[2] -= 1 | |
@nextpid.notify(message) | |
else | |
EM.stop | |
end | |
end | |
} | |
end | |
nodes.each_with_index {|node,idx| | |
next_node = nodes[(idx-1) % nodes.size] | |
node.notify([:setnext, next_node]) | |
} | |
nodes.each {|node| | |
node.notify([:printlocal]) | |
} | |
nodes.first.notify([:sendmsg, "Woot", num_messages*num_nodes]) | |
} | |
end | |
start_nodes(5, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment