Sha256: 92ad70fad3d05d6d519292980a6821cc7219ed0d1a8913ab037cd70619a04b95

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

# Celluloid mailboxes are the universal message exchange points. You won't
# be able to marshal them though, unfortunately, because they contain
# mutexes.
#
# DCell provides a message routing layer between nodes that can direct
# messages back to local mailboxes. To accomplish this, DCell adds custom
# marshalling to mailboxes so that if they're unserialized on a remote
# node you instead get a proxy object that routes messages through the
# DCell overlay network back to the node where the actor actually exists

module Celluloid
  class ActorProxy
    # Marshal uses respond_to? to determine if this object supports _dump so
    # unfortunately we have to monkeypatch in _dump support as the proxy
    # itself normally jacks respond_to? and proxies to the actor
    alias_method :__respond_to?, :respond_to?
    def respond_to?(meth)
      return false if meth == :marshal_dump
      return true  if meth == :_dump
      __respond_to? meth
    end

    # Dump an actor proxy via its mailbox
    def _dump(level)
      @mailbox._dump(level)
    end

    # Create an actor proxy object which routes messages over DCell's overlay
    # network and back to the original mailbox
    def self._load(string)
      mailbox = Celluloid::Mailbox._load(string)

      case mailbox
      when DCell::MailboxProxy
        DCell::ActorProxy.new mailbox
      when Celluloid::Mailbox
        Celluloid::ActorProxy.new(mailbox)
      else raise "funny, I did not expect to see a #{mailbox.class} here"
      end
    end
  end

  class Mailbox
    # This custom dumper registers actors with the DCell registry so they can
    # be reached remotely.
    def _dump(level)
      mailbox_id = DCell::Router.register self
      "#{mailbox_id}@#{DCell.id}"
    end

    # Create a mailbox proxy object which routes messages over DCell's overlay
    # network and back to the original mailbox
    def self._load(string)
      DCell::MailboxProxy._load(string)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dcell-0.7.1 lib/dcell/celluloid_ext.rb
dcell-0.0.1 lib/dcell/celluloid_ext.rb