Sha256: f9cdfa13dca68d479f2eac419fe04377ac641a1d76d9d4f41ccd30e071b09293

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'ext/binding_of_caller'
require 'ara/exception'

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

  # Start actor and return it
  #
  #   myActor = Actors.actor_of(MyActor)
  #   myActor.start
  def start
    @thread = Thread.new do 
      loop do
        receive(@actorQueue.pop)
      end
    end

    return self
  end

  # Stop actor
  #
  #   myActor = Actors.actor_of(MyActor).start
  #   ...
  #   myActor.stop
  def stop
    @thread.kill
  end

  # Send a simple message without expecting any response
  #
  #   myActor = Actors.actor_of(MyActor).start
  #   message = ...
  #   myActor | message
  def |(message)
    if @thread.alive?
      @actorQueue << message
    else 
      raise DeadActor, "Actor is dead!"
    end
  end
  alias :message :|

  private
  # Method receiver for the actor
  #
  # When you create an actor (simple or not) you *must* define a <tt>receive</tt> method.
  #
  #   class MyActor < Actor
  #     def receive(message)
  #       ...
  #     end
  #   end
  def receive(message)
    raise "Define me!"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ara-0.0.2 lib/ara/simple_actor.rb
ara-0.0.1 lib/ara/simple_actor.rb