Class: Roby::Distributed::RemoteID

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/distributed/base.rb

Overview

RemoteID objects are used in dRoby to reference objects on other peers. It uses the same mechanisms that DRbObject but is not converted back into a local object automatically, and does not allow to call remote methods on a remote object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, ref) ⇒ RemoteID

Creates a new RemoteID with the given URI and ID



92
93
94
95
# File 'lib/roby/distributed/base.rb', line 92

def initialize(uri, ref)
		@uri, @ref = uri, ref.to_int
		@hash = [uri, ref].hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



108
109
110
# File 'lib/roby/distributed/base.rb', line 108

def hash
  @hash
end

#refObject (readonly)

The reference ID of the object on the DRb server



89
90
91
# File 'lib/roby/distributed/base.rb', line 89

def ref
  @ref
end

#uriObject (readonly)

The URI of the DRb server



87
88
89
# File 'lib/roby/distributed/base.rb', line 87

def uri
  @uri
end

Class Method Details

._load(str) ⇒ Object

:nodoc:



100
101
102
# File 'lib/roby/distributed/base.rb', line 100

def self._load(str) # :nodoc:
   	new(*Marshal.load(str)) 
end

.from_object(obj) ⇒ Object

Returns the RemoteID object for obj. This is actually equivalent to obj.remote_id



174
175
176
# File 'lib/roby/distributed/base.rb', line 174

def self.from_object(obj)
		Roby::Distributed::RemoteID.new(DRb.current_server.uri, DRb.to_id(obj) || 0)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

:nodoc:



104
105
106
# File 'lib/roby/distributed/base.rb', line 104

def ==(other) # :nodoc:
		other.kind_of?(RemoteID) && other.ref == ref && other.uri == uri
end

#_dump(lvl) ⇒ Object

:nodoc:



97
98
99
# File 'lib/roby/distributed/base.rb', line 97

def _dump(lvl) # :nodoc:
   	@__droby_marshalled__ ||= Marshal.dump([uri, ref]) 
end

#inspectObject



128
# File 'lib/roby/distributed/base.rb', line 128

def inspect; to_s end

#local?Boolean

True if this object references a local object

Returns:

  • (Boolean)


111
# File 'lib/roby/distributed/base.rb', line 111

def local?; DRb.here?(uri) end

#local_objectObject

If this ID references a local object, returns it. Otherwise, returns self.



113
114
115
116
117
118
119
# File 'lib/roby/distributed/base.rb', line 113

def local_object
		if DRb.here?(uri)
 DRb.to_obj(ref)
		else
 self
		end
end

#pretty_print(pp) ⇒ Object



129
# File 'lib/roby/distributed/base.rb', line 129

def pretty_print(pp); pp.text to_s end

#to_drb_objectObject

Creates a DRbObject corresponding to the object referenced by this RemoteID



179
180
181
# File 'lib/roby/distributed/base.rb', line 179

def to_drb_object
		DRbObject.new_with(uri, (ref == 0 ? nil : ref))
end

#to_local(peer, create) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/roby/distributed/base.rb', line 131

def to_local(peer, create)
		object = local_object
		if object.kind_of?(RemoteID)
 if local_proxy = peer.proxies[object]
			return peer.proxy_setup(local_proxy)
 elsif !create
			return
 elsif peer.removing_proxies.has_key?(object)
			marshalled_object = peer.removing_proxies[object].last
			Distributed.debug "reusing marshalled #{marshalled_object} for #{self} from #{peer}"
			marshalled_object.remote_siblings.delete(Distributed.droby_dump)
			marshalled_object.remote_siblings[peer.droby_dump] = self

			if marshalled_object.respond_to?(:plan) && !marshalled_object.plan
  # Take care of the "proxy is GCed while the peer
  # sends us messages about it" case. In this case,
  # the object has already been removed when it is
  # marshalled (#plan == nil).
  #
  # This cannot happen in transactions: it only happens
  # in plans where one side can remove an object while
  # the other side is doing something on it
  marshalled_object.instance_variable_set(:@plan, Roby.plan)
			end

			object = peer.local_object(marshalled_object)

			if object.respond_to?(:plan) && !object.plan
  raise "#{object} has no plan !"
			end

			return object
 end
 raise ArgumentError, "#{self} has no proxy"
		else
 object
		end
end

#to_s(peer = nil) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/roby/distributed/base.rb', line 121

def to_s(peer = nil)
		if peer
 "0x#{Object.address_from_id(ref).to_s(16)}@#{peer.name}"
		else
 "0x#{Object.address_from_id(ref).to_s(16)}@#{uri}"
		end
end