Class: SimpleActor

Inherits:
Object
  • Object
show all
Defined in:
lib/ara/simple_actor.rb

Overview

This class allow you to create simple actors.

Direct Known Subclasses

Actor

Instance Method Summary collapse

Constructor Details

#initializeSimpleActor

:nodoc:



5
6
7
8
# File 'lib/ara/simple_actor.rb', line 5

def initialize #:nodoc:
  raise ActorInitializationError, "You can't initialize SimpleActor directly, use Actors.actor_of" if self.class == ::SimpleActor
  @actorQueue = Queue.new
end

Instance Method Details

#startObject

Start actor and return it

myActor = Actors.actor_of(MyActor)
myActor.start


14
15
16
17
18
19
20
21
22
23
# File 'lib/ara/simple_actor.rb', line 14

def start
  self.send(:pre_start) if self.respond_to?(:pre_start, true)
  @thread = Thread.new do 
    loop do
      receive(@actorQueue.shift)
    end
  end

  return self
end

#stopObject

Stop actor

myActor = Actors.actor_of(MyActor).start
...
myActor.stop


30
31
32
33
# File 'lib/ara/simple_actor.rb', line 30

def stop
  @thread.kill
  self.send(:post_stop) if self.respond_to?(:post_stop, true)
end

#|(message) ⇒ Object Also known as: message

Send a simple message without expecting any response

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


40
41
42
43
44
45
46
# File 'lib/ara/simple_actor.rb', line 40

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