2004-02-13 21:28:02 -05:00
|
|
|
#!/usr/local/bin/ruby
|
|
|
|
# TupleSpace
|
|
|
|
# Copyright (c) 1999-2000 Masatoshi SEKI
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
|
|
|
|
class TupleSpace
|
|
|
|
class Template
|
|
|
|
def initialize(list)
|
|
|
|
@list = list
|
|
|
|
@check_idx = []
|
|
|
|
@list.each_with_index do |x, i|
|
2013-07-20 09:41:02 -04:00
|
|
|
@check_idx.push i if x
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
@size = @list.size
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :size
|
|
|
|
alias length size
|
|
|
|
|
|
|
|
def match(tuple)
|
|
|
|
return nil if tuple.size != self.size
|
|
|
|
@check_idx.each do |i|
|
2013-07-20 09:41:02 -04:00
|
|
|
unless @list[i] === tuple[i]
|
|
|
|
return false
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@que = {}
|
|
|
|
@waiting = {}
|
2019-12-19 19:19:39 -05:00
|
|
|
@que.taint # enable tainted communication
|
2004-02-13 21:28:02 -05:00
|
|
|
@waiting.taint
|
|
|
|
self.taint
|
|
|
|
end
|
|
|
|
|
|
|
|
def wakeup_waiting(tuple)
|
|
|
|
sz = tuple.length
|
|
|
|
return nil unless @waiting[sz]
|
|
|
|
|
|
|
|
x = nil
|
|
|
|
i = -1
|
|
|
|
found = false
|
|
|
|
@waiting[sz] = @waiting[sz].find_all { |x|
|
|
|
|
if x[0].match(tuple)
|
2013-07-20 09:41:02 -04:00
|
|
|
begin
|
|
|
|
x[1].wakeup
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
|
|
|
false
|
2004-02-13 21:28:02 -05:00
|
|
|
else
|
2013-07-20 09:41:02 -04:00
|
|
|
true
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_waiting(template, thread)
|
|
|
|
sz = template.length
|
|
|
|
@waiting[sz] = [] unless @waiting[sz]
|
|
|
|
@waiting[sz].push([Template.new(template), thread])
|
|
|
|
end
|
|
|
|
private :wakeup_waiting
|
|
|
|
private :put_waiting
|
|
|
|
|
|
|
|
def get_que(template)
|
|
|
|
sz = template.length
|
|
|
|
return nil unless @que[sz]
|
|
|
|
|
|
|
|
template = Template.new(template)
|
|
|
|
|
|
|
|
x = nil
|
|
|
|
i = -1
|
|
|
|
found = false
|
|
|
|
@que[sz].each_with_index do |x, i|
|
|
|
|
if template.match(x)
|
2013-07-20 09:41:02 -04:00
|
|
|
found = true
|
|
|
|
break
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil unless found
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
@que[sz].delete_at(i)
|
|
|
|
|
|
|
|
return x
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_que(tuple)
|
|
|
|
sz = tuple.length
|
|
|
|
@que[sz] = [] unless @que[sz]
|
|
|
|
@que[sz].push tuple
|
|
|
|
end
|
|
|
|
private :get_que
|
|
|
|
private :put_que
|
|
|
|
|
|
|
|
def out(*tuples)
|
|
|
|
tuples.each do |tuple|
|
|
|
|
Thread.critical = true
|
|
|
|
put_que(tuple)
|
|
|
|
wakeup_waiting(tuple)
|
|
|
|
Thread.critical = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias put out
|
|
|
|
alias write out
|
|
|
|
|
|
|
|
def in(template, non_block=false)
|
|
|
|
begin
|
|
|
|
loop do
|
2013-07-20 09:41:02 -04:00
|
|
|
Thread.critical = true
|
|
|
|
tuple = get_que(template)
|
|
|
|
unless tuple
|
|
|
|
if non_block
|
|
|
|
raise ThreadError, "queue empty"
|
|
|
|
end
|
|
|
|
put_waiting(template, Thread.current)
|
|
|
|
Thread.stop
|
|
|
|
else
|
|
|
|
return tuple
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Thread.critical = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias get in
|
|
|
|
alias take in
|
|
|
|
|
|
|
|
def rd(template, non_block=false)
|
|
|
|
tuple = self.in(template, non_block)
|
|
|
|
out(tuple)
|
|
|
|
tuple
|
|
|
|
end
|
|
|
|
alias read rd
|
|
|
|
|
|
|
|
def mv(dest, template, non_block=false)
|
|
|
|
tuple = self.in(template, non_block)
|
|
|
|
begin
|
|
|
|
dest.out(tuple)
|
|
|
|
rescue
|
|
|
|
self.out(tuple)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias move mv
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
ts = TupleSpace.new
|
|
|
|
clients = []
|
|
|
|
servers = []
|
|
|
|
|
|
|
|
def server(ts, id)
|
|
|
|
Thread.start {
|
|
|
|
loop do
|
2013-07-20 09:41:02 -04:00
|
|
|
req = ts.in(['req', nil, nil])
|
|
|
|
ac = req[1]
|
|
|
|
num = req[2]
|
|
|
|
sleep id
|
|
|
|
ts.out([ac, id, num, num * num])
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def client(ts, n)
|
|
|
|
Thread.start {
|
|
|
|
ac = Object.new
|
2011-05-15 07:55:52 -04:00
|
|
|
tuples = (1..10).collect { |i|
|
2013-07-20 09:41:02 -04:00
|
|
|
['req', ac, i * 10 + n]
|
2004-02-13 21:28:02 -05:00
|
|
|
}
|
|
|
|
ts.out(*tuples)
|
|
|
|
ts.out(tuples[0])
|
|
|
|
puts "out: #{n}"
|
|
|
|
11.times do |i|
|
2013-07-20 09:41:02 -04:00
|
|
|
ans = ts.in([ac, nil, nil, nil])
|
|
|
|
puts "client(#{n}) server(#{ans[1]}) #{ans[2]} #{ans[3]}"
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def watcher(ts)
|
|
|
|
Thread.start {
|
|
|
|
loop do
|
2013-07-20 09:41:02 -04:00
|
|
|
begin
|
|
|
|
sleep 1
|
|
|
|
p ts.rd(['req', nil, nil], true)
|
|
|
|
rescue ThreadError
|
|
|
|
puts "'req' not found."
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
(0..3).each do |n|
|
|
|
|
servers.push(server(ts, n))
|
|
|
|
end
|
|
|
|
|
|
|
|
(1..6).each do |n|
|
|
|
|
clients.push(client(ts, n))
|
|
|
|
end
|
|
|
|
|
2011-05-15 07:55:52 -04:00
|
|
|
(1..3).each do
|
2004-02-13 21:28:02 -05:00
|
|
|
watcher(ts)
|
|
|
|
end
|
|
|
|
|
|
|
|
clients.each do |t|
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|