2004-02-17 17:29:07 -05: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
|
|
|
# rinda.rb: A Ruby implementation of the Linda distributed computing paradigm.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# <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.)
|
|
|
|
#
|
|
|
|
|
2004-04-05 11:14:20 -04:00
|
|
|
require 'drb/drb'
|
2003-10-05 08:23:33 -04:00
|
|
|
require 'thread'
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# A module to implement the Linda programming paradigm in Ruby.
|
|
|
|
# This is part of +drb+ (dRuby).
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
module Rinda
|
2004-04-06 11:26:25 -04:00
|
|
|
class RindaError < RuntimeError; end
|
|
|
|
class InvalidHashTupleKey < RindaError; end
|
2003-10-05 08:23:33 -04:00
|
|
|
class RequestCanceledError < ThreadError; end
|
|
|
|
class RequestExpiredError < ThreadError; end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# A tuple is the elementary object in Rinda programming.
|
|
|
|
# Tuples may be matched against templates if the tuple and
|
|
|
|
# the template are the same size.
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
class Tuple
|
2004-02-17 17:29:07 -05:00
|
|
|
# Initialize a tuple with an Array or a Hash.
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(ary_or_hash)
|
|
|
|
if Hash === ary_or_hash
|
|
|
|
init_with_hash(ary_or_hash)
|
|
|
|
else
|
|
|
|
init_with_ary(ary_or_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
# The number of elements in the tuple.
|
2003-10-05 08:23:33 -04:00
|
|
|
def size
|
|
|
|
@tuple.size
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
# Accessor method for elements of the tuple.
|
2003-10-05 08:23:33 -04:00
|
|
|
def [](k)
|
|
|
|
@tuple[k]
|
|
|
|
end
|
|
|
|
|
2004-04-06 11:26:25 -04:00
|
|
|
def fetch(k)
|
|
|
|
@tuple.fetch(k)
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
# Iterate through the tuple, yielding the index or key, and the
|
|
|
|
# value, thus ensuring arrays are iterated similarly to hashes.
|
2003-10-05 08:23:33 -04:00
|
|
|
def each # FIXME
|
|
|
|
if Hash === @tuple
|
|
|
|
@tuple.each { |k, v| yield(k, v) }
|
|
|
|
else
|
|
|
|
@tuple.each_with_index { |v, k| yield(k, v) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
# Return the tuple itself -- i.e the Array or hash.
|
2003-10-05 08:23:33 -04:00
|
|
|
def value
|
|
|
|
@tuple
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def init_with_ary(ary)
|
2004-04-21 10:05:40 -04:00
|
|
|
@tuple = Array.new(ary.size)
|
2003-10-05 08:23:33 -04:00
|
|
|
@tuple.size.times do |i|
|
|
|
|
@tuple[i] = ary[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_with_hash(hash)
|
|
|
|
@tuple = Hash.new
|
|
|
|
hash.each do |k, v|
|
2004-04-06 11:26:25 -04:00
|
|
|
raise InvalidHashTupleKey unless String === k
|
2003-10-05 08:23:33 -04:00
|
|
|
@tuple[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# Templates are used to match tuples in Rinda.
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
class Template < Tuple
|
2004-02-17 17:29:07 -05:00
|
|
|
# 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.
|
2003-10-05 08:23:33 -04:00
|
|
|
def match(tuple)
|
|
|
|
return false unless tuple.respond_to?(:size)
|
2004-04-06 11:26:25 -04:00
|
|
|
return false unless tuple.respond_to?(:fetch)
|
2004-04-21 10:05:40 -04:00
|
|
|
return false unless self.size == tuple.size
|
2003-10-05 08:23:33 -04:00
|
|
|
each do |k, v|
|
2004-04-06 11:26:25 -04:00
|
|
|
begin
|
|
|
|
it = tuple.fetch(k)
|
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
next if v.nil?
|
2004-04-13 10:06:50 -04:00
|
|
|
next if v == it
|
|
|
|
next if v === it
|
|
|
|
return false
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
# Alias for #match.
|
2003-10-05 08:23:33 -04:00
|
|
|
def ===(tuple)
|
|
|
|
match(tuple)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# <i>Documentation?</i>
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
class DRbObjectTemplate
|
|
|
|
def initialize(uri=nil, ref=nil)
|
|
|
|
@drb_uri = uri
|
|
|
|
@drb_ref = ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def ===(ro)
|
|
|
|
return true if super(ro)
|
|
|
|
unless @drb_uri.nil?
|
|
|
|
return false unless (@drb_uri === ro.__drburi rescue false)
|
|
|
|
end
|
|
|
|
unless @drb_ref.nil?
|
|
|
|
return false unless (@drb_ref === ro.__drbref rescue false)
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# TupleSpaceProxy allows a remote Tuplespace to appear as local.
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
class TupleSpaceProxy
|
|
|
|
def initialize(ts)
|
|
|
|
@ts = ts
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(tuple, sec=nil)
|
|
|
|
@ts.write(tuple, sec)
|
|
|
|
end
|
|
|
|
|
|
|
|
def take(tuple, sec=nil, &block)
|
|
|
|
port = []
|
|
|
|
@ts.move(DRbObject.new(port), tuple, sec, &block)
|
|
|
|
port[0]
|
|
|
|
end
|
|
|
|
|
2003-11-28 08:51:31 -05:00
|
|
|
def read(tuple, sec=nil, &block)
|
|
|
|
@ts.read(tuple, sec, &block)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_all(tuple)
|
2003-11-28 08:51:31 -05:00
|
|
|
@ts.read_all(tuple)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify(ev, tuple, sec=nil)
|
|
|
|
@ts.notify(ev, tuple, sec)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
|
|
|
# <i>Documentation?</i>
|
|
|
|
#
|
2003-10-05 08:23:33 -04:00
|
|
|
class SimpleRenewer
|
|
|
|
include DRbUndumped
|
|
|
|
def initialize(sec=180)
|
|
|
|
@sec = sec
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew
|
|
|
|
@sec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|