qrpc 0.4.0 → 0.9.0

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.
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
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "qrpc/protocol/exception-data"
3
5
 
4
6
  ##
@@ -24,51 +26,20 @@ module QRPC
24
26
  #
25
27
 
26
28
  class Exception < ::Exception
27
-
28
- ##
29
- # Gets one from protocol exception object.
30
- # Returns marshalled original or reconstructed version.
31
- #
32
- # @param [JsonRpcObjects::Generic::Error] proto JSON-RPC error object
33
- # @return [Exception, QRPC::Client::Exception] exception according
34
- # to sucessfullness of the unmarshaling
35
- #
36
-
37
- def self.get(proto)
38
-
39
- # Converts protocol exception to exception data object
40
- proto = QRPC::Protocol::ExceptionData::new(proto.data)
41
-
42
- # Tries to unmarshall
43
- if proto.dump.format == :ruby
44
- begin
45
- exception = Marshal.load(Base64.decode64(proto.dump.raw))
46
- rescue
47
- # pass
48
- end
49
- end
50
-
51
- # If unsuccessfull, creates from data
52
- if exception.nil?
53
- exception = self::new(proto)
54
- end
55
-
56
- return exception
57
- end
58
29
 
59
30
  ##
60
31
  # Constructor.
61
32
  # Initializes from protocol exception data object.
62
33
  #
63
- # @param [QRPC::Protocol::ExceptionData] data exception data object
34
+ # @param [String] name a exception name
35
+ # @param [String] message a exception message
36
+ # @param [Array] backtrace the backtrace array
64
37
  #
65
38
 
66
- def initialize(data)
67
- message = data.name.dup << ": " << data.message
68
- backtrace = data.backtrace.map { |i| Base64.decode64(i) }
69
- backtrace.reject! { |i| i.empty? }
70
-
39
+ def initialize(name, message, backtrace = [ ])
40
+ message = name + ": " + message
71
41
  super(message)
42
+
72
43
  self.set_backtrace(backtrace)
73
44
  end
74
45
 
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
2
4
  require "uuid"
3
5
  require "qrpc/protocol/request"
4
6
  require "qrpc/client/exception"
@@ -64,6 +66,20 @@ module QRPC
64
66
 
65
67
  @priority
66
68
 
69
+ ##
70
+ # Protocol.
71
+ # @since 0.9.0
72
+ #
73
+
74
+ @protocol
75
+
76
+ ##
77
+ # ID generator.
78
+ # @since 0.9.0
79
+ #
80
+
81
+ @generator
82
+
67
83
  ##
68
84
  # Job result.
69
85
  #
@@ -77,15 +93,19 @@ module QRPC
77
93
  # @param [Symbol] method job method name
78
94
  # @param [Array] arguments array of arguments for job
79
95
  # @param [Integer] priority job priority
96
+ # @param [QRPC::Generator] ID generator
97
+ # @param [QRPC::Protocol::Abstract] protocol protocol instance
80
98
  # @param [Proc] callback result callback
81
99
  #
82
100
 
83
- def initialize(client_id, method, arguments = [ ], priority = QRPC::DEFAULT_PRIORITY, &callback)
101
+ def initialize(client_id, method, arguments = [ ], priority = QRPC::DEFAULT_PRIORITY, generator = QRPC::default_generator, protocol = QRPC::default_protocol, &callback)
84
102
  @client_id = client_id
85
103
  @method = method
86
104
  @arguments = arguments
87
105
  @callback = callback
88
106
  @priority = priority
107
+ @protocol = protocol
108
+ @generator = generator
89
109
  end
90
110
 
91
111
  ##
@@ -95,21 +115,44 @@ module QRPC
95
115
 
96
116
  def id
97
117
  if @id.nil?
98
- @id = UUID.generate(:compact).to_sym
118
+ @id = @generator.generate(self)
119
+ else
120
+ @id
99
121
  end
100
-
101
- return @id
102
122
  end
103
123
 
104
124
  ##
