2004-02-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# rinda.rb: A Ruby implementation of the Linda distibuted 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.)
|
|
|
|
#
|
|
|
|
|
2004-04-05 19:14:20 +04:00
|
|
|
require 'drb/drb'
|
2003-10-05 16:23:33 +04:00
|
|
|
require 'thread'
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# A module to implement the Linda programming paradigm in Ruby.
|
|
|
|
# This is part of +drb+ (dRuby).
|
|
|
|
#
|
2003-10-05 16:23:33 +04:00
|
|
|
module Rinda
|
2004-04-06 19:26:25 +04:00
|
|
|
class RindaError < RuntimeError; end
|
|
|
|
class InvalidHashTupleKey < RindaError; end
|
2003-10-05 16:23:33 +04:00
|
|
|
class RequestCanceledError < ThreadError; end
|
|
|
|
class RequestExpiredError < ThreadError; end
|
|
|
|
|
2004-02-18 01:29:07 +03: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 16:23:33 +04:00
|
|
|
class Tuple
|
2004-02-18 01:29:07 +03:00
|
|
|
# Initialize a tuple with an Array or a Hash.
|
2003-10-05 16: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-18 01:29:07 +03:00
|
|
|
# The number of elements in the tuple.
|
2003-10-05 16:23:33 +04:00
|
|
|
def size
|
|
|
|
@tuple.size
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
# Accessor method for elements of the tuple.
|
2003-10-05 16:23:33 +04:00
|
|
|
def [](k)
|
|
|
|
@tuple[k]
|
|
|
|
end
|
|
|
|
|
2004-04-06 19:26:25 +04:00
|
|
|
def fetch(k)
|
|
|
|
@tuple.fetch(k)
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
# Iterate through the tuple, yielding the index or key, and the
|
|
|
|
# value, thus ensuring arrays are iterated similarly to hashes.
|
2003-10-05 16: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-18 01:29:07 +03:00
|
|
|
# Return the tuple itself -- i.e the Array or hash.
|
2003-10-05 16:23:33 +04:00
|
|
|
def value
|
|
|
|
@tuple
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def init_with_ary(ary)
|
|
|
|
@tuple_size = ary.size
|
|
|
|
@tuple = Array.new(@tuple_size)
|
|
|
|
@tuple.size.times do |i|
|
|
|
|
@tuple[i] = ary[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_with_hash(hash)
|
|
|
|
@tuple_size = hash[:size]
|
|
|
|
@tuple = Hash.new
|
|
|
|
hash.each do |k, v|
|
2004-04-06 19:26:25 +04:00
|
|
|
next if k == :size
|
|
|
|
raise InvalidHashTupleKey unless String === k
|
2003-10-05 16:23:33 +04:00
|
|
|
@tuple[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# Templates are used to match tuples in Rinda.
|
|
|
|
#
|
2003-10-05 16:23:33 +04:00
|
|
|
class Template < Tuple
|
2004-02-18 01:29:07 +03: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 16:23:33 +04:00
|
|
|
def match(tuple)
|
|
|
|
return false unless tuple.respond_to?(:size)
|
2004-04-06 19:26:25 +04:00
|
|
|
return false unless tuple.respond_to?(:fetch)
|
2003-10-05 16:23:33 +04:00
|
|
|
return false if @tuple_size && (@tuple_size != tuple.size)
|
|
|
|
each do |k, v|
|
2004-04-06 19:26:25 +04:00
|
|
|
begin
|
|
|
|
it = tuple.fetch(k)
|
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
end
|
2003-10-05 16:23:33 +04:00
|
|
|
next if v.nil?
|
2004-04-13 18:06:50 +04:00
|
|
|
next if v == it
|
|
|
|
next if v === it
|
|
|
|
return false
|
2003-10-05 16:23:33 +04:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
# Alias for #match.
|
2003-10-05 16:23:33 +04:00
|
|
|
def ===(tuple)
|
|
|
|
match(tuple)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# <i>Documentation?</i>
|
|
|
|
#
|
2003-10-05 16: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-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# TupleSpaceProxy allows a remote Tuplespace to appear as local.
|
|
|
|
#
|
2003-10-05 16: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 16:51:31 +03:00
|
|
|
def read(tuple, sec=nil, &block)
|
|
|
|
@ts.read(tuple, sec, &block)
|
2003-10-05 16:23:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_all(tuple)
|
2003-11-28 16:51:31 +03:00
|
|
|
@ts.read_all(tuple)
|
2003-10-05 16:23:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify(ev, tuple, sec=nil)
|
|
|
|
@ts.notify(ev, tuple, sec)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-18 01:29:07 +03:00
|
|
|
#
|
|
|
|
# <i>Documentation?</i>
|
|
|
|
#
|
2003-10-05 16:23:33 +04:00
|
|
|
class SimpleRenewer
|
|
|
|
include DRbUndumped
|
|
|
|
def initialize(sec=180)
|
|
|
|
@sec = sec
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew
|
|
|
|
@sec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|