2003-10-05 08:23:33 -04:00
|
|
|
#
|
|
|
|
# Note: Rinda::Ring API is unstable.
|
|
|
|
#
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'rinda/rinda'
|
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
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.
|
|
|
|
# Service location uses the following steps:
|
|
|
|
#
|
|
|
|
# 1. A RingServer begins listening on the broadcast UDP address.
|
|
|
|
# 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.
|
|
|
|
|
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
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Advertises +ts+ on the UDP broadcast address at +port+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(ts, port=Ring_PORT)
|
|
|
|
@ts = ts
|
|
|
|
@soc = UDPSocket.open
|
|
|
|
@soc.bind('', port)
|
|
|
|
@w_service = write_service
|
|
|
|
@r_service = reply_service
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Creates a thread that picks up UDP packets and passes them to do_write
|
|
|
|
# for decoding.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def write_service
|
|
|
|
Thread.new do
|
2011-05-18 17:19:18 -04:00
|
|
|
loop do
|
|
|
|
msg = @soc.recv(1024)
|
|
|
|
do_write(msg)
|
|
|
|
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
|
|
|
|
tuple = @ts.take([:lookup_ring, DRbObject])
|
|
|
|
Thread.new { tuple[1].call(@ts) rescue nil}
|
|
|
|
rescue
|
|
|
|
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
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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+.
|
|
|
|
|
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 = []
|
|
|
|
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|
|
2011-05-18 17:19:18 -04:00
|
|
|
soc = UDPSocket.open
|
|
|
|
begin
|
|
|
|
soc.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
|
|
|
|
soc.send(msg, 0, it, @port)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
ensure
|
|
|
|
soc.close
|
|
|
|
end
|
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
|
|
|
|
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
|
|
|
|