Class: Pantry::Communication::WaitList

Inherits:
Object
  • Object
show all
Defined in:
lib/pantry/communication/wait_list.rb

Overview

The WaitList manages futures for asynchronously waiting for responses from either the Client or the Server. Given an identity and a message, WaitList returns a Future that will be filled when the handler in question receives a message of the same Message type from that identity.

Defined Under Namespace

Classes: FutureResultWrapper

Instance Method Summary collapse

Constructor Details

#initializeWaitList

Returns a new instance of WaitList.



10
11
12
# File 'lib/pantry/communication/wait_list.rb', line 10

def initialize
  @futures = Hash.new {|hash, key| hash[key] = []}
end

Instance Method Details

#received(message) ⇒ Object



36
37
38
39
40
# File 'lib/pantry/communication/wait_list.rb', line 36

def received(message)
  if future = @futures[ message.uuid ].shift
    future.signal(FutureResultWrapper.new(message))
  end
end

#wait_for(message) ⇒ Object

Given a message being sent, create a Future for a response to this message. This keys off of the Message’s UUID, which must be kept in tact as it passes through the system.



17
18
19
20
21
# File 'lib/pantry/communication/wait_list.rb', line 17

def wait_for(message)
  future = Celluloid::Future.new
  @futures[ message.uuid ] << future
  future
end

#waiting_for?(message) ⇒ Boolean

Is there a future waiting for this response message?

Returns:

  • (Boolean)


24
25
26
# File 'lib/pantry/communication/wait_list.rb', line 24

def waiting_for?(message)
  !@futures[ message.uuid ].empty?
end