diff options
Diffstat (limited to 'sample/drb')
36 files changed, 299 insertions, 306 deletions
diff --git a/sample/drb/README.ja.rdoc b/sample/drb/README.ja.rdoc new file mode 100644 index 0000000000..3ab70f3369 --- /dev/null +++ b/sample/drb/README.ja.rdoc @@ -0,0 +1,59 @@ += サンプルスクリプト + +* 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 + +* DRbProtoclの追加例 + * http0.rb + * http0serv.rb + +* ringの使用例 + * ring_place.rb + * ring_echo.rb diff --git a/sample/drb/README.rd.ja b/sample/drb/README.rd.ja deleted file mode 100644 index 04143b9ad5..0000000000 --- a/sample/drb/README.rd.ja +++ /dev/null @@ -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.rbQueueФΤʤ֥(DQEntry) - push뤬DRbUnknownˤꥯ饤dqout.rbpopǤ롣 - * dqueue.rb --- server - * dqin.rb --- clientDQEntry֥Ȥpush - * dqout.rb --- clientDQEntry֥Ȥpop - * dqlib.rb --- DQEntry饤֥ - -* ̾ˤ뻲 - IdConvޥidǤʤ̾ǻȤ - * name.rb --- server - * namec.rb --- client - -* extservΥץ - * extserv_test.rb - -* TimerIdConvλ - * holders.rb --- serverruby -d hodlers.rbȤTimerIdConvѤ롣 - * holderc.rb --- client - -* rinda.rbλ - * rinda_ts.rb --- TupleSpaceС - * rindac.rb --- TupleSpaceclientǥץꥱclient - * rindas.rb --- TupleSpaceclientǥץꥱserver - -* observerλ - cdbiff - ((<URI:http://namazu.org/~satoru/cdbiff/>)) - * dbiff.rb --- dcdbiff server - * dcdbiff.rb --- dcdbiff client - -* drbsslλ - * drbssl_s.rb - * drbssl_c.rb - -* DRbProtoclɲ - * http0.rb - * http0serv.rb - -* ringλ - * ring_place.rb - * ring_echo.rb diff --git a/sample/drb/README.rd b/sample/drb/README.rdoc index 5cf1f51913..80ae910a8a 100644 --- a/sample/drb/README.rd +++ b/sample/drb/README.rdoc @@ -22,7 +22,7 @@ * dqout.rb --- client. pop DQEntry objects. * dqlib.rb --- define DQEntry -* IdConv customize demo: reference by name +* IdConv customize demo: reference by name * name.rb --- server * namec.rb --- client @@ -39,14 +39,14 @@ * rindac.rb --- service user * observer - cdbiff - ((<URI:http://namazu.org/~satoru/cdbiff/>)) + cdbiff - http://namazu.org/~satoru/cdbiff/ * dbiff.rb --- dcdbiff server * dcdbiff.rb --- dcdbiff client * drbssl * drbssl_s.rb * drbssl_c.rb - + * add DRbProtocl * http0.rb * http0serv.rb diff --git a/sample/drb/acl.rb b/sample/drb/acl.rb new file mode 100644 index 0000000000..d93eb9c1fc --- /dev/null +++ b/sample/drb/acl.rb @@ -0,0 +1,15 @@ +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) diff --git a/sample/drb/darray.rb b/sample/drb/darray.rb index 95ee01ff03..d2ac39513f 100644 --- a/sample/drb/darray.rb +++ b/sample/drb/darray.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- Array - Copyright (c) 1999-2001 Masatoshi SEKI + Copyright (c) 1999-2001 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/darrayc.rb b/sample/drb/darrayc.rb index 6f5ff6bb5d..579e11564e 100644 --- a/sample/drb/darrayc.rb +++ b/sample/drb/darrayc.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- Array client - Copyright (c) 1999-2001 Masatoshi SEKI + Copyright (c) 1999-2001 Masatoshi SEKI =end require 'drb/drb' @@ -19,7 +19,7 @@ p a puts "# find" p ro.find { |x| x.kind_of? String } - + puts "# each, break" ro.each do |x| next if x == "five" @@ -45,15 +45,3 @@ ro.each do |x| puts count redo if count == 3 end - -puts "# each, retry" -retried = false -ro.each do |x| - puts x - if x == 4 && !retried - puts 'retry' - retried = true - retry - end -end - diff --git a/sample/drb/dbiff.rb b/sample/drb/dbiff.rb index 8faef50b07..290eb1d28b 100644 --- a/sample/drb/dbiff.rb +++ b/sample/drb/dbiff.rb @@ -18,19 +18,19 @@ class Biff 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 + begin + sleep(@interval) + current = File::mtime(@filename) + if current > last + changed + begin + notify_observers(@filename, current) + rescue Error + end + last = current + end rescue - next + next end end end diff --git a/sample/drb/dchatc.rb b/sample/drb/dchatc.rb index b506f5bbba..2b8ddbf4cc 100644 --- a/sample/drb/dchatc.rb +++ b/sample/drb/dchatc.rb @@ -1,12 +1,12 @@ =begin distributed Ruby --- chat client - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' class ChatClient - include DRbUndumped + include DRbUndumped def initialize(name) @name = name diff --git a/sample/drb/dchats.rb b/sample/drb/dchats.rb index 012dfeeebe..58af3cf005 100644 --- a/sample/drb/dchats.rb +++ b/sample/drb/dchats.rb @@ -1,8 +1,7 @@ =begin distributed Ruby --- chat server - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end -require 'thread' require 'drb/drb' class ChatEntry @@ -47,16 +46,16 @@ class ChatServer 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 + begin + if m == there + @members[m].listen(msg2) + else + @members[m].listen(msg) + end + rescue + p $! + @members.delete(m) + end end end end diff --git a/sample/drb/dhasen.rb b/sample/drb/dhasen.rb index fb1724afa3..13ff38940e 100644 --- a/sample/drb/dhasen.rb +++ b/sample/drb/dhasen.rb @@ -1,13 +1,13 @@ =begin distributed Ruby --- dRuby Sample Server --- chasen server - Copyright (c) 1999-2001 Masatoshi SEKI + Copyright (c) 1999-2001 Masatoshi SEKI =end =begin How to play. Terminal 1 - | % ruby dhasen.rb + | % ruby dhasen.rb | druby://yourhost:7640 Terminal 2 @@ -17,13 +17,12 @@ require 'drb/drb' require 'chasen' -require 'thread' class Dhasen include DRbUndumped def initialize - @mutex = Mutex.new + @mutex = Thread::Mutex.new end def sparse(str, *arg) diff --git a/sample/drb/dhasenc.rb b/sample/drb/dhasenc.rb index 8114e9228d..dddac9882c 100644 --- a/sample/drb/dhasenc.rb +++ b/sample/drb/dhasenc.rb @@ -1,6 +1,7 @@ +# -*- coding: utf-8 -*- =begin distributed Ruby --- dRuby Sample Client -- chasen client - Copyright (c) 1999-2001 Masatoshi SEKI + Copyright (c) 1999-2001 Masatoshi SEKI =end require 'drb/drb' @@ -9,5 +10,5 @@ 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') +print dhasen.sparse("本日は、晴天なり。", "-F", '(%BB %m %M)\n', "-j") +print dhasen.sparse("本日は、晴天なり。", "-F", '(%m %M)\n') diff --git a/sample/drb/dlogc.rb b/sample/drb/dlogc.rb index c75bc7b520..3939a71827 100644 --- a/sample/drb/dlogc.rb +++ b/sample/drb/dlogc.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- Log test - Copyright (c) 1999-2001 Masatoshi SEKI + Copyright (c) 1999-2001 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/dlogd.rb b/sample/drb/dlogd.rb index 9f9aa2fd56..a87e660346 100644 --- a/sample/drb/dlogd.rb +++ b/sample/drb/dlogd.rb @@ -1,16 +1,15 @@ =begin distributed Ruby --- Log server - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' -require 'thread' class Logger def initialize(fname) @fname = fname.to_s @fp = File.open(@fname, "a+") - @queue = Queue.new + @queue = Thread::Queue.new @th = Thread.new { self.flush } end @@ -21,8 +20,8 @@ class Logger def flush begin while(1) - @fp.puts(@queue.pop) - @fp.flush + @fp.puts(@queue.pop) + @fp.flush end ensure @fp.close diff --git a/sample/drb/dqin.rb b/sample/drb/dqin.rb index 3ba1caa80c..4751335fff 100644 --- a/sample/drb/dqin.rb +++ b/sample/drb/dqin.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- store - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/dqout.rb b/sample/drb/dqout.rb index 4700e55cf7..f2b0b4ac95 100644 --- a/sample/drb/dqout.rb +++ b/sample/drb/dqout.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- fetch - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/dqueue.rb b/sample/drb/dqueue.rb index a5a43655fd..a9afa8c858 100644 --- a/sample/drb/dqueue.rb +++ b/sample/drb/dqueue.rb @@ -1,12 +1,11 @@ =begin distributed Ruby --- Queue - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end -require 'thread' require 'drb/drb' -DRb.start_service(nil, Queue.new) +DRb.start_service(nil, Thread::Queue.new) puts DRb.uri DRb.thread.join diff --git a/sample/drb/drbc.rb b/sample/drb/drbc.rb index 00132b46c8..50a86c39e8 100644 --- a/sample/drb/drbc.rb +++ b/sample/drb/drbc.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- dRuby Sample Client - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/drbch.rb b/sample/drb/drbch.rb index 495ff1c346..07fdcd5fae 100644 --- a/sample/drb/drbch.rb +++ b/sample/drb/drbch.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- dRuby Sample Client - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/drbm.rb b/sample/drb/drbm.rb index 74a15a4696..3390608cd1 100644 --- a/sample/drb/drbm.rb +++ b/sample/drb/drbm.rb @@ -1,13 +1,13 @@ =begin multiple DRbServer - Copyright (c) 1999-2002 Masatoshi SEKI + Copyright (c) 1999-2002 Masatoshi SEKI =end =begin How to play. Terminal 1 - | % ruby drbm.rb + | % ruby drbm.rb | druby://yourhost:7640 druby://yourhost:7641 Terminal 2 @@ -24,7 +24,7 @@ class Hoge def initialize(s) @str = s end - + def to_s @str end diff --git a/sample/drb/drbmc.rb b/sample/drb/drbmc.rb index c654fcea05..fd191401e6 100644 --- a/sample/drb/drbmc.rb +++ b/sample/drb/drbmc.rb @@ -1,6 +1,6 @@ =begin multiple DRbServer client - Copyright (c) 1999-2002 Masatoshi SEKI + Copyright (c) 1999-2002 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/drbs-acl.rb b/sample/drb/drbs-acl.rb index 151dd945d8..71c4f7bf42 100644 --- a/sample/drb/drbs-acl.rb +++ b/sample/drb/drbs-acl.rb @@ -1,13 +1,13 @@ =begin distributed Ruby --- dRuby Sample Server - Copyright (c) 1999-2000 Masatoshi SEKI + Copyright (c) 1999-2000 Masatoshi SEKI =end =begin How to play. Terminal 1 - | % ruby drbs.rb + | % ruby drbs.rb | druby://yourhost:7640 Terminal 2 @@ -41,7 +41,7 @@ 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) diff --git a/sample/drb/drbs.rb b/sample/drb/drbs.rb index b76e283c80..5a913d9918 100644 --- a/sample/drb/drbs.rb +++ b/sample/drb/drbs.rb @@ -1,13 +1,13 @@ =begin distributed Ruby --- dRuby Sample Server - Copyright (c) 1999-2000,2002 Masatoshi SEKI + Copyright (c) 1999-2000,2002 Masatoshi SEKI =end =begin How to play. Terminal 1 - | % ruby drbs.rb + | % ruby drbs.rb | druby://yourhost:7640 Terminal 2 diff --git a/sample/drb/extserv_test.rb b/sample/drb/extserv_test.rb index 83d871a6a2..2c4f485dc6 100644 --- a/sample/drb/extserv_test.rb +++ b/sample/drb/extserv_test.rb @@ -1,11 +1,11 @@ =begin dRuby sample - Copyright (c) 2000 Masatoshi SEKI + Copyright (c) 2000 Masatoshi SEKI = How to play * Terminal 1 - + % ruby -I. extserv_test.rb server druby://yourhost:12345 @@ -26,11 +26,11 @@ end class Foo include DRbUndumped - + def initialize(str) @str = str end - + def hello(it) "#{it}: #{self}" end @@ -55,7 +55,7 @@ when 'server' 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 @@ -72,7 +72,7 @@ else t2 = s.service('itest2').front puts t2 puts t1.hello(t2) - if (cmd == 'quit') + if (cmd == 'quit') s.service('itest1').stop_service s.service('itest2').stop_service end diff --git a/sample/drb/gw_cu.rb b/sample/drb/gw_cu.rb index 0e5ed36b8f..8079cbdc4f 100644 --- a/sample/drb/gw_cu.rb +++ b/sample/drb/gw_cu.rb @@ -13,7 +13,7 @@ class Foo end end -DRb.start_service('drubyunix:', nil) +DRb.start_service('drbunix:', nil) puts DRb.uri ro = DRbObject.new(nil, ARGV.shift) diff --git a/sample/drb/holderc.rb b/sample/drb/holderc.rb index 8dd72ebd11..e627916d76 100644 --- a/sample/drb/holderc.rb +++ b/sample/drb/holderc.rb @@ -11,7 +11,7 @@ DRb.start_service() ro = DRbObject.new(nil, there) ary = [] -10.times do +10.times do ary.push(ro.gen) end diff --git a/sample/drb/holders.rb b/sample/drb/holders.rb index 2d9974f3fc..293426faa5 100644 --- a/sample/drb/holders.rb +++ b/sample/drb/holders.rb @@ -14,7 +14,7 @@ % ruby holderc.rb druby://yourhost:1234 =end - + require 'drb/drb' @@ -36,11 +36,11 @@ end class DRbEx4 include DRbUndumped - + def initialize @curr = 1 end - + def gen begin @curr += 1 diff --git a/sample/drb/http0.rb b/sample/drb/http0.rb index 7649925282..e40d810311 100644 --- a/sample/drb/http0.rb +++ b/sample/drb/http0.rb @@ -6,20 +6,20 @@ module DRb module HTTP0 class StrStream def initialize(str='') - @buf = str + @buf = str end attr_reader :buf def read(n) - begin - return @buf[0,n] - ensure - @buf[0,n] = '' - end + begin + return @buf[0,n] + ensure + @buf[0,n] = '' + end end def write(s) - @buf.concat s + @buf.concat s end end @@ -29,47 +29,47 @@ module DRb def self.open(uri, config) unless /^http:/ =~ uri - raise(DRbBadScheme, uri) unless uri =~ /^http:/ - raise(DRbBadURI, 'can\'t parse uri:' + 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'] + @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) + 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) + @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 + 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 diff --git a/sample/drb/http0serv.rb b/sample/drb/http0serv.rb index 100d126b8f..1a58811fed 100644 --- a/sample/drb/http0serv.rb +++ b/sample/drb/http0serv.rb @@ -1,118 +1,117 @@ require 'webrick' require 'drb/drb' require 'drb/http0' -require 'thread' 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) + 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 = Queue.new + @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;' + @req = req + @res = res + @drb.push(self) + @res.body = @queue.pop + @res['content-type'] = 'application/octet-stream;' end - + def req_body - @req.body + @req.body end def reply(body) - @queue.push(body) + @queue.push(body) end - + def close - @queue.push('') + @queue.push('') end end class Server def initialize(uri, config) - @uri = uri - @config = config - @queue = Queue.new - setup_webrick(uri) + @uri = uri + @config = config + @queue = Thread::Queue.new + setup_webrick(uri) end attr_reader :uri def close - @server.shutdown if @server - @server = nil + @server.shutdown if @server + @server = nil end def push(callback) - @queue.push(callback) + @queue.push(callback) end def accept - client = @queue.pop - ServerSide.new(client, @config) + client = @queue.pop + ServerSide.new(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 + 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(callback, config) - @callback = callback - @config = config - @msg = DRbMessage.new(@config) - @req_stream = StrStream.new(@callback.req_body) + @callback = callback + @config = config + @msg = DRbMessage.new(@config) + @req_stream = StrStream.new(@callback.req_body) end - + def close - @callback.close if @callback - @callback = nil + @callback.close if @callback + @callback = nil end def alive?; false; end def recv_request - begin - @msg.recv_request(@req_stream) - rescue - close - raise $! - end + 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 + begin + return unless @callback + stream = StrStream.new + @msg.send_reply(stream, succ, result) + @callback.reply(stream.buf) + rescue + close + raise $! + end end end end diff --git a/sample/drb/name.rb b/sample/drb/name.rb index 86b478fbec..a8ad28749d 100644 --- a/sample/drb/name.rb +++ b/sample/drb/name.rb @@ -1,12 +1,12 @@ =begin distributed Ruby --- NamedObject Sample - Copyright (c) 2000-2001 Masatoshi SEKI + Copyright (c) 2000-2001 Masatoshi SEKI =end =begin How to play. -* start server +* start server Terminal 1 | % ruby name.rb druby://yourhost:7640 | druby://yourhost:7640 @@ -28,14 +28,13 @@ How to play. | druby://yourhost:7640 | [return] to exit -* continue client +* continue client Terminal 2 - type [return] + type [return] | 1 | 2 =end -require 'thread.rb' require 'drb/drb' module DRbNamedObject @@ -44,7 +43,7 @@ module DRbNamedObject def drb_name=(name) @drb_name = name - Thread.exclusive do + Thread.exclusive do raise(IndexError, name) if DRbNAMEDICT[name] DRbNAMEDICT[name] = self end @@ -62,7 +61,7 @@ class DRbNamedIdConv < DRb::DRbIdConv def to_id(obj) if obj.kind_of? DRbNamedObject - return obj.drb_name + return obj.drb_name else return super end @@ -75,7 +74,7 @@ class Seq def initialize(v, name) @counter = v - @mutex = Mutex.new + @mutex = Thread::Mutex.new self.drb_name = name end @@ -90,7 +89,7 @@ end class Front def initialize seq = Seq.new(0, 'seq') - mutex = Mutex.new + mutex = Thread::Mutex.new mutex.extend(DRbUndumped) mutex.extend(DRbNamedObject) mutex.drb_name = 'mutex' @@ -98,7 +97,7 @@ class Front @name['seq'] = seq @name['mutex'] = mutex end - + def [](k) @name[k] end @@ -108,7 +107,7 @@ 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 diff --git a/sample/drb/namec.rb b/sample/drb/namec.rb index f6db6f3022..98b9d0e532 100644 --- a/sample/drb/namec.rb +++ b/sample/drb/namec.rb @@ -1,6 +1,6 @@ =begin distributed Ruby --- NamedObject Sample Client - Copyright (c) 2000-2001 Masatoshi SEKI + Copyright (c) 2000-2001 Masatoshi SEKI =end require 'drb/drb' diff --git a/sample/drb/old_tuplespace.rb b/sample/drb/old_tuplespace.rb index 3e13b92ee1..8be1542c06 100644 --- a/sample/drb/old_tuplespace.rb +++ b/sample/drb/old_tuplespace.rb @@ -3,15 +3,13 @@ # Copyright (c) 1999-2000 Masatoshi SEKI # You can redistribute it and/or modify it under the same terms as Ruby. -require 'thread' - class TupleSpace class Template def initialize(list) @list = list @check_idx = [] @list.each_with_index do |x, i| - @check_idx.push i if x + @check_idx.push i if x end @size = @list.size end @@ -22,9 +20,9 @@ class TupleSpace def match(tuple) return nil if tuple.size != self.size @check_idx.each do |i| - unless @list[i] === tuple[i] - return false - end + unless @list[i] === tuple[i] + return false + end end return true end @@ -47,13 +45,13 @@ class TupleSpace found = false @waiting[sz] = @waiting[sz].find_all { |x| if x[0].match(tuple) - begin - x[1].wakeup - rescue ThreadError - end - false + begin + x[1].wakeup + rescue ThreadError + end + false else - true + true end } end @@ -77,12 +75,12 @@ class TupleSpace found = false @que[sz].each_with_index do |x, i| if template.match(x) - found = true - break + found = true + break end end return nil unless found - + @que[sz].delete_at(i) return x @@ -110,17 +108,17 @@ class TupleSpace 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 + 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 @@ -155,11 +153,11 @@ if __FILE__ == $0 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]) + req = ts.in(['req', nil, nil]) + ac = req[1] + num = req[2] + sleep id + ts.out([ac, id, num, num * num]) end } end @@ -167,15 +165,15 @@ if __FILE__ == $0 def client(ts, n) Thread.start { ac = Object.new - tuples = (1..10).collect { |i| - ['req', ac, i * 10 + n] + 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]}" + ans = ts.in([ac, nil, nil, nil]) + puts "client(#{n}) server(#{ans[1]}) #{ans[2]} #{ans[3]}" end } end @@ -183,12 +181,12 @@ if __FILE__ == $0 def watcher(ts) Thread.start { loop do - begin - sleep 1 - p ts.rd(['req', nil, nil], true) - rescue ThreadError - puts "'req' not found." - end + begin + sleep 1 + p ts.rd(['req', nil, nil], true) + rescue ThreadError + puts "'req' not found." + end end } end @@ -201,7 +199,7 @@ if __FILE__ == $0 clients.push(client(ts, n)) end - (1..3).each do + (1..3).each do watcher(ts) end diff --git a/sample/drb/ring_echo.rb b/sample/drb/ring_echo.rb index 0633aa839a..c54628b54c 100644 --- a/sample/drb/ring_echo.rb +++ b/sample/drb/ring_echo.rb @@ -1,14 +1,13 @@ require 'drb/drb' require 'drb/eq' require 'rinda/ring' -require 'thread' class RingEcho include DRbUndumped def initialize(name) @name = name end - + def echo(str) "#{@name}: #{str}" end diff --git a/sample/drb/ring_place.rb b/sample/drb/ring_place.rb index 0ceef7c65a..11c6c2fe80 100644 --- a/sample/drb/ring_place.rb +++ b/sample/drb/ring_place.rb @@ -18,8 +18,8 @@ if $DEBUG puts DRb.uri DRb.thread.join else - STDIN.reopen('/dev/null') - STDOUT.reopen('/dev/null', 'w') - STDERR.reopen('/dev/null', 'w') + STDIN.reopen(IO::NULL) + STDOUT.reopen(IO::NULL, 'w') + STDERR.reopen(IO::NULL, 'w') DRb.thread.join end diff --git a/sample/drb/simpletuple.rb b/sample/drb/simpletuple.rb index 3ae9208c79..4bb4b1cff9 100644 --- a/sample/drb/simpletuple.rb +++ b/sample/drb/simpletuple.rb @@ -3,8 +3,6 @@ # Copyright (c) 1999-2000 Masatoshi SEKI # You can redistribute it and/or modify it under the same terms as Ruby. -require 'thread' - class SimpleTupleSpace def initialize @hash = {} @@ -13,7 +11,7 @@ class SimpleTupleSpace @waiting.taint self.taint end - + def out(key, obj) Thread.critical = true @hash[key] ||= [] @@ -48,7 +46,7 @@ class SimpleTupleSpace Thread.critical = false end end -end +end if __FILE__ == $0 ts = SimpleTupleSpace.new @@ -58,10 +56,10 @@ if __FILE__ == $0 def server(ts) Thread.start { loop do - req = ts.in('req') - ac = req[0] - num = req[1] - ts.out(ac, num * num) + req = ts.in('req') + ac = req[0] + num = req[1] + ts.out(ac, num * num) end } end @@ -75,7 +73,7 @@ if __FILE__ == $0 } end - 3.times do + 3.times do servers.push(server(ts)) end diff --git a/sample/drb/speedc.rb b/sample/drb/speedc.rb index 14d526d48d..64b8a65021 100644 --- a/sample/drb/speedc.rb +++ b/sample/drb/speedc.rb @@ -11,7 +11,7 @@ when /^tcpromp:/, /^unixromp:/ foo = client.resolve("foo") when /^druby:/ require 'drb/drb' - + DRb.start_service foo = DRbObject.new(nil, uri) end diff --git a/sample/drb/speeds.rb b/sample/drb/speeds.rb index 76b4b29dba..7984059423 100644 --- a/sample/drb/speeds.rb +++ b/sample/drb/speeds.rb @@ -3,7 +3,7 @@ class Foo def initialize @i = 0 end - + def foo(i) @i = i i + i @@ -24,7 +24,7 @@ when /^tcpromp:/, /^unixromp:/ when /^druby:/ require 'drb/drb' - + DRb.start_service(uri, Foo.new) end |
