2004-02-13 21:28:02 -05:00
|
|
|
=begin
|
|
|
|
distributed Ruby --- NamedObject Sample
|
2011-05-15 07:55:52 -04:00
|
|
|
Copyright (c) 2000-2001 Masatoshi SEKI
|
2004-02-13 21:28:02 -05:00
|
|
|
=end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
How to play.
|
|
|
|
|
2011-05-15 07:55:52 -04:00
|
|
|
* start server
|
2004-02-13 21:28:02 -05:00
|
|
|
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
|
|
|
|
|
2011-05-15 07:55:52 -04:00
|
|
|
* continue client
|
2004-02-13 21:28:02 -05:00
|
|
|
Terminal 2
|
2011-05-15 07:55:52 -04:00
|
|
|
type [return]
|
2004-02-13 21:28:02 -05:00
|
|
|
| 1
|
|
|
|
| 2
|
|
|
|
=end
|
|
|
|
|
|
|
|
require 'thread.rb'
|
|
|
|
require 'drb/drb'
|
|
|
|
|
|
|
|
module DRbNamedObject
|
|
|
|
DRbNAMEDICT = {}
|
|
|
|
attr_reader(:drb_name)
|
|
|
|
|
|
|
|
def drb_name=(name)
|
|
|
|
@drb_name = name
|
2011-05-15 07:55:52 -04:00
|
|
|
Thread.exclusive do
|
2004-02-13 21:28:02 -05:00
|
|
|
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
|
2011-05-15 07:55:52 -04:00
|
|
|
return obj.drb_name
|
2004-02-13 21:28:02 -05:00
|
|
|
else
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Seq
|
|
|
|
include DRbUndumped
|
|
|
|
include DRbNamedObject
|
|
|
|
|
|
|
|
def initialize(v, name)
|
|
|
|
@counter = v
|
2016-08-30 02:22:30 -04:00
|
|
|
@mutex = Thread::Mutex.new
|
2004-02-13 21:28:02 -05:00
|
|
|
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')
|
2016-08-30 02:22:30 -04:00
|
|
|
mutex = Thread::Mutex.new
|
2004-02-13 21:28:02 -05:00
|
|
|
mutex.extend(DRbUndumped)
|
|
|
|
mutex.extend(DRbNamedObject)
|
|
|
|
mutex.drb_name = 'mutex'
|
|
|
|
@name = {}
|
|
|
|
@name['seq'] = seq
|
|
|
|
@name['mutex'] = mutex
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def [](k)
|
|
|
|
@name[k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
uri = ARGV.shift
|
|
|
|
|
|
|
|
name_conv = DRbNamedIdConv.new
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
DRb.install_id_conv(name_conv)
|
|
|
|
DRb.start_service(uri, Front.new)
|
|
|
|
puts DRb.uri
|
2004-06-27 11:36:32 -04:00
|
|
|
DRb.thread.join
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|