Imported File#ftype spec from rubyspecs.
[rbx.git] / lib / drb / gw.rb
blobb7a5f5383f8043f847f540ba661243244f96aac7
1 require 'drb/drb'
2 require 'monitor'
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
14   class GW
15     include MonitorMixin
16     def initialize
17       super()
18       @hash = {}
19     end
21     def [](key)
22       synchronize do
23         @hash[key]
24       end
25     end
27     def []=(key, v)
28       synchronize do
29         @hash[key] = v
30       end
31     end
32   end
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
41       self.new_with(DRb.uri, [:DRbObject, uri, ref])
42     end
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
58 =begin
59 DRb.install_id_conv(DRb::GWIdConv.new)
61 front = DRb::GW.new
63 s1 = DRb::DRbServer.new('drbunix:/tmp/gw_b_a', front)
64 s2 = DRb::DRbServer.new('drbunix:/tmp/gw_b_c', front)
66 s1.thread.join
67 s2.thread.join
68 =end
70 =begin
71 # foo.rb
73 require 'drb/drb'
75 class Foo
76   include DRbUndumped
77   def initialize(name, peer=nil)
78     @name = name
79     @peer = peer
80   end
82   def ping(obj)
83     puts "#{@name}: ping: #{obj.inspect}"
84     @peer.ping(self) if @peer
85   end
86 end
87 =end
89 =begin
90 # gw_a.rb
91 require 'drb/unix'
92 require 'foo'
94 obj = Foo.new('a')
95 DRb.start_service("drbunix:/tmp/gw_a", obj)
97 robj = DRbObject.new_with_uri('drbunix:/tmp/gw_b_a')
98 robj[:a] = obj
100 DRb.thread.join
101 =end
103 =begin
104 # gw_c.rb
105 require 'drb/unix'
106 require 'foo'
108 foo = Foo.new('c', nil)
110 DRb.start_service("drbunix:/tmp/gw_c", nil)
112 robj = DRbObject.new_with_uri("drbunix:/tmp/gw_b_c")
114 puts "c->b"
115 a = robj[:a]
116 sleep 2
118 a.ping(foo)
120 DRb.thread.join
121 =end