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
|
|
|
|
loop do
|
2004-05-04 14:53:26 -04:00
|
|
|
msg = @soc.recv(1024)
|
2003-10-05 08:23:33 -04:00
|
|
|
do_write(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
begin
|
|
|
|
tuple, sec = Marshal.load(msg)
|
|
|
|
@ts.write(tuple, sec)
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
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
|
|
|
|
loop do
|
|
|
|
do_reply
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
unless @@finger
|
|
|
|
@@finger = self.new
|
|
|
|
@@finger.lookup_ring_any
|
|
|
|
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|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
th = Thread.new do
|
|
|
|
self.lookup_ring(timeout) do |ts|
|
|
|
|
queue.push(ts)
|
|
|
|
end
|
|
|
|
queue.push(nil)
|
|
|
|
while it = queue.pop
|
|
|
|
@rings.push(it)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@primary = queue.pop
|
|
|
|
raise('RingNotFound') if @primary.nil?
|
|
|
|
@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
|
|
|
|
place = Rinda::RingServer.new(ts)
|
|
|
|
$stdin.gets
|
|
|
|
when 'w'
|
|
|
|
finger = Rinda::RingFinger.new(nil)
|
|
|
|
finger.lookup_ring do |ts|
|
|
|
|
p ts
|
|
|
|
ts.write([:hello, :world])
|
|
|
|
end
|
|
|
|
when 'r'
|
|
|
|
finger = Rinda::RingFinger.new(nil)
|
|
|
|
finger.lookup_ring do |ts|
|
|
|
|
p ts
|
|
|
|
p ts.take([nil, nil])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|