RDoc documentation from Eric Hodel <drbrain@segment7.net> added.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9459 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
seki 2005-10-24 15:38:47 +00:00
parent 141666b1e2
commit def653cd80
4 changed files with 449 additions and 93 deletions

View File

@ -1,3 +1,8 @@
Tue Oct 25 00:35:33 2005 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
* lib/rinda/*: RDoc documentation from Eric Hodel
<drbrain@segment7.net> added.
Mon Oct 24 21:14:29 2005 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Oct 24 21:14:29 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in, io.c: use sys/syscall.h if syscall.h is not available. * configure.in, io.c: use sys/syscall.h if syscall.h is not available.

View File

@ -1,100 +1,150 @@
#
# rinda.rb: A Ruby implementation of the Linda distributed computing paradigm.
#
# <i>Introduction to Linda/rinda?</i>
#
# <i>Why is this library separate from <tt>drb</tt>?</i>
#
# <i>Example(s)</i>
#
# (See the samples directory in the Ruby distribution, from 1.8.2 onwards.)
#
require 'drb/drb' require 'drb/drb'
require 'thread' require 'thread'
##
# A module to implement the Linda distributed computing paradigm in Ruby.
# #
# A module to implement the Linda programming paradigm in Ruby. # Rinda is part of DRb (dRuby).
# This is part of +drb+ (dRuby).
# #
# == Example(s)
#
# See the sample/drb/ directory in the Ruby distribution, from 1.8.2 onwards.
#
#--
# TODO
# == Introduction to Linda/rinda?
#
# == Why is this library separate from DRb?
module Rinda module Rinda
##
# Rinda error base class
class RindaError < RuntimeError; end class RindaError < RuntimeError; end
##
# Raised when a hash-based tuple has an invalid key.
class InvalidHashTupleKey < RindaError; end class InvalidHashTupleKey < RindaError; end
##
# Raised when trying to use a canceled tuple.
class RequestCanceledError < ThreadError; end class RequestCanceledError < ThreadError; end
##
# Raised when trying to use an expired tuple.
class RequestExpiredError < ThreadError; end class RequestExpiredError < ThreadError; end
# ##
# A tuple is the elementary object in Rinda programming. # A tuple is the elementary object in Rinda programming.
# Tuples may be matched against templates if the tuple and # Tuples may be matched against templates if the tuple and
# the template are the same size. # the template are the same size.
#
class Tuple class Tuple
# Initialize a tuple with an Array or a Hash.
##
# Creates a new Tuple from +ary_or_hash+ which must be an Array or Hash.
def initialize(ary_or_hash) def initialize(ary_or_hash)
if hash?(ary_or_hash) if hash?(ary_or_hash)
init_with_hash(ary_or_hash) init_with_hash(ary_or_hash)
else else
init_with_ary(ary_or_hash) init_with_ary(ary_or_hash)
end end
end end
##
# The number of elements in the tuple. # The number of elements in the tuple.
def size def size
@tuple.size @tuple.size
end end
##
# Accessor method for elements of the tuple. # Accessor method for elements of the tuple.
def [](k) def [](k)
@tuple[k] @tuple[k]
end end
##
# Fetches item +k+ from the tuple.
def fetch(k) def fetch(k)
@tuple.fetch(k) @tuple.fetch(k)
end end
##
# Iterate through the tuple, yielding the index or key, and the # Iterate through the tuple, yielding the index or key, and the
# value, thus ensuring arrays are iterated similarly to hashes. # value, thus ensuring arrays are iterated similarly to hashes.
def each # FIXME def each # FIXME
if Hash === @tuple if Hash === @tuple
@tuple.each { |k, v| yield(k, v) } @tuple.each { |k, v| yield(k, v) }
else else
@tuple.each_with_index { |v, k| yield(k, v) } @tuple.each_with_index { |v, k| yield(k, v) }
end end
end end
# Return the tuple itself -- i.e the Array or hash. ##
# Return the tuple itself
def value def value
@tuple @tuple
end end
private private
def hash?(ary_or_hash) def hash?(ary_or_hash)
ary_or_hash.respond_to?(:keys) ary_or_hash.respond_to?(:keys)
end end
##
# Munges +ary+ into a valid Tuple.
def init_with_ary(ary) def init_with_ary(ary)
@tuple = Array.new(ary.size) @tuple = Array.new(ary.size)
@tuple.size.times do |i| @tuple.size.times do |i|
@tuple[i] = ary[i] @tuple[i] = ary[i]
end end
end end
##
# Ensures +hash+ is a valid Tuple.
def init_with_hash(hash) def init_with_hash(hash)
@tuple = Hash.new @tuple = Hash.new
hash.each do |k, v| hash.each do |k, v|
raise InvalidHashTupleKey unless String === k raise InvalidHashTupleKey unless String === k
@tuple[k] = v @tuple[k] = v
end end
end end
end end
# ##
# Templates are used to match tuples in Rinda. # Templates are used to match tuples in Rinda.
#
class Template < Tuple class Template < Tuple
# Perform the matching of a tuple against a template. An
# element with a +nil+ value in a template acts as a wildcard, ##
# matching any value in the corresponding position in the tuple. # Matches this template against +tuple+. The +tuple+ must be the same
# size as the template. An element with a +nil+ value in a template acts
# as a wildcard, matching any value in the corresponding position in the
# tuple. Elements of the template match the +tuple+ if the are #== or
# #===.
#
# Template.new([:foo, 5]).match Tuple.new([:foo, 5]) # => true
# Template.new([:foo, nil]).match Tuple.new([:foo, 5]) # => true
# Template.new([String]).match Tuple.new(['hello']) # => true
#
# Template.new([:foo]).match Tuple.new([:foo, 5]) # => false
# Template.new([:foo, 6]).match Tuple.new([:foo, 5]) # => false
# Template.new([:foo, nil]).match Tuple.new([:foo]) # => false
# Template.new([:foo, 6]).match Tuple.new([:foo]) # => false
def match(tuple) def match(tuple)
return false unless tuple.respond_to?(:size) return false unless tuple.respond_to?(:size)
return false unless tuple.respond_to?(:fetch) return false unless tuple.respond_to?(:fetch)
@ -105,84 +155,129 @@ module Rinda
rescue rescue
return false return false
end end
next if v.nil? next if v.nil?
next if v == it next if v == it
next if v === it next if v === it
return false return false
end end
return true return true
end end
##
# Alias for #match. # Alias for #match.
def ===(tuple) def ===(tuple)
match(tuple) match(tuple)
end end
end end
# ##
# <i>Documentation?</i> # <i>Documentation?</i>
#
class DRbObjectTemplate class DRbObjectTemplate
##
# Creates a new DRbObjectTemplate that will match against +uri+ and +ref+.
def initialize(uri=nil, ref=nil) def initialize(uri=nil, ref=nil)
@drb_uri = uri @drb_uri = uri
@drb_ref = ref @drb_ref = ref
end end
##
# This DRbObjectTemplate matches +ro+ if the remote object's drburi and
# drbref are the same. +nil+ is used as a wildcard.
def ===(ro) def ===(ro)
return true if super(ro) return true if super(ro)
unless @drb_uri.nil? unless @drb_uri.nil?
return false unless (@drb_uri === ro.__drburi rescue false) return false unless (@drb_uri === ro.__drburi rescue false)
end end
unless @drb_ref.nil? unless @drb_ref.nil?
return false unless (@drb_ref === ro.__drbref rescue false) return false unless (@drb_ref === ro.__drbref rescue false)
end end
true true
end end
end end
# ##
# TupleSpaceProxy allows a remote Tuplespace to appear as local. # TupleSpaceProxy allows a remote Tuplespace to appear as local.
#
class TupleSpaceProxy class TupleSpaceProxy
##
# Creates a new TupleSpaceProxy to wrap +ts+.
def initialize(ts) def initialize(ts)
@ts = ts @ts = ts
end end
##
# Adds +tuple+ to the proxied TupleSpace. See TupleSpace#write.
def write(tuple, sec=nil) def write(tuple, sec=nil)
@ts.write(tuple, sec) @ts.write(tuple, sec)
end end
##
# Takes +tuple+ from the proxied TupleSpace. See TupleSpace#take.
def take(tuple, sec=nil, &block) def take(tuple, sec=nil, &block)
port = [] port = []
@ts.move(DRbObject.new(port), tuple, sec, &block) @ts.move(DRbObject.new(port), tuple, sec, &block)
port[0] port[0]
end end
##
# Reads +tuple+ from the proxied TupleSpace. See TupleSpace#read.
def read(tuple, sec=nil, &block) def read(tuple, sec=nil, &block)
@ts.read(tuple, sec, &block) @ts.read(tuple, sec, &block)
end end
##
# Reads all tuples matching +tuple+ from the proxied TupleSpace. See
# TupleSpace#read_all.
def read_all(tuple) def read_all(tuple)
@ts.read_all(tuple) @ts.read_all(tuple)
end end
##
# Registers for notifications of event +ev+ on the proxied TupleSpace.
# See TupleSpace#notify
def notify(ev, tuple, sec=nil) def notify(ev, tuple, sec=nil)
@ts.notify(ev, tuple, sec) @ts.notify(ev, tuple, sec)
end end
end end
# ##
# <i>Documentation?</i> # An SimpleRenewer allows a TupleSpace to check if a TupleEntry is still
# # alive.
class SimpleRenewer class SimpleRenewer
include DRbUndumped include DRbUndumped
##
# Creates a new SimpleRenewer that keeps an object alive for another +sec+
# seconds.
def initialize(sec=180) def initialize(sec=180)
@sec = sec @sec = sec
end end
##
# Called by the TupleSpace to check if the object is still alive.
def renew def renew
@sec @sec
end end
end end
end end

View File

@ -6,10 +6,29 @@ require 'rinda/rinda'
require 'thread' require 'thread'
module Rinda module Rinda
##
# The default port Ring discovery will use.
Ring_PORT = 7647 Ring_PORT = 7647
##
# 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.
# 3. The RingServer recieves the UDP packet and connects back to the
# provided DRb URI with the DRb service.
class RingServer class RingServer
include DRbUndumped include DRbUndumped
##
# Advertises +ts+ on the UDP broadcast address at +port+.
def initialize(ts, port=Ring_PORT) def initialize(ts, port=Ring_PORT)
@ts = ts @ts = ts
@soc = UDPSocket.open @soc = UDPSocket.open
@ -18,6 +37,10 @@ module Rinda
@r_service = reply_service @r_service = reply_service
end end
##
# Creates a thread that picks up UDP packets and passes them to do_write
# for decoding.
def write_service def write_service
Thread.new do Thread.new do
loop do loop do
@ -27,6 +50,10 @@ module Rinda
end end
end end
##
# Extracts the response URI from +msg+ and adds it to TupleSpace where it
# will be picked up by +reply_service+ for notification.
def do_write(msg) def do_write(msg)
Thread.new do Thread.new do
begin begin
@ -37,6 +64,9 @@ module Rinda
end end
end end
##
# Creates a thread that notifies waiting clients from the TupleSpace.
def reply_service def reply_service
Thread.new do Thread.new do
loop do loop do
@ -45,15 +75,34 @@ module Rinda
end end
end end
##
# Pulls lookup tuples out of the TupleSpace and sends their DRb object the
# address of the local TupleSpace.
def do_reply def do_reply
tuple = @ts.take([:lookup_ring, DRbObject]) tuple = @ts.take([:lookup_ring, DRbObject])
Thread.new { tuple[1].call(@ts) rescue nil} Thread.new { tuple[1].call(@ts) rescue nil}
rescue rescue
end end
end end
##
# 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.
class RingFinger class RingFinger
@@broadcast_list = ['<broadcast>', 'localhost']
@@finger = nil @@finger = nil
##
# Creates a singleton RingFinger and looks for a RingServer. Returns the
# created RingFinger.
def self.finger def self.finger
unless @@finger unless @@finger
@@finger = self.new @@finger = self.new
@ -62,27 +111,56 @@ module Rinda
@@finger @@finger
end end
##
# Returns the first advertised TupleSpace.
def self.primary def self.primary
finger.primary finger.primary
end end
##
# Contains all discoverd TupleSpaces except for the primary.
def self.to_a def self.to_a
finger.to_a finger.to_a
end end
@@broadcast_list = ['<broadcast>', 'localhost'] ##
# 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+.
def initialize(broadcast_list=@@broadcast_list, port=Ring_PORT) def initialize(broadcast_list=@@broadcast_list, port=Ring_PORT)
@broadcast_list = broadcast_list || ['localhost'] @broadcast_list = broadcast_list || ['localhost']
@port = port @port = port
@primary = nil @primary = nil
@rings = [] @rings = []
end end
attr_accessor :broadcast_list, :port, :primary
##
# Contains all discovered TupleSpaces except for the primary.
def to_a def to_a
@rings @rings
end end
##
# Iterates over all discovered TupleSpaces starting with the primary.
def each def each
lookup_ring_any unless @primary lookup_ring_any unless @primary
return unless @primary return unless @primary
@ -90,6 +168,11 @@ module Rinda
@rings.each { |x| yield(x) } @rings.each { |x| yield(x) }
end end
##
# Looks up RingServers waiting +timeout+ seconds. RingServers will be
# given +block+ as a callback, which will be called with the remote
# TupleSpace.
def lookup_ring(timeout=5, &block) def lookup_ring(timeout=5, &block)
return lookup_ring_any(timeout) unless block_given? return lookup_ring_any(timeout) unless block_given?
@ -108,6 +191,10 @@ module Rinda
sleep(timeout) sleep(timeout)
end end
##
# Returns the first found remote TupleSpace. Any further recovered
# TupleSpaces can be found by calling +to_a+.
def lookup_ring_any(timeout=5) def lookup_ring_any(timeout=5)
queue = Queue.new queue = Queue.new
@ -125,19 +212,38 @@ module Rinda
raise('RingNotFound') if @primary.nil? raise('RingNotFound') if @primary.nil?
@primary @primary
end end
end end
##
# 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].
class RingProvider class RingProvider
##
# Creates a RingProvider that will provide a +klass+ service running on
# +front+, with a +description+. +renewer+ is optional.
def initialize(klass, front, desc, renewer = nil) def initialize(klass, front, desc, renewer = nil)
@tuple = [:name, klass, front, desc] @tuple = [:name, klass, front, desc]
@renewer = renewer || Rinda::SimpleRenewer.new @renewer = renewer || Rinda::SimpleRenewer.new
end end
##
# Advertises this service on the primary remote TupleSpace.
def provide def provide
ts = Rinda::RingFinger.primary ts = Rinda::RingFinger.primary
ts.write(@tuple, @renewer) ts.write(@tuple, @renewer)
end end
end end
end end
if __FILE__ == $0 if __FILE__ == $0
@ -162,3 +268,4 @@ if __FILE__ == $0
end end
end end
end end

View File

@ -1,47 +1,66 @@
#
# = tuplespace: <i>???</i>
#
# <i>Overview of rinda/tuplespace.rb</i>
#
# <i>Example(s)</i>
#
require 'monitor' require 'monitor'
require 'thread' require 'thread'
require 'drb/drb' require 'drb/drb'
require 'rinda/rinda' require 'rinda/rinda'
module Rinda module Rinda
#
##
# A TupleEntry is a Tuple (i.e. a possible entry in some Tuplespace) # A TupleEntry is a Tuple (i.e. a possible entry in some Tuplespace)
# together with expiry and cancellation data. # together with expiry and cancellation data.
#
class TupleEntry class TupleEntry
include DRbUndumped include DRbUndumped
attr_accessor :expires
##
# Creates a TupleEntry based on +ary+ with an optional renewer or expiry
# time +sec+.
#
# A renewer must implement the +renew+ method which returns a Numeric,
# nil, or true to indicate when the tuple has expired.
def initialize(ary, sec=nil) def initialize(ary, sec=nil)
@cancel = false @cancel = false
@expires = nil
@tuple = make_tuple(ary) @tuple = make_tuple(ary)
@renewer = nil @renewer = nil
renew(sec) renew(sec)
end end
attr_accessor :expires
##
# Marks this TupleEntry as canceled.
def cancel def cancel
@cancel = true @cancel = true
end end
##
# A TupleEntry is dead when it is canceled or expired.
def alive? def alive?
!canceled? && !expired? !canceled? && !expired?
end end
##
# Return the object which makes up the tuple itself: the Array # Return the object which makes up the tuple itself: the Array
# or Hash. # or Hash.
def value; @tuple.value; end def value; @tuple.value; end
##
# Returns the canceled status.
def canceled?; @cancel; end def canceled?; @cancel; end
##
# Has this tuple expired? (true/false). # Has this tuple expired? (true/false).
#
# A tuple has expired when its expiry timer based on the +sec+ argument to
# #initialize runs out.
def expired? def expired?
return true unless @expires return true unless @expires
return false if @expires > Time.now return false if @expires > Time.now
@ -51,8 +70,8 @@ module Rinda
return @expires < Time.now return @expires < Time.now
end end
# Reset the expiry data according to the supplied argument. If ##
# the argument is: # Reset the expiry time according to +sec_or_renewer+.
# #
# +nil+:: it is set to expire in the far future. # +nil+:: it is set to expire in the far future.
# +false+:: it has expired. # +false+:: it has expired.
@ -60,19 +79,19 @@ module Rinda
# #
# Otherwise the argument refers to some kind of renewer object # Otherwise the argument refers to some kind of renewer object
# which will reset its expiry time. # which will reset its expiry time.
def renew(sec_or_renewer) def renew(sec_or_renewer)
sec, @renewer = get_renewer(sec_or_renewer) sec, @renewer = get_renewer(sec_or_renewer)
@expires = make_expires(sec) @expires = make_expires(sec)
end end
# Create an expiry time. Called with: ##
# # Returns an expiry Time based on +sec+ which can be one of:
# +true+:: the expiry time is the start of 1970 (i.e. expired). # Numeric:: +sec+ seconds into the future
# +nil+:: it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when # +true+:: the expiry time is the start of 1970 (i.e. expired)
# UNIX clocks will die) # +nil+:: it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when
# # UNIX clocks will die)
# otherwise it is +sec+ seconds into the
# future.
def make_expires(sec=nil) def make_expires(sec=nil)
case sec case sec
when Numeric when Numeric
@ -84,29 +103,43 @@ module Rinda
end end
end end
# Accessor method for the tuple. ##
# Retrieves +key+ from the tuple.
def [](key) def [](key)
@tuple[key] @tuple[key]
end end
##
# Fetches +key+ from the tuple.
def fetch(key) def fetch(key)
@tuple.fetch(key) @tuple.fetch(key)
end end
##
# The size of the tuple. # The size of the tuple.
def size def size
@tuple.size @tuple.size
end end
# Create a new tuple from the supplied object (array-like). ##
# Creates a Rinda::Tuple for +ary+.
def make_tuple(ary) def make_tuple(ary)
Rinda::Tuple.new(ary) Rinda::Tuple.new(ary)
end end
private private
# Given +true+, +nil+, or +Numeric+, returns that (suitable input to
# make_expires) and +nil+ (no actual +renewer+), else it return the ##
# time data from the supplied +renewer+. # Returns a valid argument to make_expires and the renewer or nil.
#
# Given +true+, +nil+, or Numeric, returns that value and +nil+ (no actual
# renewer). Otherwise it returns an expiry value from calling +it.renew+
# and the renewer.
def get_renewer(it) def get_renewer(it)
case it case it
when Numeric, true, nil when Numeric, true, nil
@ -119,35 +152,42 @@ module Rinda
end end
end end
end end
end end
# ##
# The same as a TupleEntry but with methods to do matching. # A TemplateEntry is a Template together with expiry and cancellation data.
#
class TemplateEntry < TupleEntry class TemplateEntry < TupleEntry
##
# Matches this TemplateEntry against +tuple+. See Template#match for
# details on how a Template matches a Tuple.
def match(tuple) def match(tuple)
@tuple.match(tuple) @tuple.match(tuple)
end end
alias === match alias === match
# Create a new Template from the supplied object. def make_tuple(ary) # :nodoc:
def make_tuple(ary)
Rinda::Template.new(ary) Rinda::Template.new(ary)
end end
end end
# ##
# <i>Documentation?</i> # <i>Documentation?</i>
#
class WaitTemplateEntry < TemplateEntry class WaitTemplateEntry < TemplateEntry
attr_reader :found
def initialize(place, ary, expires=nil) def initialize(place, ary, expires=nil)
super(ary, expires) super(ary, expires)
@place = place @place = place
@cond = place.new_cond @cond = place.new_cond
@found = nil @found = nil
end end
attr_reader :found
def cancel def cancel
super super
@ -168,12 +208,39 @@ module Rinda
@cond.signal @cond.signal
end end
end end
end end
##
# A NotifyTemplateEntry is returned by TupleSpace#notify and is notified of
# TupleSpace changes. You may receive either your subscribed event or the
# 'close' event when iterating over notifications.
# #
# <i>Documentation?</i> # See TupleSpace#notify_event for valid notification types.
# #
# == Example
#
# ts = Rinda::TupleSpace.new
# observer = ts.notify 'write', [nil]
#
# Thread.start do
# observer.each { |t| p t }
# end
#
# 3.times { |i| ts.write [i] }
#
# Outputs:
#
# ['write', [0]]
# ['write', [1]]
# ['write', [2]]
class NotifyTemplateEntry < TemplateEntry class NotifyTemplateEntry < TemplateEntry
##
# Creates a new NotifyTemplateEntry that watches +place+ for +event+s that
# match +tuple+.
def initialize(place, event, tuple, expires=nil) def initialize(place, event, tuple, expires=nil)
ary = [event, Rinda::Template.new(tuple)] ary = [event, Rinda::Template.new(tuple)]
super(ary, expires) super(ary, expires)
@ -181,10 +248,17 @@ module Rinda
@done = false @done = false
end end
##
# Called by TupleSpace to notify this NotifyTemplateEntry of a new event.
def notify(ev) def notify(ev)
@queue.push(ev) @queue.push(ev)
end end
##
# Retrieves a notification. Raises RequestExpiredError when this
# NotifyTemplateEntry expires.
def pop def pop
raise RequestExpiredError if @done raise RequestExpiredError if @done
it = @queue.pop it = @queue.pop
@ -192,7 +266,10 @@ module Rinda
return it return it
end end
def each ##
# Yields event/tuple pairs until this NotifyTemplateEntry expires.
def each # :yields: event, tuple
while !@done while !@done
it = pop it = pop
yield(it) yield(it)
@ -201,17 +278,22 @@ module Rinda
ensure ensure
cancel cancel
end end
end end
# ##
# TupleBag is an unordered collection of tuples. It is the basis # TupleBag is an unordered collection of tuples. It is the basis
# of Tuplespace. # of Tuplespace.
#
class TupleBag class TupleBag
def initialize
def initialize # :nodoc:
@hash = {} @hash = {}
end end
##
# +true+ if the TupleBag to see if it has any expired entries.
def has_expires? def has_expires?
@hash.each do |k, v| @hash.each do |k, v|
v.each do |tuple| v.each do |tuple|
@ -221,43 +303,55 @@ module Rinda
false false
end end
# Add the object to the TupleBag. ##
# Add +ary+ to the TupleBag.
def push(ary) def push(ary)
size = ary.size size = ary.size
@hash[size] ||= [] @hash[size] ||= []
@hash[size].push(ary) @hash[size].push(ary)
end end
# Remove the object from the TupleBag. ##
# Removes +ary+ from the TupleBag.
def delete(ary) def delete(ary)
size = ary.size size = ary.size
@hash.fetch(size, []).delete(ary) @hash.fetch(size, []).delete(ary)
end end
# Finds all tuples that match the template and are alive. ##
# Finds all live tuples that match +template+.
def find_all(template) def find_all(template)
@hash.fetch(template.size, []).find_all do |tuple| @hash.fetch(template.size, []).find_all do |tuple|
tuple.alive? && template.match(tuple) tuple.alive? && template.match(tuple)
end end
end end
# Finds a template that matches and is alive. ##
# Finds a live tuple that matches +template+.
def find(template) def find(template)
@hash.fetch(template.size, []).find do |tuple| @hash.fetch(template.size, []).find do |tuple|
tuple.alive? && template.match(tuple) tuple.alive? && template.match(tuple)
end end
end end
# Finds all tuples in the TupleBag which when treated as ##
# templates, match the supplied tuple and are alive. # Finds all tuples in the TupleBag which when treated as templates, match
# +tuple+ and are alive.
def find_all_template(tuple) def find_all_template(tuple)
@hash.fetch(tuple.size, []).find_all do |template| @hash.fetch(tuple.size, []).find_all do |template|
template.alive? && template.match(tuple) template.alive? && template.match(tuple)
end end
end end
# Delete tuples which are not alive from the TupleBag. Returns ##
# the list of tuples so deleted. # Delete tuples which dead tuples from the TupleBag, returning the deleted
# tuples.
def delete_unless_alive def delete_unless_alive
deleted = [] deleted = []
@hash.keys.each do |size| @hash.keys.each do |size|
@ -273,15 +367,28 @@ module Rinda
end end
deleted deleted
end end
end end
# ##
# The Tuplespace manages access to the tuples it contains, # The Tuplespace manages access to the tuples it contains,
# ensuring mutual exclusion requirements are met. # ensuring mutual exclusion requirements are met.
# #
# The +sec+ option for the write, take, move, read and notify methods may
# either be a number of seconds or a Renewer object.
class TupleSpace class TupleSpace
include DRbUndumped include DRbUndumped
include MonitorMixin include MonitorMixin
##
# Creates a new TupleSpace. +period+ is used to control how often to look
# for dead tuples after modifications to the TupleSpace.
#
# If no dead tuples are found +period+ seconds after the last
# modification, the TupleSpace will stop looking for dead tuples.
def initialize(period=60) def initialize(period=60)
super() super()
@bag = TupleBag.new @bag = TupleBag.new
@ -292,7 +399,9 @@ module Rinda
@keeper = nil @keeper = nil
end end
# Put a tuple into the tuplespace. ##
# Adds +tuple+
def write(tuple, sec=nil) def write(tuple, sec=nil)
entry = TupleEntry.new(tuple, sec) entry = TupleEntry.new(tuple, sec)
start_keeper start_keeper
@ -317,11 +426,16 @@ module Rinda
entry entry
end end
# Remove an entry from the Tuplespace. ##
# Removes +tuple+
def take(tuple, sec=nil, &block) def take(tuple, sec=nil, &block)
move(nil, tuple, sec, &block) move(nil, tuple, sec, &block)
end end
##
# Moves +tuple+ to +port+.
def move(port, tuple, sec=nil) def move(port, tuple, sec=nil)
template = WaitTemplateEntry.new(self, tuple, sec) template = WaitTemplateEntry.new(self, tuple, sec)
yield(template) if block_given? yield(template) if block_given?
@ -356,6 +470,9 @@ module Rinda
end end
end end
##
# Reads +tuple+, but does not remove it.
def read(tuple, sec=nil) def read(tuple, sec=nil)
template = WaitTemplateEntry.new(self, tuple, sec) template = WaitTemplateEntry.new(self, tuple, sec)
yield(template) if block_given? yield(template) if block_given?
@ -377,6 +494,9 @@ module Rinda
end end
end end
##
# Returns all tuples matching +tuple+. Does not remove the found tuples.
def read_all(tuple) def read_all(tuple)
template = WaitTemplateEntry.new(self, tuple, nil) template = WaitTemplateEntry.new(self, tuple, nil)
synchronize do synchronize do
@ -387,6 +507,18 @@ module Rinda
end end
end end
##
# Registers for notifications of +event+. Returns a NotifyTemplateEntry.
# See NotifyTemplateEntry for examples of how to listen for notifications.
#
# +event+ can be:
# 'write':: A tuple was added
# 'take':: A tuple was taken or moved
# 'delete':: A tuple was lost after being overwritten or expiring
#
# The TupleSpace will also notify you of the 'close' event when the
# NotifyTemplateEntry has expired.
def notify(event, tuple, sec=nil) def notify(event, tuple, sec=nil)
template = NotifyTemplateEntry.new(self, event, tuple, sec) template = NotifyTemplateEntry.new(self, event, tuple, sec)
synchronize do synchronize do
@ -396,6 +528,10 @@ module Rinda
end end
private private
##
# Removes dead tuples.
def keep_clean def keep_clean
synchronize do synchronize do
@read_waiter.delete_unless_alive.each do |e| @read_waiter.delete_unless_alive.each do |e|
@ -413,6 +549,10 @@ module Rinda
end end
end end
##
# Notifies all registered listeners for +event+ of a status change of
# +tuple+.
def notify_event(event, tuple) def notify_event(event, tuple)
ev = [event, tuple] ev = [event, tuple]
@notify_waiter.find_all_template(ev).each do |template| @notify_waiter.find_all_template(ev).each do |template|
@ -420,6 +560,9 @@ module Rinda
end end
end end
##
# Creates a thread that scans the tuplespace for expired tuples.
def start_keeper def start_keeper
return if @keeper && @keeper.alive? return if @keeper && @keeper.alive?
@keeper = Thread.new do @keeper = Thread.new do
@ -430,11 +573,17 @@ module Rinda
end end
end end
##
# Checks the tuplespace to see if it needs cleaning.
def need_keeper? def need_keeper?
return true if @bag.has_expires? return true if @bag.has_expires?
return true if @read_waiter.has_expires? return true if @read_waiter.has_expires?
return true if @take_waiter.has_expires? return true if @take_waiter.has_expires?
return true if @notify_waiter.has_expires? return true if @notify_waiter.has_expires?
end end
end end
end end