2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-10-24 11:38:47 -04:00
|
|
|
require 'drb/drb'
|
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
##
|
|
|
|
# A module to implement the Linda distributed computing paradigm in Ruby.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# Rinda is part of DRb (dRuby).
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# == Example(s)
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# See the sample/drb/ directory in the Ruby distribution, from 1.8.2 onwards.
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
#--
|
|
|
|
# TODO
|
|
|
|
# == Introduction to Linda/rinda?
|
2004-02-17 17:29:07 -05:00
|
|
|
#
|
2005-10-24 11:38:47 -04:00
|
|
|
# == Why is this library separate from DRb?
|
2004-02-17 17:29:07 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
module Rinda
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Rinda error base class
|
|
|
|
|
2004-04-06 11:26:25 -04:00
|
|
|
class RindaError < RuntimeError; end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Raised when a hash-based tuple has an invalid key.
|
|
|
|
|
2004-04-06 11:26:25 -04:00
|
|
|
class InvalidHashTupleKey < RindaError; end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Raised when trying to use a canceled tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class RequestCanceledError < ThreadError; end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Raised when trying to use an expired tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class RequestExpiredError < ThreadError; end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
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.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class Tuple
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new Tuple from +ary_or_hash+ which must be an Array or Hash.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(ary_or_hash)
|
2005-10-16 01:43:19 -04:00
|
|
|
if hash?(ary_or_hash)
|
2005-10-24 11:38:47 -04:00
|
|
|
init_with_hash(ary_or_hash)
|
2003-10-05 08:23:33 -04:00
|
|
|
else
|
2005-10-24 11:38:47 -04:00
|
|
|
init_with_ary(ary_or_hash)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# The number of elements in the tuple.
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def size
|
|
|
|
@tuple.size
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# Accessor method for elements of the tuple.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def [](k)
|
|
|
|
@tuple[k]
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Fetches item +k+ from the tuple.
|
|
|
|
|
2004-04-06 11:26:25 -04:00
|
|
|
def fetch(k)
|
|
|
|
@tuple.fetch(k)
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
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.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def each # FIXME
|
|
|
|
if Hash === @tuple
|
2005-10-24 11:38:47 -04:00
|
|
|
@tuple.each { |k, v| yield(k, v) }
|
2003-10-05 08:23:33 -04:00
|
|
|
else
|
2005-10-24 11:38:47 -04:00
|
|
|
@tuple.each_with_index { |v, k| yield(k, v) }
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Return the tuple itself
|
2003-10-05 08:23:33 -04:00
|
|
|
def value
|
|
|
|
@tuple
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2005-10-16 01:43:19 -04:00
|
|
|
def hash?(ary_or_hash)
|
|
|
|
ary_or_hash.respond_to?(:keys)
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Munges +ary+ into a valid Tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
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|
|
2005-10-24 11:38:47 -04:00
|
|
|
@tuple[i] = ary[i]
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Ensures +hash+ is a valid Tuple.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
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
|
2005-10-24 11:38:47 -04:00
|
|
|
@tuple[k] = v
|
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
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# Templates are used to match tuples in Rinda.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class Template < Tuple
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
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
|
2005-10-24 11:38:47 -04:00
|
|
|
next if v.nil?
|
2004-04-13 10:06:50 -04:00
|
|
|
next if v == it
|
|
|
|
next if v === it
|
2005-10-24 11:38:47 -04:00
|
|
|
return false
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# Alias for #match.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def ===(tuple)
|
|
|
|
match(tuple)
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
2004-02-17 17:29:07 -05:00
|
|
|
# <i>Documentation?</i>
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class DRbObjectTemplate
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new DRbObjectTemplate that will match against +uri+ and +ref+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(uri=nil, ref=nil)
|
|
|
|
@drb_uri = uri
|
|
|
|
@drb_ref = ref
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# This DRbObjectTemplate matches +ro+ if the remote object's drburi and
|
|
|
|
# drbref are the same. +nil+ is used as a wildcard.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def ===(ro)
|
|
|
|
return true if super(ro)
|
|
|
|
unless @drb_uri.nil?
|
2005-10-24 11:38:47 -04:00
|
|
|
return false unless (@drb_uri === ro.__drburi rescue false)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
unless @drb_ref.nil?
|
2005-10-24 11:38:47 -04:00
|
|
|
return false unless (@drb_ref === ro.__drbref rescue false)
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
true
|
|
|
|
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
|
|
|
# TupleSpaceProxy allows a remote Tuplespace to appear as local.
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class TupleSpaceProxy
|
2013-03-23 14:35:23 -04:00
|
|
|
##
|
|
|
|
# A Port ensures that a moved tuple arrives properly at its destination
|
|
|
|
# and does not get lost.
|
|
|
|
#
|
|
|
|
# See https://bugs.ruby-lang.org/issues/8125
|
|
|
|
|
|
|
|
class Port # :nodoc:
|
|
|
|
attr_reader :value
|
|
|
|
|
|
|
|
def self.deliver
|
|
|
|
port = new
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield(port)
|
|
|
|
ensure
|
|
|
|
port.close
|
|
|
|
end
|
|
|
|
|
|
|
|
port.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@open = true
|
|
|
|
@value = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Don't let the DRb thread push to it when remote sends tuple
|
|
|
|
|
|
|
|
def close
|
|
|
|
@open = false
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Stores +value+ and ensure it does not get marshaled multiple times.
|
|
|
|
|
|
|
|
def push value
|
|
|
|
raise 'port closed' unless @open
|
|
|
|
|
|
|
|
@value = value
|
|
|
|
|
|
|
|
nil # avoid Marshal
|
|
|
|
end
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new TupleSpaceProxy to wrap +ts+.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(ts)
|
|
|
|
@ts = ts
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Adds +tuple+ to the proxied TupleSpace. See TupleSpace#write.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def write(tuple, sec=nil)
|
|
|
|
@ts.write(tuple, sec)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Takes +tuple+ from the proxied TupleSpace. See TupleSpace#take.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def take(tuple, sec=nil, &block)
|
2013-03-23 14:35:23 -04:00
|
|
|
Port.deliver do |port|
|
|
|
|
@ts.move(DRbObject.new(port), tuple, sec, &block)
|
|
|
|
end
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Reads +tuple+ from the proxied TupleSpace. See TupleSpace#read.
|
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Reads all tuples matching +tuple+ from the proxied TupleSpace. See
|
|
|
|
# TupleSpace#read_all.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Registers for notifications of event +ev+ on the proxied TupleSpace.
|
|
|
|
# See TupleSpace#notify
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def notify(ev, tuple, sec=nil)
|
|
|
|
@ts.notify(ev, tuple, sec)
|
|
|
|
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
|
|
|
##
|
|
|
|
# An SimpleRenewer allows a TupleSpace to check if a TupleEntry is still
|
|
|
|
# alive.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
class SimpleRenewer
|
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
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new SimpleRenewer that keeps an object alive for another +sec+
|
|
|
|
# seconds.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def initialize(sec=180)
|
|
|
|
@sec = sec
|
|
|
|
end
|
|
|
|
|
2005-10-24 11:38:47 -04:00
|
|
|
##
|
|
|
|
# Called by the TupleSpace to check if the object is still alive.
|
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
def renew
|
|
|
|
@sec
|
|
|
|
end
|
|
|
|
end
|
2005-10-24 11:38:47 -04:00
|
|
|
|
2003-10-05 08:23:33 -04:00
|
|
|
end
|
|
|
|
|