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:



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

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


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

def start
  @thread = Thread.new do 
    loop do
      receive(@actorQueue.pop)
    end
  end

  return self
end

#stopObject

Stop actor

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


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

def stop
  @thread.kill
end

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

Send a simple message without expecting any response

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


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

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