zmqjsonrpc 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -5
- data/lib/simple_json_rpc/client.rb +4 -1
- data/lib/simple_json_rpc/server.rb +7 -2
- data/zmqjsonrpc.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2948d618a2efcf6543835ede91e97a605278dc60
|
4
|
+
data.tar.gz: 63ddf0cd675fda196303132b7ec8d18ccb550917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6753183268ac0cc54651d43d8892391fae2ba51f53cb9d160df1bdd24ec2551bffdd0a726d54379a5e3bef61c7c72d3d29b438940132654040eddb71e6790a30
|
7
|
+
data.tar.gz: 80d7c16ac87b2f5f652e5d6662f4cbc92b008b8b9911a8efaf6405a93ab6896491bae86ecc59909df891194dfae145dea432117c0d0c0f724dadddf3322b194b
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# zmqjsonrpc
|
1
|
+
# zmqjsonrpc [![Gem Version](https://img.shields.io/gem/v/zmqjsonrpc.svg)](https://rubygems.org/gems/zmqjsonrpc)
|
2
2
|
|
3
3
|
This gem implements a very simple [JSON RPC 2.0](http://www.jsonrpc.org/specification) client and server which uses zeroMQ for transport.
|
4
|
-
Let's not talk
|
4
|
+
Let's not talk too much, let's see some code:
|
5
5
|
|
6
6
|
```ruby
|
7
7
|
require 'rubygems'
|
@@ -10,6 +10,16 @@ Let's not talk to much, let's see some code:
|
|
10
10
|
# client request to a running server
|
11
11
|
client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200")
|
12
12
|
client.some_method(1, "b", [1,{a:1}])
|
13
|
+
|
14
|
+
# -or- a client with logger and error handling
|
15
|
+
require 'logger'
|
16
|
+
logger = Logger.new(STDOUT)
|
17
|
+
client = ZmqJsonRpc::Client.new("tcp://127.0.0.1:49200", 10000, loggger)
|
18
|
+
begin
|
19
|
+
client.faulty_method()
|
20
|
+
rescue ZmqJsonRpc::ClientError => e
|
21
|
+
puts "bad things can happen..."
|
22
|
+
end
|
13
23
|
```
|
14
24
|
|
15
25
|
```ruby
|
@@ -27,6 +37,13 @@ Let's not talk to much, let's see some code:
|
|
27
37
|
proxy = Proxy.new()
|
28
38
|
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
|
29
39
|
server.server_loop
|
40
|
+
|
41
|
+
# -or- a server with your own logger
|
42
|
+
require 'logger'
|
43
|
+
logger = Logger.new(STDOUT)
|
44
|
+
# ...
|
45
|
+
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200", logger)
|
46
|
+
|
30
47
|
|
31
48
|
# -or- dispatch a thread
|
32
49
|
server = ZmqJsonRpc::Server.new(proxy, "tcp://*:49200")
|
@@ -39,6 +56,7 @@ Let's not talk to much, let's see some code:
|
|
39
56
|
|
40
57
|
## Resources
|
41
58
|
|
59
|
+
* [API reference documentation](http://www.rubydoc.info/github/bisdn/zmqjsonrpc/)
|
42
60
|
* The [JSON RPC 2.0 Spec](http://www.jsonrpc.org/specification)
|
43
61
|
* The used [ZeroMQ gem](https://github.com/chuckremes/ffi-rzmq) and [good examples](http://github.com/andrewvc/learn-ruby-zeromq)
|
44
62
|
* [Gem making](http://guides.rubygems.org/make-your-own-gem/)
|
@@ -51,11 +69,11 @@ Let's not talk to much, let's see some code:
|
|
51
69
|
* Keep the client connection alive instead of re-establishing every time.
|
52
70
|
* Add more tests.
|
53
71
|
|
54
|
-
##
|
72
|
+
## License
|
55
73
|
|
56
74
|
This code is released under the terms of MIT License.
|
57
75
|
|
58
76
|
## Contribute
|
59
77
|
|
60
|
-
Please do so! Just send a
|
61
|
-
Especially, adding webrick for transport would be nice.
|
78
|
+
Please do so! Just send a message or send a pull request.
|
79
|
+
Especially, adding webrick for transport would be nice.
|
@@ -14,9 +14,10 @@ module ZmqJsonRpc
|
|
14
14
|
# The client does not keep the connection alive. For each request, a new connection is esablished and torn down after the response.
|
15
15
|
# This could become a performance issue.
|
16
16
|
class Client
|
17
|
-
def initialize(connect="tcp://127.0.0.1:49200", timeout=10000)
|
17
|
+
def initialize(connect="tcp://127.0.0.1:49200", timeout=10000, logger=nil)
|
18
18
|
@connect = connect
|
19
19
|
@timeout = timeout
|
20
|
+
@logger = logger
|
20
21
|
end
|
21
22
|
|
22
23
|
def send_rpc(method, params=[])
|
@@ -36,12 +37,14 @@ module ZmqJsonRpc
|
|
36
37
|
method: method.to_s,
|
37
38
|
params: params
|
38
39
|
}
|
40
|
+
@logger.debug "zmqjsonrpc client sends request: #{request.inspect})" unless @logger.nil?
|
39
41
|
rc = @socket.send_string(request.to_json) # this will always succeed, even if the server is not reachable.
|
40
42
|
|
41
43
|
# interpret response
|
42
44
|
response = ''
|
43
45
|
rc = @socket.recv_string(response)
|
44
46
|
raise "Could talk to the server (server unreachable? time out?)" if rc < 0
|
47
|
+
@logger.debug "zmqjsonrpc client got response: #{response})" unless @logger.nil?
|
45
48
|
resjson = JSON.parse(response)
|
46
49
|
# check response
|
47
50
|
raise "Response's id did not match the sent id" if resjson["id"] != req_id
|
@@ -36,6 +36,7 @@ module ZmqJsonRpc
|
|
36
36
|
|
37
37
|
def handle_request(request)
|
38
38
|
begin
|
39
|
+
@logger.debug "zmqjsonrpc server received request: #{request}" unless @logger.nil?
|
39
40
|
req_id = nil
|
40
41
|
rpc = JSON.parse(request)
|
41
42
|
raise "Received unsupprted jsonrpc version (#{rpc['jsonrpc']})" if rpc["jsonrpc"].strip != "2.0"
|
@@ -43,17 +44,18 @@ module ZmqJsonRpc
|
|
43
44
|
method = rpc["method"]
|
44
45
|
params = rpc["params"]
|
45
46
|
|
46
|
-
@logger.
|
47
|
+
@logger.debug "zmqjsonrpc server calls proxy method: #{method}(#{params.collect {|p| p.inspect}.join(", ")})" unless @logger.nil?
|
47
48
|
result = @proxy.send(method.to_sym, *params)
|
48
49
|
response = {
|
49
50
|
id: rid,
|
50
51
|
jsonrpc: "2.0",
|
51
52
|
result: result
|
52
53
|
}
|
54
|
+
@logger.debug "zmqjsonrpc server sends response: #{response.inspect})" unless @logger.nil?
|
53
55
|
return response.to_json
|
54
56
|
rescue => e
|
55
57
|
# If there is more time to spare, we could implement the actual error codes here.
|
56
|
-
@logger.warn "
|
58
|
+
@logger.warn "zmqjsonrpc server caugth error during request handling: #{e.message.strip})" unless @logger.nil?
|
57
59
|
response = {
|
58
60
|
id: rid,
|
59
61
|
jsonrpc: "2.0",
|
@@ -63,6 +65,7 @@ module ZmqJsonRpc
|
|
63
65
|
data: e.backtrace.inspect
|
64
66
|
}
|
65
67
|
}
|
68
|
+
@logger.debug "zmqjsonrpc server sends response: #{response.inspect})" unless @logger.nil?
|
66
69
|
return response.to_json
|
67
70
|
end
|
68
71
|
end
|
@@ -71,6 +74,7 @@ module ZmqJsonRpc
|
|
71
74
|
@context = ZMQ::Context.new(1)
|
72
75
|
@socket = @context.socket(ZMQ::REP)
|
73
76
|
@socket.bind(@connect)
|
77
|
+
@logger.info "zmqjsonrpc server listens to #{@connect}" unless @logger.nil?
|
74
78
|
begin
|
75
79
|
loop do
|
76
80
|
request = ''
|
@@ -79,6 +83,7 @@ module ZmqJsonRpc
|
|
79
83
|
@socket.send_string(response)
|
80
84
|
end
|
81
85
|
ensure
|
86
|
+
@logger.info "zmqjsonrpc server shuts down" unless @logger.nil?
|
82
87
|
@socket.close
|
83
88
|
# @context.terminate
|
84
89
|
end
|
data/zmqjsonrpc.gemspec
CHANGED
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = "zmqjsonrpc"
|
9
|
-
spec.version = "0.1"
|
9
|
+
spec.version = "0.1.1"
|
10
10
|
spec.authors = ["Tom Rothe"]
|
11
11
|
spec.email = ["tom@bisdn.de"]
|
12
12
|
spec.description = 'Simple JSON RPC 2.0 client and server via zmq.'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zmqjsonrpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Rothe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi-rzmq
|