Class: ZmqJsonRpc::Client
- Inherits:
-
Object
- Object
- ZmqJsonRpc::Client
- Defined in:
- lib/simple_json_rpc/client.rb
Overview
You shall instanciate this class with a connect string which follows the zeroMQ conventions: api.zeromq.org/2-1:zmq-connect After connecting you can call any method on this class and it will be sent to the server. If something goes wrong ZmqJsonRpc::ClientError is thrown. timeout is measured in milliseconds.
NOTE The client does not keep the connection alive. For each request, a new connection is esablished and torn down after the response. This could become a performance issue.
Instance Method Summary collapse
-
#initialize(connect = "tcp://127.0.0.1:49200", timeout = 10000, logger = nil) ⇒ Client
constructor
A new instance of Client.
- #method_missing(meth, *args, &block) ⇒ Object
- #send_rpc(method, params = []) ⇒ Object
Constructor Details
#initialize(connect = "tcp://127.0.0.1:49200", timeout = 10000, logger = nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 |
# File 'lib/simple_json_rpc/client.rb', line 17 def initialize(connect="tcp://127.0.0.1:49200", timeout=10000, logger=nil) @connect = connect @timeout = timeout @logger = logger end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
62 63 64 |
# File 'lib/simple_json_rpc/client.rb', line 62 def method_missing(meth, *args, &block) self.send_rpc(meth, args) end |
Instance Method Details
#send_rpc(method, params = []) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple_json_rpc/client.rb', line 23 def send_rpc(method, params=[]) begin # connect socket @context = ZMQ::Context.new(1) @socket = @context.socket(ZMQ::REQ) @socket.connect(@connect) @socket.setsockopt(ZMQ::SNDTIMEO, @timeout) @socket.setsockopt(ZMQ::RCVTIMEO, @timeout) # build and send request req_id = SecureRandom.uuid request = { id: req_id, jsonrpc: "2.0", method: method.to_s, params: params } @logger.debug "zmqjsonrpc client sends request: #{request.inspect})" unless @logger.nil? rc = @socket.send_string(request.to_json) # this will always succeed, even if the server is not reachable. # interpret response response = '' rc = @socket.recv_string(response) raise "Could talk to the server (server unreachable? time out?)" if rc < 0 @logger.debug "zmqjsonrpc client got response: #{response})" unless @logger.nil? resjson = JSON.parse(response) # check response raise "Response's id did not match the sent id" if resjson["id"] != req_id raise "Response's version number is not supported (#{resjson["jsonrpc"]})" if resjson["jsonrpc"].strip != "2.0" raise "Server returned error (#{resjson["error"]["code"] || "?"}): #{resjson["error"]["message"]}\n#{resjson["error"]["data"]}" if resjson["error"] return resjson["result"] rescue => e raise ClientError, e. ensure @socket.close rescue '' end end |