rjr 0.18.2 → 0.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/bin/rjr-client +16 -9
  4. data/bin/rjr-server +2 -1
  5. data/examples/client.rb +21 -19
  6. data/examples/server.rb +1 -1
  7. data/examples/structured_server.rb +1 -0
  8. data/examples/tcp.rb +1 -0
  9. data/lib/rjr/common.rb +1 -226
  10. data/lib/rjr/core_ext.rb +63 -0
  11. data/lib/rjr/dispatcher.rb +75 -219
  12. data/lib/rjr/messages.rb +8 -0
  13. data/lib/rjr/messages/compressed.rb +264 -0
  14. data/lib/rjr/messages/notification.rb +95 -0
  15. data/lib/rjr/messages/request.rb +99 -0
  16. data/lib/rjr/messages/response.rb +128 -0
  17. data/lib/rjr/node.rb +100 -97
  18. data/lib/rjr/node_callback.rb +43 -0
  19. data/lib/rjr/nodes/amqp.rb +12 -11
  20. data/lib/rjr/nodes/easy.rb +4 -4
  21. data/lib/rjr/nodes/local.rb +13 -12
  22. data/lib/rjr/nodes/multi.rb +1 -1
  23. data/lib/rjr/nodes/tcp.rb +15 -13
  24. data/lib/rjr/nodes/template.rb +4 -4
  25. data/lib/rjr/nodes/unix.rb +15 -13
  26. data/lib/rjr/nodes/web.rb +15 -14
  27. data/lib/rjr/nodes/ws.rb +12 -11
  28. data/lib/rjr/request.rb +128 -0
  29. data/lib/rjr/result.rb +75 -0
  30. data/lib/rjr/util/args.rb +145 -0
  31. data/lib/rjr/{em_adapter.rb → util/em_adapter.rb} +0 -0
  32. data/lib/rjr/util/handles_methods.rb +115 -0
  33. data/lib/rjr/util/has_messages.rb +50 -0
  34. data/lib/rjr/{inspect.rb → util/inspect.rb} +1 -1
  35. data/lib/rjr/util/json_parser.rb +101 -0
  36. data/lib/rjr/util/logger.rb +128 -0
  37. data/lib/rjr/{thread_pool.rb → util/thread_pool.rb} +2 -0
  38. data/lib/rjr/version.rb +1 -1
  39. data/site/jrw.js +1 -1
  40. data/specs/args_spec.rb +144 -0
  41. data/specs/dispatcher_spec.rb +399 -211
  42. data/specs/em_adapter_spec.rb +31 -18
  43. data/specs/handles_methods_spec.rb +154 -0
  44. data/specs/has_messages_spec.rb +54 -0
  45. data/specs/inspect_spec.rb +1 -1
  46. data/specs/json_parser_spec.rb +169 -0
  47. data/specs/messages/notification_spec.rb +59 -0
  48. data/specs/messages/request_spec.rb +66 -0
  49. data/specs/messages/response_spec.rb +94 -0
  50. data/specs/node_callbacks_spec.rb +47 -0
  51. data/specs/node_spec.rb +465 -56
  52. data/specs/request_spec.rb +147 -0
  53. data/specs/result_spec.rb +144 -0
  54. data/specs/thread_pool_spec.rb +1 -1
  55. metadata +41 -11
  56. data/lib/rjr/errors.rb +0 -23
  57. data/lib/rjr/message.rb +0 -351
  58. data/lib/rjr/semaphore.rb +0 -58
  59. data/specs/message_spec.rb +0 -229
