summaryrefslogtreecommitdiff
path: root/lib/drb/gw.rb
blob: 012a2b0a11761e72cb21fe102105555e539fbab5 (plain)
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
56
57
58
59
60
require 'drb/drb'
require 'monitor'

module DRb
  class GWIdConv < DRbIdConv
    def to_obj(ref)
      if Array === ref && ref[0] == :DRbObject
        it = DRbObject.new(nil)
        it.reinit(ref[1], ref[2])
        return it
      end
      super(ref)
    end
  end

  class GW
    include MonitorMixin
    def initialize
      super()
      @hash = {}
    end

    def [](key)
      synchronize do
        @hash[key]
      end
    end

    def []=(key, v)
      synchronize do
        @hash[key] = v
      end
    end
  end

  class DRbObject
    def self._load(s)
      uri, ref = Marshal.load(s)
      if DRb.uri == uri
        return ref ? DRb.to_obj(ref) : DRb.front
      end

      it = self.new(nil)
      it.reinit(DRb.uri, [:DRbObject, uri, ref])
      it
    end

    def _dump(lv)
      if DRb.uri == @uri
        if Array === @ref && @ref[0] == :DRbObject
          Marshal.dump([@ref[1], @ref[2]])
        else
          Marshal.dump([@uri, @ref]) # ??
        end
      else
        Marshal.dump([DRb.uri, [:DRbObject, @uri, @ref]])
      end
    end
  end
end