msgpack-rpc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/msgpack/rpc.rb +41 -3
- data/test/msgpack_rpc_test.rb +70 -34
- metadata +1 -1
data/Rakefile
CHANGED
@@ -17,7 +17,7 @@ DESCRIPTION = "RPC library using MessagePack, a ainary-based efficient dat
|
|
17
17
|
RUBYFORGE_PROJECT = "msgpack"
|
18
18
|
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
19
19
|
BIN_FILES = %w( )
|
20
|
-
VERS = "0.1.
|
20
|
+
VERS = "0.1.1"
|
21
21
|
|
22
22
|
#REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
23
23
|
REV = nil
|
data/lib/msgpack/rpc.rb
CHANGED
@@ -282,6 +282,39 @@ class ClientSession
|
|
282
282
|
end
|
283
283
|
|
284
284
|
|
285
|
+
class AsyncResult
|
286
|
+
def initialize
|
287
|
+
@responder = nil
|
288
|
+
@sent = false
|
289
|
+
end
|
290
|
+
|
291
|
+
def result(retval, err = nil)
|
292
|
+
unless @sent
|
293
|
+
if @responder
|
294
|
+
@responder.result(retval, err)
|
295
|
+
else
|
296
|
+
@result = [retval, err]
|
297
|
+
end
|
298
|
+
@sent = true
|
299
|
+
end
|
300
|
+
nil
|
301
|
+
end
|
302
|
+
|
303
|
+
def error(err)
|
304
|
+
result(nil, err)
|
305
|
+
nil
|
306
|
+
end
|
307
|
+
|
308
|
+
def responder=(res)
|
309
|
+
@responder = res
|
310
|
+
if @sent && @result
|
311
|
+
@responder.result(*@result)
|
312
|
+
@result = nil
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
|
285
318
|
class ServerSession
|
286
319
|
def initialize(obj, accept = obj.public_methods)
|
287
320
|
@obj = obj
|
@@ -302,7 +335,11 @@ class ServerSession
|
|
302
335
|
res.error($!.to_s)
|
303
336
|
return
|
304
337
|
end
|
305
|
-
|
338
|
+
if ret.is_a?(AsyncResult)
|
339
|
+
ret.responder = res
|
340
|
+
else
|
341
|
+
res.result(ret)
|
342
|
+
end
|
306
343
|
end
|
307
344
|
|
308
345
|
def on_notify(method, param)
|
@@ -323,6 +360,8 @@ end
|
|
323
360
|
Loop = ::Rev::Loop
|
324
361
|
|
325
362
|
module LoopUtil
|
363
|
+
attr_reader :loop
|
364
|
+
|
326
365
|
class Timer < Rev::TimerWatcher
|
327
366
|
def initialize(interval, repeating, &block)
|
328
367
|
@block = block
|
@@ -361,7 +400,7 @@ class Client
|
|
361
400
|
@timer = Timer.new(1, true) { @s.step_timeout }
|
362
401
|
loop.attach(@timer)
|
363
402
|
end
|
364
|
-
attr_reader :
|
403
|
+
attr_reader :host, :port
|
365
404
|
|
366
405
|
def close
|
367
406
|
@timer.detach
|
@@ -411,7 +450,6 @@ class Server
|
|
411
450
|
@loop = loop
|
412
451
|
@socks = []
|
413
452
|
end
|
414
|
-
attr_reader :loop
|
415
453
|
|
416
454
|
def listen(host, port, obj, accept = obj.public_methods)
|
417
455
|
lsock = ::Rev::TCPServer.new(host, port, Server::Socket, obj, accept)
|
data/test/msgpack_rpc_test.rb
CHANGED
@@ -7,6 +7,10 @@ $port = 65500
|
|
7
7
|
class MessagePackRPCTest < Test::Unit::TestCase
|
8
8
|
|
9
9
|
class MyServer
|
10
|
+
def initialize(svr)
|
11
|
+
@svr = svr
|
12
|
+
end
|
13
|
+
|
10
14
|
def hello
|
11
15
|
"ok"
|
12
16
|
end
|
@@ -19,6 +23,22 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
19
23
|
raise "raised"
|
20
24
|
end
|
21
25
|
|
26
|
+
def async
|
27
|
+
as = MessagePack::RPC::AsyncResult.new
|
28
|
+
@svr.start_timer(1, false) do
|
29
|
+
as.result "async"
|
30
|
+
end
|
31
|
+
as
|
32
|
+
end
|
33
|
+
|
34
|
+
def async_exception
|
35
|
+
as = MessagePack::RPC::AsyncResult.new
|
36
|
+
@svr.start_timer(1, false) do
|
37
|
+
as.error "async"
|
38
|
+
end
|
39
|
+
as
|
40
|
+
end
|
41
|
+
|
22
42
|
private
|
23
43
|
def hidden
|
24
44
|
end
|
@@ -29,21 +49,33 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
29
49
|
port = $port += 1
|
30
50
|
|
31
51
|
svr = MessagePack::RPC::Server.new
|
32
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
52
|
+
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
33
53
|
svr.close
|
34
54
|
end
|
35
55
|
|
36
56
|
|
37
|
-
def
|
57
|
+
def next_port
|
38
58
|
port = $port += 1
|
59
|
+
end
|
60
|
+
|
61
|
+
def start_server
|
62
|
+
port = next_port
|
39
63
|
|
40
64
|
svr = MessagePack::RPC::Server.new
|
41
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
65
|
+
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
42
66
|
Thread.start do
|
43
67
|
svr.run
|
44
68
|
end
|
45
69
|
|
46
70
|
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
71
|
+
cli.timeout = 10
|
72
|
+
|
73
|
+
return svr, cli
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def test_call
|
78
|
+
svr, cli = start_server
|
47
79
|
|
48
80
|
result = cli.call(:hello)
|
49
81
|
assert_equal(result, "ok")
|
@@ -59,12 +91,13 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
59
91
|
port = $port += 1
|
60
92
|
|
61
93
|
svr = MessagePack::RPC::Server.new
|
62
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
94
|
+
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
63
95
|
Thread.start do
|
64
96
|
svr.run
|
65
97
|
end
|
66
98
|
|
67
99
|
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
100
|
+
cli.timeout = 10
|
68
101
|
|
69
102
|
req1 = cli.send(:hello)
|
70
103
|
req2 = cli.send(:sum, 1, 2)
|
@@ -82,18 +115,10 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
82
115
|
|
83
116
|
|
84
117
|
def test_callback
|
85
|
-
|
86
|
-
|
87
|
-
svr = MessagePack::RPC::Server.new
|
88
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
89
|
-
Thread.start do
|
90
|
-
svr.run
|
91
|
-
end
|
118
|
+
svr, cli = start_server
|
92
119
|
|
93
120
|
count = 0
|
94
121
|
|
95
|
-
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
96
|
-
|
97
122
|
cli.callback(:hello) do |error, result|
|
98
123
|
assert_equal(result, "ok")
|
99
124
|
assert_nil(error)
|
@@ -115,18 +140,10 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
115
140
|
|
116
141
|
|
117
142
|
def test_hidden
|
118
|
-
|
119
|
-
|
120
|
-
svr = MessagePack::RPC::Server.new
|
121
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
122
|
-
Thread.start do
|
123
|
-
svr.run
|
124
|
-
end
|
143
|
+
svr, cli = start_server
|
125
144
|
|
126
145
|
count = 0
|
127
146
|
|
128
|
-
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
129
|
-
|
130
147
|
rejected = false
|
131
148
|
begin
|
132
149
|
cli.call(:hidden)
|
@@ -141,21 +158,40 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
141
158
|
|
142
159
|
|
143
160
|
def test_exception
|
144
|
-
|
161
|
+
svr, cli = start_server
|
145
162
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
163
|
+
raised = false
|
164
|
+
begin
|
165
|
+
cli.call(:exception)
|
166
|
+
rescue MessagePack::RPC::RemoteError
|
167
|
+
assert_equal($!.message, "raised")
|
168
|
+
raised = true
|
150
169
|
end
|
151
170
|
|
152
|
-
|
171
|
+
assert_equal(raised, true)
|
172
|
+
|
173
|
+
cli.close
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
def test_async
|
178
|
+
svr, cli = start_server
|
179
|
+
|
180
|
+
result = cli.call(:async)
|
181
|
+
assert_equal(result, "async")
|
182
|
+
|
183
|
+
cli.close
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
def test_async_exception
|
188
|
+
svr, cli = start_server
|
153
189
|
|
154
190
|
raised = false
|
155
191
|
begin
|
156
|
-
cli.call(:
|
192
|
+
cli.call(:async_exception)
|
157
193
|
rescue MessagePack::RPC::RemoteError
|
158
|
-
assert_equal($!.message, "
|
194
|
+
assert_equal($!.message, "async")
|
159
195
|
raised = true
|
160
196
|
end
|
161
197
|
|
@@ -166,15 +202,15 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
166
202
|
|
167
203
|
|
168
204
|
def test_loop
|
169
|
-
port =
|
205
|
+
port = next_port
|
170
206
|
|
171
207
|
loop = MessagePack::RPC::Loop.new
|
172
208
|
|
173
209
|
svr = MessagePack::RPC::Server.new(loop)
|
174
|
-
svr.listen("0.0.0.0", port, MyServer.new)
|
210
|
+
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
175
211
|
|
176
212
|
cli = MessagePack::RPC::Client.new("127.0.0.1", port, loop)
|
177
|
-
cli.timeout =
|
213
|
+
cli.timeout = 10
|
178
214
|
|
179
215
|
count = 0
|
180
216
|
|
@@ -200,7 +236,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
200
236
|
|
201
237
|
|
202
238
|
def test_timeout
|
203
|
-
port =
|
239
|
+
port = next_port
|
204
240
|
|
205
241
|
lsock = TCPServer.new("0.0.0.0", port)
|
206
242
|
|