mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
DRb has been extracted as bundled gems
This commit is contained in:
parent
66aaf5b67f
commit
9503a77da2
47 changed files with 0 additions and 1965 deletions
|
@ -125,12 +125,3 @@ Note that the use of +public_send+ is also dangerous, as +send+ itself is
|
||||||
public:
|
public:
|
||||||
|
|
||||||
1.public_send("send", "eval", "...ruby code to be executed...")
|
1.public_send("send", "eval", "...ruby code to be executed...")
|
||||||
|
|
||||||
== DRb
|
|
||||||
|
|
||||||
As DRb allows remote clients to invoke arbitrary methods, it is not suitable to
|
|
||||||
expose to untrusted clients.
|
|
||||||
|
|
||||||
When using DRb, try to avoid exposing it over the network if possible. If this
|
|
||||||
isn't possible and you need to expose DRb to the world, you *must* configure an
|
|
||||||
appropriate security policy with +DRb::ACL+.
|
|
||||||
|
|
|
@ -68,7 +68,6 @@ DEFAULT_GEM_LIBS = %w[
|
||||||
debug
|
debug
|
||||||
delegate
|
delegate
|
||||||
did_you_mean
|
did_you_mean
|
||||||
drb
|
|
||||||
english
|
english
|
||||||
erb
|
erb
|
||||||
fileutils
|
fileutils
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
= サンプルスクリプト
|
|
||||||
|
|
||||||
* Arrayをリモートから利用してイテレータを試す。
|
|
||||||
* darray.rb --- server
|
|
||||||
* darrayc.rb --- client
|
|
||||||
|
|
||||||
* 簡易チャット
|
|
||||||
* dchats.rb --- server
|
|
||||||
* dchatc.rb --- client
|
|
||||||
|
|
||||||
* 分散chasen
|
|
||||||
* dhasen.rb --- server
|
|
||||||
* dhasenc.rb --- client
|
|
||||||
|
|
||||||
* 簡易ログサーバ
|
|
||||||
* dlogd.rb --- server
|
|
||||||
* dlogc.rb --- client
|
|
||||||
|
|
||||||
* Queueサーバ。
|
|
||||||
クライアントdqin.rbはQueueサーバの知らないオブジェクト(DQEntry)を
|
|
||||||
pushするがDRbUnknownによりクライアントdqout.rbがpopできる。
|
|
||||||
* dqueue.rb --- server
|
|
||||||
* dqin.rb --- client。DQEntryオブジェクトをpushする
|
|
||||||
* dqout.rb --- client。DQEntryオブジェクトをpopする
|
|
||||||
* dqlib.rb --- DQEntryを定義したライブラリ
|
|
||||||
|
|
||||||
* 名前による参照
|
|
||||||
IdConvをカスタマイズしてidでなく名前で参照する例
|
|
||||||
* name.rb --- server
|
|
||||||
* namec.rb --- client
|
|
||||||
|
|
||||||
* extservのサンプル
|
|
||||||
* extserv_test.rb
|
|
||||||
|
|
||||||
* TimerIdConvの使用例
|
|
||||||
* holders.rb --- server。ruby -d hodlers.rbとするとTimerIdConvを使用する。
|
|
||||||
* holderc.rb --- client
|
|
||||||
|
|
||||||
* rinda.rbの使用例
|
|
||||||
* rinda_ts.rb --- TupleSpaceサーバ。
|
|
||||||
* rindac.rb --- TupleSpaceのclientでアプリケーションのclient
|
|
||||||
* rindas.rb --- TupleSpaceのclientでアプリケーションのserver
|
|
||||||
|
|
||||||
* observerの使用例
|
|
||||||
cdbiff - http://namazu.org/~satoru/cdbiff/
|
|
||||||
* dbiff.rb --- dcdbiff server
|
|
||||||
* dcdbiff.rb --- dcdbiff client
|
|
||||||
|
|
||||||
* drbsslの使用例
|
|
||||||
* drbssl_s.rb
|
|
||||||
* drbssl_c.rb
|
|
||||||
|
|
||||||
* DRbProtocolの追加例
|
|
||||||
* http0.rb
|
|
||||||
* http0serv.rb
|
|
||||||
|
|
||||||
* ringの使用例
|
|
||||||
* ring_place.rb
|
|
||||||
* ring_echo.rb
|
|
|
@ -1,56 +0,0 @@
|
||||||
= Sample scripts
|
|
||||||
|
|
||||||
* array and iterator
|
|
||||||
* darray.rb --- server
|
|
||||||
* darrayc.rb --- client
|
|
||||||
|
|
||||||
* simple chat
|
|
||||||
* dchats.rb --- server
|
|
||||||
* dchatc.rb --- client
|
|
||||||
|
|
||||||
* distributed chasen (for Japanese)
|
|
||||||
* dhasen.rb --- server
|
|
||||||
* dhasenc.rb --- client
|
|
||||||
|
|
||||||
* simple log server
|
|
||||||
* dlogd.rb --- server
|
|
||||||
* dlogc.rb --- client
|
|
||||||
|
|
||||||
* Queue server, and DRbUnknown demo
|
|
||||||
* dqueue.rb --- server
|
|
||||||
* dqin.rb --- client. push DQEntry objects.
|
|
||||||
* dqout.rb --- client. pop DQEntry objects.
|
|
||||||
* dqlib.rb --- define DQEntry
|
|
||||||
|
|
||||||
* IdConv customize demo: reference by name
|
|
||||||
* name.rb --- server
|
|
||||||
* namec.rb --- client
|
|
||||||
|
|
||||||
* extserv
|
|
||||||
* extserv_test.rb
|
|
||||||
|
|
||||||
* IdConv customize demo 2: using TimerIdConv
|
|
||||||
* holders.rb --- server
|
|
||||||
* holderc.rb --- client
|
|
||||||
|
|
||||||
* rinda, remote tuplespace
|
|
||||||
* rinda_ts.rb --- TupleSpace server.
|
|
||||||
* rindas.rb --- provide simple service via TupleSpace.
|
|
||||||
* rindac.rb --- service user
|
|
||||||
|
|
||||||
* observer
|
|
||||||
cdbiff - http://namazu.org/~satoru/cdbiff/
|
|
||||||
* dbiff.rb --- dcdbiff server
|
|
||||||
* dcdbiff.rb --- dcdbiff client
|
|
||||||
|
|
||||||
* drbssl
|
|
||||||
* drbssl_s.rb
|
|
||||||
* drbssl_c.rb
|
|
||||||
|
|
||||||
* add DRbProtocol
|
|
||||||
* http0.rb
|
|
||||||
* http0serv.rb
|
|
||||||
|
|
||||||
* Rinda::Ring
|
|
||||||
* ring_place.rb
|
|
||||||
* ring_echo.rb
|
|
|
@ -1,15 +0,0 @@
|
||||||
require 'drb/acl'
|
|
||||||
|
|
||||||
list = %w(deny all
|
|
||||||
allow 192.168.1.1
|
|
||||||
allow ::ffff:192.168.1.2
|
|
||||||
allow 192.168.1.3
|
|
||||||
)
|
|
||||||
|
|
||||||
addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
|
|
||||||
|
|
||||||
acl = ACL.new
|
|
||||||
p acl.allow_addr?(addr)
|
|
||||||
|
|
||||||
acl = ACL.new(list, ACL::DENY_ALLOW)
|
|
||||||
p acl.allow_addr?(addr)
|
|
|
@ -1,12 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- Array
|
|
||||||
Copyright (c) 1999-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
here = ARGV.shift
|
|
||||||
DRb.start_service(here, [1, 2, "III", 4, "five", 6])
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- Array client
|
|
||||||
Copyright (c) 1999-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
there = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service(nil, nil)
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
p ro.size
|
|
||||||
|
|
||||||
puts "# collect"
|
|
||||||
a = ro.collect { |x|
|
|
||||||
x + x
|
|
||||||
}
|
|
||||||
p a
|
|
||||||
|
|
||||||
puts "# find"
|
|
||||||
p ro.find { |x| x.kind_of? String }
|
|
||||||
|
|
||||||
puts "# each, break"
|
|
||||||
ro.each do |x|
|
|
||||||
next if x == "five"
|
|
||||||
puts x
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "# each, break"
|
|
||||||
ro.each do |x|
|
|
||||||
break if x == "five"
|
|
||||||
puts x
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "# each, next"
|
|
||||||
ro.each do |x|
|
|
||||||
next if x == "five"
|
|
||||||
puts x
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "# each, redo"
|
|
||||||
count = 0
|
|
||||||
ro.each do |x|
|
|
||||||
count += 1
|
|
||||||
puts count
|
|
||||||
redo if count == 3
|
|
||||||
end
|
|
|
@ -1,51 +0,0 @@
|
||||||
#
|
|
||||||
# dbiff.rb - distributed cdbiff (server)
|
|
||||||
# * original: cdbiff by Satoru Takabayashi <http://namazu.org/~satoru/cdbiff>
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/eq'
|
|
||||||
require 'drb/observer'
|
|
||||||
|
|
||||||
class Biff
|
|
||||||
include DRb::DRbObservable
|
|
||||||
|
|
||||||
def initialize(filename, interval)
|
|
||||||
super()
|
|
||||||
@filename = filename
|
|
||||||
@interval = interval
|
|
||||||
end
|
|
||||||
|
|
||||||
def run
|
|
||||||
last = Time.now
|
|
||||||
while true
|
|
||||||
begin
|
|
||||||
sleep(@interval)
|
|
||||||
current = File::mtime(@filename)
|
|
||||||
if current > last
|
|
||||||
changed
|
|
||||||
begin
|
|
||||||
notify_observers(@filename, current)
|
|
||||||
rescue Error
|
|
||||||
end
|
|
||||||
last = current
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
next
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def main
|
|
||||||
filename = "/var/mail/#{ENV['USER']}"
|
|
||||||
interval = 15
|
|
||||||
uri = 'druby://:19903'
|
|
||||||
|
|
||||||
biff = Biff.new(filename, interval)
|
|
||||||
|
|
||||||
DRb.start_service(uri, biff)
|
|
||||||
biff.run
|
|
||||||
end
|
|
||||||
|
|
||||||
main
|
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
#
|
|
||||||
# dcdbiff.rb - distributed cdbiff (client)
|
|
||||||
# * original: cdbiff by Satoru Takabayashi <http://namazu.org/~satoru/cdbiff>
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/eq'
|
|
||||||
|
|
||||||
class Notify
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(biff, command)
|
|
||||||
@biff = biff
|
|
||||||
@command = command
|
|
||||||
|
|
||||||
@biff.add_observer(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update(filename, time)
|
|
||||||
p [filename, time] if $DEBUG
|
|
||||||
system(@command)
|
|
||||||
end
|
|
||||||
|
|
||||||
def done
|
|
||||||
begin
|
|
||||||
@biff.delete_observer(self)
|
|
||||||
rescue
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def main
|
|
||||||
command = 'eject'
|
|
||||||
uri = 'druby://localhost:19903'
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
biff = DRbObject.new(nil, uri)
|
|
||||||
notify = Notify.new(biff, command)
|
|
||||||
|
|
||||||
trap("INT"){ notify.done }
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
||||||
main
|
|
|
@ -1,41 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- chat client
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class ChatClient
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(name)
|
|
||||||
@name = name
|
|
||||||
@key = nil
|
|
||||||
end
|
|
||||||
attr_reader(:name)
|
|
||||||
attr_accessor(:key)
|
|
||||||
|
|
||||||
def message(there, str)
|
|
||||||
raise 'invalid key' unless @key == there
|
|
||||||
puts str
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
begin
|
|
||||||
there = ARGV.shift
|
|
||||||
name = ARGV.shift
|
|
||||||
raise "usage" unless (there and name)
|
|
||||||
rescue
|
|
||||||
$stderr.puts("usage: #{$0} <server_uri> <your_name>")
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
DRb.start_service
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
|
|
||||||
chat = ChatClient.new(name)
|
|
||||||
entry = ro.add_member(chat)
|
|
||||||
while gets
|
|
||||||
entry.say($_)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,69 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- chat server
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class ChatEntry
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(server, there)
|
|
||||||
@server = server
|
|
||||||
@there = there
|
|
||||||
@name = there.name
|
|
||||||
@key = there.key = Time.now
|
|
||||||
end
|
|
||||||
attr :name, true
|
|
||||||
attr :there
|
|
||||||
|
|
||||||
def say(str)
|
|
||||||
@server.distribute(@there, str)
|
|
||||||
end
|
|
||||||
|
|
||||||
def listen(str)
|
|
||||||
@there.message(@key, str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
class ChatServer
|
|
||||||
def initialize
|
|
||||||
@mutex = Thread::Mutex.new
|
|
||||||
@members = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_member(there)
|
|
||||||
client = ChatEntry.new(self, there)
|
|
||||||
@mutex.synchronize do
|
|
||||||
@members[there] = client
|
|
||||||
end
|
|
||||||
client
|
|
||||||
end
|
|
||||||
|
|
||||||
def distribute(there, str)
|
|
||||||
name = @members[there].name
|
|
||||||
msg = "<#{name}> #{str}"
|
|
||||||
msg2 = ">#{name}< #{str}"
|
|
||||||
@mutex.synchronize do
|
|
||||||
for m in @members.keys
|
|
||||||
begin
|
|
||||||
if m == there
|
|
||||||
@members[m].listen(msg2)
|
|
||||||
else
|
|
||||||
@members[m].listen(msg)
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
p $!
|
|
||||||
@members.delete(m)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
here = ARGV.shift
|
|
||||||
DRb.start_service(here, ChatServer.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
|
@ -1,41 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Server --- chasen server
|
|
||||||
Copyright (c) 1999-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
How to play.
|
|
||||||
|
|
||||||
Terminal 1
|
|
||||||
| % ruby dhasen.rb
|
|
||||||
| druby://yourhost:7640
|
|
||||||
|
|
||||||
Terminal 2
|
|
||||||
| % ruby dhasenc.rb druby://yourhost:7640
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'chasen'
|
|
||||||
|
|
||||||
class Dhasen
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@mutex = Thread::Mutex.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def sparse(str, *arg)
|
|
||||||
@mutex.synchronize do
|
|
||||||
Chasen.getopt(*arg)
|
|
||||||
Chasen.sparse(str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
DRb.start_service(nil, Dhasen.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Client -- chasen client
|
|
||||||
Copyright (c) 1999-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
there = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
DRb.start_service
|
|
||||||
dhasen = DRbObject.new(nil, there)
|
|
||||||
|
|
||||||
print dhasen.sparse("本日は、晴天なり。", "-F", '(%BB %m %M)\n', "-j")
|
|
||||||
print dhasen.sparse("本日は、晴天なり。", "-F", '(%m %M)\n')
|
|
|
@ -1,16 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- Log test
|
|
||||||
Copyright (c) 1999-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
there = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
ro.log(123)
|
|
||||||
ro.log("hello")
|
|
||||||
sleep 2
|
|
||||||
ro.log("wakeup")
|
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- Log server
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class Logger
|
|
||||||
def initialize(fname)
|
|
||||||
@fname = fname.to_s
|
|
||||||
@fp = File.open(@fname, "a+")
|
|
||||||
@queue = Thread::Queue.new
|
|
||||||
@th = Thread.new { self.flush }
|
|
||||||
end
|
|
||||||
|
|
||||||
def log(str)
|
|
||||||
@queue.push("#{Time.now}\t" + str.to_s)
|
|
||||||
end
|
|
||||||
|
|
||||||
def flush
|
|
||||||
begin
|
|
||||||
while(1)
|
|
||||||
@fp.puts(@queue.pop)
|
|
||||||
@fp.flush
|
|
||||||
end
|
|
||||||
ensure
|
|
||||||
@fp.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
here = ARGV.shift
|
|
||||||
DRb.start_service(here, Logger.new('/usr/tmp/dlogd.log'))
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- store
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'dqlib'
|
|
||||||
|
|
||||||
there = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
queue = DRbObject.new(nil, there)
|
|
||||||
queue.push(DQEntry.new(DRb.uri))
|
|
|
@ -1,14 +0,0 @@
|
||||||
class DQEntry
|
|
||||||
def initialize(name)
|
|
||||||
@name = name
|
|
||||||
end
|
|
||||||
|
|
||||||
def greeting
|
|
||||||
"Hello, This is #{@name}."
|
|
||||||
end
|
|
||||||
alias to_s greeting
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
puts DQEntry.new('DQEntry')
|
|
||||||
end
|
|
|
@ -1,14 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- fetch
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'dqlib'
|
|
||||||
|
|
||||||
there = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
queue = DRbObject.new(nil, there)
|
|
||||||
entry = queue.pop
|
|
||||||
puts entry.greeting
|
|
|
@ -1,11 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- Queue
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
DRb.start_service(nil, Thread::Queue.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
|
|
|
@ -1,45 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Client
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class DRbEx2
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(n)
|
|
||||||
@n = n
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_i
|
|
||||||
@n.to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
there = ARGV.shift
|
|
||||||
unless there
|
|
||||||
$stderr.puts("usage: #{$0} <server_uri>")
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service()
|
|
||||||
ro = DRbObject.new_with_uri(there)
|
|
||||||
|
|
||||||
puts ro
|
|
||||||
p ro.to_a
|
|
||||||
puts ro.hello
|
|
||||||
p ro.hello
|
|
||||||
puts ro.sample(DRbEx2.new(1), 2, 3)
|
|
||||||
puts ro.sample(1, ro.sample(DRbEx2.new(1), 2, 3), DRbEx2.new(3))
|
|
||||||
|
|
||||||
begin
|
|
||||||
ro.err
|
|
||||||
rescue DRb::DRbUnknownError
|
|
||||||
p $!
|
|
||||||
p $!.unknown
|
|
||||||
rescue RuntimeError
|
|
||||||
p $!
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,48 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Client
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/http'
|
|
||||||
|
|
||||||
class DRbEx2
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(n)
|
|
||||||
@n = n
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_i
|
|
||||||
@n.to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
there = ARGV.shift
|
|
||||||
unless there
|
|
||||||
$stderr.puts("usage: #{$0} <server_uri>")
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb::DRbConn.proxy_map['x68k'] = 'http://x68k/~mas/http_cgi.rb'
|
|
||||||
|
|
||||||
DRb.start_service()
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
|
|
||||||
puts ro
|
|
||||||
p ro.to_a
|
|
||||||
puts ro.hello
|
|
||||||
p ro.hello
|
|
||||||
puts ro.sample(DRbEx2.new(1), 2, 3)
|
|
||||||
puts ro.sample(1, ro.sample(DRbEx2.new(1), 2, 3), DRbEx2.new(3))
|
|
||||||
|
|
||||||
begin
|
|
||||||
ro.err
|
|
||||||
rescue DRb::DRbUnknownError
|
|
||||||
p $!
|
|
||||||
p $!.unknown
|
|
||||||
rescue RuntimeError
|
|
||||||
p $!
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,60 +0,0 @@
|
||||||
=begin
|
|
||||||
multiple DRbServer
|
|
||||||
Copyright (c) 1999-2002 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
How to play.
|
|
||||||
|
|
||||||
Terminal 1
|
|
||||||
| % ruby drbm.rb
|
|
||||||
| druby://yourhost:7640 druby://yourhost:7641
|
|
||||||
|
|
||||||
Terminal 2
|
|
||||||
| % ruby drbmc.rb druby://yourhost:7640 druby://yourhost:7641
|
|
||||||
| [#<DRb::DRbObject .... @uri="druby://yourhost:7640">, "FOO"]
|
|
||||||
| [#<DRb::DRbObject .... @uri="druby://yourhost:7641">, "FOO"]
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class Hoge
|
|
||||||
include DRbUndumped
|
|
||||||
def initialize(s)
|
|
||||||
@str = s
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
@str
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
def initialize(s='FOO')
|
|
||||||
@hoge = Hoge.new(s)
|
|
||||||
end
|
|
||||||
|
|
||||||
def hello
|
|
||||||
@hoge
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Bar < Foo
|
|
||||||
def initialize(foo)
|
|
||||||
@hoge = foo.hello
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
foo = Foo.new
|
|
||||||
s1 = DRb::DRbServer.new('druby://:7640', foo)
|
|
||||||
s2 = DRb::DRbServer.new('druby://:7641', Bar.new(foo))
|
|
||||||
|
|
||||||
puts "#{s1.uri} #{s2.uri}"
|
|
||||||
|
|
||||||
s1.thread.join
|
|
||||||
s2.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
=begin
|
|
||||||
multiple DRbServer client
|
|
||||||
Copyright (c) 1999-2002 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
s1 = ARGV.shift
|
|
||||||
s2 = ARGV.shift
|
|
||||||
unless s1 && s2
|
|
||||||
$stderr.puts("usage: #{$0} <server_uri1> <server_uri2>")
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service()
|
|
||||||
r1 = DRbObject.new(nil, s1)
|
|
||||||
r2 = DRbObject.new(nil, s2)
|
|
||||||
|
|
||||||
p [r1.hello, r1.hello.to_s]
|
|
||||||
p [r2.hello, r2.hello.to_s]
|
|
||||||
end
|
|
|
@ -1,51 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Server
|
|
||||||
Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
How to play.
|
|
||||||
|
|
||||||
Terminal 1
|
|
||||||
| % ruby drbs.rb
|
|
||||||
| druby://yourhost:7640
|
|
||||||
|
|
||||||
Terminal 2
|
|
||||||
| % ruby drbc.rb druby://yourhost:7640
|
|
||||||
| "hello"
|
|
||||||
| 6
|
|
||||||
| 10
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
require 'acl'
|
|
||||||
|
|
||||||
class DRbEx
|
|
||||||
def initialize
|
|
||||||
@hello = 'hello'
|
|
||||||
end
|
|
||||||
|
|
||||||
def hello
|
|
||||||
info = Thread.current['DRb']
|
|
||||||
p info['socket'].peeraddr if info
|
|
||||||
@hello
|
|
||||||
end
|
|
||||||
|
|
||||||
def sample(a, b, c)
|
|
||||||
a.to_i + b.to_i + c.to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
acl = ACL.new(%w(deny all
|
|
||||||
allow 192.168.1.*
|
|
||||||
allow localhost))
|
|
||||||
|
|
||||||
DRb.install_acl(acl)
|
|
||||||
|
|
||||||
DRb.start_service(nil, DRbEx.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- dRuby Sample Server
|
|
||||||
Copyright (c) 1999-2000,2002 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
How to play.
|
|
||||||
|
|
||||||
Terminal 1
|
|
||||||
| % ruby drbs.rb
|
|
||||||
| druby://yourhost:7640
|
|
||||||
|
|
||||||
Terminal 2
|
|
||||||
| % ruby drbc.rb druby://yourhost:7640
|
|
||||||
| "hello"
|
|
||||||
| ....
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class DRbEx
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@hello = 'hello'
|
|
||||||
end
|
|
||||||
|
|
||||||
def hello
|
|
||||||
cntxt = Thread.current['DRb']
|
|
||||||
if cntxt
|
|
||||||
p cntxt['server'].uri
|
|
||||||
p cntxt['client'].peeraddr
|
|
||||||
end
|
|
||||||
Foo::Unknown.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def err
|
|
||||||
raise FooError
|
|
||||||
end
|
|
||||||
|
|
||||||
def sample(a, b, c)
|
|
||||||
a.to_i + b.to_i + c.to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
class Unknown
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class FooError < RuntimeError
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
DRb.start_service(ARGV.shift || 'druby://:7640', DRbEx.new)
|
|
||||||
puts DRb.uri
|
|
||||||
Thread.new do
|
|
||||||
sleep 10
|
|
||||||
DRb.stop_service
|
|
||||||
end
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'drb'
|
|
||||||
require 'drb/ssl'
|
|
||||||
|
|
||||||
there = ARGV.shift || "drbssl://localhost:3456"
|
|
||||||
|
|
||||||
config = Hash.new
|
|
||||||
config[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER
|
|
||||||
config[:SSLVerifyCallback] = lambda{|ok,x509_store|
|
|
||||||
p [ok, x509_store.error_string]
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
DRb.start_service(nil,nil,config)
|
|
||||||
h = DRbObject.new(nil, there)
|
|
||||||
while line = gets
|
|
||||||
p h.hello(line.chomp)
|
|
||||||
end
|
|
|
@ -1,31 +0,0 @@
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'drb'
|
|
||||||
require 'drb/ssl'
|
|
||||||
|
|
||||||
here = ARGV.shift || "drbssl://localhost:3456"
|
|
||||||
|
|
||||||
class HelloWorld
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def hello(name)
|
|
||||||
"Hello, #{name}."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
config = Hash.new
|
|
||||||
config[:verbose] = true
|
|
||||||
begin
|
|
||||||
data = open("sample.key"){|io| io.read }
|
|
||||||
config[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(data)
|
|
||||||
data = open("sample.crt"){|io| io.read }
|
|
||||||
config[:SSLCertificate] = OpenSSL::X509::Certificate.new(data)
|
|
||||||
rescue
|
|
||||||
$stderr.puts "Switching to use self-signed certificate"
|
|
||||||
config[:SSLCertName] =
|
|
||||||
[ ["C","JP"], ["O","Foo.DRuby.Org"], ["CN", "Sample"] ]
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service(here, HelloWorld.new, config)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
|
@ -1,80 +0,0 @@
|
||||||
=begin
|
|
||||||
dRuby sample
|
|
||||||
Copyright (c) 2000 Masatoshi SEKI
|
|
||||||
|
|
||||||
= How to play
|
|
||||||
|
|
||||||
* Terminal 1
|
|
||||||
|
|
||||||
% ruby -I. extserv_test.rb server
|
|
||||||
druby://yourhost:12345
|
|
||||||
|
|
||||||
* Terminal 2
|
|
||||||
|
|
||||||
% ruby -I. extserv_test.rb druby://yourhost:12345
|
|
||||||
...
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
def ARGV.shift
|
|
||||||
it = super()
|
|
||||||
raise "usage:\nserver: #{$0} server [<uri>]\nclient: #{$0} [quit] <uri>" unless it
|
|
||||||
it
|
|
||||||
end
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(str)
|
|
||||||
@str = str
|
|
||||||
end
|
|
||||||
|
|
||||||
def hello(it)
|
|
||||||
"#{it}: #{self}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
@str
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
cmd = ARGV.shift
|
|
||||||
case cmd
|
|
||||||
when 'itest1', 'itest2'
|
|
||||||
require 'drb/extserv'
|
|
||||||
|
|
||||||
front = Foo.new(cmd)
|
|
||||||
server = DRb::DRbServer.new(nil, front)
|
|
||||||
es = DRb::ExtServ.new(ARGV.shift, ARGV.shift, server)
|
|
||||||
server.thread.join
|
|
||||||
|
|
||||||
when 'server'
|
|
||||||
require 'drb/extservm'
|
|
||||||
|
|
||||||
DRb::ExtServManager.command['itest1'] = "ruby -I. #{$0} itest1"
|
|
||||||
DRb::ExtServManager.command['itest2'] = "ruby -I. #{$0} itest2"
|
|
||||||
|
|
||||||
s = DRb::ExtServManager.new
|
|
||||||
DRb.start_service(ARGV.shift, s)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
|
|
||||||
|
|
||||||
else
|
|
||||||
uri = (cmd == 'quit') ? ARGV.shift : cmd
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
s = DRbObject.new(nil, uri)
|
|
||||||
t1 = s.service('itest1').front
|
|
||||||
puts t1
|
|
||||||
t2 = s.service('itest2').front
|
|
||||||
puts t2
|
|
||||||
puts t1.hello(t2)
|
|
||||||
if (cmd == 'quit')
|
|
||||||
s.service('itest1').stop_service
|
|
||||||
s.service('itest2').stop_service
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def foo(n)
|
|
||||||
n + n
|
|
||||||
end
|
|
||||||
|
|
||||||
def bar(n)
|
|
||||||
yield(n) + yield(n)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service(nil)
|
|
||||||
puts DRb.uri
|
|
||||||
|
|
||||||
ro = DRbObject.new(nil, ARGV.shift)
|
|
||||||
ro[:tcp] = Foo.new
|
|
||||||
gets
|
|
||||||
|
|
||||||
it = ro[:unix]
|
|
||||||
p [it, it.foo(1)]
|
|
||||||
gets
|
|
||||||
|
|
||||||
p it.bar('2') {|n| n * 3}
|
|
||||||
gets
|
|
||||||
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/unix'
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def foo(n)
|
|
||||||
n + n
|
|
||||||
end
|
|
||||||
|
|
||||||
def bar(n)
|
|
||||||
yield(n) + yield(n)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service('drbunix:', nil)
|
|
||||||
puts DRb.uri
|
|
||||||
|
|
||||||
ro = DRbObject.new(nil, ARGV.shift)
|
|
||||||
ro[:unix] = Foo.new
|
|
||||||
gets
|
|
||||||
|
|
||||||
it = ro[:tcp]
|
|
||||||
p [it, it.foo(1)]
|
|
||||||
gets
|
|
||||||
|
|
||||||
p it.bar('2') {|n| n * 3}
|
|
||||||
gets
|
|
|
@ -1,10 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/unix'
|
|
||||||
require 'drb/gw'
|
|
||||||
|
|
||||||
DRb.install_id_conv(DRb::GWIdConv.new)
|
|
||||||
gw = DRb::GW.new
|
|
||||||
s1 = DRb::DRbServer.new(ARGV.shift, gw)
|
|
||||||
s2 = DRb::DRbServer.new(ARGV.shift, gw)
|
|
||||||
s1.thread.join
|
|
||||||
s2.thread.join
|
|
|
@ -1,22 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
begin
|
|
||||||
there = ARGV.shift || raise
|
|
||||||
rescue
|
|
||||||
$stderr.puts("usage: #{$0} <server_uri>")
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service()
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
|
|
||||||
ary = []
|
|
||||||
10.times do
|
|
||||||
ary.push(ro.gen)
|
|
||||||
end
|
|
||||||
|
|
||||||
sleep 5 if $DEBUG
|
|
||||||
|
|
||||||
ary.each do |e|
|
|
||||||
p e.sample([1])
|
|
||||||
end
|
|
|
@ -1,63 +0,0 @@
|
||||||
=begin
|
|
||||||
= How to play.
|
|
||||||
|
|
||||||
== with timeridconv:
|
|
||||||
% ruby -d holders.rb
|
|
||||||
druby://yourhost:1234
|
|
||||||
|
|
||||||
% ruby holderc.rb druby://yourhost:1234
|
|
||||||
|
|
||||||
|
|
||||||
== without timeridconv:
|
|
||||||
% ruby holders.rb
|
|
||||||
druby://yourhost:1234
|
|
||||||
|
|
||||||
% ruby holderc.rb druby://yourhost:1234
|
|
||||||
=end
|
|
||||||
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class DRbEx3
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize(n)
|
|
||||||
@v = n
|
|
||||||
end
|
|
||||||
|
|
||||||
def sample(list)
|
|
||||||
sum = 0
|
|
||||||
list.each do |e|
|
|
||||||
sum += e.to_i
|
|
||||||
end
|
|
||||||
@v * sum
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class DRbEx4
|
|
||||||
include DRbUndumped
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@curr = 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def gen
|
|
||||||
begin
|
|
||||||
@curr += 1
|
|
||||||
DRbEx3.new(@curr)
|
|
||||||
ensure
|
|
||||||
GC.start
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
if $DEBUG
|
|
||||||
require 'drb/timeridconv'
|
|
||||||
DRb.install_id_conv(DRb::TimerIdConv.new(2))
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service(nil, DRbEx4.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
|
@ -1,77 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'net/http'
|
|
||||||
require 'uri'
|
|
||||||
|
|
||||||
module DRb
|
|
||||||
module HTTP0
|
|
||||||
class StrStream
|
|
||||||
def initialize(str='')
|
|
||||||
@buf = str
|
|
||||||
end
|
|
||||||
attr_reader :buf
|
|
||||||
|
|
||||||
def read(n)
|
|
||||||
begin
|
|
||||||
return @buf[0,n]
|
|
||||||
ensure
|
|
||||||
@buf[0,n] = ''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def write(s)
|
|
||||||
@buf.concat s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.uri_option(uri, config)
|
|
||||||
return uri, nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.open(uri, config)
|
|
||||||
unless /^http:/ =~ uri
|
|
||||||
raise(DRbBadScheme, uri) unless uri =~ /^http:/
|
|
||||||
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
|
||||||
end
|
|
||||||
ClientSide.new(uri, config)
|
|
||||||
end
|
|
||||||
|
|
||||||
class ClientSide
|
|
||||||
def initialize(uri, config)
|
|
||||||
@uri = uri
|
|
||||||
@res = nil
|
|
||||||
@config = config
|
|
||||||
@msg = DRbMessage.new(config)
|
|
||||||
@proxy = ENV['HTTP_PROXY']
|
|
||||||
end
|
|
||||||
|
|
||||||
def close; end
|
|
||||||
def alive?; false; end
|
|
||||||
|
|
||||||
def send_request(ref, msg_id, *arg, &b)
|
|
||||||
stream = StrStream.new
|
|
||||||
@msg.send_request(stream, ref, msg_id, *arg, &b)
|
|
||||||
@reply_stream = StrStream.new
|
|
||||||
post(@uri, stream.buf)
|
|
||||||
end
|
|
||||||
|
|
||||||
def recv_reply
|
|
||||||
@msg.recv_reply(@reply_stream)
|
|
||||||
end
|
|
||||||
|
|
||||||
def post(url, data)
|
|
||||||
it = URI.parse(url)
|
|
||||||
path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
|
|
||||||
http = Net::HTTP.new(it.host, it.port)
|
|
||||||
sio = StrStream.new
|
|
||||||
http.post(path, data, {'Content-Type'=>'application/octetstream;'}) do |str|
|
|
||||||
sio.write(str)
|
|
||||||
if @config[:load_limit] < sio.buf.size
|
|
||||||
raise TypeError, 'too large packet'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@reply_stream = sio
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
DRbProtocol.add_protocol(HTTP0)
|
|
||||||
end
|
|
|
@ -1,120 +0,0 @@
|
||||||
require 'webrick'
|
|
||||||
require 'drb/drb'
|
|
||||||
require_relative 'http0'
|
|
||||||
|
|
||||||
module DRb
|
|
||||||
module HTTP0
|
|
||||||
|
|
||||||
def self.open_server(uri, config)
|
|
||||||
unless /^http:/ =~ uri
|
|
||||||
raise(DRbBadScheme, uri) unless uri =~ /^http:/
|
|
||||||
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
|
||||||
end
|
|
||||||
Server.new(uri, config)
|
|
||||||
end
|
|
||||||
|
|
||||||
class Callback < WEBrick::HTTPServlet::AbstractServlet
|
|
||||||
def initialize(config, drb)
|
|
||||||
@config = config
|
|
||||||
@drb = drb
|
|
||||||
@queue = Thread::Queue.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def do_POST(req, res)
|
|
||||||
@req = req
|
|
||||||
@res = res
|
|
||||||
@drb.push(self)
|
|
||||||
@res.body = @queue.pop
|
|
||||||
@res['content-type'] = 'application/octet-stream;'
|
|
||||||
end
|
|
||||||
|
|
||||||
def req_body
|
|
||||||
@req.body
|
|
||||||
end
|
|
||||||
|
|
||||||
def reply(body)
|
|
||||||
@queue.push(body)
|
|
||||||
end
|
|
||||||
|
|
||||||
def close
|
|
||||||
@queue.push('')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Server
|
|
||||||
def initialize(uri, config)
|
|
||||||
@uri = uri
|
|
||||||
@config = config
|
|
||||||
@queue = Thread::Queue.new
|
|
||||||
setup_webrick(uri)
|
|
||||||
end
|
|
||||||
attr_reader :uri
|
|
||||||
|
|
||||||
def close
|
|
||||||
@server.shutdown if @server
|
|
||||||
@server = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def push(callback)
|
|
||||||
@queue.push(callback)
|
|
||||||
end
|
|
||||||
|
|
||||||
def accept
|
|
||||||
client = @queue.pop
|
|
||||||
ServerSide.new(uri, client, @config)
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup_webrick(uri)
|
|
||||||
logger = WEBrick::Log::new($stderr, WEBrick::Log::FATAL)
|
|
||||||
u = URI.parse(uri)
|
|
||||||
s = WEBrick::HTTPServer.new(:Port => u.port,
|
|
||||||
:AddressFamily => Socket::AF_INET,
|
|
||||||
:BindAddress => u.host,
|
|
||||||
:Logger => logger,
|
|
||||||
:ServerType => Thread)
|
|
||||||
s.mount(u.path, Callback, self)
|
|
||||||
@server = s
|
|
||||||
s.start
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ServerSide
|
|
||||||
def initialize(uri, callback, config)
|
|
||||||
@uri = uri
|
|
||||||
@callback = callback
|
|
||||||
@config = config
|
|
||||||
@msg = DRbMessage.new(@config)
|
|
||||||
@req_stream = StrStream.new(@callback.req_body)
|
|
||||||
end
|
|
||||||
attr_reader :uri
|
|
||||||
|
|
||||||
def close
|
|
||||||
@callback.close if @callback
|
|
||||||
@callback = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def alive?; false; end
|
|
||||||
|
|
||||||
def recv_request
|
|
||||||
begin
|
|
||||||
@msg.recv_request(@req_stream)
|
|
||||||
rescue
|
|
||||||
close
|
|
||||||
raise $!
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def send_reply(succ, result)
|
|
||||||
begin
|
|
||||||
return unless @callback
|
|
||||||
stream = StrStream.new
|
|
||||||
@msg.send_reply(stream, succ, result)
|
|
||||||
@callback.reply(stream.buf)
|
|
||||||
rescue
|
|
||||||
close
|
|
||||||
raise $!
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,117 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- NamedObject Sample
|
|
||||||
Copyright (c) 2000-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
How to play.
|
|
||||||
|
|
||||||
* start server
|
|
||||||
Terminal 1
|
|
||||||
| % ruby name.rb druby://yourhost:7640
|
|
||||||
| druby://yourhost:7640
|
|
||||||
| [return] to exit
|
|
||||||
|
|
||||||
* start client
|
|
||||||
Terminal 2
|
|
||||||
| % ruby namec.rb druby://yourhost:7640
|
|
||||||
| #<DRb::DRbObject:0x40164174 @uri="druby://yourhost:7640", @ref="seq">
|
|
||||||
| #<DRb::DRbObject:0x40163c9c @uri="druby://yourhost:7640", @ref="mutex">
|
|
||||||
| 1
|
|
||||||
| 2
|
|
||||||
| [return] to continue
|
|
||||||
|
|
||||||
* restart server
|
|
||||||
Terminal 1
|
|
||||||
type [return]
|
|
||||||
| % ruby name.rb druby://yourhost:7640
|
|
||||||
| druby://yourhost:7640
|
|
||||||
| [return] to exit
|
|
||||||
|
|
||||||
* continue client
|
|
||||||
Terminal 2
|
|
||||||
type [return]
|
|
||||||
| 1
|
|
||||||
| 2
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
module DRbNamedObject
|
|
||||||
DRbNAMEDICT = {}
|
|
||||||
DRBNAMEMUTEX = Thread::Mutex.new
|
|
||||||
attr_reader(:drb_name)
|
|
||||||
|
|
||||||
def drb_name=(name)
|
|
||||||
@drb_name = name
|
|
||||||
DRBNAMEMUTEX.synchronize do
|
|
||||||
raise(IndexError, name) if DRbNAMEDICT[name]
|
|
||||||
DRbNAMEDICT[name] = self
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class DRbNamedIdConv < DRb::DRbIdConv
|
|
||||||
def initialize
|
|
||||||
@dict = DRbNamedObject::DRbNAMEDICT
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_obj(ref)
|
|
||||||
@dict.fetch(ref) do super end
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_id(obj)
|
|
||||||
if obj.kind_of? DRbNamedObject
|
|
||||||
return obj.drb_name
|
|
||||||
else
|
|
||||||
return super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Seq
|
|
||||||
include DRbUndumped
|
|
||||||
include DRbNamedObject
|
|
||||||
|
|
||||||
def initialize(v, name)
|
|
||||||
@counter = v
|
|
||||||
@mutex = Thread::Mutex.new
|
|
||||||
self.drb_name = name
|
|
||||||
end
|
|
||||||
|
|
||||||
def next_value
|
|
||||||
@mutex.synchronize do
|
|
||||||
@counter += 1
|
|
||||||
return @counter
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Front
|
|
||||||
def initialize
|
|
||||||
seq = Seq.new(0, 'seq')
|
|
||||||
mutex = Thread::Mutex.new
|
|
||||||
mutex.extend(DRbUndumped)
|
|
||||||
mutex.extend(DRbNamedObject)
|
|
||||||
mutex.drb_name = 'mutex'
|
|
||||||
@name = {}
|
|
||||||
@name['seq'] = seq
|
|
||||||
@name['mutex'] = mutex
|
|
||||||
end
|
|
||||||
|
|
||||||
def [](k)
|
|
||||||
@name[k]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
uri = ARGV.shift
|
|
||||||
|
|
||||||
name_conv = DRbNamedIdConv.new
|
|
||||||
|
|
||||||
DRb.install_id_conv(name_conv)
|
|
||||||
DRb.start_service(uri, Front.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
=begin
|
|
||||||
distributed Ruby --- NamedObject Sample Client
|
|
||||||
Copyright (c) 2000-2001 Masatoshi SEKI
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
begin
|
|
||||||
there = ARGV.shift || raise
|
|
||||||
rescue
|
|
||||||
puts "usage: #{$0} <server_uri>"
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service()
|
|
||||||
ro = DRbObject.new(nil, there)
|
|
||||||
|
|
||||||
seq = ro["seq"]
|
|
||||||
mutex = ro["mutex"]
|
|
||||||
|
|
||||||
p seq
|
|
||||||
p mutex
|
|
||||||
|
|
||||||
mutex.synchronize do
|
|
||||||
p seq.next_value
|
|
||||||
p seq.next_value
|
|
||||||
end
|
|
||||||
|
|
||||||
puts '[return] to continue'
|
|
||||||
gets
|
|
||||||
|
|
||||||
mutex.synchronize do
|
|
||||||
p seq.next_value
|
|
||||||
p seq.next_value
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,212 +0,0 @@
|
||||||
#!/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|
|
|
||||||
@check_idx.push i if x
|
|
||||||
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|
|
|
||||||
unless @list[i] === tuple[i]
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@que = {}
|
|
||||||
@waiting = {}
|
|
||||||
@que.taint # enable tainted communication
|
|
||||||
@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)
|
|
||||||
begin
|
|
||||||
x[1].wakeup
|
|
||||||
rescue ThreadError
|
|
||||||
end
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
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)
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil unless found
|
|
||||||
|
|
||||||
@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
|
|
||||||
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
|
|
||||||
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
|
|
||||||
req = ts.in(['req', nil, nil])
|
|
||||||
ac = req[1]
|
|
||||||
num = req[2]
|
|
||||||
sleep id
|
|
||||||
ts.out([ac, id, num, num * num])
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def client(ts, n)
|
|
||||||
Thread.start {
|
|
||||||
ac = Object.new
|
|
||||||
tuples = (1..10).collect { |i|
|
|
||||||
['req', ac, i * 10 + n]
|
|
||||||
}
|
|
||||||
ts.out(*tuples)
|
|
||||||
ts.out(tuples[0])
|
|
||||||
puts "out: #{n}"
|
|
||||||
11.times do |i|
|
|
||||||
ans = ts.in([ac, nil, nil, nil])
|
|
||||||
puts "client(#{n}) server(#{ans[1]}) #{ans[2]} #{ans[3]}"
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def watcher(ts)
|
|
||||||
Thread.start {
|
|
||||||
loop do
|
|
||||||
begin
|
|
||||||
sleep 1
|
|
||||||
p ts.rd(['req', nil, nil], true)
|
|
||||||
rescue ThreadError
|
|
||||||
puts "'req' not found."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
(0..3).each do |n|
|
|
||||||
servers.push(server(ts, n))
|
|
||||||
end
|
|
||||||
|
|
||||||
(1..6).each do |n|
|
|
||||||
clients.push(client(ts, n))
|
|
||||||
end
|
|
||||||
|
|
||||||
(1..3).each do
|
|
||||||
watcher(ts)
|
|
||||||
end
|
|
||||||
|
|
||||||
clients.each do |t|
|
|
||||||
t.join
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'rinda/tuplespace'
|
|
||||||
|
|
||||||
uri = ARGV.shift
|
|
||||||
DRb.start_service(uri, Rinda::TupleSpace.new)
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
|
@ -1,17 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'rinda/rinda'
|
|
||||||
|
|
||||||
uri = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
ts = Rinda::TupleSpaceProxy.new(DRbObject.new(nil, uri))
|
|
||||||
|
|
||||||
(1..10).each do |n|
|
|
||||||
ts.write(['sum', DRb.uri, n])
|
|
||||||
end
|
|
||||||
|
|
||||||
(1..10).each do |n|
|
|
||||||
ans = ts.take(['ans', DRb.uri, n, nil])
|
|
||||||
p [ans[2], ans[3]]
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'rinda/rinda'
|
|
||||||
|
|
||||||
def do_it(v)
|
|
||||||
puts "do_it(#{v})"
|
|
||||||
v + v
|
|
||||||
end
|
|
||||||
|
|
||||||
uri = ARGV.shift || raise("usage: #{$0} <server_uri>")
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
ts = Rinda::TupleSpaceProxy.new(DRbObject.new(nil, uri))
|
|
||||||
|
|
||||||
while true
|
|
||||||
r = ts.take(['sum', nil, nil])
|
|
||||||
v = do_it(r[2])
|
|
||||||
ts.write(['ans', r[1], r[2], v])
|
|
||||||
end
|
|
|
@ -1,29 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'drb/eq'
|
|
||||||
require 'rinda/ring'
|
|
||||||
|
|
||||||
class RingEcho
|
|
||||||
include DRbUndumped
|
|
||||||
def initialize(name)
|
|
||||||
@name = name
|
|
||||||
end
|
|
||||||
|
|
||||||
def echo(str)
|
|
||||||
"#{@name}: #{str}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
|
|
||||||
renewer = Rinda::SimpleRenewer.new
|
|
||||||
|
|
||||||
finder = Rinda::RingFinger.new
|
|
||||||
ts = finder.lookup_ring_any
|
|
||||||
ts.read_all([:name, :RingEcho, nil, nil]).each do |tuple|
|
|
||||||
p tuple[2]
|
|
||||||
puts tuple[2].echo('Hello, World') rescue nil
|
|
||||||
end
|
|
||||||
ts.write([:name, :RingEcho, RingEcho.new(DRb.uri), ''], renewer)
|
|
||||||
|
|
||||||
DRb.thread.join
|
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
require 'rinda/ring'
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
class Inspector
|
|
||||||
def initialize
|
|
||||||
end
|
|
||||||
|
|
||||||
def primary
|
|
||||||
Rinda::RingFinger.primary
|
|
||||||
end
|
|
||||||
|
|
||||||
def list_place
|
|
||||||
Rinda::RingFinger.to_a
|
|
||||||
end
|
|
||||||
|
|
||||||
def list(idx = -1)
|
|
||||||
if idx < 0
|
|
||||||
ts = primary
|
|
||||||
else
|
|
||||||
ts = list_place[idx]
|
|
||||||
raise "RingNotFound" unless ts
|
|
||||||
end
|
|
||||||
ts.read_all([:name, nil, nil, nil])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def main
|
|
||||||
DRb.start_service
|
|
||||||
r = Inspector.new
|
|
||||||
end
|
|
|
@ -1,25 +0,0 @@
|
||||||
require 'drb/drb'
|
|
||||||
require 'rinda/ring'
|
|
||||||
require 'rinda/tuplespace'
|
|
||||||
|
|
||||||
unless $DEBUG
|
|
||||||
# Run as a daemon...
|
|
||||||
exit!( 0 ) if fork
|
|
||||||
Process.setsid
|
|
||||||
exit!( 0 ) if fork
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.start_service(ARGV.shift)
|
|
||||||
|
|
||||||
ts = Rinda::TupleSpace.new
|
|
||||||
place = Rinda::RingServer.new(ts)
|
|
||||||
|
|
||||||
if $DEBUG
|
|
||||||
puts DRb.uri
|
|
||||||
DRb.thread.join
|
|
||||||
else
|
|
||||||
STDIN.reopen(IO::NULL)
|
|
||||||
STDOUT.reopen(IO::NULL, 'w')
|
|
||||||
STDERR.reopen(IO::NULL, 'w')
|
|
||||||
DRb.thread.join
|
|
||||||
end
|
|
|
@ -1,89 +0,0 @@
|
||||||
#!/usr/local/bin/ruby
|
|
||||||
# SimpleTupleSpace
|
|
||||||
# Copyright (c) 1999-2000 Masatoshi SEKI
|
|
||||||
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
||||||
|
|
||||||
class SimpleTupleSpace
|
|
||||||
def initialize
|
|
||||||
@hash = {}
|
|
||||||
@waiting = {}
|
|
||||||
@hash.taint
|
|
||||||
@waiting.taint
|
|
||||||
self.taint
|
|
||||||
end
|
|
||||||
|
|
||||||
def out(key, obj)
|
|
||||||
Thread.critical = true
|
|
||||||
@hash[key] ||= []
|
|
||||||
@waiting[key] ||= []
|
|
||||||
@hash[key].push obj
|
|
||||||
begin
|
|
||||||
t = @waiting[key].shift
|
|
||||||
@waiting.delete(key) if @waiting[key].length == 0
|
|
||||||
t.wakeup if t
|
|
||||||
rescue ThreadError
|
|
||||||
retry
|
|
||||||
ensure
|
|
||||||
Thread.critical = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def in(key)
|
|
||||||
Thread.critical = true
|
|
||||||
@hash[key] ||= []
|
|
||||||
@waiting[key] ||= []
|
|
||||||
begin
|
|
||||||
loop do
|
|
||||||
if @hash[key].length == 0
|
|
||||||
@waiting[key].push Thread.current
|
|
||||||
Thread.stop
|
|
||||||
else
|
|
||||||
return @hash[key].shift
|
|
||||||
end
|
|
||||||
end
|
|
||||||
ensure
|
|
||||||
@hash.delete(key) if @hash[key].length == 0
|
|
||||||
Thread.critical = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
ts = SimpleTupleSpace.new
|
|
||||||
clients = []
|
|
||||||
servers = []
|
|
||||||
|
|
||||||
def server(ts)
|
|
||||||
Thread.start {
|
|
||||||
loop do
|
|
||||||
req = ts.in('req')
|
|
||||||
ac = req[0]
|
|
||||||
num = req[1]
|
|
||||||
ts.out(ac, num * num)
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def client(ts, n)
|
|
||||||
Thread.start {
|
|
||||||
ac = Object.new
|
|
||||||
ts.out('req', [ac, n])
|
|
||||||
ans = ts.in(ac)
|
|
||||||
puts "#{n}: #{ans}"
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
3.times do
|
|
||||||
servers.push(server(ts))
|
|
||||||
end
|
|
||||||
|
|
||||||
(1..6).each do |n|
|
|
||||||
clients.push(client(ts, n))
|
|
||||||
end
|
|
||||||
|
|
||||||
clients.each do |t|
|
|
||||||
t.join
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
#!/usr/local/bin/ruby
|
|
||||||
|
|
||||||
uri = ARGV.shift || raise("usage: #{$0} URI")
|
|
||||||
N = (ARGV.shift || 100).to_i
|
|
||||||
|
|
||||||
case uri
|
|
||||||
when /^tcpromp:/, /^unixromp:/
|
|
||||||
require 'romp'
|
|
||||||
|
|
||||||
client = ROMP::Client.new(uri, false)
|
|
||||||
foo = client.resolve("foo")
|
|
||||||
when /^druby:/
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
DRb.start_service
|
|
||||||
foo = DRbObject.new(nil, uri)
|
|
||||||
end
|
|
||||||
|
|
||||||
N.times do |n|
|
|
||||||
foo.foo(n)
|
|
||||||
end
|
|
|
@ -1,31 +0,0 @@
|
||||||
class Foo
|
|
||||||
attr_reader :i
|
|
||||||
def initialize
|
|
||||||
@i = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def foo(i)
|
|
||||||
@i = i
|
|
||||||
i + i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# server = ROMP::Server.new('tcpromp://localhost:4242', nil, true)
|
|
||||||
|
|
||||||
uri = ARGV.shift || raise("usage: #{$0} URI")
|
|
||||||
foo = Foo.new
|
|
||||||
|
|
||||||
case uri
|
|
||||||
when /^tcpromp:/, /^unixromp:/
|
|
||||||
require 'romp'
|
|
||||||
|
|
||||||
server = ROMP::Server.new(uri, nil, true)
|
|
||||||
server.bind(foo, "foo")
|
|
||||||
|
|
||||||
when /^druby:/
|
|
||||||
require 'drb/drb'
|
|
||||||
|
|
||||||
DRb.start_service(uri, Foo.new)
|
|
||||||
end
|
|
||||||
|
|
||||||
DRb.thread.join
|
|
Loading…
Add table
Add a link
Reference in a new issue