Class: PostOffice

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pregel.rb

Instance Method Summary collapse

Constructor Details

#initializePostOffice

Returns a new instance of PostOffice.



10
11
12
13
# File 'lib/pregel.rb', line 10

def initialize
  @mailboxes = Hash.new
  @mutex = Mutex.new
end

Instance Method Details

#deliver(to, msg) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/pregel.rb', line 15

def deliver(to, msg)
  @mutex.synchronize do
    if @mailboxes[to]
      @mailboxes[to].push msg
    else
      @mailboxes[to] = [msg]
    end
  end
end

#read(box) ⇒ Object



25
26
27
28
29
# File 'lib/pregel.rb', line 25

def read(box)
  @mutex.synchronize do
    @mailboxes.delete(box) || []
  end
end