Class: DCell::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/dcell/router.rb

Overview

Route incoming messages to their recipient actors

Class Method Summary collapse

Class Method Details

.find(mailbox_id) ⇒ Object

Find a mailbox by its ID



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dcell/router.rb', line 25

def find(mailbox_id)
  @lock.synchronize do
    ref = @table[mailbox_id]
    return unless ref
    begin
      ref.__getobj__
    rescue WeakRef::RefError
      # The referenced actor is dead, so prune the registry
      @table.delete mailbox_id
      nil
    end
  end
end

.gcObject

Prune all entries that point to dead objects



62
63
64
65
66
67
68
# File 'lib/dcell/router.rb', line 62

def gc
  @lock.synchronize do
    @table.each do |id, ref|
      @table.delete id unless ref.weakref_alive?
    end
  end
end

.register(mailbox) ⇒ Object

Enter a mailbox into the registry



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dcell/router.rb', line 11

def register(mailbox)
  id = mailbox.object_id.to_s(16)

  @lock.synchronize do
    ref = @table[id]
    unless ref && ref.weakref_alive?
      @table[id] = WeakRef.new(mailbox)
    end
  end

  id
end

.route(mailbox_id, message) ⇒ Object

Route a message to a given mailbox ID



40
41
42
43
44
45
46
47
48
# File 'lib/dcell/router.rb', line 40

def route(mailbox_id, message)
  recipient = find mailbox_id

  if recipient
    recipient << message
  else
    Celluloid::Logger.debug("received message for invalid actor: #{mailbox_id.inspect}")
  end
end

.route_system_event(mailbox_id, event) ⇒ Object

Route a system event to a given mailbox ID



51
52
53
54
55
56
57
58
59
# File 'lib/dcell/router.rb', line 51

def route_system_event(mailbox_id, event)
  recipient = find mailbox_id

  if recipient
    recipient.system_event event
  else
    Celluloid::Logger.debug("received message for invalid actor: #{mailbox_id.inspect}")
  end
end