2003-10-05 08:23:33 -04:00
|
|
|
#
|
|
|
|
# Note: Rinda::Ring API is unstable.
|
|
|
|
#
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'rinda/rinda'
|
|
|
|
require 'thread'
|
2013-03-23 16:10:11 -04:00
|
|
|
require 'ipaddr'
|
2003-10-05 08:23:33 -04:00
|
|
|
|
|
|
|
module Rinda
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The default port Ring discovery will use.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
Ring_PORT = 7647
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# A RingServer allows a Rinda::TupleSpace to be located via UDP broadcasts.
|
2013-03-23 16:36:42 -04:00
|
|
|
# Default service location uses the following steps:
|
2005-10-24 11:38:47 -04:00
|
|
|
#
|
2013-03-23 16:36:42 -04:00
|
|
|
# 1. A RingServer begins listening on the network broadcast UDP address.
|
2005-10-24 11:38:47 -04:00
|
|
|
# 2. A RingFinger sends a UDP packet containing the DRb URI where it will
|
|
|
|
# listen for a reply.
|
2008-06-04 05:37:38 -04:00
|
|
|
# 3. The RingServer receives the UDP packet and connects back to the
|
2005-10-24 11:38:47 -04:00
|
|
|
# provided DRb URI with the DRb service.
|
2013-03-23 16:36:42 -04:00
|
|
|
#
|
|
|
|
# A RingServer requires a TupleSpace:
|
|
|
|
#
|
|
|
|
# ts = Rinda::TupleSpace.new
|
|
|
|
# rs = Rinda::RingServer.new
|
|
|
|
#
|
|
|
|
# RingServer can also listen on multicast addresses for announcements. This
|
|
|
|
# allows multiple RingServers to run on the same host. To use network
|
|
|
|
# broadcast and multicast:
|
|
|
|
#
|
|
|
|
# ts = Rinda::TupleSpace.new
|
|
|
|
# rs = Rinda::RingServer.new ts, %w[Socket::INADDR_ANY, 239.0.0.1 ff02::1]
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class RingServer
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
include DRbUndumped
|
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
##
|
|
|
|
# Special renewer for the RingServer to allow shutdown
|
|
|
|
|
|
|
|
class Renewer # :nodoc:
|
|
|
|
include DRbUndumped
|
|
|
|
|
|
|
|
##
|
|
|
|
# Set to false to shutdown future requests using this Renewer
|
|
|
|
|
2013-03-25 14:50:48 -04:00
|
|
|
attr_writer :renew
|
2013-03-23 16:10:11 -04:00
|
|
|
|
|
|
|
def initialize # :nodoc:
|
|
|
|
@renew = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew # :nodoc:
|
|
|
|
@renew ? 1 : true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2013-03-23 16:36:42 -04:00
|
|
|
# Advertises +ts+ on the given +addresses+ at +port+.
|
|
|
|
#
|
|
|
|
# If +addresses+ is omitted only the UDP broadcast address is used.
|
|
|
|
#
|
|
|
|
# +addresses+ can contain multiple addresses. If a multicast address is
|
|
|
|
# given in +addresses+ then the RingServer will listen for multicast
|
|
|
|
# queries.
|
2013-04-25 11:43:22 -04:00
|
|
|
#
|
|
|
|
# If you use IPv4 multicast you may need to set an address of the inbound
|
|
|
|
# interface which joins a multicast group.
|
|
|
|
#
|
|
|
|
# ts = Rinda::TupleSpace.new
|
|
|
|
# rs = Rinda::RingServer.new(ts, [['239.0.0.1', '9.5.1.1']])
|
|
|
|
#
|
|
|
|
# You can set addresses as an Array Object. The first element of the
|
|
|
|
# Array is a multicast address and the second is an inbound interface
|
|
|
|
# address. If the second is omitted then '0.0.0.0' is used.
|
|
|
|
#
|
|
|
|
# If you use IPv6 multicast you may need to set both the local interface
|
|
|
|
# address and the inbound interface index:
|
|
|
|
#
|
|
|
|
# rs = Rinda::RingServer.new(ts, [['ff02::1', '::1', 1]])
|
|
|
|
#
|
|
|
|
# The first element is a multicast address and the second is an inbound
|
|
|
|
# interface address. The third is an inbound interface index.
|
|
|
|
#
|
|
|
|
# At this time there is no easy way to get an interface index by name.
|
|
|
|
#
|
|
|
|
# If the second is omitted then '::1' is used.
|
|
|
|
# If the third is omitted then 0 (default interface) is used.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
def initialize(ts, addresses=[Socket::INADDR_ANY], port=Ring_PORT)
|
|
|
|
@port = port
|
|
|
|
|
|
|
|
if Integer === addresses then
|
|
|
|
addresses, @port = [Socket::INADDR_ANY], addresses
|
|
|
|
end
|
|
|
|
|
|
|
|
@renewer = Renewer.new
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
@ts = ts
|
2013-04-25 11:36:33 -04:00
|
|
|
@sockets = []
|
|
|
|
addresses.each do |address|
|
2013-04-25 11:43:22 -04:00
|
|
|
if Array === address
|
|
|
|
make_socket(*address)
|
|
|
|
else
|
|
|
|
make_socket(address)
|
|
|
|
end
|
2013-03-23 16:10:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@w_services = write_services
|
|
|
|
@r_service = reply_service
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2013-03-23 16:10:11 -04:00
|
|
|
# Creates a socket at +address+
|
2013-04-25 11:43:22 -04:00
|
|
|
#
|
|
|
|
# If +address+ is multicast address then +interface_address+ and
|
|
|
|
# +multicast_interface+ can be set as optional.
|
|
|
|
#
|
|
|
|
# A created socket is bound to +interface_address+. If you use IPv4
|
|
|
|
# multicast then the interface of +interface_address+ is used as the
|
|
|
|
# inbound interface. If +interface_address+ is omitted or nil then
|
|
|
|
# '0.0.0.0' or '::1' is used.
|
|
|
|
#
|
|
|
|
# If you use IPv6 multicast then +multicast_interface+ is used as the
|
|
|
|
# inbound interface. +multicast_interface+ is a network interface index.
|
|
|
|
# If +multicast_interface+ is omitted then 0 (default interface) is used.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2013-04-25 11:43:22 -04:00
|
|
|
def make_socket(address, interface_address=nil, multicast_interface=0)
|
2013-03-23 16:10:11 -04:00
|
|
|
addrinfo = Addrinfo.udp(address, @port)
|
|
|
|
|
|
|
|
socket = Socket.new(addrinfo.pfamily, addrinfo.socktype,
|
|
|
|
addrinfo.protocol)
|
2013-04-25 11:36:33 -04:00
|
|
|
@sockets << socket
|
2013-03-23 16:10:11 -04:00
|
|
|
|
|
|
|
if addrinfo.ipv4_multicast? or addrinfo.ipv6_multicast? then
|
|
|
|
if Socket.const_defined?(:SO_REUSEPORT) then
|
|
|
|
socket.setsockopt(:SOCKET, :SO_REUSEPORT, true)
|
|
|
|
else
|
|
|
|
socket.setsockopt(:SOCKET, :SO_REUSEADDR, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
if addrinfo.ipv4_multicast? then
|
2013-04-25 11:43:22 -04:00
|
|
|
interface_address = '0.0.0.0' if interface_address.nil?
|
|
|
|
socket.bind(Addrinfo.udp(interface_address, @port))
|
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
mreq = IPAddr.new(addrinfo.ip_address).hton +
|
2013-04-25 11:43:22 -04:00
|
|
|
IPAddr.new(interface_address).hton
|
2013-03-23 16:10:11 -04:00
|
|
|
|
|
|
|
socket.setsockopt(:IPPROTO_IP, :IP_ADD_MEMBERSHIP, mreq)
|
|
|
|
else
|
2013-04-25 11:43:22 -04:00
|
|
|
interface_address = '::1' if interface_address.nil?
|
|
|
|
socket.bind(Addrinfo.udp(interface_address, @port))
|
|
|
|
|
|
|
|
mreq = IPAddr.new(addrinfo.ip_address).hton +
|
|
|
|
[multicast_interface].pack('I')
|
2013-03-23 16:10:11 -04:00
|
|
|
|
|
|
|
socket.setsockopt(:IPPROTO_IPV6, :IPV6_JOIN_GROUP, mreq)
|
|
|
|
end
|
2013-04-25 11:43:22 -04:00
|
|
|
else
|
|
|
|
socket.bind(addrinfo)
|
2013-03-23 16:10:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates threads that pick up UDP packets and passes them to do_write for
|
|
|
|
# decoding.
|
|
|
|
|
|
|
|
def write_services
|
|
|
|
@sockets.map do |s|
|
|
|
|
Thread.new(s) do |socket|
|
|
|
|
loop do
|
|
|
|
msg = socket.recv(1024)
|
|
|
|
do_write(msg)
|
|
|
|
end
|
2011-05-18 17:19:18 -04:00
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Extracts the response URI from +msg+ and adds it to TupleSpace where it
|
|
|
|
# will be picked up by +reply_service+ for notification.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def do_write(msg)
|
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
tuple, sec = Marshal.load(msg)
|
|
|
|
@ts.write(tuple, sec)
|
|
|
|
rescue
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Creates a thread that notifies waiting clients from the TupleSpace.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def reply_service
|
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
loop do
|
|
|
|
do_reply
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Pulls lookup tuples out of the TupleSpace and sends their DRb object the
|
|
|
|
# address of the local TupleSpace.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def do_reply
|
2013-11-27 00:50:21 -05:00
|
|
|
tuple = @ts.take([:lookup_ring, nil], @renewer)
|
2003-10-05 08:23:33 -04:00
|
|
|
Thread.new { tuple[1].call(@ts) rescue nil}
|
|
|
|
rescue
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
##
|
|
|
|
# Shuts down the RingServer
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
@renewer.renew = false
|
|
|
|
|
|
|
|
@w_services.each do |thread|
|
|
|
|
thread.kill
|
2014-05-25 03:32:26 -04:00
|
|
|
thread.join
|
2013-03-23 16:10:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@sockets.each do |socket|
|
|
|
|
socket.close
|
|
|
|
end
|
|
|
|
|
|
|
|
@r_service.kill
|
2014-05-25 03:32:26 -04:00
|
|
|
@r_service.join
|
2013-03-23 16:10:11 -04:00
|
|
|
end
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# RingFinger is used by RingServer clients to discover the RingServer's
|
|
|
|
# TupleSpace. Typically, all a client needs to do is call
|
|
|
|
# RingFinger.primary to retrieve the remote TupleSpace, which it can then
|
|
|
|
# begin using.
|
2013-03-23 16:36:42 -04:00
|
|
|
#
|
|
|
|
# To find the first available remote TupleSpace:
|
|
|
|
#
|
|
|
|
# Rinda::RingFinger.primary
|
|
|
|
#
|
|
|
|
# To create a RingFinger that broadcasts to a custom list:
|
|
|
|
#
|
|
|
|
# rf = Rinda::RingFinger.new ['localhost', '192.0.2.1']
|
|
|
|
# rf.primary
|
|
|
|
#
|
|
|
|
# Rinda::RingFinger also understands multicast addresses and sets them up
|
|
|
|
# properly. This allows you to run multiple RingServers on the same host:
|
|
|
|
#
|
|
|
|
# rf = Rinda::RingFinger.new ['239.0.0.1']
|
|
|
|
# rf.primary
|
|
|
|
#
|
|
|
|
# You can set the hop count (or TTL) for multicast searches using
|
|
|
|
# #multicast_hops.
|
|
|
|
#
|
|
|
|
# If you use IPv6 multicast you may need to set both an address and the
|
|
|
|
# outbound interface index:
|
|
|
|
#
|
|
|
|
# rf = Rinda::RingFinger.new ['ff02::1']
|
|
|
|
# rf.multicast_interface = 1
|
|
|
|
# rf.primary
|
|
|
|
#
|
|
|
|
# At this time there is no easy way to get an interface index by name.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class RingFinger
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
@@broadcast_list = ['<broadcast>', 'localhost']
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
@@finger = nil
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a singleton RingFinger and looks for a RingServer. Returns the
|
|
|
|
# created RingFinger.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def self.finger
|
2009-03-05 22:56:38 -05:00
|
|
|
unless @@finger
|
2011-05-18 17:19:18 -04:00
|
|
|
@@finger = self.new
|
|
|
|
@@finger.lookup_ring_any
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
@@finger
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Returns the first advertised TupleSpace.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def self.primary
|
|
|
|
finger.primary
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2008-06-04 05:37:38 -04:00
|
|
|
# Contains all discovered TupleSpaces except for the primary.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def self.to_a
|
|
|
|
finger.to_a
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# The list of addresses where RingFinger will send query packets.
|
|
|
|
|
|
|
|
attr_accessor :broadcast_list
|
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
##
|
|
|
|
# Maximum number of hops for sent multicast packets (if using a multicast
|
|
|
|
# address in the broadcast list). The default is 1 (same as UDP
|
|
|
|
# broadcast).
|
|
|
|
|
|
|
|
attr_accessor :multicast_hops
|
|
|
|
|
|
|
|
##
|
|
|
|
# The interface index to send IPv6 multicast packets from.
|
|
|
|
|
|
|
|
attr_accessor :multicast_interface
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# The port that RingFinger will send query packets to.
|
|
|
|
|
|
|
|
attr_accessor :port
|
|
|
|
|
|
|
|
##
|
|
|
|
# Contain the first advertised TupleSpace after lookup_ring_any is called.
|
|
|
|
|
|
|
|
attr_accessor :primary
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new RingFinger that will look for RingServers at +port+ on
|
|
|
|
# the addresses in +broadcast_list+.
|
2013-03-23 16:36:42 -04:00
|
|
|
#
|
|
|
|
# If +broadcast_list+ contains a multicast address then multicast queries
|
|
|
|
# will be made using the given multicast_hops and multicast_interface.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(broadcast_list=@@broadcast_list, port=Ring_PORT)
|
|
|
|
@broadcast_list = broadcast_list || ['localhost']
|
|
|
|
@port = port
|
|
|
|
@primary = nil
|
|
|
|
@rings = []
|
2013-03-23 16:10:11 -04:00
|
|
|
|
|
|
|
@multicast_hops = 1
|
|
|
|
@multicast_interface = 0
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Contains all discovered TupleSpaces except for the primary.
|
2003-10-05 08:23:33 -04:00
|
|
|
|
|
|
|
def to_a
|
|
|
|
@rings
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Iterates over all discovered TupleSpaces starting with the primary.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def each
|
|
|
|
lookup_ring_any unless @primary
|
|
|
|
return unless @primary
|
|
|
|
yield(@primary)
|
|
|
|
@rings.each { |x| yield(x) }
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Looks up RingServers waiting +timeout+ seconds. RingServers will be
|
|
|
|
# given +block+ as a callback, which will be called with the remote
|
|
|
|
# TupleSpace.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def lookup_ring(timeout=5, &block)
|
|
|
|
return lookup_ring_any(timeout) unless block_given?
|
|
|
|
|
|
|
|
msg = Marshal.dump([[:lookup_ring, DRbObject.new(block)], timeout])
|
|
|
|
@broadcast_list.each do |it|
|
2013-03-23 16:10:11 -04:00
|
|
|
send_message(it, msg)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
sleep(timeout)
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Returns the first found remote TupleSpace. Any further recovered
|
|
|
|
# TupleSpaces can be found by calling +to_a+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def lookup_ring_any(timeout=5)
|
|
|
|
queue = Queue.new
|
|
|
|
|
2010-11-08 15:59:01 -05:00
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
self.lookup_ring(timeout) do |ts|
|
|
|
|
queue.push(ts)
|
|
|
|
end
|
|
|
|
queue.push(nil)
|
2012-05-08 15:35:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@primary = queue.pop
|
|
|
|
raise('RingNotFound') if @primary.nil?
|
|
|
|
|
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
while it = queue.pop
|
|
|
|
@rings.push(it)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
@primary
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2013-03-23 16:10:11 -04:00
|
|
|
##
|
|
|
|
# Creates a socket for +address+ with the appropriate multicast options
|
|
|
|
# for multicast addresses.
|
|
|
|
|
|
|
|
def make_socket(address) # :nodoc:
|
|
|
|
addrinfo = Addrinfo.udp(address, @port)
|
|
|
|
|
|
|
|
soc = Socket.new(addrinfo.pfamily, addrinfo.socktype, addrinfo.protocol)
|
2014-05-31 22:37:27 -04:00
|
|
|
begin
|
|
|
|
if addrinfo.ipv4_multicast? then
|
|
|
|
soc.setsockopt(Socket::Option.ipv4_multicast_loop(1))
|
|
|
|
soc.setsockopt(Socket::Option.ipv4_multicast_ttl(@multicast_hops))
|
|
|
|
elsif addrinfo.ipv6_multicast? then
|
|
|
|
soc.setsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_LOOP, true)
|
|
|
|
soc.setsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_HOPS,
|
|
|
|
[@multicast_hops].pack('I'))
|
|
|
|
soc.setsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_IF,
|
|
|
|
[@multicast_interface].pack('I'))
|
|
|
|
else
|
|
|
|
soc.setsockopt(:SOL_SOCKET, :SO_BROADCAST, true)
|
|
|
|
end
|
2013-03-23 16:10:11 -04:00
|
|
|
|
2014-05-31 22:37:27 -04:00
|
|
|
soc.connect(addrinfo)
|
|
|
|
rescue Exception
|
|
|
|
soc.close
|
|
|
|
raise
|
2013-03-23 16:10:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
soc
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_message(address, message) # :nodoc:
|
|
|
|
soc = make_socket(address)
|
|
|
|
|
|
|
|
soc.send(message, 0)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
ensure
|
|
|
|
soc.close if soc
|
|
|
|
end
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# RingProvider uses a RingServer advertised TupleSpace as a name service.
|
|
|
|
# TupleSpace clients can register themselves with the remote TupleSpace and
|
|
|
|
# look up other provided services via the remote TupleSpace.
|
|
|
|
#
|
|
|
|
# Services are registered with a tuple of the format [:name, klass,
|
|
|
|
# DRbObject, description].
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class RingProvider
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a RingProvider that will provide a +klass+ service running on
|
|
|
|
# +front+, with a +description+. +renewer+ is optional.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(klass, front, desc, renewer = nil)
|
|
|
|
@tuple = [:name, klass, front, desc]
|
|
|
|
@renewer = renewer || Rinda::SimpleRenewer.new
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Advertises this service on the primary remote TupleSpace.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def provide
|
|
|
|
ts = Rinda::RingFinger.primary
|
|
|
|
ts.write(@tuple, @renewer)
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
DRb.start_service
|
|
|
|
case ARGV.shift
|
|
|
|
when 's'
|
|
|
|
require 'rinda/tuplespace'
|
|
|
|
ts = Rinda::TupleSpace.new
|
2010-11-08 15:59:01 -05:00
|
|
|
Rinda::RingServer.new(ts)
|
2003-10-05 08:23:33 -04:00
|
|
|
$stdin.gets
|
|
|
|
when 'w'
|
|
|
|
finger = Rinda::RingFinger.new(nil)
|
2008-12-18 02:54:50 -05:00
|
|
|
finger.lookup_ring do |ts2|
|
|
|
|
p ts2
|
|
|
|
ts2.write([:hello, :world])
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
when 'r'
|
|
|
|
finger = Rinda::RingFinger.new(nil)
|
2008-12-18 02:54:50 -05:00
|
|
|
finger.lookup_ring do |ts2|
|
|
|
|
p ts2
|
|
|
|
p ts2.take([nil, nil])
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|