Skip to content

Commit

Permalink
Add DCell support for Futures
Browse files Browse the repository at this point in the history
Make the Router to duck-type on the recipient to support Futures
  • Loading branch information
halorgium committed Apr 15, 2012
1 parent 95af993 commit 3220b31
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/dcell.rb
Expand Up @@ -14,6 +14,7 @@
require 'dcell/responses'
require 'dcell/router'
require 'dcell/rpc'
require 'dcell/future'
require 'dcell/server'
require 'dcell/info_service'

Expand Down
11 changes: 11 additions & 0 deletions lib/dcell/celluloid_ext.rb
Expand Up @@ -66,4 +66,15 @@ def self._load(string)
DCell::RPC._load(string)
end
end

class Future
def _dump(level)
address = DCell::Router.register self
DCell::Future.new(address,DCell.id,DCell.addr)._dump(level)
end

def self._load(string)
DCell::Future._load(string)
end
end
end
29 changes: 29 additions & 0 deletions lib/dcell/future.rb
@@ -0,0 +1,29 @@
module DCell
class Future
def initialize(address,node_id,node_addr)
@address = address
@node_id = node_id
@node_addr = node_addr
end

def <<(message)
node = Node[@node_id]
node = Node.new(@node_id, @node_addr) unless node
node.send_message! Message::Relay.new(self, message)
end

def _dump(level)
Marshal.dump [@address, @node_id, @node_addr]
end

# Loader for custom marshal format
def self._load(string)
address, node_id, node_addr = Marshal.load(string)
if node_id == DCell.id
Router.find(address)
else
new(address, node_id, node_addr)
end
end
end
end
18 changes: 11 additions & 7 deletions lib/dcell/router.rb
Expand Up @@ -44,24 +44,28 @@ def find(mailbox_address)
end

# Route a message to a given mailbox ID
def route(mailbox_address, message)
recipient = find mailbox_address
def route(recipient, message)
unless recipient.respond_to?(:signal)
recipient = find recipient
end

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

# Route a system event to a given mailbox ID
def route_system_event(mailbox_address, event)
recipient = find mailbox_address
def route_system_event(recipient, event)
unless recipient.respond_to?(:system_event)
recipient = find recipient
end

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

Expand Down

1 comment on commit 3220b31

@halorgium
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required to get the duck-typing functional.

diff --git a/lib/celluloid/mailbox.rb b/lib/celluloid/mailbox.rb
index bd7573d..9aa54af 100644
--- a/lib/celluloid/mailbox.rb
+++ b/lib/celluloid/mailbox.rb
@@ -32,6 +32,7 @@ module Celluloid
         @mutex.unlock rescue nil
       end
     end
+    alias_method :signal, :<<

     # Add a high-priority system event to the Mailbox
     def system_event(event)

Please sign in to comment.