| 1 | #!/usr/bin/env ruby
|
| 2 |
|
| 3 | # cf. http://lamp.epfl.ch/~phaller/doc/ActorsTutorial.html
|
| 4 |
|
| 5 | $:.push File.join( File.dirname(__FILE__), "..", "..", "lib" )
|
| 6 |
|
| 7 | require 'dramatis/actor'
|
| 8 |
|
| 9 | class Ping
|
| 10 | include Dramatis::Actor
|
| 11 | def initialize times, pong
|
| 12 | @pings_left = times
|
| 13 | release( pong ).ping actor.name
|
| 14 | end
|
| 15 | def pong caller
|
| 16 | if @pings_left % 1000 == 0
|
| 17 | puts "Ping: pong"
|
| 18 | end
|
| 19 | if @pings_left > 0
|
| 20 | @pings_left -= 1
|
| 21 | release( caller ).ping actor.name
|
| 22 | end
|
| 23 | end
|
| 24 | end
|
| 25 |
|
| 26 | class Pong
|
| 27 | include Dramatis::Actor
|
| 28 | def initialize
|
| 29 | @pong_count = 0
|
| 30 | end
|
| 31 | def ping caller
|
| 32 | if @pong_count % 1000 == 0
|
| 33 | puts "Pong: ping #{@pong_count}"
|
| 34 | end
|
| 35 | @pong_count += 1
|
| 36 | release( caller ).pong actor.name
|
| 37 | end
|
| 38 | end
|
| 39 |
|
| 40 | pong = Pong.new
|
| 41 | ping = Ping.new 10000, pong
|