Class: ZmqJsonRpc::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_json_rpc/server.rb

Overview

This class implements the server:

USE

class Proxy
  def some_method(a,b)
    return [a,b,{"XXXX"=> 1}]
  end
end
server = ZmqJsonRpc::Server.new(Proxy.new())
server.server_loop

If you want a non blocking server do

 server = ZmqJsonRpc::Server.new(Proxy.new())
 thread = Thread.new {
   server.server_loop
 }
# later either do
thread.join # waiting for the server to finish -- which is never!
# or
thread.exit # or end the thread

Instance Method Summary collapse

Constructor Details

#initialize(proxy, connect = "tcp://*:49200", logger = nil) ⇒ Server

For errors the spec says: The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range, but not defined explicitly below is reserved for future use. The error codes are nearly the same as those suggested for XML-RPC at the following url: xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

For details on the zmq gem, please see github.com/chuckremes/ffi-rzmq (best to look in the code)


31
32
33
34
35
# File 'lib/simple_json_rpc/server.rb', line 31

def initialize(proxy, connect="tcp://*:49200", logger=nil)
  @connect = connect
  @proxy = proxy
  @logger = logger
end

Instance Method Details

#handle_request(request) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_json_rpc/server.rb', line 37

def handle_request(request)
  begin
    @logger.debug "zmqjsonrpc server received request: #{request}" unless @logger.nil?
    req_id = nil
    rpc = JSON.parse(request)
    raise "Received unsupprted jsonrpc version (#{rpc['jsonrpc']})" if rpc["jsonrpc"].strip != "2.0"
    rid = rpc["id"]
    method = rpc["method"]
    params = rpc["params"]

    @logger.debug "zmqjsonrpc server calls proxy method: #{method}(#{params.collect {|p| p.inspect}.join(", ")})" unless @logger.nil?
    result = @proxy.send(method.to_sym, *params)
    response = {
      id: rid,
      jsonrpc: "2.0",
      result: result
    }
    @logger.debug "zmqjsonrpc server sends response: #{response.inspect})" unless @logger.nil?
    return response.to_json
  rescue => e
    # If there is more time to spare, we could implement the actual error codes here.
    @logger.warn "zmqjsonrpc server caugth error during request handling: #{e.message.strip})" unless @logger.nil?
    response = {
      id: rid,
      jsonrpc: "2.0",
      error: {
        code: 32603,
        message: e.message,
        data: e.backtrace.inspect
      }
    }
    @logger.debug "zmqjsonrpc server sends response: #{response.inspect})" unless @logger.nil?
    return response.to_json
  end
end

#server_loopObject


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/simple_json_rpc/server.rb', line 73

def server_loop
  @context = ZMQ::Context.new(1)
  @socket = @context.socket(ZMQ::REP)
  @socket.bind(@connect)
  @logger.info "zmqjsonrpc server listens to #{@connect}" unless @logger.nil?
  begin
    loop do
      request = ''
      rc = @socket.recv_string(request)
      response = handle_request(request)
      @socket.send_string(response)
    end
  ensure
    @logger.info "zmqjsonrpc server shuts down" unless @logger.nil?
    @socket.close
    # @context.terminate
  end
end