Class: Actor

Inherits:
SimpleActor show all
Defined in:
lib/ara/actor.rb

Overview

Actor class

Direct Known Subclasses

Routing::Dispatcher

Instance Method Summary collapse

Methods inherited from SimpleActor

#start, #stop, #|

Constructor Details

#initializeActor

:nodoc:



7
8
9
10
11
12
# File 'lib/ara/actor.rb', line 7

def initialize #:nodoc:
  raise ActorInitializationError, "You can't initialize Actor directly, use Actors.actor_of" if self.class == ::Actor
  super
  @main = Queue.new
  @mutex = Mutex.new
end

Instance Method Details

#<(message, response_method = nil) ⇒ Object Also known as: async_message

Send an asynchronous message

myActor = Actors.actor_of(MyActor).start
message = ...
myActor < message

The code after this call will be executed without waiting the actor. You must add a result method to get the actor’ reponse.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ara/actor.rb', line 38

def <(message, response_method = nil)
   _actor = self
   if @thread.alive?
      @actorQueue << message

      BindingOfCaller.binding_of_caller do |bind|
         self_of_caller = eval("self", bind)
         Thread.new do
            _result = @main.shift
            begin
               if block_given?
                  yield _result
               elsif response_method != nil
                  self_of_caller.send( response_method.to_sym, _result )
               elsif self_of_caller.respond_to? :actor_response_with_name
                  self_of_caller.send( :actor_response_with_name, _actor, _result ) 
               else
                  self_of_caller.send( :actor_response, _result )
               end
            rescue => e
               Ara.fatal(e)
               raise ActorResponseError, "Error while sending response : #{e}"
            end
         end
      end
   else
      raise DeadActor, "Actor is dead!"
   end
end

#<<(message) ⇒ Object Also known as: sync_message

Send a synchronous message

myActor = Actors.actor_of(MyActor).start
message = ...
result = myActor << message

All code after this call will be executed only when the actor has finished it’s work



21
22
23
24
25
26
27
28
# File 'lib/ara/actor.rb', line 21

def <<(message)
  if @thread.alive?
    @actorQueue << message
    @main.shift
  else 
    raise DeadActor, "Actor is dead!"
  end
end