1 | require 'drb/drb'
|
---|
2 | require 'monitor'
|
---|
3 |
|
---|
4 | module DRb
|
---|
5 | class GWIdConv < DRbIdConv
|
---|
6 | def to_obj(ref)
|
---|
7 | if Array === ref && ref[0] == :DRbObject
|
---|
8 | return DRbObject.new_with(ref[1], ref[2])
|
---|
9 | end
|
---|
10 | super(ref)
|
---|
11 | end
|
---|
12 | end
|
---|
13 |
|
---|
14 | class GW
|
---|
15 | include MonitorMixin
|
---|
16 | def initialize
|
---|
17 | super()
|
---|
18 | @hash = {}
|
---|
19 | end
|
---|
20 |
|
---|
21 | def [](key)
|
---|
22 | synchronize do
|
---|
23 | @hash[key]
|
---|
24 | end
|
---|
25 | end
|
---|
26 |
|
---|
27 | def []=(key, v)
|
---|
28 | synchronize do
|
---|
29 | @hash[key] = v
|
---|
30 | end
|
---|
31 | end
|
---|
32 | end
|
---|
33 |
|
---|
34 | class DRbObject
|
---|
35 | def self._load(s)
|
---|
36 | uri, ref = Marshal.load(s)
|
---|
37 | if DRb.uri == uri
|
---|
38 | return ref ? DRb.to_obj(ref) : DRb.front
|
---|
39 | end
|
---|
40 |
|
---|
41 | self.new_with(DRb.uri, [:DRbObject, uri, ref])
|
---|
42 | end
|
---|
43 |
|
---|
44 | def _dump(lv)
|
---|
45 | if DRb.uri == @uri
|
---|
46 | if Array === @ref && @ref[0] == :DRbObject
|
---|
47 | Marshal.dump([@ref[1], @ref[2]])
|
---|
48 | else
|
---|
49 | Marshal.dump([@uri, @ref]) # ??
|
---|
50 | end
|
---|
51 | else
|
---|
52 | Marshal.dump([DRb.uri, [:DRbObject, @uri, @ref]])
|
---|
53 | end
|
---|
54 | end
|
---|
55 | end
|
---|
56 | end
|
---|
57 |
|
---|
58 | =begin
|
---|
59 | DRb.install_id_conv(DRb::GWIdConv.new)
|
---|
60 |
|
---|
61 | front = DRb::GW.new
|
---|
62 |
|
---|
63 | s1 = DRb::DRbServer.new('drbunix:/tmp/gw_b_a', front)
|
---|
64 | s2 = DRb::DRbServer.new('drbunix:/tmp/gw_b_c', front)
|
---|
65 |
|
---|
66 | s1.thread.join
|
---|
67 | s2.thread.join
|
---|
68 | =end
|
---|
69 |
|
---|
70 | =begin
|
---|
71 | # foo.rb
|
---|
72 |
|
---|
73 | require 'drb/drb'
|
---|
74 |
|
---|
75 | class Foo
|
---|
76 | include DRbUndumped
|
---|
77 | def initialize(name, peer=nil)
|
---|
78 | @name = name
|
---|
79 | @peer = peer
|
---|
80 | end
|
---|
81 |
|
---|
82 | def ping(obj)
|
---|
83 | puts "#{@name}: ping: #{obj.inspect}"
|
---|
84 | @peer.ping(self) if @peer
|
---|
85 | end
|
---|
86 | end
|
---|
87 | =end
|
---|
88 |
|
---|
89 | =begin
|
---|
90 | # gw_a.rb
|
---|
91 | require 'drb/unix'
|
---|
92 | require 'foo'
|
---|
93 |
|
---|
94 | obj = Foo.new('a')
|
---|
95 | DRb.start_service("drbunix:/tmp/gw_a", obj)
|
---|
96 |
|
---|
97 | robj = DRbObject.new_with_uri('drbunix:/tmp/gw_b_a')
|
---|
98 | robj[:a] = obj
|
---|
99 |
|
---|
100 | DRb.thread.join
|
---|
101 | =end
|
---|
102 |
|
---|
103 | =begin
|
---|
104 | # gw_c.rb
|
---|
105 | require 'drb/unix'
|
---|
106 | require 'foo'
|
---|
107 |
|
---|
108 | foo = Foo.new('c', nil)
|
---|
109 |
|
---|
110 | DRb.start_service("drbunix:/tmp/gw_c", nil)
|
---|
111 |
|
---|
112 | robj = DRbObject.new_with_uri("drbunix:/tmp/gw_b_c")
|
---|
113 |
|
---|
114 | puts "c->b"
|
---|
115 | a = robj[:a]
|
---|
116 | sleep 2
|
---|
117 |
|
---|
118 | a.ping(foo)
|
---|
119 |
|
---|
120 | DRb.thread.join
|
---|
121 | =end
|
---|
122 |
|
---|