Last active
August 29, 2015 14:19
-
-
Save havenwood/08e859ddd53c84be6c56 to your computer and use it in GitHub Desktop.
An example TupleSpace wrapper around Ruby's stdlib Rinda::TupleSpace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'tuple_space' | |
space = TupleSpace.new | |
# Add Tuples (Arrays) to the TupleSpace: | |
space << [0, false] | |
space << [1, true] | |
space << [42, :the_answer_to_life_the_universe_and_everything] | |
# Read a matching Tuple: | |
space[Numeric, /answer/] | |
#=> [42, :the_answer_to_life_the_universe_and_everything] | |
# Remove a matching Tuple: | |
space >> [Numeric, /answer/] | |
#=> [42, :the_answer_to_life_the_universe_and_everything] | |
# Look for a Tuple that isn't there: | |
space[Numeric, /answer/] | |
#=> nil | |
# Read all matching Tuples: | |
space.read_all [Fixnum, nil] | |
#=> [[0, false], [1, true]] | |
# Tuples come in different sizes: | |
space << [:aim, true, Time.now] | |
space[:aim, nil, Time] | |
#=> [:aim, true, 2222-02-22 02:22:22 -0700] | |
space[nil, nil, nil] | |
#=> [:aim, true, 2222-02-22 02:22:22 -0700] | |
# Use a proc as a matcher to search for Tuples: | |
single_digit_positive_integer = -> n { n.integer? && n >= 0 && n <= 9 } | |
space.read_all [single_digit_positive_integer, nil] | |
#=> [[0, false], [1, true]] | |
# Have an observer notify about matching :read, :write or :delete actions: | |
tuple_pair_written = space.notify :write, [nil, nil] | |
Thread.new do | |
tuple_pair_written.each do |action, tuple| | |
puts "Alert! Saw a TupleSpace #{action}: #{tuple.inspect}" | |
end | |
end | |
sleep 0.1 # A moment for the Thread to spawn. | |
space << [1, 2] | |
#>> Alert! Saw a TupleSpace write: [1, 2] | |
# Happy TupleSpacing 'till Tue Jan 19 03:14:07 GMT Standard Time 2038! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rinda/tuplespace' | |
class TupleSpace | |
def initialize reaper_period = 60 | |
@tuple_space = Rinda::TupleSpace.new reaper_period | |
end | |
def << tuple | |
@tuple_space.write tuple | |
end | |
alias write << | |
def >> tuple | |
@tuple_space.take tuple, true | |
rescue Rinda::RequestExpiredError | |
end | |
alias delete >> | |
def [] *tuple | |
@tuple_space.read tuple, true | |
rescue Rinda::RequestExpiredError | |
end | |
def read tuple | |
@tuple_space.read tuple, true | |
rescue Rinda::RequestExpiredError | |
end | |
def read_all tuple | |
@tuple_space.read_all tuple | |
end | |
def notify event, tuple | |
@tuple_space.notify event.to_s, tuple | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment