Class: Isono::NodeModules::RpcChannel
- Includes:
- Logger
- Defined in:
- lib/isono/node_modules/rpc_channel.rb
Defined Under Namespace
Classes: DuplicateEndpointError, OneshotResponseContext, RequestContext, ResponseContext, RpcError, UnknownEndpointError
Instance Attribute Summary collapse
-
#amq ⇒ Object
readonly
Returns the value of attribute amq.
Attributes inherited from Base
Instance Method Summary collapse
-
#register_endpoint(endpoint, app, opts = {}) ⇒ Object
Register a new RPC endpoint.
-
#request(endpoint, command, *args, &blk) ⇒ RequestContext, any
Make a RPC request to an endpoint.
-
#unregister_endpoint(endpoint) ⇒ Object
Unregister endpoint.
Methods included from Logger
Methods inherited from Base
#config_section, #initialize, #manifest, #value_object
Constructor Details
This class inherits a constructor from Isono::NodeModules::Base
Instance Attribute Details
#amq ⇒ Object (readonly)
Returns the value of attribute amq.
105 106 107 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 105 def amq @amq end |
Instance Method Details
#register_endpoint(endpoint, app, opts = {}) ⇒ Object
Register a new RPC endpoint.
This method works in sync mode if called at non-EM reactor thread.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 165 def register_endpoint(endpoint, app, opts={}) raise TypeError unless app.respond_to?(:call) opts = {:exclusive=>true, :prefetch=>1}.merge(opts) # create receive queue for new RPC endpoint. endpoint_proc = proc { |header, data| data = Serializer.instance.unmarshal(data) resctx = if data[:oneshot] OneshotResponseContext.new(@endpoints[endpoint][:ch].response_exchange, header) else ResponseContext.new(@endpoints[endpoint][:ch].response_exchange, header) end begin req = Rack::Request.new({:sender=>header.reply_to['command-recv.'.size..-1], :message_id=>header. }.merge(data)) res = Rack::Response.new(resctx) ret = app.call(req, res) rescue ::Exception => e logger.error(e) resctx.response(e) unless resctx.responded? end } EventMachine.schedule { ch = node.create_channel ch.instance_eval %Q{ def endpoint_queue self.queue("isono.rpc.endpoint.#{endpoint}", {:exclusive=>false, :auto_delete=>true}) end def response_exchange self.default_exchange end } ch.prefetch(opts[:prefetch].to_i) if opts[:prefetch].to_i > 0 # stores hash here that is for thread safety. @endpoints[endpoint]={:app=>app, :opts=>opts, :ch=>ch} ch.endpoint_queue.subscribe(:ack=>true, &endpoint_proc) event.publish('rpc/register', :args=>[endpoint]) } end |
#request(endpoint, command, *args, &blk) ⇒ RequestContext, any
Make a RPC request to an endpoint.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 140 def request(endpoint, command, *args, &blk) req = RequestContext.new(endpoint, command, args) # the block is to setup the request context prior to sending. if blk # async r = blk.call(req) req = r if r.is_a?(RequestContext) send_request(req) req else # sync req = req.synchronize send_request(req) req.wait end end |
#unregister_endpoint(endpoint) ⇒ Object
Unregister endpoint.
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 216 def unregister_endpoint(endpoint) if @endpoints.has_key?(endpoint) EventMachine.schedule { data = @endpoints.delete(endpoint) # endpoint_queue is :auto_delete=>true so that it will be deleted # in case of zero consumers. data[:ch].endpoint_queue.unsubscribe event.publish('rpc/unregister', :args=>[endpoint]) } end end |