qrpc 0.4.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGES.txt +9 -6
  2. data/Gemfile +10 -4
  3. data/Gemfile.lock +34 -12
  4. data/LICENSE.txt +1 -1
  5. data/README.md +92 -25
  6. data/Rakefile +2 -1
  7. data/TODO.md +1 -3
  8. data/VERSION +1 -1
  9. data/lib/qrpc/client.rb +13 -5
  10. data/lib/qrpc/client/dispatcher.rb +66 -50
  11. data/lib/qrpc/client/exception.rb +8 -37
  12. data/lib/qrpc/client/job.rb +49 -16
  13. data/lib/qrpc/general.rb +61 -1
  14. data/lib/qrpc/generator/object-id.rb +43 -0
  15. data/lib/qrpc/generator/uuid.rb +43 -0
  16. data/lib/qrpc/locator.rb +11 -85
  17. data/lib/qrpc/locator/em-jack.rb +160 -0
  18. data/lib/qrpc/locator/evented-queue.rb +101 -0
  19. data/lib/qrpc/protocol/abstract.rb +119 -0
  20. data/lib/qrpc/protocol/abstract/error.rb +54 -0
  21. data/lib/qrpc/protocol/abstract/object.rb +81 -0
  22. data/lib/qrpc/protocol/abstract/request.rb +126 -0
  23. data/lib/qrpc/protocol/abstract/response.rb +103 -0
  24. data/lib/qrpc/protocol/json-rpc.rb +32 -0
  25. data/lib/qrpc/protocol/json-rpc/error.rb +71 -0
  26. data/lib/qrpc/protocol/json-rpc/native/exception-data.rb +247 -0
  27. data/lib/qrpc/protocol/json-rpc/native/qrpc-object.rb +137 -0
  28. data/lib/qrpc/protocol/json-rpc/request.rb +140 -0
  29. data/lib/qrpc/protocol/json-rpc/response.rb +146 -0
  30. data/lib/qrpc/protocol/object.rb +32 -0
  31. data/lib/qrpc/protocol/object/error.rb +46 -0
  32. data/lib/qrpc/protocol/object/request.rb +111 -0
  33. data/lib/qrpc/protocol/object/response.rb +93 -0
  34. data/lib/qrpc/server.rb +63 -48
  35. data/lib/qrpc/server/dispatcher.rb +5 -107
  36. data/lib/qrpc/server/job.rb +69 -19
  37. data/qrpc.gemspec +55 -19
  38. data/test-both.rb +85 -0
  39. data/test-client.rb +36 -8
  40. data/test-server.rb +17 -12
  41. metadata +181 -31
  42. data/lib/qrpc/protocol/exception-data.rb +0 -227
  43. data/lib/qrpc/protocol/qrpc-object.rb +0 -103
  44. data/lib/qrpc/protocol/request.rb +0 -46
