Class: Celluloid::Mailbox

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/vendor/celluloid/lib/celluloid/mailbox.rb

Overview

Actors communicate with asynchronous messages. Messages are buffered in Mailboxes until Actors can act upon them.

Instance Method Summary collapse

Methods included from Enumerable

#to_json

Constructor Details

#initializeMailbox

Returns a new instance of Mailbox.



15
16
17
18
19
20
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 15

def initialize
  @messages = []
  @lock  = Mutex.new
  @dead = false
  @condition = ConditionVariable.new
end

Instance Method Details

#<<(message) ⇒ Object

Add a message to the Mailbox



23
24
25
26
27
28
29
30
31
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 23

def <<(message)
  @lock.synchronize do
    raise MailboxError, "dead recipient" if @dead

    @messages << message
    @condition.signal
  end
  nil
end

#alive?Boolean

Is the mailbox alive?

Returns:

  • (Boolean)


105
106
107
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 105

def alive?
  !@dead
end

#each(&block) ⇒ Object

Iterate through the mailbox



115
116
117
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 115

def each(&block)
  to_a.each(&block)
end

#inspectObject

Inspect the contents of the Mailbox



120
121
122
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 120

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} @messages=[#{map { |m| m.inspect }.join(', ')}]>"
end

#next_messageObject

Retrieve the next message in the mailbox



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 73

def next_message
  message = nil

  if block_given?
    index = @messages.index do |msg|
      yield(msg) || msg.is_a?(SystemEvent)
    end

    message = @messages.slice!(index, 1).first if index
  else
    message = @messages.shift
  end

  raise message if message.is_a? SystemEvent
  message
end

#receive(timeout = nil, &block) ⇒ Object

Receive a message from the Mailbox



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 45

def receive(timeout = nil, &block)
  message = nil

  @lock.synchronize do
    raise MailboxError, "attempted to receive from a dead mailbox" if @dead

    begin
      message = next_message(&block)

      unless message
        if timeout
          now = Time.now
          wait_until ||= now + timeout
          wait_interval = wait_until - now
          return if wait_interval < 0
        else
          wait_interval = nil
        end

        @condition.wait(@lock, wait_interval)
      end
    end until message
  end

  message
end

#shutdownObject

Shut down this mailbox and clean up its contents



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 91

def shutdown
  messages = nil

  @lock.synchronize do
    messages = @messages
    @messages = []
    @dead = true
  end

  messages.each { |msg| msg.cleanup if msg.respond_to? :cleanup }
  true
end

#system_event(event) ⇒ Object

Add a high-priority system event to the Mailbox



34
35
36
37
38
39
40
41
42
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 34

def system_event(event)
  @lock.synchronize do
    unless @dead # Silently fail if messages are sent to dead actors
      @messages.unshift event
      @condition.signal
    end
  end
  nil
end

#to_aObject

Cast to an array



110
111
112
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 110

def to_a
  @lock.synchronize { @messages.dup }
end