Sha256: 7d78c98a8f0ea45b3d92277fdb440d08ff88d939881340dacb05962d665c139a

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

require 'weakref'

module DCell
  # Route incoming messages to their recipient actors
  class Router
    @lock = Mutex.new
    @table = {}

    class << self
      # Enter a mailbox into the registry
      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

      # Find a mailbox by its ID
      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

      # Route a message to a given mailbox ID
      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 a system event to a given mailbox ID
      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

      # Prune all entries that point to dead objects
      def gc
        @lock.synchronize do
          @table.each do |id, ref|
            @table.delete id unless ref.weakref_alive?
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dcell-0.9.0 lib/dcell/router.rb
dcell-0.8.0 lib/dcell/router.rb
dcell-0.7.1 lib/dcell/router.rb
dcell-0.0.1 lib/dcell/router.rb