rawjsonrpc 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +17 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +126 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/rawjsonrpc/client.rb +112 -0
- data/lib/rawjsonrpc/error.rb +13 -0
- data/lib/rawjsonrpc/server.rb +132 -0
- data/lib/rawjsonrpc.rb +18 -0
- data/test/helper.rb +18 -0
- data/test/test_rawjsonrpc.rb +110 -0
- metadata +88 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.6.4"
|
11
|
+
end
|
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Alexander Schrode
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
= rawjsonrpc
|
2
|
+
|
3
|
+
rawjsonrpc is a libary to create a jsonrpc client for every type of stream. It
|
4
|
+
also profides one client for tcpsocket, an server for one client and on server
|
5
|
+
based on gserver from the ruby stdlib.
|
6
|
+
This is libary is a my first try in ruby to create a gem. Its more a test how
|
7
|
+
gems work but i hope i can help somebody with this lib.
|
8
|
+
|
9
|
+
== load lib
|
10
|
+
|
11
|
+
install libary
|
12
|
+
|
13
|
+
gem install rawjsonrpc
|
14
|
+
|
15
|
+
include rawjsonrpc in your programm
|
16
|
+
|
17
|
+
require 'rawjsonrpc'
|
18
|
+
|
19
|
+
== Use the TCP client
|
20
|
+
|
21
|
+
Create a client to send rpc calls.
|
22
|
+
|
23
|
+
client = RawJsonRpc::ClientSocket.new('192.168.2.10', 8080) #creates TCP client
|
24
|
+
ret = client.request("func1", nil) #calles func1 with no para
|
25
|
+
#returns result in ret
|
26
|
+
ret = client.request("func2", 1, 3.0, :test, "om") #calles func2 with 4 paras
|
27
|
+
ret = client.func3([1, 2, 3, 4]) #calles func3 with a array
|
28
|
+
client.notification("noti", 1, "t") #send notifaction no
|
29
|
+
# #return value
|
30
|
+
client.close()
|
31
|
+
|
32
|
+
== Use server TCP simple
|
33
|
+
|
34
|
+
Use the libary to serve functions over tcp ip.
|
35
|
+
|
36
|
+
client = RawJsonRpc::SimpleServerSocket.new(8080)
|
37
|
+
client.add_method("func1", method("do_stuff1")) # adds do_stuff1 as "func1"
|
38
|
+
client.add_method("foo", method(:bar)) # adds bar as foo
|
39
|
+
client.add_block("func2"){|a,b, *c| # adds a block with 2 args
|
40
|
+
#do stuff # and args as func2
|
41
|
+
}
|
42
|
+
client.serve()
|
43
|
+
|
44
|
+
== use gserver TCP
|
45
|
+
|
46
|
+
Lock in the stdlib for usage. The server delivers automaticly the functions.
|
47
|
+
Beware of adding methods or blocks after starting.
|
48
|
+
|
49
|
+
|
50
|
+
server = RawJsonRpc::TCPServer.new(#args)
|
51
|
+
|
52
|
+
== implement your own jsonclient
|
53
|
+
|
54
|
+
Boilerplate Code to create your own client.
|
55
|
+
|
56
|
+
class superclient < RawJsonRpc::RawClientJsonRpc
|
57
|
+
def initiailize(*args)
|
58
|
+
# init stream
|
59
|
+
end
|
60
|
+
|
61
|
+
def send_request(jsonstring)
|
62
|
+
#send stream
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_response
|
66
|
+
#return the response
|
67
|
+
end
|
68
|
+
|
69
|
+
def close
|
70
|
+
#close resources if needed
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
== implement your server
|
75
|
+
|
76
|
+
The boilerplattecode to create a new server.
|
77
|
+
|
78
|
+
class superserver
|
79
|
+
include RawJsonRpc::RawServerJsonRpc
|
80
|
+
|
81
|
+
def initizial(*args)
|
82
|
+
#init
|
83
|
+
end
|
84
|
+
|
85
|
+
def serve
|
86
|
+
#get data from client
|
87
|
+
response = execute(jsonstring) #transfers the string and calls function
|
88
|
+
#return response string or nil if its a
|
89
|
+
# #notification
|
90
|
+
#send response
|
91
|
+
end
|
92
|
+
|
93
|
+
def end
|
94
|
+
#stuff
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
== known issues
|
99
|
+
|
100
|
+
* Block arguments can't be checkt because they are blocks not lamdbas
|
101
|
+
|
102
|
+
== Your implementations
|
103
|
+
|
104
|
+
feel free to contribute your client and servers to the project.
|
105
|
+
|
106
|
+
== TODO
|
107
|
+
* add examples
|
108
|
+
* add doc for gserver implementation
|
109
|
+
* add a piping server and client
|
110
|
+
* fix block argumentchecks
|
111
|
+
|
112
|
+
== Contributing to rawjsonrpc
|
113
|
+
|
114
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
115
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
116
|
+
* Fork the project
|
117
|
+
* Start a feature/bugfix branch
|
118
|
+
* Commit and push until you are happy with your contribution
|
119
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
120
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
121
|
+
|
122
|
+
== Copyright
|
123
|
+
|
124
|
+
Copyright (c) 2012 Alexander Schrode. See LICENSE.txt for
|
125
|
+
further details.
|
126
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "rawjsonrpc"
|
18
|
+
gem.homepage = "http://github.com/schroder-/rawjsonrpc"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A basic libary for json rpc that allow to create for your stream type. It includes a TCPIp Server and Client.}
|
21
|
+
gem.description = %Q{A basic libary for json rpc that allow you to easly create one for your streamtype .It includes tcp json rpc on socket connections. Version for single clients server, one for multiple clients server based on gserver are served and a TCP client.}
|
22
|
+
gem.email = "midix01@googlemail.com"
|
23
|
+
gem.authors = ["Alexander Schrode"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "rawjsonrpc #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# client.rb : Implements the Clientside
|
4
|
+
#
|
5
|
+
# Copyright January 2012, Alexander Schrode. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'json'
|
10
|
+
require 'socket'
|
11
|
+
require_relative 'error'
|
12
|
+
|
13
|
+
module RawJsonRpc
|
14
|
+
# Implements the base class for all Clients.
|
15
|
+
# The class get all methode calls generate the json data from it
|
16
|
+
#
|
17
|
+
# <b>Every client must implement get_response and send_request</b>.
|
18
|
+
#
|
19
|
+
# =Examples
|
20
|
+
# ==send request
|
21
|
+
#
|
22
|
+
## client.request("run", [1,2,3]) # calls: run params: 1 2 3
|
23
|
+
## ret = client.request("attack", "alpha", 1, 3.0) # calls: attack
|
24
|
+
## # params "alpha", 1, 3.0
|
25
|
+
## # returns data into ret
|
26
|
+
# ==send request shortcut
|
27
|
+
#
|
28
|
+
## client.run([1,2,3])
|
29
|
+
## ret = client.attack("alpha", 1, 3.0)
|
30
|
+
#
|
31
|
+
# ==send notification (no return value)
|
32
|
+
## client.notification("beat", [:alpha, 123], "fast")
|
33
|
+
#
|
34
|
+
class RawClientJsonRpc
|
35
|
+
|
36
|
+
# redirect calls
|
37
|
+
def method_missing(name, *args)
|
38
|
+
if args.empty?
|
39
|
+
request(name)
|
40
|
+
else
|
41
|
+
request(name, *args)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
def id
|
47
|
+
@id_count ||= 0
|
48
|
+
@id_count += 1
|
49
|
+
end
|
50
|
+
|
51
|
+
public
|
52
|
+
# returns the server response must be implmented
|
53
|
+
def get_response
|
54
|
+
raise NotImplementedError.new("method not overriden")
|
55
|
+
end
|
56
|
+
|
57
|
+
# Send the request to server must be implmented.
|
58
|
+
# takes json string.
|
59
|
+
def send_request para
|
60
|
+
raise NotImplementedError.new("method not overriden")
|
61
|
+
end
|
62
|
+
|
63
|
+
public
|
64
|
+
# Sends a notification to server.
|
65
|
+
# A Notifications don't return any value
|
66
|
+
def notification(function_name, *para)
|
67
|
+
para = {'method' => function_name, 'params' => para, 'id' => nil}
|
68
|
+
.to_json
|
69
|
+
send_request para
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
# Sends a request to server
|
73
|
+
# return a value or raise an RpcException if server sends an error.
|
74
|
+
def request(function_name, *para)
|
75
|
+
id_count = id
|
76
|
+
para = {'method' => function_name, 'params' => para, 'id' => id_count}
|
77
|
+
.to_json
|
78
|
+
send_request(para)
|
79
|
+
result = get_response
|
80
|
+
if result.empty? or result == nil
|
81
|
+
raise(RpcError.new("empty respons"))
|
82
|
+
end
|
83
|
+
response = JSON.load(result)
|
84
|
+
if response["error"] != nil
|
85
|
+
raise(RpcError.new(response["error"]))
|
86
|
+
elsif response["id"] != id_count
|
87
|
+
raise(RpcError.new("server send wrong data."))
|
88
|
+
end
|
89
|
+
response["result"]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
# Implements the raw json rpc client over a tcp socket.
|
93
|
+
# Includes the RawClientJsonRpcBase Module
|
94
|
+
class ClientSock < RawClientJsonRpc
|
95
|
+
# Init the socket with host.
|
96
|
+
def initialize(ip, port)
|
97
|
+
@socket = TCPSocket.new(ip, port)
|
98
|
+
end
|
99
|
+
# closes the socket connection
|
100
|
+
def close
|
101
|
+
@socket.close
|
102
|
+
end
|
103
|
+
private
|
104
|
+
def get_response
|
105
|
+
@socket.gets
|
106
|
+
end
|
107
|
+
|
108
|
+
def send_request str
|
109
|
+
@socket.puts str
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# :stopdoc:
|
3
|
+
# error.rb : Implements all Error Classes
|
4
|
+
#
|
5
|
+
# Copyright January 2012, Alexander Schrode. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
# :startdoc:
|
9
|
+
|
10
|
+
|
11
|
+
class RpcError < StandardError
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# server.rb : Implements the base server and some basic servers
|
4
|
+
#
|
5
|
+
# Copyright January 2012, Alexander Schrode. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'socket'
|
10
|
+
require 'logger'
|
11
|
+
require 'json'
|
12
|
+
require 'gserver'
|
13
|
+
require_relative 'error'
|
14
|
+
|
15
|
+
module RawJsonRpc
|
16
|
+
# Implements the basic server functions
|
17
|
+
# When implementing a new server type you must only give a jsonstring to the
|
18
|
+
# execute function.
|
19
|
+
# =Example
|
20
|
+
#
|
21
|
+
# Add Functions
|
22
|
+
#
|
23
|
+
## server.add_method("run", methode(:test)) #adds methode test as "run"
|
24
|
+
## server.add_block("attack"){ |target, dir, times|
|
25
|
+
## #stuff
|
26
|
+
## }
|
27
|
+
module RawServerJsonRpc
|
28
|
+
# Adds methode to be executed
|
29
|
+
def add_method(name, func)
|
30
|
+
@rawjsonrpc_funcs ||= {}
|
31
|
+
@rawjsonrpc_blocks ||= {}
|
32
|
+
@rawjsonrpc_funcs[name] = func
|
33
|
+
end
|
34
|
+
# Adds blocks that get executed
|
35
|
+
# Arguments can not checkt because they are procs no lambdas if you can
|
36
|
+
# provied a hack plz contribute.
|
37
|
+
def add_block(name, &block)
|
38
|
+
@rawjsonrpc_blocks ||= {}
|
39
|
+
@rawjsonrpc_funcs ||= {}
|
40
|
+
@rawjsonrpc_blocks[name] = block
|
41
|
+
end
|
42
|
+
protected
|
43
|
+
# Handels json string input and calls the methode and returns a return
|
44
|
+
# string or nil if its a notification.
|
45
|
+
def execute(input)
|
46
|
+
begin
|
47
|
+
request_data = JSON.load(input)
|
48
|
+
@rawjsonrpc_noti = if request_data["id"] == nil then true
|
49
|
+
else false
|
50
|
+
end
|
51
|
+
@rawjsonrpc_id = request_data["id"]
|
52
|
+
if @rawjsonrpc_funcs.has_key?(request_data["method"])
|
53
|
+
ret = @rawjsonrpc_funcs[request_data["method"]].call(*request_data["params"])
|
54
|
+
elsif @rawjsonrpc_blocks.has_key?(request_data["method"])
|
55
|
+
ret = @rawjsonrpc_blocks[request_data["method"]].call(*request_data["params"])
|
56
|
+
else
|
57
|
+
@rawjsonrpc_back = request_data
|
58
|
+
raise("No Methode")
|
59
|
+
end
|
60
|
+
if request_data["id"] != nil
|
61
|
+
response_data = {"result" => ret, "error" => nil, "id" => request_data["id"]}
|
62
|
+
.to_json
|
63
|
+
else nil
|
64
|
+
end
|
65
|
+
rescue => ex
|
66
|
+
unless @rawjsonrpc_noti
|
67
|
+
response = {"result" => nil, "error" => ex.message,
|
68
|
+
"id" => @rawjsonrpc_id}.to_json
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
# Implements RawServerJsonRpcBase for a TCPServer, that can only handle
|
74
|
+
# one client at once.
|
75
|
+
class ServerSocket
|
76
|
+
include RawServerJsonRpc
|
77
|
+
# sets the port for serving. Optimal you can add a logger object to log
|
78
|
+
# server activities.
|
79
|
+
def initialize(port, logger=nil)
|
80
|
+
@port = port
|
81
|
+
@log = logger
|
82
|
+
end
|
83
|
+
private
|
84
|
+
#checks logger
|
85
|
+
def check(logger, para)
|
86
|
+
if @log != nil
|
87
|
+
logger.call(para)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
public
|
91
|
+
# Starts the servering the methods for clients
|
92
|
+
def server
|
93
|
+
check log.level, "start serving data at " + @port.to_s
|
94
|
+
server = TCPServer.new(@port)
|
95
|
+
client = server.accept
|
96
|
+
loop do
|
97
|
+
begin
|
98
|
+
data = client.gets
|
99
|
+
if data == nil or data == "END\n"
|
100
|
+
check @log.info, "client closed connection"
|
101
|
+
client.close
|
102
|
+
client = server.accept
|
103
|
+
next
|
104
|
+
end
|
105
|
+
check @log.info, "recived from client " + data
|
106
|
+
data = execute(data)
|
107
|
+
if data != nil
|
108
|
+
check @log.info, "send to client " + data
|
109
|
+
client.puts(data)
|
110
|
+
end
|
111
|
+
rescue SocketError
|
112
|
+
check @log.info, "client closed connection"
|
113
|
+
client.close
|
114
|
+
client = server.accept
|
115
|
+
# Exception from get killed
|
116
|
+
rescue Errno::EPIPE
|
117
|
+
check @log.info, "server get killed"
|
118
|
+
exit
|
119
|
+
rescue => ex
|
120
|
+
check @log.fatal, "server get killed from: " + ex.message
|
121
|
+
client.close
|
122
|
+
raise ex
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
# Implements the RawServerJsonRpcBase as GServer the stdlib SocketServer. For
|
128
|
+
# more information go one to the stdlib.
|
129
|
+
class TCPServer < GServer
|
130
|
+
include RawServerJsonRpc
|
131
|
+
end
|
132
|
+
end
|
data/lib/rawjsonrpc.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding:utf-8
|
2
|
+
# License:: MIT
|
3
|
+
# Author:: Alexander Schrode( MailTo:: midix01@googlemail.de)
|
4
|
+
|
5
|
+
# = RAW JSON RPC
|
6
|
+
# The rawjsonrpc libary implements base classes / modules to write jsonrpc or
|
7
|
+
# servers. So that you can easly implments for your stream type with minmal
|
8
|
+
# efford.
|
9
|
+
# Also it provides a TCP client and two TCP Servers.
|
10
|
+
# Feel free to provid your implmentations to the project.
|
11
|
+
|
12
|
+
module RawJsonRpc
|
13
|
+
# :nodoc:
|
14
|
+
VERSION = "0.0.1"# :nodoc:
|
15
|
+
end
|
16
|
+
require_relative 'rawjsonrpc/error'
|
17
|
+
require_relative 'rawjsonrpc/server'
|
18
|
+
require_relative 'rawjsonrpc/client'
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'rawjsonrpc'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
def no_arg
|
5
|
+
"no arg"
|
6
|
+
end
|
7
|
+
|
8
|
+
def one_arg(a)
|
9
|
+
a
|
10
|
+
end
|
11
|
+
|
12
|
+
def two_arg(a, b)
|
13
|
+
b
|
14
|
+
end
|
15
|
+
|
16
|
+
def three_arg(a, b, c)
|
17
|
+
c
|
18
|
+
end
|
19
|
+
|
20
|
+
def no
|
21
|
+
"no"
|
22
|
+
end
|
23
|
+
|
24
|
+
def damaged
|
25
|
+
raise ("error")
|
26
|
+
end
|
27
|
+
|
28
|
+
class Rawjsonrpc < Test::Unit::TestCase
|
29
|
+
class TestClientOK < RawJsonRpc::RawClientJsonRpc
|
30
|
+
include RawJsonRpc::RawServerJsonRpc
|
31
|
+
def initialize()
|
32
|
+
add_method("no", method(:no_arg))
|
33
|
+
add_method("one", method(:one_arg))
|
34
|
+
add_method("two", method(:two_arg))
|
35
|
+
add_method("three", method(:three_arg))
|
36
|
+
add_method("not", method(:no))
|
37
|
+
add_method("damaged", method(:damaged))
|
38
|
+
add_block("block_add"){ |a, b|
|
39
|
+
a + b
|
40
|
+
}
|
41
|
+
add_block("block_raise"){ |a, b|
|
42
|
+
}
|
43
|
+
end
|
44
|
+
def get_response
|
45
|
+
@data
|
46
|
+
end
|
47
|
+
def send_request(patern)
|
48
|
+
@data = execute(patern)
|
49
|
+
end
|
50
|
+
def close()
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
#test the simple server request
|
55
|
+
def test_simple_request
|
56
|
+
client = TestClientOK.new()
|
57
|
+
assert_equal("no arg", client.request("no"))
|
58
|
+
assert_equal("one arg", client.request("one", "one arg"))
|
59
|
+
assert_equal(2, client.request("two", 1, 2))
|
60
|
+
assert_equal(3.0, client.request("three", 1.0, 2.0, 3.0))
|
61
|
+
client.close()
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_simple_notification
|
65
|
+
sleep(0.2)
|
66
|
+
client = TestClientOK.new()
|
67
|
+
client.notification("not")
|
68
|
+
client.close()
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_simple_name_request
|
72
|
+
client = TestClientOK.new()
|
73
|
+
assert_equal({"a" => "abc", "b" => [1,2,3]},
|
74
|
+
client.request("one", {:a => "abc", :b => [1,2,3]}))
|
75
|
+
assert_equal("no arg", client.no)
|
76
|
+
assert_equal("a", client.one(:a))
|
77
|
+
assert_equal(3.0, client.two(1.0, 3.0))
|
78
|
+
assert_equal(3, client.three(1, 2, 3))
|
79
|
+
client.close()
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_block
|
83
|
+
client = TestClientOK.new()
|
84
|
+
assert_equal(4, client.block_add(2,2))
|
85
|
+
client.close()
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_simple_exception
|
89
|
+
client = TestClientOK.new()
|
90
|
+
assert_raise(RpcError){
|
91
|
+
#no function
|
92
|
+
client.alpha
|
93
|
+
}
|
94
|
+
assert_raise(RpcError){
|
95
|
+
#wrong arguments
|
96
|
+
client.request("two", "alpha")
|
97
|
+
}
|
98
|
+
assert_raise(RpcError){
|
99
|
+
#call damaged function
|
100
|
+
client.damaged
|
101
|
+
}
|
102
|
+
=begin doesnt work because blocks can't check arguments errors!!
|
103
|
+
assert_raise(RpcError){
|
104
|
+
client.block_raise
|
105
|
+
}
|
106
|
+
=end
|
107
|
+
client.close()
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rawjsonrpc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Schrode
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &79053450 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *79053450
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
requirement: &79052420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.6.4
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *79052420
|
36
|
+
description: A basic libary for json rpc that allow you to easly create one for your
|
37
|
+
streamtype .It includes tcp json rpc on socket connections. Version for single clients
|
38
|
+
server, one for multiple clients server based on gserver are served and a TCP client.
|
39
|
+
email: midix01@googlemail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- .document
|
47
|
+
- Gemfile
|
48
|
+
- Gemfile.lock
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- lib/rawjsonrpc.rb
|
54
|
+
- lib/rawjsonrpc/client.rb
|
55
|
+
- lib/rawjsonrpc/error.rb
|
56
|
+
- lib/rawjsonrpc/server.rb
|
57
|
+
- test/helper.rb
|
58
|
+
- test/test_rawjsonrpc.rb
|
59
|
+
homepage: http://github.com/schroder-/rawjsonrpc
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
hash: 211403905
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.11
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A basic libary for json rpc that allow to create for your stream type. It
|
87
|
+
includes a TCPIp Server and Client.
|
88
|
+
test_files: []
|