free_mutant/lib/mutant/actor.rb

62 lines
1.3 KiB
Ruby
Raw Normal View History

2014-10-23 07:37:53 -04:00
module Mutant
# A minimal actor implementation
module Actor
# Error raised when actor signalling protocol is violated
class ProtocolError < RuntimeError
end # ProtocolError
# Undefined message payload
Undefined = Class.new do
INSPECT = 'Mutant::Actor::Undefined'.freeze
# Return object inspection
#
# @return [String]
#
# @api private
def inspect
INSPECT
end
end.new
# Message being exchanged between actors
class Message
include Concord::Public.new(:type, :payload)
# Return new message
#
# @param [Symbol] type
# @param [Object] payload
#
# @return [Message]
#
2014-12-01 13:33:47 -05:00
# @api private
2014-10-23 07:37:53 -04:00
def self.new(_type, _payload = Undefined)
super
end
end # Message
2015-01-17 17:17:39 -05:00
# Binding to othersactors sender for simple RPC
2014-10-23 07:37:53 -04:00
class Binding
include Concord.new(:mailbox, :other)
2014-10-23 07:37:53 -04:00
# Send message and wait for reply
#
# @param [Symbol] type
#
# @return [Object]
#
2014-12-01 13:33:47 -05:00
# @api private
2014-10-23 07:37:53 -04:00
def call(type)
other.call(Message.new(type, mailbox.sender))
message = mailbox.receiver.call
2014-10-23 07:37:53 -04:00
fail ProtocolError, "Expected #{type} but got #{message.type}" unless type.equal?(message.type)
message.payload
end
end # Binding
end # Actor
end # Mutant