105
125
  # Converts job to JSON.
126
+ #
106
127
  # @return [String] job in JSON form (JSON-RPC)
128
+ # @deprecated (since 0.4.0) Use +#serialize+.
129
+ # @see #serialize
107
130
  #
108
131
 
109
132
  def to_json
110
- QRPC::Protocol::Request::create(@client_id, @id, @method, @arguments, @priority).to_json
133
+ JsonRpc::Request::create(@client_id, @id, @method, @arguments, @priority).to_json
111
134
  end
135
+
136
+ ##
137
+ # Serializes job using serializer.
138
+ #
139
+ # @return [Object] serialized object
140
+ # @since 0.4.0
141
+ #
112
142
 
143
+ def serialize
144
+ options = {
145
+ :client_id => @client_id,
146
+ :id => @id,
147
+ :method => @method,
148
+ :arguments => @arguments,
149
+ :priority => @priority,
150
+ :notification => self.notification?
151
+ }
152
+
153
+ @protocol.request::new(options).serialize
154
+ end
155
+
113
156
  ##
114
157
  # Indicates message is notification. So callback isn't set
115
158
  # and it doesn't expect any result.
@@ -130,17 +173,7 @@ module QRPC
130
173
  if not result.error?
131
174
  @result = result.result
132
175
  else
133
- =begin
134
- STDERR.write(">>> Exception while call ID: " << self.id.to_s << "\n")
135
- =end
136
- exception = QRPC::Client::Exception::get(result.error)
137
- =begin
138
- STDERR.write exception.class.name.dup << ": " << exception.message << "\n"
139
-
140
- exception.backtrace.each do |i|
141
- STDERR.write " from " << i << "\n"
142
- end
143
- =end
176
+ exception = result.error
144
177
  raise exception
145
178
  end
146
179
 
data/lib/qrpc/general.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "qrpc/generator/uuid"
2
5
 
3
6
  ##
4
7
  # General QRPC module.
@@ -33,5 +36,62 @@ module QRPC
33
36
  #
34
37
 
35
38
  DEFAULT_PRIORITY = 50
36
-
39
+
40
+ ##
41
+ # Holds default generator module link.
42
+ # @since 0.9.0
43
+ #
44
+
45
+ DEFAULT_GENERATOR = QRPC::Generator::UUID
46
+
47
+ ##
48
+ # Holds default protocol instance.
49
+ # @since 0.4.0
50
+ #
51
+
52
+ @@default_protocol = nil
53
+
54
+ ##
55
+ # Holds default generator instance.
56
+ # @since 0.9.0
57
+ #
58
+
59
+ @@default_generator = nil
60
+
61
+ ##
62
+ # Returns default protocol instance.
63
+ #
64
+ # @return [QRPC::Protocol::Abstract] protocol instance
65
+ # @since 0.9.0
66
+ #
67
+
68
+ def self.default_protocol
69
+ if @@default_protocol.nil?
70
+ begin
71
+ @@default_protocol = QRPC::Protocol::JsonRpc::new(:serializer => JsonRpcObjects::Serializer::JSON::new)
72
+ rescue NameError
73
+ require "json-rpc-objects/serializer/json" # >= 0.4.1
74
+ require "qrpc/protocol/json-rpc"
75
+ retry
76
+ end
77
+ else
78
+ @@default_protocol
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Returns default generator instance.
84
+ #
85
+ # @return [QRPC::Generator::UUID] generator instance
86
+ # @since 0.9.0
87
+ #
88
+
89
+ def self.default_generator
90
+ if @@default_generator.nil?
91
+ @@default_generator = QRPC::DEFAULT_GENERATOR::new
92
+ else
93
+ @@default_generator
94
+ end
95
+ end
96
+
37
97
  end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "hash-utils/object" # >= 1.1.0
