[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

disabling DRbUndumped

Ara.T.Howard

2/9/2007 9:50:00 PM

6 Answers

Pit Capitain

2/10/2007 8:49:00 AM

0

ara.t.howard@noaa.gov schrieb:
>
> anyone got a slick way of temporaily disabling DRbUndumped?

Ara, can you tell us a bit more, probably via a testcase?

Regards,
Pit

Eric Hodel

2/10/2007 10:43:00 AM

0

On Feb 9, 2007, at 13:49, ara.t.howard@noaa.gov wrote:

> 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.

Mauricio Fernández

2/10/2007 4:13:00 PM

0

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://eige... - singular Ruby
** Latest postings **
What's new in Ruby 1.9, Feb. 07 update
http://eige.../hiki.rb?Changes-in-Ruby-1.9-update-6
Firebrigade: automated, sandboxed testing of RubyGems packages by other devels
http://eige.../hiki.rb?firebrigade-launched

Ara.T.Howard

2/10/2007 5:55:00 PM

0

Ara.T.Howard

2/10/2007 5:59:00 PM

0

Pit Capitain

2/10/2007 10:34:00 PM

0

ara.t.howard@noaa.gov schrieb:
> (... 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