jsonrpc2.0-tcp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jsonrpc2.0-tcp'
3
+ s.version = '0.1.0'
4
+ s.summary = 'JsonRpc 2.0 TCP client and server'
5
+ s.authors = ['Jannik Theiß']
6
+ s.email = 'dev@coldsun.org'
7
+ s.homepage = 'https://github.com/jtee/jsonrpc2.0-tpc'
8
+ s.files = ['jsonrpc2.0-tcp.gemspec', 'lib/jsonrpc2.0-tcp.rb', 'lib/jsonrpc2.0-tcp/Server.rb', 'lib/jsonrpc2.0-tcp/Client.rb', 'lib/jsonrpc2.0-tcp/ConnectionThread.rb']
9
+ s.required_ruby_version = '>= 1.9.2'
10
+ s.add_dependency('jsonrpc2.0', '>= 0.1.0')
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'socket'
2
+ require 'jsonrpc2.0/client'
3
+
4
+ class JsonRpcTcpClient < JsonRpcClient
5
+ def initialize(hostname, port)
6
+ @socket = TCPSocket.new(hostname, port)
7
+ end
8
+
9
+ def close
10
+ @socket.close
11
+ end
12
+
13
+
14
+ private
15
+
16
+ def send_request(request)
17
+ @socket.puts request
18
+ @socket.gets
19
+ end
20
+ end
21
+
22
+ if __FILE__ == $0
23
+ cl = JsonRpcTcpClient.new('localhost', 60006)
24
+ cl.r_add(22, 33)
25
+ end
@@ -0,0 +1,29 @@
1
+ class ConnectionThread < Thread
2
+ attr_accessor :loginMethod, :timeout
3
+ attr_reader :restrict
4
+
5
+ def loginMethod=(method)
6
+ @loginMethod = method
7
+ @restrict = method
8
+ end
9
+
10
+ def login
11
+ @restrict = nil
12
+ end
13
+
14
+ def start_timeout(timeout)
15
+ @timeoutThread.exit unless @timeoutThread == nil
16
+
17
+ @timeoutThread = Thread.start(self, @timeout) do |thread, timeout|
18
+ sleep timeout
19
+ thread.exit
20
+ end
21
+ end
22
+
23
+ def stop_timeout
24
+ unless @timeoutThread == nil
25
+ @timeoutThread.exit
26
+ @timeoutThread = nil
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,72 @@
1
+ require 'socket'
2
+ require 'jsonrpc2.0/server'
3
+ require_relative './ConnectionThread.rb'
4
+
5
+ class JsonRpcTcpServer < JsonRpcServer
6
+ def initialize(port, opts = {})
7
+ @loginMethod = opts['loginMethod']
8
+ @shutdownTimeout = opts['shutdownTimeout']
9
+
10
+ @server = TCPServer.new('localhost', port)
11
+ @runLoop = nil
12
+ @connections = nil
13
+ end
14
+
15
+ def run()
16
+ if @runLoop == nil
17
+ @connections = ThreadGroup.new
18
+
19
+ @runLoop = Thread.start do
20
+ loop do
21
+ thr = ConnectionThread.start(@server.accept) do |conn|
22
+ selfThread = Thread.current
23
+ selfThread.loginMethod = @loginMethod
24
+
25
+ while @runLoop != nil and (message = conn.gets) != nil
26
+ selfThread.stop_timeout
27
+ conn.puts handle_message(message, selfThread.restrict)
28
+ end
29
+
30
+ conn.close
31
+ end
32
+
33
+ @connections.add thr
34
+ end
35
+ end
36
+
37
+ @runLoop.join
38
+ @runLoop = nil
39
+
40
+ @connections.list.each do |conn|
41
+ conn.join
42
+ end
43
+ @connections = nil
44
+ end
45
+ end
46
+
47
+ def login
48
+ Thread.current.login
49
+ end
50
+
51
+ def stop
52
+ @runLoop.exit
53
+ @runLoop = nil
54
+
55
+ unless @shutdownTimeout == nil
56
+ @connections.list.each do |conn|
57
+ conn.start_timeout(@shutdownTimeout)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ if __FILE__ == $0
64
+ srv = JsonRpcTcpServer.new(60042)
65
+
66
+ Thread.start do
67
+ gets
68
+ srv.stop
69
+ end
70
+
71
+ srv.run
72
+ end
@@ -0,0 +1,2 @@
1
+ require_relative './jsonrpc2.0-tcp/Client.rb'
2
+ require_relative './jsonrpc2.0-tcp/Server.rb'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonrpc2.0-tcp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jannik Theiß
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-27 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jsonrpc2.0
16
+ requirement: &2153514260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153514260
25
+ description:
26
+ email: dev@coldsun.org
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - jsonrpc2.0-tcp.gemspec
32
+ - lib/jsonrpc2.0-tcp.rb
33
+ - lib/jsonrpc2.0-tcp/Server.rb
34
+ - lib/jsonrpc2.0-tcp/Client.rb
35
+ - lib/jsonrpc2.0-tcp/ConnectionThread.rb
36
+ homepage: https://github.com/jtee/jsonrpc2.0-tpc
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.9.2
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: JsonRpc 2.0 TCP client and server
60
+ test_files: []