@@ -0,0 +1,137 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "json-rpc-objects/generic/object"
5
+ require "json-rpc-objects/request"
6
+ require "qrpc/protocol/json-rpc"
7
+ require "qrpc/general"
8
+ require "hash-utils"
9
+
10
+ ##
11
+ # General QRPC module.
12
+ #
13
+
14
+ module QRPC
15
+
16
+ ##
17
+ # JSON RPC helper module.
18
+ # @since 0.2.0
19
+ #
20
+
21
+ module Protocol
22
+
23
+ ##
24
+ # JSON-RPC protocol implementation.
25
+ # @since 0.9.0
26
+ #
27
+
28
+ class JsonRpc
29
+
30
+ ##
31
+ # Native JSON-RPC classes.
32
+ # @since 0.9.0
33
+ #
34
+
35
+ module Native
36
+
37
+ ##
38
+ # QRPC JSON-RPC QRPC object. Extends the
39
+ # +JsonRpcObjects::Generic::Object+. See its documentation for
40
+ # additional methods.
41
+ #
42
+ # @since 0.2.0
43
+ #
44
+
45
+ class QrpcObject < JsonRpcObjects::Generic::Object
46
+
47
+ ##
48
+ # Holds JSON-RPC version indication.
49
+ #
50
+
51
+ VERSION = JsonRpcObjects::Request::VERSION
52
+
53
+ ##
54
+ # Creates new QRPC JSON-RPC object.
55
+ #
56
+ # @param [Hash] QRPC object optional arguments
57
+ # @return [QRPC::Protocol::QrpcObject] new instance
58
+ #
59
+
60
+ def self.create(opts = { })
61
+ self::new(opts)
62
+ end
63
+
64
+ ##
65
+ # Checks correctness of the object data.
66
+ #
67
+
68
+ def check!
69
+ self.normalize!
70
+
71
+ if (not @priority.nil?) and not (@priority.kind_of? Numeric)
72
+ raise Exception::new("Priority is expected to be Numeric.")
73
+ end
74
+
75
+ if not (@notification.boolean?)
76
+ raise Exception::new("Notification is expected to be Boolean.")
77
+ end
78
+ end
79
+
80
+ ##
81
+ # Renders data to output form.
82
+ # @return [Hash] with data of object
83
+ #
84
+
85
+ def output
86
+ result = {
87
+ "version" => "1.0.2"
88
+ }
89
+
90
+ if not @priority.nil?
91
+ result["priority"] = @priority
92
+ end
93
+
94
+ if not @client.nil?
95
+ result["client"] = @client.to_s
96
+ end
97
+
98
+ if @notification.true?
99
+ result["notification"] = @notification
100
+ end
101
+
102
+ return result
103
+ end
104
+
105
+
106
+ protected
107
+
108
+ ##
109
+ # Assigns data.
110
+ #
111
+
112
+ def data=(value, mode = nil)
113
+ data = __convert_data(value, mode)
114
+
115
+ @priority = data[:priority]
116
+ @client = data[:client]
117
+ @notification = data[:notification]
118
+ end
119
+
120
+ ##
121
+ # Converts data to standard (defined) format.
122
+ #
123
+
124
+ def normalize!
125
+ if not @priority.nil?
126
+ @priority = @priority.to_i
127
+ end
128
+
129
+ if @notification.nil?
130
+ @notification = false
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "qrpc/general"
5
+ require "qrpc/protocol/abstract/request"
6
+ require "qrpc/protocol/json-rpc/native/qrpc-object"
7
+
8
+ ##
9
+ # General QRPC module.
10
+ #
11
+
12
+ module QRPC
13
+
14
+ ##
15
+ # Protocols helper module.
16
+ # @since 0.9.0
17
+ #
18
+
19
+ module Protocol
20
+
21
+ ##
22
+ # JSON-RPC protocol implementation.
23
+ # @since 0.9.0
24
+ #
25
+
26
+ class JsonRpc
27
+
28
+ ##
29
+ # JSON-RPC request implementation.
30
+ # @since 0.9.0
31
+ #
32
+
33
+ class Request < QRPC::Protocol::Abstract::Request
34
+
35
+ ##
36
+ # Holds native object.
37
+ #
38
+
39
+ attr_writer :native
40
+ @native
41
+
42
+ ##
43
+ # Parses the data for new object.
44
+ #
45
+ # @param [String] raw raw data
46
+ # @return [Request] new request according to data
47
+ #
48
+
49
+ def self.parse(raw)
50
+ object = self::new
51
+ object.native = JsonRpcObjects::Request::parse(raw, :wd, self::options.serializer)
52
+ return object
53
+ end
54
+
55
+
56
+ ##
57
+ # Returns the native object.
58
+ # @return [JsonRpcObjects::Response] native response object
59
+ #
60
+
61
+ def native
62
+ if @native.nil?
63
+ client_id = @options.client_id.to_s
64
+ qrpc = QRPC::Protocol::JsonRpc::Native::QrpcObject::create(:client => client_id, :priority => @options.priority, :notification => @options.notification)
65
+ qrpc.serializer = @options.serializer
66
+
67
+ @native = JsonRpcObjects::Request::create(@options[:method], @options.arguments, :id => @options.id, :qrpc => qrpc.output)
68
+ @native.serializer = @options.serializer
69
+ end
70
+
71
+ @native
72
+ end
73
+
74
+ ##
75
+ # Serializes object to the resultant form.
76
+ # @return [String] serialized form
77
+ #
78
+
79
+ def serialize
80
+ self.native.serialize
81
+ end
82
+
83
+ ##
84
+ # Returns ID of the request.
85
+ # @return [Object] request ID
86
+ #
87
+
88
+ def id
89
+ self.native.id
90
+ end
91
+
92
+ ##
93
+ # Returns method identifier of the request.
94
+ # @return [Symbol]
95
+ #
96
+
97
+ def method
98
+ @native.method
99
+ end
100
+
101
+ ##
102
+ # Returns method params of the request.
103
+ # @return [Array]
104
+ #
105
+
106
+ def params
107
+ @native.params
108
+ end
109
+
110
+ ##
111
+ # Returns the QRPC request priority.
112
+ # @return [Integer]
113
+ #
114
+
115
+ def priority
116
+ @native.qrpc["priority"]
117
+ end
118
+
119
+ ##
120
+ # Returns the QRPC request client identifier.
121
+ # @return [Object]
122
+ #
123
+
124
+ def client
125
+ @native.qrpc["client"]
126
+ end
127
+
128
+ ##
129
+ # Indicates, job is notification.
130
+ # @return [Boolean]
131
+ #
132
+
133
+ def notification?
134
+ @native.qrpc["notification"]
135
+ end
136
+
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "base64"
5
+
6
+ require "qrpc/general"
7
+ require "qrpc/protocol/abstract/response"
8
+ require "qrpc/protocol/json-rpc/native/qrpc-object"
9
+
10
+ ##
11
+ # General QRPC module.
12
+ #
13
+
14
+ module QRPC
15
+
16
+ ##
17
+ # Protocols helper module.
18
+ # @since 0.9.0
19
+ #
20
+
21
+ module Protocol
22
+
23
+ ##
24
+ # JSON-RPC protocol implementation.
25
+ # @since 0.9.0
26
+ #
27
+
28
+ class JsonRpc
29
+
30
+ ##
31
+ # JSON-RPC response implementation.
32
+ # @since 0.9.0
33
+ #
34
+
35
+ class Response < QRPC::Protocol::Abstract::Response
36
+
37
+ ##
38
+ # Holds native object.
39
+ #
40
+
41
+ attr_writer :native
42
+ @native
43
+
44
+ ##
45
+ # Parses the data for new object.
46
+ #
47
+ # @param [String] raw raw data
48
+ # @return [Response] new request according to data
49
+ #
50
+
51
+ def self.parse(raw)
52
+ object = self::new
53
+ object.native = JsonRpcObjects::Response::parse(raw, :wd, self::options.serializer)
54
+ return object
55
+ end
56
+
57
+ ##
58
+ # Returns the native object.
59
+ # @return [JsonRpcObjects::Response] native response object
60
+ #
61
+
62
+ def native
63
+ if @native.nil?
64
+ result = @options.result
65
+ error = @options.error
66
+ request = @options.request
67
+
68
+ error_native = error.nil? ? nil : error.native
69
+ @native = request.native.class::version.response::create(result, error_native, :id => request.id)
70
+ @native.serializer = @options.serializer
71
+ @native.qrpc = QRPC::Protocol::JsonRpc::Native::QrpcObject::create.output
72
+ end
73
+
74
+ @native
75
+ end
76
+
77
+ ##
78
+ # Serializes object to the resultant form.
79
+ # @return [String] serialized form
80
+ #
81
+
82
+ def serialize
83
+ self.native.serialize
84
+ end
85
+
86
+ ##
87
+ # Returns ID of the response.
88
+ # @return [Object] response ID
89
+ #
90
+
91
+ def id
92
+ self.native.id
93
+ end
94
+
95
+ ##
96
+ # Indicates, error state of the response.
97
+ # @return [Boolean] error indication
98
+ #
99
+
100
+ def error?
101
+ self.native.error?
102
+ end
103
+
104
+ ##
105
+ # Returns response error.
106
+ # @return [Exception] error object
107
+ #
108
+
109
+ def error
110
+
111
+ # Converts protocol exception to exception data object
112
+ proto = QRPC::Protocol::JsonRpc::Native::ExceptionData::new(native.error.data)
113
+
114
+ # Tries to unmarshall
115
+ if proto.dump.format == :ruby
116
+ begin
117
+ exception = Marshal.load(Base64.decode64(proto.dump.raw))
118
+ rescue
119
+ # pass
120
+ end
121
+ end
122
+
123
+ # If unsuccessfull, creates from data
124
+ if exception.nil?
125
+ backtrace = data.backtrace.map { |i| Base64.decode64(i) }
126
+ backtrace.reject! { |i| i.empty? }
127
+ exception = self::new(data.name, data.message, backtrace)
128
+ end
129
+
130
+ return exception
131
+
132
+ end
133
+
134
+ ##
135
+ # Returns response result.
136
+ # @return [Object] response result
137
+ #
138
+
139
+ def result
140
+ self.native.result
141
+ end
142
+
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ # (c) 2012 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "qrpc/protocol/abstract"
5
+
6
+ ##
7
+ # General QRPC module.
8
+ #
9
+
10
+ module QRPC
11
+
12
+ ##
13
+ # Protocols helper module.
14
+ # @since 0.9.0
15
+ #
16
+
17
+ module Protocol
18
+
19
+ ##
20
+ # Object protocol implementation.
21
+ # @since 0.9.0
22
+ #
23
+
24
+ class Object < Abstract
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ require "qrpc/protocol/object/error"
31
+ require "qrpc/protocol/object/request"
32
+ require "qrpc/protocol/object/response"