forked from arton/async-druby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-druby.rb
55 lines (53 loc) · 1.27 KB
/
async-druby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# = async-druby.rb
#
# dRuby async style client addoon
#
# Copyright(c) 2013 arton. You can redistribute it and/or
# modify it under the same terms as Ruby.
#
# Author: arton
#
# == Overview
#
# async-druby is monkey patch for dRuby.
# it adds methods named with async_ prefix for each DRbObject.
#
# ==== clinet code
#
# require 'async-druby'
#
# # The URI to connect to
# SERVER_URI="druby://localhost:8787"
#
# timeserver = DRbObject.new_with_uri(SERVER_URI)
#
# current = timeserver.get_current_time # synchronous call as usual
#
# timeserver.async_get_current_time do |current| # async callback style
# puts ccurrent # => async-druby will call back this block
# end
# sleep # you may continue to run client for doing another work
#
# === restrictions
#
# You don't call server method that takes block parameter.
#
require 'drb/drb'
module DRb
class DRbObject
alias _method_missing method_missing
def method_missing(msg_id, *a, &b)
if msg_id.to_s.index('async_') == 0 && b
drb = Thread.current['DRb']
Thread.start do
Thread.current['DRb'] = drb
ret = _method_missing(msg_id[6..-1].to_sym, *a)
b.call(ret)
end
else
_method_missing(msg_id, *a, &b)
end
end
end
end