2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-05 08:23:33 -04:00
|
|
|
require 'monitor'
|
|
|
|
require 'drb/drb'
|
2018-11-02 13:52:33 -04:00
|
|
|
require_relative 'rinda'
|
2008-07-26 20:04:38 -04:00
|
|
|
require 'forwardable'
|
2003-10-05 08:23:33 -04:00
|
|
|
|
|
|
|
module Rinda
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# A TupleEntry is a Tuple (i.e. a possible entry in some Tuplespace)
|
|
|
|
# together with expiry and cancellation data.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class TupleEntry
|
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
|
|
|
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.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(ary, sec=nil)
|
|
|
|
@cancel = false
|
2005-10-24 11:38:47 -04:00
|
|
|
@expires = nil
|
2005-09-24 22:56:26 -04:00
|
|
|
@tuple = make_tuple(ary)
|
2003-10-05 08:23:33 -04:00
|
|
|
@renewer = nil
|
|
|
|
renew(sec)
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Marks this TupleEntry as canceled.
|
2003-10-05 08:23:33 -04:00
|
|
|
|
|
|
|
def cancel
|
|
|
|
@cancel = true
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# A TupleEntry is dead when it is canceled or expired.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def alive?
|
|
|
|
!canceled? && !expired?
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# Return the object which makes up the tuple itself: the Array
|
|
|
|
# or Hash.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2005-09-24 22:56:26 -04:00
|
|
|
def value; @tuple.value; end
|
2004-02-17 17:29:07 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Returns the canceled status.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def canceled?; @cancel; end
|
2004-02-17 17:29:07 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# Has this tuple expired? (true/false).
|
2005-10-24 11:38:47 -04:00
|
|
|
#
|
|
|
|
# A tuple has expired when its expiry timer based on the +sec+ argument to
|
|
|
|
# #initialize runs out.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def expired?
|
|
|
|
return true unless @expires
|
|
|
|
return false if @expires > Time.now
|
|
|
|
return true if @renewer.nil?
|
|
|
|
renew(@renewer)
|
|
|
|
return true unless @expires
|
|
|
|
return @expires < Time.now
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2009-03-05 22:56:38 -05:00
|
|
|
# Reset the expiry time according to +sec_or_renewer+.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# +nil+:: it is set to expire in the far future.
|
2014-05-02 03:25:16 -04:00
|
|
|
# +true+:: it has expired.
|
2004-02-17 17:29:07 -05:00
|
|
|
# Numeric:: it will expire in that many seconds.
|
|
|
|
#
|
|
|
|
# Otherwise the argument refers to some kind of renewer object
|
2009-03-05 22:56:38 -05:00
|
|
|
# which will reset its expiry time.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def renew(sec_or_renewer)
|
|
|
|
sec, @renewer = get_renewer(sec_or_renewer)
|
|
|
|
@expires = make_expires(sec)
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Returns an expiry Time based on +sec+ which can be one of:
|
|
|
|
# Numeric:: +sec+ seconds into the future
|
|
|
|
# +true+:: the expiry time is the start of 1970 (i.e. expired)
|
|
|
|
# +nil+:: it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when
|
|
|
|
# UNIX clocks will die)
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def make_expires(sec=nil)
|
|
|
|
case sec
|
|
|
|
when Numeric
|
2005-09-24 22:56:26 -04:00
|
|
|
Time.now + sec
|
2003-10-05 08:23:33 -04:00
|
|
|
when true
|
2005-09-24 22:56:26 -04:00
|
|
|
Time.at(1)
|
2003-10-05 08:23:33 -04:00
|
|
|
when nil
|
2005-09-24 22:56:26 -04:00
|
|
|
Time.at(2**31-1)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Retrieves +key+ from the tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def [](key)
|
2005-09-24 22:56:26 -04:00
|
|
|
@tuple[key]
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Fetches +key+ from the tuple.
|
|
|
|
|
2004-04-06 11:26:25 -04:00
|
|
|
def fetch(key)
|
2005-09-24 22:56:26 -04:00
|
|
|
@tuple.fetch(key)
|
2004-04-06 11:26:25 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# The size of the tuple.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def size
|
2005-09-24 22:56:26 -04:00
|
|
|
@tuple.size
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Creates a Rinda::Tuple for +ary+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def make_tuple(ary)
|
|
|
|
Rinda::Tuple.new(ary)
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
private
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def get_renewer(it)
|
|
|
|
case it
|
|
|
|
when Numeric, true, nil
|
2005-09-24 22:56:26 -04:00
|
|
|
return it, nil
|
2003-10-05 08:23:33 -04:00
|
|
|
else
|
2005-09-24 22:56:26 -04:00
|
|
|
begin
|
|
|
|
return it.renew, it
|
|
|
|
rescue Exception
|
|
|
|
return it, nil
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
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
|
|
|
##
|
|
|
|
# A TemplateEntry is a Template together with expiry and cancellation data.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class TemplateEntry < TupleEntry
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Matches this TemplateEntry against +tuple+. See Template#match for
|
|
|
|
# details on how a Template matches a Tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def match(tuple)
|
2005-09-24 22:56:26 -04:00
|
|
|
@tuple.match(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-09-24 22:56:26 -04:00
|
|
|
alias === match
|
2003-10-05 08:23:33 -04:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
def make_tuple(ary) # :nodoc:
|
2003-10-05 08:23:33 -04:00
|
|
|
Rinda::Template.new(ary)
|
|
|
|
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
|
|
|
##
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# <i>Documentation?</i>
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class WaitTemplateEntry < TemplateEntry
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
attr_reader :found
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(place, ary, expires=nil)
|
|
|
|
super(ary, expires)
|
|
|
|
@place = place
|
|
|
|
@cond = place.new_cond
|
|
|
|
@found = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancel
|
|
|
|
super
|
|
|
|
signal
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait
|
|
|
|
@cond.wait
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def read(tuple)
|
|
|
|
@found = tuple
|
|
|
|
signal
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def signal
|
|
|
|
@place.synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
@cond.signal
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
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
|
|
|
##
|
|
|
|
# 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.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# See TupleSpace#notify_event for valid notification types.
|
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
|
|
|
# ts = Rinda::TupleSpace.new
|
|
|
|
# observer = ts.notify 'write', [nil]
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# Thread.start do
|
|
|
|
# observer.each { |t| p t }
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# 3.times { |i| ts.write [i] }
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# Outputs:
|
|
|
|
#
|
|
|
|
# ['write', [0]]
|
|
|
|
# ['write', [1]]
|
|
|
|
# ['write', [2]]
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class NotifyTemplateEntry < TemplateEntry
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new NotifyTemplateEntry that watches +place+ for +event+s that
|
|
|
|
# match +tuple+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(place, event, tuple, expires=nil)
|
|
|
|
ary = [event, Rinda::Template.new(tuple)]
|
|
|
|
super(ary, expires)
|
2016-08-30 02:22:30 -04:00
|
|
|
@queue = Thread::Queue.new
|
2003-10-05 08:23:33 -04:00
|
|
|
@done = false
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Called by TupleSpace to notify this NotifyTemplateEntry of a new event.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def notify(ev)
|
|
|
|
@queue.push(ev)
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Retrieves a notification. Raises RequestExpiredError when this
|
|
|
|
# NotifyTemplateEntry expires.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def pop
|
|
|
|
raise RequestExpiredError if @done
|
|
|
|
it = @queue.pop
|
|
|
|
@done = true if it[0] == 'close'
|
|
|
|
return it
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Yields event/tuple pairs until this NotifyTemplateEntry expires.
|
|
|
|
|
|
|
|
def each # :yields: event, tuple
|
2003-10-05 08:23:33 -04:00
|
|
|
while !@done
|
|
|
|
it = pop
|
|
|
|
yield(it)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
rescue
|
2003-10-05 08:23:33 -04:00
|
|
|
ensure
|
|
|
|
cancel
|
|
|
|
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
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# TupleBag is an unordered collection of tuples. It is the basis
|
|
|
|
# of Tuplespace.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class TupleBag
|
2008-07-26 20:04:38 -04:00
|
|
|
class TupleBin
|
|
|
|
extend Forwardable
|
|
|
|
def_delegators '@bin', :find_all, :delete_if, :each, :empty?
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@bin = []
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def add(tuple)
|
|
|
|
@bin.push(tuple)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def delete(tuple)
|
|
|
|
idx = @bin.rindex(tuple)
|
|
|
|
@bin.delete_at(idx) if idx
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* ext/pathname/lib/pathname.rb, ext/tk/lib/multi-tk.rb,
ext/tk/sample/demos-en/widget, lib/benchmark.rb, lib/irb/cmd/fork.rb,
lib/mkmf.rb, lib/net/ftp.rb, lib/net/smtp.rb, lib/open3.rb,
lib/pstore.rb, lib/rexml/element.rb, lib/rexml/light/node.rb,
lib/rinda/tuplespace.rb, lib/rss/maker/base.rb,
lib/rss/maker/entry.rb, lib/scanf.rb, lib/set.rb, lib/shell.rb,
lib/shell/command-processor.rb, lib/shell/process-controller.rb,
lib/shell/system-command.rb, lib/uri/common.rb: remove unused block
arguments to avoid creating Proc objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-11-05 03:37:47 -04:00
|
|
|
def find
|
2008-07-26 20:04:38 -04:00
|
|
|
@bin.reverse_each do |x|
|
|
|
|
return x if yield(x)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
def initialize # :nodoc:
|
2003-10-05 08:23:33 -04:00
|
|
|
@hash = {}
|
2008-08-13 04:03:30 -04:00
|
|
|
@enum = enum_for(:each_entry)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# +true+ if the TupleBag to see if it has any expired entries.
|
|
|
|
|
2005-02-28 10:41:14 -05:00
|
|
|
def has_expires?
|
2008-07-26 20:04:38 -04:00
|
|
|
@enum.find do |tuple|
|
|
|
|
tuple.expires
|
2005-02-28 10:41:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2008-07-26 20:04:38 -04:00
|
|
|
# Add +tuple+ to the TupleBag.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def push(tuple)
|
|
|
|
key = bin_key(tuple)
|
|
|
|
@hash[key] ||= TupleBin.new
|
|
|
|
@hash[key].add(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2008-07-26 20:04:38 -04:00
|
|
|
# Removes +tuple+ from the TupleBag.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def delete(tuple)
|
|
|
|
key = bin_key(tuple)
|
|
|
|
bin = @hash[key]
|
|
|
|
return nil unless bin
|
|
|
|
bin.delete(tuple)
|
|
|
|
@hash.delete(key) if bin.empty?
|
|
|
|
tuple
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Finds all live tuples that match +template+.
|
2003-10-05 08:23:33 -04:00
|
|
|
def find_all(template)
|
2008-07-26 20:04:38 -04:00
|
|
|
bin_for_find(template).find_all do |tuple|
|
2005-09-24 22:56:26 -04:00
|
|
|
tuple.alive? && template.match(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Finds a live tuple that matches +template+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def find(template)
|
2008-07-26 20:04:38 -04:00
|
|
|
bin_for_find(template).find do |tuple|
|
2005-09-24 22:56:26 -04:00
|
|
|
tuple.alive? && template.match(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Finds all tuples in the TupleBag which when treated as templates, match
|
|
|
|
# +tuple+ and are alive.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def find_all_template(tuple)
|
2008-07-26 20:04:38 -04:00
|
|
|
@enum.find_all do |template|
|
2005-09-24 22:56:26 -04:00
|
|
|
template.alive? && template.match(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Delete tuples which dead tuples from the TupleBag, returning the deleted
|
|
|
|
# tuples.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def delete_unless_alive
|
|
|
|
deleted = []
|
2008-07-26 20:04:38 -04:00
|
|
|
@hash.each do |key, bin|
|
|
|
|
bin.delete_if do |tuple|
|
2005-09-24 22:56:26 -04:00
|
|
|
if tuple.alive?
|
2008-07-26 20:04:38 -04:00
|
|
|
false
|
2005-09-24 22:56:26 -04:00
|
|
|
else
|
|
|
|
deleted.push(tuple)
|
2008-07-26 20:04:38 -04:00
|
|
|
true
|
2005-09-24 22:56:26 -04:00
|
|
|
end
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
deleted
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
private
|
|
|
|
def each_entry(&blk)
|
|
|
|
@hash.each do |k, v|
|
|
|
|
v.each(&blk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bin_key(tuple)
|
|
|
|
head = tuple[0]
|
|
|
|
if head.class == Symbol
|
|
|
|
return head
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bin_for_find(template)
|
|
|
|
key = bin_key(template)
|
|
|
|
key ? @hash.fetch(key, []) : @enum
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# The Tuplespace manages access to the tuples it contains,
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# ensuring mutual exclusion requirements are met.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# The +sec+ option for the write, take, move, read and notify methods may
|
|
|
|
# either be a number of seconds or a Renewer object.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class TupleSpace
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
include DRbUndumped
|
|
|
|
include MonitorMixin
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
def initialize(period=60)
|
2003-10-05 08:23:33 -04:00
|
|
|
super()
|
|
|
|
@bag = TupleBag.new
|
|
|
|
@read_waiter = TupleBag.new
|
|
|
|
@take_waiter = TupleBag.new
|
|
|
|
@notify_waiter = TupleBag.new
|
2004-02-17 17:29:07 -05:00
|
|
|
@period = period
|
2005-02-28 10:41:14 -05:00
|
|
|
@keeper = nil
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Adds +tuple+
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def write(tuple, sec=nil)
|
2008-07-26 20:04:38 -04:00
|
|
|
entry = create_entry(tuple, sec)
|
2003-10-05 08:23:33 -04:00
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
if entry.expired?
|
|
|
|
@read_waiter.find_all_template(entry).each do |template|
|
|
|
|
template.read(tuple)
|
|
|
|
end
|
|
|
|
notify_event('write', entry.value)
|
|
|
|
notify_event('delete', entry.value)
|
|
|
|
else
|
|
|
|
@bag.push(entry)
|
2008-07-26 20:04:38 -04:00
|
|
|
start_keeper if entry.expires
|
2005-09-24 22:56:26 -04:00
|
|
|
@read_waiter.find_all_template(entry).each do |template|
|
|
|
|
template.read(tuple)
|
|
|
|
end
|
|
|
|
@take_waiter.find_all_template(entry).each do |template|
|
|
|
|
template.signal
|
|
|
|
end
|
|
|
|
notify_event('write', entry.value)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
entry
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Removes +tuple+
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def take(tuple, sec=nil, &block)
|
|
|
|
move(nil, tuple, sec, &block)
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Moves +tuple+ to +port+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def move(port, tuple, sec=nil)
|
|
|
|
template = WaitTemplateEntry.new(self, tuple, sec)
|
|
|
|
yield(template) if block_given?
|
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
entry = @bag.find(template)
|
|
|
|
if entry
|
|
|
|
port.push(entry.value) if port
|
|
|
|
@bag.delete(entry)
|
|
|
|
notify_event('take', entry.value)
|
2013-03-24 18:09:35 -04:00
|
|
|
return port ? nil : entry.value
|
2005-09-24 22:56:26 -04:00
|
|
|
end
|
2004-02-15 05:07:18 -05:00
|
|
|
raise RequestExpiredError if template.expired?
|
2003-10-05 08:23:33 -04:00
|
|
|
|
2005-09-24 22:56:26 -04:00
|
|
|
begin
|
|
|
|
@take_waiter.push(template)
|
2008-07-26 20:04:38 -04:00
|
|
|
start_keeper if template.expires
|
2005-09-24 22:56:26 -04:00
|
|
|
while true
|
|
|
|
raise RequestCanceledError if template.canceled?
|
|
|
|
raise RequestExpiredError if template.expired?
|
|
|
|
entry = @bag.find(template)
|
|
|
|
if entry
|
|
|
|
port.push(entry.value) if port
|
|
|
|
@bag.delete(entry)
|
|
|
|
notify_event('take', entry.value)
|
2013-03-24 18:09:35 -04:00
|
|
|
return port ? nil : entry.value
|
2005-09-24 22:56:26 -04:00
|
|
|
end
|
|
|
|
template.wait
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@take_waiter.delete(template)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Reads +tuple+, but does not remove it.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def read(tuple, sec=nil)
|
|
|
|
template = WaitTemplateEntry.new(self, tuple, sec)
|
|
|
|
yield(template) if block_given?
|
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
entry = @bag.find(template)
|
|
|
|
return entry.value if entry
|
2004-02-15 05:07:18 -05:00
|
|
|
raise RequestExpiredError if template.expired?
|
2003-10-05 08:23:33 -04:00
|
|
|
|
2005-09-24 22:56:26 -04:00
|
|
|
begin
|
|
|
|
@read_waiter.push(template)
|
2008-07-26 20:04:38 -04:00
|
|
|
start_keeper if template.expires
|
2005-09-24 22:56:26 -04:00
|
|
|
template.wait
|
|
|
|
raise RequestCanceledError if template.canceled?
|
|
|
|
raise RequestExpiredError if template.expired?
|
|
|
|
return template.found
|
|
|
|
ensure
|
|
|
|
@read_waiter.delete(template)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Returns all tuples matching +tuple+. Does not remove the found tuples.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def read_all(tuple)
|
|
|
|
template = WaitTemplateEntry.new(self, tuple, nil)
|
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
entry = @bag.find_all(template)
|
|
|
|
entry.collect do |e|
|
|
|
|
e.value
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def notify(event, tuple, sec=nil)
|
|
|
|
template = NotifyTemplateEntry.new(self, event, tuple, sec)
|
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
@notify_waiter.push(template)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
template
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
private
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def create_entry(tuple, sec)
|
|
|
|
TupleEntry.new(tuple, sec)
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Removes dead tuples.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def keep_clean
|
|
|
|
synchronize do
|
2005-09-24 22:56:26 -04:00
|
|
|
@read_waiter.delete_unless_alive.each do |e|
|
|
|
|
e.signal
|
|
|
|
end
|
|
|
|
@take_waiter.delete_unless_alive.each do |e|
|
|
|
|
e.signal
|
|
|
|
end
|
|
|
|
@notify_waiter.delete_unless_alive.each do |e|
|
|
|
|
e.notify(['close'])
|
|
|
|
end
|
|
|
|
@bag.delete_unless_alive.each do |e|
|
|
|
|
notify_event('delete', e.value)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Notifies all registered listeners for +event+ of a status change of
|
|
|
|
# +tuple+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def notify_event(event, tuple)
|
|
|
|
ev = [event, tuple]
|
|
|
|
@notify_waiter.find_all_template(ev).each do |template|
|
2005-09-24 22:56:26 -04:00
|
|
|
template.notify(ev)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Creates a thread that scans the tuplespace for expired tuples.
|
|
|
|
|
2005-02-28 10:41:14 -05:00
|
|
|
def start_keeper
|
|
|
|
return if @keeper && @keeper.alive?
|
|
|
|
@keeper = Thread.new do
|
2008-07-26 20:04:38 -04:00
|
|
|
while true
|
2005-02-28 10:41:14 -05:00
|
|
|
sleep(@period)
|
2008-07-26 20:04:38 -04:00
|
|
|
synchronize do
|
|
|
|
break unless need_keeper?
|
|
|
|
keep_clean
|
|
|
|
end
|
2005-09-24 22:56:26 -04:00
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
2005-02-28 10:41:14 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Checks the tuplespace to see if it needs cleaning.
|
|
|
|
|
2005-02-28 10:41:14 -05:00
|
|
|
def need_keeper?
|
|
|
|
return true if @bag.has_expires?
|
|
|
|
return true if @read_waiter.has_expires?
|
|
|
|
return true if @take_waiter.has_expires?
|
|
|
|
return true if @notify_waiter.has_expires?
|
|
|
|
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
|
2005-10-24 11:38:47 -04:00
|
|
|
|