Class: DRb::DRbObject

Inherits:
Object show all
Defined in:
lib/drb/drb.rb,
lib/drb/eq.rb,
lib/drb/gw.rb

Overview

Object wrapping a reference to a remote drb object.

Method calls on this object are relayed to the remote object that this object is a stub for.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DRbObject) initialize(obj, uri = nil)

Create a new remote object stub.

obj is the (local) object we want to create a stub for. Normally this is nil. uri is the URI of the remote object that this will be a stub for.



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/drb/drb.rb', line 1041

def initialize(obj, uri=nil)
  @uri = nil
  @ref = nil
  if obj.nil?
	return if uri.nil?
	@uri, option = DRbProtocol.uri_option(uri, DRb.config)
	@ref = DRbURIOption.new(option) unless option.nil?
  else
	@uri = uri ? uri : (DRb.uri rescue nil)
	@ref = obj ? DRb.to_id(obj) : nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(msg_id, *a, &b)

Routes method calls to the referenced object.



1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/drb/drb.rb', line 1079

def method_missing(msg_id, *a, &b)
  if DRb.here?(@uri)
	obj = DRb.to_obj(@ref)
	DRb.current_server.check_insecure_method(obj, msg_id)
	return obj.__send__(msg_id, *a, &b)
  end

  succ, result = self.class.with_friend(@uri) do
    DRbConn.open(@uri) do |conn|
      conn.send_message(self, msg_id, a, b)
    end
  end

  if succ
    return result
  elsif DRbUnknown === result
    raise result
  else
    bt = self.class.prepare_backtrace(@uri, result)
	result.set_backtrace(bt + caller)
    raise result
  end
end

Class Method Details

+ (Object) _load(s)

Unmarshall a marshalled DRbObject.

If the referenced object is located within the local server, then the object itself is returned. Otherwise, a new DRbObject is created to act as a stub for the remote referenced object.



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/drb/drb.rb', line 1003

def self._load(s)
  uri, ref = Marshal.load(s)

  if DRb.here?(uri)
	obj = DRb.to_obj(ref)
    if ((! obj.tainted?) && Thread.current[:drb_untaint])
      Thread.current[:drb_untaint].push(obj)
    end
    return obj
  end

  self.new_with(uri, ref)
end

+ (Object) new_with(uri, ref)



1017
1018
1019
1020
1021
1022
# File 'lib/drb/drb.rb', line 1017

def self.new_with(uri, ref)
  it = self.allocate
  it.instance_variable_set('@uri', uri)
  it.instance_variable_set('@ref', ref)
  it
end

+ (Object) new_with_uri(uri)

Create a new DRbObject from a URI alone.



1025
1026
1027
# File 'lib/drb/drb.rb', line 1025

def self.new_with_uri(uri)
  self.new(nil, uri)
end

+ (Object) prepare_backtrace(uri, result)



1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/drb/drb.rb', line 1114

def self.prepare_backtrace(uri, result)
  prefix = "(#{uri}) "
  bt = []
  result.backtrace.each do |x|
    break if /`__send__'$/ =~ x
    if /^\(druby:\/\// =~ x
      bt.push(x)
    else
      bt.push(prefix + x)
    end
  end
  bt
end

+ (Object) with_friend(uri)



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
# File 'lib/drb/drb.rb', line 1103

def self.with_friend(uri)
  friend = DRb.fetch_server(uri)
  return yield() unless friend

  save = Thread.current['DRb']
  Thread.current['DRb'] = { 'server' => friend }
  return yield
ensure
  Thread.current['DRb'] = save if friend
end

Instance Method Details

- (Object) ==(other) Also known as: eql?



3
4
5
6
# File 'lib/drb/eq.rb', line 3

def ==(other)
  return false unless DRbObject === other
 (@ref == other.__drbref) && (@uri == other.__drburi)
end

- (Object) __drbref

Get the reference of the object, if local.



1060
1061
1062
# File 'lib/drb/drb.rb', line 1060

def __drbref
  @ref
end

- (Object) __drburi

Get the URI of the remote object.



1055
1056
1057
# File 'lib/drb/drb.rb', line 1055

def __drburi
  @uri
end

- (Object) _dump(lv)

Marshall this object.

The URI and ref of the object are marshalled.



1032
1033
1034
# File 'lib/drb/drb.rb', line 1032

def _dump(lv)
  Marshal.dump([@uri, @ref])
end

- (Object) hash



8
9
10
# File 'lib/drb/eq.rb', line 8

def hash
  [@uri, @ref].hash
end

- (Object) pretty_print(q)

:nodoc:



1128
1129
1130
# File 'lib/drb/drb.rb', line 1128

def pretty_print(q)   # :nodoc:
  q.pp_object(self)
end

- (Object) pretty_print_cycle(q)

:nodoc:



1132
1133
1134
1135
1136
1137
# File 'lib/drb/drb.rb', line 1132

def pretty_print_cycle(q)   # :nodoc:
  q.object_address_group(self) {
    q.breakable
    q.text '...'
  }
end

- (Boolean) respond_to?(msg_id, priv = false)

Returns:

  • (Boolean)


1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/drb/drb.rb', line 1067

def respond_to?(msg_id, priv=false)
  case msg_id
  when :_dump
    true
  when :marshal_dump
    false
  else
    method_missing(:respond_to?, msg_id, priv)
  end
end