class Celluloid::SyncCall - celluloid-0.16.0 Documentation

# class Celluloid::SyncCall

Synchronous calls wait for a response

### Attributes

chain_id[R]
sender[R]
task[R]

### Public Class Methods

new(sender, method, arguments = [], block = nil, task = Thread.current[:celluloid_task], chain_id = CallChain.current_id) click to toggle source
Calls superclass method Celluloid::Call.new

    # File lib/celluloid/calls.rb, line 53
    def initialize(sender, method, arguments = [], block = nil, task = Thread.current[:celluloid_task], chain_id = CallChain.current_id)
      super(method, arguments, block)

      @sender   = sender
      @task     = task
      @chain_id = chain_id || Celluloid.uuid
    end

### Public Instance Methods

cleanup() click to toggle source

    # File lib/celluloid/calls.rb, line 78
    def cleanup
      exception = DeadActorError.new("attempted to call a dead actor")
      respond ErrorResponse.new(self, exception)
    end

dispatch(obj) click to toggle source
Calls superclass method Celluloid::Call#dispatch

    # File lib/celluloid/calls.rb, line 61
    def dispatch(obj)
      CallChain.current_id = @chain_id
      result = super(obj)
      respond SuccessResponse.new(self, result)
    rescue Exception => ex
      # Exceptions that occur during synchronous calls are reraised in the
      # context of the sender
      respond ErrorResponse.new(self, ex)

      # Aborting indicates a protocol error on the part of the sender
      # It should crash the sender, but the exception isn't reraised
      # Otherwise, it's a bug in this actor and should be reraised
      raise unless ex.is_a?(AbortError)
    ensure
      CallChain.current_id = nil
    end

respond(message) click to toggle source

    # File lib/celluloid/calls.rb, line 83
    def respond(message)
      @sender << message
    end

response() click to toggle source

    # File lib/celluloid/calls.rb, line 87
    def response
      Celluloid.suspend(:callwait, self)
    end

value() click to toggle source

    # File lib/celluloid/calls.rb, line 91
    def value
      response.value
    end

wait() click to toggle source

    # File lib/celluloid/calls.rb, line 95
    def wait
      loop do
        message = Celluloid.mailbox.receive do |msg|
          msg.respond_to?(:call) and msg.call == self
        end

        if message.is_a?(SystemEvent)
          Thread.current[:celluloid_actor].handle_system_event(message)
        else
          # FIXME: add check for receiver block execution
          if message.respond_to?(:value)
            # FIXME: disable block execution if on :sender and (exclusive or outside of task)
            # probably now in Call
            break message
          else
            message.dispatch
          end
        end
      end
    end