disabling DRbUndumped

E

Eric Hodel

anyone got a slick way of temporaily disabling DRbUndumped?

Hrm, you'd need to move #_dump around to get this to work. Since
#_dump isn't defined on Object, raising TypeError or calling super
isn't going to work, and I'm not smart enough to make it work.
 
M

Mauricio Fernandez

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
 
A

ara.t.howard

Hrm, you'd need to move #_dump around to get this to work. Since #_dump
isn't defined on Object, raising TypeError or calling super isn't going to
work, and I'm not smart enough to make it work.

i've just been doing

def marshal src
klass = src.class
dst = klass.allocate
src.instance_variables.each do |ivar|
...
...
...

because the objects in question are simply state... but it's a work around.

sigh.

-a
 
A

ara.t.howard

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

<snip>

coming it at from the other direction - NICE!

boy - it might be easier to patch drb though... seems like a good feature to
have?

thanks a bunch - i'll play with this

cheers.

-a
 
P

Pit Capitain

(... mauricio's as always fine solution ...)

thanks a bunch - i'll play with this

Ara, do you need to control this on the client or on the server? Per
object or per class? There are several possible implementations based on
Mauricio's code. For example, with the following the disable/enable
methods work on a per object basis:

module DRbUndumped

@undumped_disabled = {}

def self.undumped_disabled? obj
@undumped_disabled[ DRb.to_id(obj) ]
end

def self.disable_undumped obj
@undumped_disabled[ DRb.to_id(obj) ] = true
end

def self.enable_undumped obj
@undumped_disabled.delete DRb.to_id(obj)
end

def undumped_disabled?
DRbUndumped.undumped_disabled? self
end

def disable_undumped
DRbUndumped.disable_undumped self
end

def enable_undumped
DRbUndumped.enable_undumped self
end

def kind_of? arg
(arg != DRbUndumped || !undumped_disabled?) && super
end

def respond_to? arg
(arg != :_dump || !undumped_disabled?) && super
end

end

It doesn't allow to re-enable DRbUndumped on the client side. If you
need this, the code has to be changed a little bit.

Regards,
Pit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,811
Latest member
Adisty

Latest Threads

Top