diff options
Diffstat (limited to 'sample/drb')
| -rw-r--r-- | sample/drb/README.ja.rdoc (renamed from sample/drb/README.rd.ja) | 4 | ||||
| -rw-r--r-- | sample/drb/README.rdoc (renamed from sample/drb/README.rd) | 8 | ||||
| -rw-r--r-- | sample/drb/acl.rb | 15 | ||||
| -rw-r--r-- | sample/drb/dbiff.rb | 22 | ||||
| -rw-r--r-- | sample/drb/dchats.rb | 23 | ||||
| -rw-r--r-- | sample/drb/dhasen.rb | 3 | ||||
| -rw-r--r-- | sample/drb/dlogd.rb | 7 | ||||
| -rw-r--r-- | sample/drb/dqueue.rb | 3 | ||||
| -rw-r--r-- | sample/drb/gw_cu.rb | 2 | ||||
| -rw-r--r-- | sample/drb/http0.rb | 60 | ||||
| -rw-r--r-- | sample/drb/http0serv.rb | 113 | ||||
| -rw-r--r-- | sample/drb/name.rb | 8 | ||||
| -rw-r--r-- | sample/drb/old_tuplespace.rb | 78 | ||||
| -rw-r--r-- | sample/drb/ring_echo.rb | 1 | ||||
| -rw-r--r-- | sample/drb/ring_place.rb | 6 | ||||
| -rw-r--r-- | sample/drb/simpletuple.rb | 10 |
16 files changed, 185 insertions, 178 deletions
diff --git a/sample/drb/README.rd.ja b/sample/drb/README.ja.rdoc index ec78dc70dd..1697b1b704 100644 --- a/sample/drb/README.rd.ja +++ b/sample/drb/README.ja.rdoc @@ -42,7 +42,7 @@ * rindas.rb --- TupleSpaceのclientでアプリケーションのserver * observerの使用例 - cdbiff - ((<URI:http://namazu.org/~satoru/cdbiff/>)) + cdbiff - http://namazu.org/~satoru/cdbiff/ * dbiff.rb --- dcdbiff server * dcdbiff.rb --- dcdbiff client @@ -50,7 +50,7 @@ * drbssl_s.rb * drbssl_c.rb -* DRbProtoclの追加例 +* DRbProtocolの追加例 * http0.rb * http0serv.rb diff --git a/sample/drb/README.rd b/sample/drb/README.rdoc index 5cf1f51913..fcb4182410 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,15 +39,15 @@ * 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 + +* add DRbProtocol * 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/dbiff.rb b/sample/drb/dbiff.rb index b50edc0898..290eb1d28b 100644 --- a/sample/drb/dbiff.rb +++ b/sample/drb/dbiff.rb @@ -19,18 +19,18 @@ class Biff 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 + 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/dchats.rb b/sample/drb/dchats.rb index ccb2c7c9c5..c96486a452 100644 --- a/sample/drb/dchats.rb +++ b/sample/drb/dchats.rb @@ -2,7 +2,6 @@ distributed Ruby --- chat server Copyright (c) 1999-2000 Masatoshi SEKI =end -require 'thread' require 'drb/drb' class ChatEntry @@ -29,7 +28,7 @@ end class ChatServer def initialize - @mutex = Mutex.new + @mutex = Thread::Mutex.new @members = {} end @@ -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 651b9c6c8a..13ff38940e 100644 --- a/sample/drb/dhasen.rb +++ b/sample/drb/dhasen.rb @@ -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/dlogd.rb b/sample/drb/dlogd.rb index fef7ca0f1d..a87e660346 100644 --- a/sample/drb/dlogd.rb +++ b/sample/drb/dlogd.rb @@ -4,13 +4,12 @@ =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/dqueue.rb b/sample/drb/dqueue.rb index 1c8878c080..a9afa8c858 100644 --- a/sample/drb/dqueue.rb +++ b/sample/drb/dqueue.rb @@ -3,10 +3,9 @@ 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/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/http0.rb b/sample/drb/http0.rb index d4c9f6b7fb..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 8318123fa9..2e853312e1 100644 --- a/sample/drb/http0serv.rb +++ b/sample/drb/http0serv.rb @@ -1,118 +1,119 @@ require 'webrick' require 'drb/drb' -require 'drb/http0' -require 'thread' +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) + 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(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 + 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) + 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 + @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 9527d47764..6d88186dab 100644 --- a/sample/drb/name.rb +++ b/sample/drb/name.rb @@ -35,16 +35,16 @@ How to play. | 2 =end -require 'thread.rb' require 'drb/drb' module DRbNamedObject DRbNAMEDICT = {} + DRBNAMEMUTEX = Thread::Mutex.new attr_reader(:drb_name) def drb_name=(name) @drb_name = name - Thread.exclusive do + DRBNAMEMUTEX.synchronize do raise(IndexError, name) if DRbNAMEDICT[name] DRbNAMEDICT[name] = self end @@ -75,7 +75,7 @@ class Seq def initialize(v, name) @counter = v - @mutex = Mutex.new + @mutex = Thread::Mutex.new self.drb_name = name end @@ -90,7 +90,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' diff --git a/sample/drb/old_tuplespace.rb b/sample/drb/old_tuplespace.rb index 0da9fa84c3..2d5310086e 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 @@ -33,7 +31,7 @@ class TupleSpace def initialize @que = {} @waiting = {} - @que.taint # enable tainted comunication + @que.taint # enable tainted communication @waiting.taint self.taint 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,8 +75,8 @@ 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 @@ -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 @@ -168,14 +166,14 @@ if __FILE__ == $0 Thread.start { ac = Object.new tuples = (1..10).collect { |i| - ['req', ac, i * 10 + n] + ['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 diff --git a/sample/drb/ring_echo.rb b/sample/drb/ring_echo.rb index 3b743cab8e..c54628b54c 100644 --- a/sample/drb/ring_echo.rb +++ b/sample/drb/ring_echo.rb @@ -1,7 +1,6 @@ require 'drb/drb' require 'drb/eq' require 'rinda/ring' -require 'thread' class RingEcho include DRbUndumped 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 1b9b7a35a7..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 = {} @@ -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 |
