Class: Nanite::MapperProxy
- Includes:
- AMQPHelper
- Defined in:
- lib/nanite/mapper_proxy.rb
Overview
This class allows sending requests to nanite agents without having to run a local mapper. It is used by Actor.request which can be used by actors than need to send requests to remote agents. All requests go through the mapper for security purposes.
Instance Attribute Summary collapse
-
#amqp ⇒ Object
Returns the value of attribute amqp.
-
#identity ⇒ Object
Returns the value of attribute identity.
-
#options ⇒ Object
Returns the value of attribute options.
-
#pending_requests ⇒ Object
Returns the value of attribute pending_requests.
-
#serializer ⇒ Object
Returns the value of attribute serializer.
Class Method Summary collapse
-
.instance ⇒ Object
Accessor for actor.
Instance Method Summary collapse
-
#handle_intermediate_result(res) ⇒ Object
Handle intermediary result.
-
#handle_result(res) ⇒ Object
Handle final result.
-
#initialize(id, opts) ⇒ MapperProxy
constructor
A new instance of MapperProxy.
- #push(type, payload = '', opts = {}) ⇒ Object
-
#request(type, payload = '', opts = {}, &blk) ⇒ Object
Send request to given agent through the mapper.
Methods included from AMQPHelper
Constructor Details
#initialize(id, opts) ⇒ MapperProxy
Returns a new instance of MapperProxy.
22 23 24 25 26 27 28 29 |
# File 'lib/nanite/mapper_proxy.rb', line 22 def initialize(id, opts) @options = opts || {} @identity = id @pending_requests = {} @amqp = start_amqp() @serializer = Serializer.new([:format]) @@instance = self end |
Instance Attribute Details
#amqp ⇒ Object
Returns the value of attribute amqp.
15 16 17 |
# File 'lib/nanite/mapper_proxy.rb', line 15 def amqp @amqp end |
#identity ⇒ Object
Returns the value of attribute identity.
15 16 17 |
# File 'lib/nanite/mapper_proxy.rb', line 15 def identity @identity end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/nanite/mapper_proxy.rb', line 15 def @options end |
#pending_requests ⇒ Object
Returns the value of attribute pending_requests.
15 16 17 |
# File 'lib/nanite/mapper_proxy.rb', line 15 def pending_requests @pending_requests end |
#serializer ⇒ Object
Returns the value of attribute serializer.
15 16 17 |
# File 'lib/nanite/mapper_proxy.rb', line 15 def serializer @serializer end |
Class Method Details
.instance ⇒ Object
Accessor for actor
18 19 20 |
# File 'lib/nanite/mapper_proxy.rb', line 18 def self.instance @@instance if defined?(@@instance) end |
Instance Method Details
#handle_intermediate_result(res) ⇒ Object
Handle intermediary result
55 56 57 58 |
# File 'lib/nanite/mapper_proxy.rb', line 55 def handle_intermediate_result(res) handlers = pending_requests[res.token] handlers[:intermediate_handler].call(res) if handlers && handlers[:intermediate_handler] end |
#handle_result(res) ⇒ Object
Handle final result
61 62 63 64 |
# File 'lib/nanite/mapper_proxy.rb', line 61 def handle_result(res) handlers = pending_requests.delete(res.token) handlers[:result_handler].call(res) if handlers && handlers[:result_handler] end |
#push(type, payload = '', opts = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/nanite/mapper_proxy.rb', line 44 def push(type, payload = '', opts = {}) raise "Mapper proxy not initialized" unless identity && push = Push.new(type, payload, nil, opts) push.from = identity push.token = Identity.generate push.persistent = opts.key?(:persistent) ? opts[:persistent] : [:persistent] Nanite::Log.info("SEND #{push.to_s([:tags, :target])}") amqp.fanout('request', :no_declare => [:secure]).publish(serializer.dump(push)) end |
#request(type, payload = '', opts = {}, &blk) ⇒ Object
Send request to given agent through the mapper
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nanite/mapper_proxy.rb', line 32 def request(type, payload = '', opts = {}, &blk) raise "Mapper proxy not initialized" unless identity && request = Request.new(type, payload, nil, opts) request.from = identity request.token = Identity.generate request.persistent = opts.key?(:persistent) ? opts[:persistent] : [:persistent] pending_requests[request.token] = { :intermediate_handler => opts[:intermediate_handler], :result_handler => blk } Nanite::Log.info("SEND #{request.to_s([:tags, :target])}") amqp.fanout('request', :no_declare => [:secure]).publish(serializer.dump(request)) end |