em-jsonrpc 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/em-jsonrpc.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "yajl"
2
+ require "securerandom"
3
+
4
+ require "em-jsonrpc/version"
5
+ require "em-jsonrpc/constants"
6
+ require "em-jsonrpc/client"
7
+ require "em-jsonrpc/server"
@@ -1,11 +1,3 @@
1
- require "eventmachine"
2
- require "yajl"
3
- require "securerandom"
4
-
5
- require "em-jsonrpc/version"
6
- require "em-jsonrpc/constants"
7
-
8
-
9
1
  module EventMachine::JsonRPC
10
2
 
11
3
  class Client < EM::Connection
@@ -193,9 +185,6 @@ module EventMachine::JsonRPC
193
185
  end # class Client
194
186
 
195
187
 
196
- class ConnectionError < StandardError ; end
197
-
198
-
199
188
  def self.connect_tcp(host, port, handler, request_timeout=nil, parser_options={}, &block)
200
189
  raise Error, "EventMachine is not running" unless EM.reactor_running?
201
190
 
@@ -1,18 +1,11 @@
1
- require "eventmachine"
2
- require "yajl"
3
-
4
- require "em-jsonrpc/version"
5
- require "em-jsonrpc/constants"
6
-
7
-
8
1
  module EventMachine::JsonRPC
9
2
 
10
3
  class Server < EM::Connection
11
4
  attr_reader :encoder
12
-
5
+
13
6
  def initialize(*options)
14
7
  parser_options = options.first || {}
15
-
8
+
16
9
  if parser_options[:symbolize_keys]
17
10
  @key_jsonrpc = :jsonrpc
18
11
  @key_id = :id
@@ -24,10 +17,10 @@ module EventMachine::JsonRPC
24
17
  @key_method = KEY_METHOD
25
18
  @key_params = KEY_PARAMS
26
19
  end
27
-
20
+
28
21
  @parser = Yajl::Parser.new parser_options
29
22
  @parser.on_parse_complete = method(:obj_parsed)
30
-
23
+
31
24
  @state = :data
32
25
  end
33
26
 
@@ -53,7 +46,7 @@ module EventMachine::JsonRPC
53
46
 
54
47
  def obj_parsed(obj)
55
48
  @encoder ||= Yajl::Encoder.new
56
-
49
+
57
50
  case obj
58
51
  # Individual request/notification.
59
52
  when Hash
@@ -79,19 +72,19 @@ module EventMachine::JsonRPC
79
72
  return false
80
73
  end
81
74
  end
82
-
75
+
83
76
  unless obj[@key_jsonrpc] == "2.0"
84
77
  invalid_request obj, CODE_INVALID_REQUEST, MSG_INVALID_REQ_JSONRPC
85
78
  reply_error id, CODE_INVALID_REQUEST, MSG_INVALID_REQ_JSONRPC
86
79
  return false
87
80
  end
88
-
81
+
89
82
  unless (method = obj[@key_method]).is_a? String
90
83
  invalid_request obj, CODE_INVALID_REQUEST, MSG_INVALID_REQ_METHOD
91
84
  reply_error id, CODE_INVALID_REQUEST, MSG_INVALID_REQ_METHOD
92
85
  return false
93
86
  end
94
-
87
+
95
88
  if (params = obj[@key_params])
96
89
  unless params.is_a? Array or params.is_a? Hash
97
90
  invalid_request obj, CODE_INVALID_REQUEST, MSG_INVALID_REQ_PARAMS
@@ -137,7 +130,7 @@ module EventMachine::JsonRPC
137
130
  def batch_not_supported_error(obj)
138
131
  $stderr.puts "batch request received but not implemented"
139
132
  end
140
-
133
+
141
134
  # This method could be overriden in the user's inherited class.
142
135
  def invalid_request(obj, code, message=nil)
