Sha256: 955191ddc8aae438adcfe9c0ea5d614be6e83ab520f66f6c11f71a90ac2163da

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

# Attempt to provide thread-safe communication between EventMachine and AMF Socket.
# 1. Always use EM.schedule to push work to the reactor thread.
# 2. Always use @actor.deliver_message! to push work to the actor's thread pool.

module Tribe
  module EM
    module AmfSocket
      class ActorProxy < ::AmfSocket::AmfRpcConnection
        private

        def initialize(actor_class, options = {})
          @actor_class = actor_class || raise('You must provide an actor class.')
          @logger = Workers::LogProxy.new(options[:logger])

          @actor = @actor_class.new({ :actor_proxy => self, :logger => @logger })
        end

        #
        # EM Callbacks.
        #

        public

        def post_init
          super

          @actor.deliver_message!(:post_init, nil)
        end

        def unbind
          super

          @actor.deliver_message!(:unbind, nil)
        end

        def receive_message(message)
          @actor.deliver_message!(:receive_message, message)
        end

        #
        # Public methods.  Call these from the associated actor.
        #

        public

        def close(after_writing = false)
          ::EM.schedule { close_connection(after_writing) }

          return nil
        end

        def write_message(command, params = {})
          ::EM.schedule { send_message(command, params) }

          return nil
        end

        #
        # Private methods.
        #

        private

        def write(data)
          raise 'This method can not be used with AMF Socket.'
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tribe_em_amfsocket-0.4.0 lib/tribe_em_amfsocket/actor_proxy.rb
tribe_em_amfsocket-0.3.1 lib/tribe_em_amfsocket/actor_proxy.rb