# frozen_string_literal: false
# Gateway id conversion forms a gateway between different DRb protocols or
# The gateway needs to install this id conversion and create servers for
# each of the protocols or networks it will be a gateway between. It then
# needs to create a server that attaches to each of these networks. For
# DRb.install_id_conv DRb::GWIdConv.new
# s1 = DRb::DRbServer.new 'drbunix:/path/to/gateway', gw
# s2 = DRb::DRbServer.new 'druby://example:10000', gw
# Each client must register services with the gateway, for example:
# DRb.start_service 'drbunix:', nil # an anonymous server
# gw = DRbObject.new nil, 'drbunix:/path/to/gateway'
# gw[:unix] = some_service
class GWIdConv < DRbIdConv
def to_obj(ref) # :nodoc:
if Array === ref && ref[0] == :DRbObject
return DRbObject.new_with(ref[1], ref[2])
# The GW provides a synchronized store for participants in the gateway to
# Retrieves +key+ from the GW
# Stores value +v+ at +key+ in the GW
class DRbObject # :nodoc:
uri, ref = Marshal.load(s)
return ref ? DRb.to_obj(ref) : DRb.front
self.new_with(DRb.uri, [:DRbObject, uri, ref])
if Array === @ref && @ref[0] == :DRbObject
Marshal.dump([@ref[1], @ref[2]])
Marshal.dump([@uri, @ref]) # ??
Marshal.dump([DRb.uri, [:DRbObject, @uri, @ref]])
DRb.install_id_conv(DRb::GWIdConv.new)
s1 = DRb::DRbServer.new('drbunix:/tmp/gw_b_a', front)
s2 = DRb::DRbServer.new('drbunix:/tmp/gw_b_c', front)
def initialize(name, peer=nil)
puts "#{@name}: ping: #{obj.inspect}"
@peer.ping(self) if @peer
DRb.start_service("drbunix:/tmp/gw_a", obj)
robj = DRbObject.new_with_uri('drbunix:/tmp/gw_b_a')
DRb.start_service("drbunix:/tmp/gw_c", nil)
robj = DRbObject.new_with_uri("drbunix:/tmp/gw_b_c")