msgpack-rpc 0.1.1 → 0.1.2
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 +31 -21
- data/test/msgpack_rpc_test.rb +25 -16
- metadata +2 -2
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.2"
|
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
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# MessagePack
|
2
|
+
# MessagePack-RPC for Ruby
|
3
3
|
#
|
4
4
|
# Copyright (C) 2009 FURUHASHI Sadayuki
|
5
5
|
#
|
@@ -74,11 +74,12 @@ module RPCSocket
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def on_message(msg)
|
77
|
-
|
77
|
+
case msg[0]
|
78
|
+
when REQUEST
|
78
79
|
on_request(msg[1], msg[2], msg[3])
|
79
|
-
|
80
|
-
on_response(msg[1], msg[
|
81
|
-
|
80
|
+
when RESPONSE
|
81
|
+
on_response(msg[1], msg[2], msg[3])
|
82
|
+
when NOTIFY
|
82
83
|
on_notify(msg[1], msg[2])
|
83
84
|
else
|
84
85
|
raise RPCError.new("unknown message type #{msg[0]}")
|
@@ -103,17 +104,17 @@ module RPCSocket
|
|
103
104
|
@session.on_notify(method, param)
|
104
105
|
end
|
105
106
|
|
106
|
-
def on_response(msgid,
|
107
|
+
def on_response(msgid, error, result)
|
107
108
|
return unless @session
|
108
|
-
@session.on_response(msgid,
|
109
|
+
@session.on_response(msgid, error, result)
|
109
110
|
end
|
110
111
|
|
111
112
|
def send_request(msgid, method, param)
|
112
113
|
send_message [REQUEST, msgid, method, param]
|
113
114
|
end
|
114
115
|
|
115
|
-
def send_response(msgid,
|
116
|
-
send_message [RESPONSE, msgid,
|
116
|
+
def send_response(msgid, result, error)
|
117
|
+
send_message [RESPONSE, msgid, error, result]
|
117
118
|
end
|
118
119
|
|
119
120
|
def send_notify(method, param)
|
@@ -233,7 +234,7 @@ class ClientSession
|
|
233
234
|
end
|
234
235
|
|
235
236
|
|
236
|
-
def on_response(msgid,
|
237
|
+
def on_response(msgid, error, result)
|
237
238
|
if req = @reqtable.delete(msgid)
|
238
239
|
req.call error, result
|
239
240
|
end
|
@@ -278,6 +279,7 @@ class ClientSession
|
|
278
279
|
def notify_real(method, param)
|
279
280
|
method = method.to_s unless method.is_a?(Integer)
|
280
281
|
@sock.send_notify method, param
|
282
|
+
nil
|
281
283
|
end
|
282
284
|
end
|
283
285
|
|
@@ -327,24 +329,21 @@ class ServerSession
|
|
327
329
|
|
328
330
|
def on_request(method, param, res)
|
329
331
|
begin
|
330
|
-
|
331
|
-
raise NoMethodError, "method `#{method}' is not accepted"
|
332
|
-
end
|
333
|
-
ret = @obj.send(method, *param)
|
332
|
+
result = forward_method(method, param)
|
334
333
|
rescue
|
335
334
|
res.error($!.to_s)
|
336
335
|
return
|
337
336
|
end
|
338
|
-
if
|
339
|
-
|
337
|
+
if result.is_a?(AsyncResult)
|
338
|
+
result.responder = res
|
340
339
|
else
|
341
|
-
res.result(
|
340
|
+
res.result(result)
|
342
341
|
end
|
343
342
|
end
|
344
343
|
|
345
344
|
def on_notify(method, param)
|
346
|
-
|
347
|
-
|
345
|
+
forward_method(method, param)
|
346
|
+
rescue
|
348
347
|
end
|
349
348
|
|
350
349
|
def on_response(msgid, error, result)
|
@@ -355,6 +354,14 @@ class ServerSession
|
|
355
354
|
# do nothing
|
356
355
|
@sock = nil
|
357
356
|
end
|
357
|
+
|
358
|
+
private
|
359
|
+
def forward_method(method, param)
|
360
|
+
unless @accept.include?(method)
|
361
|
+
raise NoMethodError, "method `#{method}' is not accepted"
|
362
|
+
end
|
363
|
+
@obj.send(method, *param)
|
364
|
+
end
|
358
365
|
end
|
359
366
|
|
360
367
|
Loop = ::Rev::Loop
|
@@ -382,8 +389,9 @@ module LoopUtil
|
|
382
389
|
|
383
390
|
def stop
|
384
391
|
@loop.stop
|
385
|
-
# attach dummy timer
|
386
|
-
@loop.attach
|
392
|
+
# attach/detach dummy timer
|
393
|
+
@loop.attach(Rev::TimerWatcher.new(0, false)).detach
|
394
|
+
nil
|
387
395
|
end
|
388
396
|
end
|
389
397
|
|
@@ -406,6 +414,7 @@ class Client
|
|
406
414
|
@timer.detach
|
407
415
|
@rsock.detach
|
408
416
|
@s.close
|
417
|
+
nil
|
409
418
|
end
|
410
419
|
|
411
420
|
def send(method, *args)
|
@@ -463,6 +472,7 @@ class Server
|
|
463
472
|
lsock.close
|
464
473
|
true
|
465
474
|
}
|
475
|
+
nil
|
466
476
|
end
|
467
477
|
|
468
478
|
include LoopUtil
|
data/test/msgpack_rpc_test.rb
CHANGED
@@ -45,8 +45,13 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
45
45
|
end
|
46
46
|
|
47
47
|
|
48
|
-
def
|
48
|
+
def next_port
|
49
49
|
port = $port += 1
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def test_listen
|
54
|
+
port = next_port
|
50
55
|
|
51
56
|
svr = MessagePack::RPC::Server.new
|
52
57
|
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
@@ -54,10 +59,6 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
54
59
|
end
|
55
60
|
|
56
61
|
|
57
|
-
def next_port
|
58
|
-
port = $port += 1
|
59
|
-
end
|
60
|
-
|
61
62
|
def start_server
|
62
63
|
port = next_port
|
63
64
|
|
@@ -65,6 +66,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
65
66
|
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
66
67
|
Thread.start do
|
67
68
|
svr.run
|
69
|
+
svr.close
|
68
70
|
end
|
69
71
|
|
70
72
|
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
@@ -84,20 +86,12 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
84
86
|
assert_equal(result, 3)
|
85
87
|
|
86
88
|
cli.close
|
89
|
+
svr.stop
|
87
90
|
end
|
88
91
|
|
89
92
|
|
90
93
|
def test_send
|
91
|
-
|
92
|
-
|
93
|
-
svr = MessagePack::RPC::Server.new
|
94
|
-
svr.listen("0.0.0.0", port, MyServer.new(svr))
|
95
|
-
Thread.start do
|
96
|
-
svr.run
|
97
|
-
end
|
98
|
-
|
99
|
-
cli = MessagePack::RPC::Client.new("127.0.0.1", port)
|
100
|
-
cli.timeout = 10
|
94
|
+
svr, cli = start_server
|
101
95
|
|
102
96
|
req1 = cli.send(:hello)
|
103
97
|
req2 = cli.send(:sum, 1, 2)
|
@@ -111,6 +105,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
111
105
|
assert_nil(req2.error)
|
112
106
|
|
113
107
|
cli.close
|
108
|
+
svr.stop
|
114
109
|
end
|
115
110
|
|
116
111
|
|
@@ -135,6 +130,17 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
135
130
|
cli.loop.run_once
|
136
131
|
end
|
137
132
|
|
133
|
+
cli.close
|
134
|
+
svr.stop
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
def test_notify
|
139
|
+
svr, cli = start_server
|
140
|
+
|
141
|
+
cli.notify(:hello)
|
142
|
+
cli.notify(:sum, 1, 2)
|
143
|
+
|
138
144
|
cli.close
|
139
145
|
end
|
140
146
|
|
@@ -154,6 +160,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
154
160
|
assert_equal(rejected, true)
|
155
161
|
|
156
162
|
cli.close
|
163
|
+
svr.stop
|
157
164
|
end
|
158
165
|
|
159
166
|
|
@@ -171,6 +178,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
171
178
|
assert_equal(raised, true)
|
172
179
|
|
173
180
|
cli.close
|
181
|
+
svr.stop
|
174
182
|
end
|
175
183
|
|
176
184
|
|
@@ -181,6 +189,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
181
189
|
assert_equal(result, "async")
|
182
190
|
|
183
191
|
cli.close
|
192
|
+
svr.stop
|
184
193
|
end
|
185
194
|
|
186
195
|
|
@@ -198,6 +207,7 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
198
207
|
assert_equal(raised, true)
|
199
208
|
|
200
209
|
cli.close
|
210
|
+
svr.stop
|
201
211
|
end
|
202
212
|
|
203
213
|
|
@@ -255,6 +265,5 @@ class MessagePackRPCTest < Test::Unit::TestCase
|
|
255
265
|
cli.close
|
256
266
|
lsock.close
|
257
267
|
end
|
258
|
-
|
259
268
|
end
|
260
269
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msgpack-rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- FURUHASHI Sadayuki
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-19 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|