Project

zmqjsonrpc

0.0
No commit activity in last 3 years
No release in over 3 years
Simple JSON RPC 2.0 client and server via zmq.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

~> 2.0.4
 Project Readme

zmqjsonrpc Gem Version

This gem implements a very simple JSON RPC 2.0 client and server which uses zeroMQ for transport. Please install libzmq, e.g. via apt-get install libzmq3-dev.

Let's not talk too much, let's see some code:

  require 'rubygems'
  require 'zmqjsonrpc'
  
  # client request to a running server
  client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200")
  client.some_method(1, "b", [1,{a:1}])

  # -or- a client with logger and error handling
  require 'logger'
  logger = Logger.new(STDOUT)
  client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200", 10000, loggger)
  begin
    client.faulty_method()
  rescue ZmqJsonRpc::ClientError => e
    puts "bad things can happen..."
  end
  require 'rubygems'
  require 'zmqjsonrpc'

  class Proxy
    def some_method(a,b,c)
      # do you thing
      return ["xyz", 77]
    end
  end

  # blocking server
  proxy = Proxy.new()
  server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
  server.server_loop

  # -or- a server with your own logger
  require 'logger'
  logger = Logger.new(STDOUT)
  # ...
  server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200", logger)

  
  # -or- dispatch a thread
  server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
  thread = Thread.new {
    server.server_loop
  }
  # and cancel the thread if you want to shut the server down
  thread.exit

Resources

Stuff left to do

  • Add support for by-name parameters (see the spec)
  • Add individual exception classes in the client.
  • Send different error codes if something goes wrong in the server.
  • Keep the client connection alive instead of re-establishing every time.
  • Add more tests.

License

This code is released under the terms of MIT License.

Contribute

Please do so! Just send a message or send a pull request. Especially, adding webrick for transport would be nice.