-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# An Actor ring example | ||
# | ||
# Here we construct a ring of interconnected Actors which each know the | ||
# next Actor to send messages to. Any message sent from the parent Actor | ||
# is delivered around the ring and back to the parent. | ||
|
||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'revactor' | ||
|
||
NCHILDREN = 10 | ||
|
||
class RingNode | ||
extend Actorize | ||
|
||
def initialize | ||
loop do | ||
Actor.receive do |filter| | ||
filter.when(Case::Array[:msg, Case::Any]) do |_, message| | ||
handle_message(message) | ||
@next_node << [:msg, message] if @next_node | ||
end | ||
|
||
filter.when(Case::Array[:next_node, Case::Any]) do |_, node| | ||
puts "Setting next node of #{self} to #{node}" | ||
@next_node = node | ||
end | ||
|
||
filter.when(Object) do |msg| | ||
puts "Unhandled: #{msg.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
def handle_message(message) | ||
end | ||
end | ||
|
||
class CountNode < RingNode | ||
def initialize | ||
@n = 0 | ||
@t = Time.now | ||
|
||
super | ||
end | ||
|
||
def handle_message(message) | ||
@n += 1 | ||
|
||
if @n % 10_000 == 0 | ||
t = Time.now | ||
puts "10_000 msgs in #{t-@t} secs: #{Float(10_000) / (t-@t)}" | ||
@t = t | ||
@n = 0 | ||
end | ||
end | ||
end | ||
|
||
children = (NCHILDREN-1).times.map { RingNode.spawn } | ||
children << CountNode.spawn | ||
|
||
p :linking | ||
# link up all actors | ||
children.each_cons(2) do |a, b| | ||
a << [:next_node, b] | ||
end | ||
children.last << [:next_node, children.first] | ||
|
||
p :start | ||
children.first << [:msg, 'ping'] | ||
|
||
Actor.receive do |filter| | ||
filter.when(Object) do |obj| | ||
p obj | ||
end | ||
end |