143
136
  $stderr.puts "error #{code}: #{message}"
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
2
  module JsonRPC
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/ruby
2
+ # coding: utf-8
3
+
4
+ require "eventmachine-le"
5
+ require "em-jsonrpc"
6
+
7
+
8
+ class MyHandler < EM::JsonRPC::Client
9
+
10
+ def do_request
11
+ puts "--- my: do_request()"
12
+ request = send_request "subtract", [42, 23]
13
+ request.callback do |result|
14
+ puts "INFO: result = #{result.inspect}"
15
+ EM.add_timer(SecureRandom.random_number) { do_request }
16
+ end
17
+ request.errback do |error|
18
+ puts "ERROR: result = #{error.inspect}"
19
+ EM.add_timer(SecureRandom.random_number) { do_request }
20
+ end
21
+ end
22
+
23
+ def connection_failed
24
+ $stderr.puts "ERROR: connection failed"
25
+ exit 1
26
+ end
27
+
28
+ def connection_terminated
29
+ @connection_attemp ||= 0
30
+ @connection_attemp += 1
31
+ $stderr.puts "WARN: connection terminated, reconnecting (#{@connection_attemp} attemp)..."
32
+ EM.add_timer(0.5) { connect_again }
33
+ end
34
+
35
+ def parsing_error
36
+ $stderr.puts "ERROR: parsing error"
37
+ end
38
+
39
+ end
40
+
41
+
42
+ EM.set_max_timers 500000
43
+
44
+ EM.run do
45
+
46
+ yajl_options = { :symbolize_keys => true }
47
+ EM.connect "127.0.0.1", 8888, MyHandler, "127.0.0.1", 8888, nil, yajl_options do |conn|
48
+ conn.do_request
49
+ EM.add_periodic_timer(1) { puts "." }
50
+ EM.add_periodic_timer(4) { puts "DEBUG: #{conn.pending_requests.size} pending requests" ; sleep 0.5 }
51
+ end
52
+
53
+ end
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/ruby
2
2
  # coding: utf-8
3
3
 
4
- ### TMP
5
- $LOAD_PATH.insert 0, File.expand_path(File.join(File.dirname(__FILE__), "../", "lib"))
6
- require "em-jsonrpc/server"
4
+ require "eventmachine-le"
5
+ require "em-jsonrpc"
7
6
 
8
7
 
9
8
  class MyJsonRpcServer < EM::JsonRPC::Server
metadata CHANGED
@@ -1,98 +1,70 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: em-jsonrpc
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Inaki Baz Castillo
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-01-02 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: eventmachine
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yajl-ruby
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
31
22
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: yajl-ruby
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
37
25
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- - 8
44
- - 0
45
- version: 0.8.0
46
- type: :runtime
47
- version_requirements: *id002
48
- description: em-jsonrpc provides a JSON RPC 2.0 TCP/UnixSocket client and server to be integrated within EventMachine reactor
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ description: em-jsonrpc provides a JSON RPC 2.0 TCP/UnixSocket client and server to
31
+ be integrated within EventMachine reactor
49
32
  email: ibc@aliax.net
50
33
  executables: []
51
-
52
34
  extensions: []
53
-
54
35
  extra_rdoc_files: []
55
-
56
- files:
36
+ files:
37
+ - lib/em-jsonrpc.rb
57
38
  - lib/em-jsonrpc/version.rb
58
39
  - lib/em-jsonrpc/constants.rb
59
40
  - lib/em-jsonrpc/server.rb
60
41
  - lib/em-jsonrpc/client.rb
61
- - test/test-em-jsonrpc-client.rb
62
- - test/test-em-jsonrpc-server.rb
63
- has_rdoc: true
42
+ - test/em-jsonrpc-client.rb
43
+ - test/jsonrpc-client.rb
44
+ - test/em-jsonrpc-server.rb
64
45
  homepage: https://github.com/ibc/em-jsonrpc
65
46
  licenses: []
66
-
67
47
  post_install_message:
68
48
  rdoc_options: []
69
-
70
- require_paths:
49
+ require_paths:
71
50
  - lib
72
- required_ruby_version: !ruby/object:Gem::Requirement
51
+ required_ruby_version: !ruby/object:Gem::Requirement
73
52
  none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- segments:
78
- - 1
79
- - 8
80
- - 7
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
81
56
  version: 1.8.7
82
- required_rubygems_version: !ruby/object:Gem::Requirement
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
58
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
- version: "0"
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
90
63
  requirements: []
91
-
92
64
  rubyforge_project:
93
- rubygems_version: 1.3.7
65
+ rubygems_version: 1.8.23
94
66
  signing_key:
95
67
  specification_version: 3
96
68
  summary: JSON RCP 2.0 client and server for EventMachine over TCP or UnixSocket
97
69
  test_files: []
98
-
70
+ has_rdoc: false