Sha256: a498f0c1643af7735394b141e10b5b762f098a880f0d8f0fda590830c1e5384a

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'dcell'

module Elaine
  module Distributed
    class PostOffice
      include Celluloid
      include Celluloid::Logger

      attr_reader :mailboxes
      attr_reader :zipcodes

      def initialize
        @mailboxes = Hash.new
        @zipcodes = Hash.new
      end

      def zipcodes=(zipcodes)
        @zipcodes = zipcodes

        # do we need to initialize all the mailboxes here?
        # might be smart?
        @mailboxes = Hash.new
        my_id = DCell.me.id
        @zipcodes.each_pair do |k, v|
          if v == my_id
            debug "Creating mailbox for: #{k}"
            @mailboxes[k] = []
          end
        end

      end

      def address(to)
        node = DCell::Node[@zipcodes[to]]
      end


      def deliver(to, msg)
        
        node = address(to)

        if node.id.eql?(DCell.me.id)
          # debug "Delivering to mailbox: #{to}"
          @mailboxes[to].push msg
          # debug "Done delivering to mailbox: #{to}"
          nil
        else
          # debug "Delivering message to remote mailbox: #{msg}"
          node[:postoffice].async.deliver(to, msg)
          # debug "Finished delivery remnote box: to #{node.id}"
          nil
        end
      end

      def read(mailbox)
        node = address(mailbox)
        if node.id.eql?(Dcell.me.id)
          @mailboxes[mailbox]
        else
          node[:postoffice].read mailbox
        end
      end

      def read_all(mailbox)
        node = address(mailbox)
        # debug "node: #{node}"
        # debug "node.id: '#{node.id}'"
        # debug "DCell.me.id: '#{DCell.me.id}'"
        if node.id.eql?(DCell.me.id)
          msgs = @mailboxes[mailbox].map { |v| v }
          @mailboxes[mailbox].clear
          msgs
        else
          raise "Can't destructively read a non-local mailbox!"
        end
      end

    end # class PostOffice
  end # module Distributed
end # module Elaine

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elaine-0.0.3 lib/elaine/distributed/post_office.rb