5
+
6
+ ##
7
+ # General QRPC module.
8
+ #
9
+
10
+ module QRPC
11
+
12
+ ##
13
+ # General generators module.
14
+ # @since 0.9.0
15
+ #
16
+
17
+ module Generator
18
+
19
+ ##
20
+ # Ruby object ID generator.
21
+ # @since 0.9.0
22
+ #
23
+
24
+ class ObjectID
25
+
26
+ ##
27
+ # Generates Ruby object ID as ID for given object.
28
+ #
29
+ # @param [Object] object required object
30
+ # @return [Symbol] new ID
31
+ #
32
+
33
+ def generate(object)
34
+ object.object_id
35
+ end
36
+
37
+ alias :generate! :generate
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "uuid"
5
+
6
+ ##
7
+ # General QRPC module.
8
+ #
9
+
10
+ module QRPC
11
+
12
+ ##
13
+ # General generators module.
14
+ #
15
+
16
+ module Generator
17
+
18
+ ##
19
+ # UUID generator.
20
+ # @since 0.9.0
21
+ #
22
+
23
+ class UUID
24
+
25
+ ##
26
+ # Generates UUID as ID for given object.
27
+ #
28
+ # @param [Object] object required object
29
+ # @return [String] new ID
30
+ # @since 0.9.0
31
+ #
32
+
33
+ def generate(object = nil)
34
+ ::UUID.generate(:compact).to_sym
35
+ end
36
+
37
+ alias :generate! :generate
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
data/lib/qrpc/locator.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
3
 
4
+ require "qrpc/locator/em-jack"
3
5
 
4
6
  ##
5
7
  # General QRPC module.
@@ -9,96 +11,20 @@ module QRPC
9
11
 
10
12
  ##
11
13
  # Resource locator.
14
+ # @deprecated (since 0.9.0) in favour to locators for each queue types
12
15
  #
13
16
 
14
- class Locator
15
-
16
- ##
17
- # Contains queue name.
18
- # @return [Symbol]
19
- #
20
-
21
- attr_accessor :queue
22
- @queue
23
-
24
- ##
25
- # Contains host.
26
- # @return [String]
27
- #
28
-
29
- attr_accessor :host
30
- @host
31
-
32
- ##
33
- # Contains port.
34
- # @return [Integer]
35
- #
36
-
37
- attr_accessor :port
38
- @port
39
-
40
- ##
41
- # Parser.
42
- #
43
-
44
- PARSER = /^(.+)@(.+)(?:\:(\d+))?$/
45
-
46
- ##
47
- # Default port.
48
- #
49
-
50
- DEFAULT_PORT = 11300
51
-
52
- ##
53
- # Constructor.
54
- #
55
- # @param [String, Symbol] queue queue name
56
- # @param [String] host host name
57
- # @param [Integer] port port of the host
58
- #
59
-
60
- def initialize(queue, host = "localhost", port = 11300)
61
- @queue = queue.to_s
62
- @host = host
63
- @port = port
64
- end
65
-
17
+ module Locator
18
+
66
19
  ##
67
- # Parses the locator.
68
- # Excpects form +<queue>@<host>:<port>+. Port is optional.
20
+ # Returns new instance of {EMJackLocator}.
21
+ # @returns [EMJackLocator]
69
22
  #
70
- # @param [String] string locator in string form
71
- # @return [QRPC::Locator] new instance
72
- #
73
-
74
- def self.parse(string)
75
- match = string.match(self::PARSER)
76
-
77
- queue = match[1]
78
- host = match[2]
79
-
80
- if match.length == 3
81
- port = match[3]
82
- else
83
- port = self::DEFAULT_PORT
84
- end
85
-
86
- port = port.to_i
87
-
88
- ##
89
-
90
- return self::new(queue, host, port);
91
- end
92
-
93
- ##
94
- # Converts back to string.
95
- # @return [String] locator in string form
96
- #
97
-
98
- def to_s
99
- @queue.dup << "@" << @host << ":" << @port.to_s
23
+
24
+ def self.new(*args, &block)
25
+ EMJackLocator::new(*args, &block)
100
26
  end
101
27
 
102
28
  end
103
-
29
+
104
30
  end