module Nanite
  class Agent
    def push_to_exchange(type, domain, payload="")
      req = Request.new(type, payload, identity)
      req.token = Nanite.gensym
      req.reply_to = nil
      amq.topic('push exchange').publish(dump_packet(req), :key => "nanite.push.#{domain}")
    end

    def subscribe_to_exchange(domain)
      amq.queue("exchange#{identity}").bind(amq.topic('push exchange'), :key => "nanite.push.#{domain}").subscribe{ |packet|
        dispatcher.handle(load_packet(packet))
      }
    end
  end
end

module Nanite
  # Using these methods actors can participate in distributed processing
  # with other nodes using topic exchange publishing (classic pub/sub with
  # matching)
  #
  # This lets you handle a work to do to a single agent from a mapper in your
  # Merb/Rails app, and let agents to self-organize who does what, as long as the
  # properly collect the result to return to requesting peer.
  class Agent
    def push_to_exchange(type, domain, payload="")
      req = Request.new(type, payload, identity)
      req.token = Nanite.gensym
      req.reply_to = nil
      amq.topic('push exchange').publish(dump_packet(req), :key => "nanite.push.#{domain}")
    end

    def subscribe_to_exchange(domain)
      amq.queue("exchange#{identity}").bind(amq.topic('push exchange'), :key => "nanite.push.#{domain}").subscribe{ |packet|
        dispatcher.handle(load_packet(packet))
      }
    end
  end
end

Document how Nanite agents can participate in distrubuted processing using topic queues and push_to_exchange/subscribe_to_exchange.
Document how Nanite agents can participate in distrubuted processing using topic queues and push_to_exchange/subscribe_to_exchange.

Signed-off-by: ezmobius <4aca359fa1b78e51bc8226c4f161d16fdc1e21c8@engineyard.com>