On Sat, Feb 10, 2007 at 06:49:59AM +0900, ara.t.howard / noaa.gov wrote:
> anyone got a slick way of temporaily disabling DRbUndumped?

Strained, but works. It can be made thread-safe quite easily also.


$ cat drb_client.rb #!/usr/bin/env ruby require 'drb' require 'time_service' DRb.start_service r_obj = DRbObject.new_with_uri(ARGV.shift) p r_obj puts "-" * 40 def d(o) puts "OBJECT -> #{o.inspect}" p o.get_time end t = r_obj.time d t puts "\nDisabling undumped" t.disable_undumped t = r_obj.time d t puts "\nEnabling" r_obj.enable_undumped(TimeServer) t = r_obj.time d t
$ ruby drb_client.rb druby://localhost:50011 #<DRb::DRbObject:0xa7d9d4e0 @ref=nil, @uri="druby://localhost:50011"> ---------------------------------------- OBJECT -> #<DRb::DRbObject:0xa7d9ccfc @ref=-739674518, @uri="druby://localhost:50011"> Sat Feb 10 17:04:04 +0100 2007 Disabling undumped OBJECT -> #<TimeServer:0xa7d9bf3c> Sat Feb 10 17:04:04 +0100 2007 Enabling OBJECT -> #<DRb::DRbObject:0xa7d9b4ec @ref=-739674518, @uri="druby://localhost:50011"> Sat Feb 10 17:04:04 +0100 2007
$ cat time_service.rb require 'drb' require 'suspend_undumped' class TimeServer include DRb::DRbUndumped def get_time; return Time.now end end
$ cat suspend_undumped.rb module DRb::DRbUndumped disabled = @disabled = {} ::Kernel.module_eval do old_kind_of = instance_method(:kind_of?) define_method(:kind_of?) do |klass| #puts "OBJ is #{self.inspect} in #{caller[0]}" if $DEBUG if DRb::DRbUndumped == klass && disabled[self.class] false else old_kind_of.bind(self).call(klass) end end end define_method(:respond_to?) do |meth| if disabled[self.class] && :_dump == meth false else super end end # all these executed in the server def self.disable_undumped(klass) @disabled[klass] = true end def self.enable_undumped(klass) @disabled.delete(klass) end def disable_undumped DRb::DRbUndumped.disable_undumped(self.class) end end class Object def enable_undumped(klass) # in the server DRb::DRbUndumped.enable_undumped(klass) end end class DRb::DRbObject def enable_undumped(klass) # in the client method_missing(:enable_undumped, klass) end end
Finally, a trivial server (just so the email is self-complete):
require 'drb' require 'time_service' URI = "druby://localhost:50011" Service = Struct.new(:time, :foo) obj = Service.new(TimeServer.new, nil) DRb.start_service(URI, obj) puts DRb.uri DRb.thread.join
-- Mauricio Fernandez - http://eigenclass.org - singular Ruby ** Latest postings ** What's new in Ruby 1.9, Feb. 07 update http://eigenclass.org/hiki.rb?Changes-in-Ruby-1.9-update-6 Firebrigade: automated, sandboxed testing of RubyGems packages by other devels http://eigenclass.org/hiki.rb?firebrigade-launched