Class: Actor

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

Overview

Actor class

Instance Method Summary collapse

Methods inherited from SimpleActor

#start, #stop, #|

Constructor Details

#initializeActor

:nodoc:



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

def initialize #:nodoc:
  raise ActorInitializationError, "You can't initialize Actor directly, use Actors.actor_of" if self.class == ::Actor
  super
  @main = Queue.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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ara/actor.rb', line 36

def <(message, response_method = nil)
  if @thread.alive?
    @actorQueue << message
    BindingOfCaller.binding_of_caller do |bind|
       self_of_caller = eval("self", bind)
       Thread.new do
          _result = @main.pop
          begin
          if block_given?
            yield _result
          elsif response_method != nil
            self_of_caller.send( response_method.to_sym, _result )
          else
            self_of_caller.send( :actor_response, _result )
          end
          rescue => 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



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

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