Commits

Alessio Caiazza committed 4f3c41e

link a trapping

  • Participants
  • Parent commits 90511d6

Comments (0)

Files changed (1)

 require 'celluloid'
-Celluloid.logger.level = Logger::FATAL
+#Celluloid.logger.level = Logger::FATAL
 
 class NecroMonger
   include Celluloid
     self.pilot = NecroMonger.new_link
   end
   
-  def land_near_riddick
-    #raise Exception, "Kaboom!"
-    pilot.fight! :riddick
+  def attack_riddick_amanjaro
+    raise NecroMonger::CannotWinError, "Kaboom!"
   end
 end
 
 vehicle = NecroMongerFighter.new
 pilot = vehicle.pilot
 puts pilot.inspect
-vehicle.land_near_riddick
+vehicle.attack_riddick_amanjaro!
+sleep 1
 puts pilot.inspect
-puts vehicle.inspect
+puts vehicle.inspect
+
+class LogisticChief
+  include Celluloid
+  
+  def prepare_reinforcements
+    if (count_assets[:NecroMongerFighter] || 0) < 3
+      lord_marshal = Celluloid::Actor[:lord_marshal]
+      3.times { lord_marshal.link NecroMongerFighter.new }
+    else
+      p "We have enough troops Lord Marshal!"
+    end
+  end
+  
+  def count_assets
+    Celluloid::Actor.all.inject({}) do |sum,itm|
+      begin
+        idx = itm.class.name.to_sym
+        current = sum[idx] || 0
+        sum[idx] = current + 1
+        sum
+      rescue Celluloid::DeadActorError
+      end
+    end
+  end
+end
+
+class LordMarshal
+  include Celluloid
+  trap_exit :necromonger_died
+  attr_reader :logistic
+  
+  def initialize
+    @logistic = LogisticChief.new_link
+  end
+
+  def necromonger_died(actor, reason)
+    p "Damn Riddick! #{actor.inspect} has died because of a #{reason.class}"
+    @logistic.prepare_reinforcements!
+  end
+  
+end
+
+zhylaw = (LordMarshal.supervise_as :lord_marshal).actor
+vehicle = NecroMongerFighter.new
+
+zhylaw.link vehicle.pilot
+puts zhylaw.logistic.count_assets
+vehicle.attack_riddick_amanjaro!
+sleep 1
+puts zhylaw.logistic.count_assets
+sleep 10