@@ -1,58 +0,0 @@
1
- #
2
- # $Id: semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp $
3
- #
4
- # Copied unmodified from:
5
- # http://www.imasy.or.jp/~fukumoto/ruby/semaphore.rb
6
- # Originally licensed under The Ruby License:
7
- # http://raa.ruby-lang.org/project/semaphore/
8
-
9
- class CountingSemaphore
10
-
11
- def initialize(initvalue = 0)
12
- @counter = initvalue
13
- @waiting_list = []
14
- end
15
-
16
- def wait
17
- Thread.critical = true
18
- if (@counter -= 1) < 0
19
- @waiting_list.push(Thread.current)
20
- Thread.stop
21
- end
22
- self
23
- ensure
24
- Thread.critical = false
25
- end
26
-
27
- def signal
28
- Thread.critical = true
29
- begin
30
- if (@counter += 1) <= 0
31
- t = @waiting_list.shift
32
- t.wakeup if t
33
- end
34
- rescue ThreadError
35
- retry
36
- end
37
- self
38
- ensure
39
- Thread.critical = false
40
- end
41
-
42
- alias down wait
43
- alias up signal
44
- alias P wait
45
- alias V signal
46
-
47
- def exclusive
48
- wait
49
- yield
50
- ensure
51
- signal
52
- end
53
-
54
- alias synchronize exclusive
55
-
56
- end
57
-
58
- Semaphore = CountingSemaphore
@@ -1,229 +0,0 @@
1
- require 'rjr/dispatcher'
2
- require 'rjr/message'
3
-
4
- module RJR
5
- describe RequestMessage do
6
- it "should accept request parameters" do
7
- msg = RequestMessage.new :method => 'test',
8
- :args => ['a', 1],
9
- :headers => {:h => 2}
10
- msg.jr_method.should == "test"
11
- msg.jr_args.should =~ ['a', 1]
12
- msg.headers.should have_key(:h)
13
- msg.headers[:h].should == 2
14
- msg.msg_id.should =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
15
- end
16
-
17
- it "should return bool indicating if string is a request msg"
18
-
19
- it "should produce valid json" do
20
- msg = RequestMessage.new :method => 'test',
21
- :args => ['a', 1],
22
- :headers => {:h => 2}
23
-
24
- msg_string = msg.to_s
25
- msg_string.should include('"h":2')
26
- msg_string.should include('"method":"test"')
27
- msg_string.should include('"params":["a",1]')
28
- msg_string.should include('"jsonrpc":"2.0"')
29
- msg_string.should include('"id":"'+msg.msg_id+'"')
30
- end
31
-
32
- it "should parse request message string" do
33
- msg_uuid = gen_uuid
34
- msg_string = '{"jsonrpc": "2.0", ' +
35
- '"id": "' + msg_uuid + '", ' +
36
- '"method": "test", "params": ["a", 1]}'
37
- msg = RequestMessage.new :message => msg_string
38
- msg.json_message.should == msg_string
39
- msg.jr_method.should == 'test'
40
- msg.jr_args.should =~ ['a', 1]
41
- msg.msg_id.should == msg_uuid
42
- end
43
-
44
- it "should extract optional headers from message string" do
45
- msg_uuid = gen_uuid
46
- msg_string = '{"jsonrpc": "2.0", ' +
47
- '"id": "' + msg_uuid + '", ' +
48
- '"method": "test", "params": ["a", 1], ' +
49
- '"h": 2}'
50
- msg = RequestMessage.new :message => msg_string, :headers => {'f' => 'g'}
51
- msg.json_message.should == msg_string
52
- msg.headers.should have_key 'h'
53
- msg.headers.should have_key 'f'
54
- msg.headers['h'].should == 2
55
- msg.headers['f'].should == 'g'
56
- end
57
-
58
- it "should fail if parsing invalid message string" do
59
- lambda {
60
- msg = RequestMessage.new :message => 'foobar'
61
- }.should raise_error JSON::ParserError
62
- end
63
-
64
- end
65
-
66
- describe ResponseMessage do
67
- it "should accept response parameters" do
68
- msg_id = gen_uuid
69
- msg = ResponseMessage.new :id => msg_id,
70
- :result => Result.new(:result => 'success'),
71
- :headers => {:h => 2}
72
- msg.msg_id.should == msg_id
73
- msg.result.result == 'success'
74
- msg.headers.should have_key(:h)
75
- msg.headers[:h].should == 2
76
- end
77
-
78
- it "should return bool indicating if string is a response msg"
79
-
80
- it "should produce valid result response json" do
81
- msg_id = gen_uuid
82
- msg = ResponseMessage.new :id => msg_id,
83
- :result => RJR::Result.new(:result => 'success'),
84
- :headers => {:h => 2}
85
- msg_string = msg.to_s
86
- msg_string.should include('"id":"'+msg_id+'"')
87
- msg_string.should include('"result":"success"')
88
- msg_string.should include('"h":2')
89
- msg_string.should include('"jsonrpc":"2.0"')
90
- end
91
-
92
- it "should produce valid error response json" do
93
- msg_id = gen_uuid
94
- msg = ResponseMessage.new :id => msg_id,
95
- :result =>
96
- RJR::Result.new(:error_code => 404,
97
- :error_msg => 'not found',
98
- :error_class => ArgumentError),
99
- :headers => {:h => 2}
100
- msg_string = msg.to_s
101
- msg_string.should include('"id":"'+msg_id+'"')
102
- msg_string.should include('"h":2')
103
- msg_string.should include('"jsonrpc":"2.0"')
104
- msg_string.should include('"error":{')
105
- msg_string.should include('"code":404')
106
- msg_string.should include('"message":"not found"')
107
- end
108
-
109
-
110
- it "should parse result response message string" do
111
- msg_id = gen_uuid
112
- msg_string = '{"id":"' + msg_id + '", ' +
113
- '"result":"success","jsonrpc":"2.0"}'
114
- msg = ResponseMessage.new :message => msg_string
115
- msg.json_message.should == msg_string
116
- msg.msg_id.should == msg_id
117
- msg.result.success.should == true
118
- msg.result.failed.should == false
119
- msg.result.result.should == "success"
120
- end
121
-
122
- it "should parse error response message string" do
123
- msg_id = gen_uuid
124
- msg_string = '{"id":"' + msg_id + '", ' +
125
- '"error":{"code":404,"message":"not found","class":"ArgumentError"}, "jsonrpc":"2.0"}'
126
- msg = ResponseMessage.new :message => msg_string
127
- msg.json_message.should == msg_string
128
- msg.msg_id.should == msg_id
129
- msg.result.success.should == false
130
- msg.result.failed.should == true
131
- msg.result.error_code.should == 404
132
- msg.result.error_msg.should == "not found"
133
- msg.result.error_class.should == 'ArgumentError'
134
- end
135
-
136
- it "should extract optional headers from message string" do
137
- msg_id = gen_uuid
138
- msg_string = '{"id":"' + msg_id + '", ' +
139
- '"result":"success","h":2,"jsonrpc":"2.0"}'
140
- msg = ResponseMessage.new :message => msg_string
141
- msg.json_message.should == msg_string
142
- msg.headers.should have_key 'h'
143
- msg.headers['h'].should == 2
144
- end
145
-
146
- it "should fail if parsing invalid message string" do
147
- lambda {
148
- msg = ResponseMessage.new :message => 'foobar'
149
- }.should raise_error JSON::ParserError
150
- end
151
- end
152
-
153
- describe NotificationMessage do
154
- it "should accept notification parameters" do
155
- msg = NotificationMessage.new :method => 'test',
156
- :args => ['a', 1],
157
- :headers => {:h => 2}
158
- msg.jr_method.should == "test"
159
- msg.jr_args.should =~ ['a', 1]
160
- msg.headers.should have_key(:h)
161
- msg.headers[:h].should == 2
162
- end
163
-
164
- it "should return bool indicating if string is a notification msg"
165
-
166
- it "should produce valid json" do
167
- msg = NotificationMessage.new :method => 'test',
168
- :args => ['a', 1],
169
- :headers => {:h => 2}
170
-
171
- msg_string = msg.to_s
172
- msg_string.should include('"h":2')
173
- msg_string.should include('"method":"test"')
174
- msg_string.should include('"params":["a",1]')
175
- msg_string.should include('"jsonrpc":"2.0"')
176
- msg_string.should_not include('"id":"')
177
- end
178
-
179
- it "should parse notification message string" do
180
- msg_string = '{"jsonrpc": "2.0", ' +
181
- '"method": "test", "params": ["a", 1]}'
182
- msg = NotificationMessage.new :message => msg_string
183
- msg.json_message.should == msg_string
184
- msg.jr_method.should == 'test'
185
- msg.jr_args.should =~ ['a', 1]
186
- end
187
-
188
- it "should extract optional headers from message string" do
189
- msg_string = '{"jsonrpc": "2.0", ' +
190
- '"method": "test", "params": ["a", 1], ' +
191
- '"h": 2}'
192
- msg = NotificationMessage.new :message => msg_string, :headers => {'f' => 'g'}
193
- msg.json_message.should == msg_string
194
- msg.headers.should have_key 'h'
195
- msg.headers.should have_key 'f'
196
- msg.headers['h'].should == 2
197
- msg.headers['f'].should == 'g'
198
- end
199
-
200
- it "should fail if parsing invalid message string" do
201
- lambda {
202
- msg = NotificationMessage.new :message => 'foobar'
203
- }.should raise_error JSON::ParserError
204
- end
205
- end
206
-
207
- describe MessageUtil do
208
- after(:each) do
209
- MessageUtil.clear
210
- end
211
-
212
- it "should extract json messages out of a message stream"
213
-
214
- it "should store preformatted messages" do
215
- MessageUtil.message 'foobar', 'raboof'
216
- MessageUtil.message('foobar').should == 'raboof'
217
- end
218
-
219
- it "should clear preformatted messages" do
220
- MessageUtil.message 'foobar', 'raboof'
221
- MessageUtil.clear
222
- MessageUtil.message('foobar').should be_nil
223
- end
224
-
225
- it "should return rand preformatted message"
226
- it "should return rand preformatted message matching transport"
227
- end
228
-
229
- end