Skip to content

Instantly share code, notes, and snippets.

@wjlroe
Created May 13, 2010 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjlroe/399784 to your computer and use it in GitHub Desktop.
Save wjlroe/399784 to your computer and use it in GitHub Desktop.
#!/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