Class: Roby::Distributed::DRobyModel
- Defined in:
- lib/roby/distributed/protocol.rb
Overview
Dumps a model (an event, task or planner class). When unmarshalling, it tries to search for the same model. If it does not find it, it rebuilds the same hierarchy using anonymous classes, basing itself on the less abstract class known to both the remote and local sides.
Direct Known Subclasses
Defined Under Namespace
Modules: Dump
Constant Summary collapse
Instance Attribute Summary collapse
-
#ancestors ⇒ Object
readonly
The set of ancestors for the model, as a [name, remote_id] array.
Class Method Summary collapse
-
._load(str) ⇒ Object
:nodoc:.
-
.local_model(ancestors) ⇒ Object
Returns a local representation of the given model.
-
.local_to_remote ⇒ Object
A class => ID object which maps the anonymous classes we built for remote models to the remote ID of these remote models.
-
.remote_to_local ⇒ Object
A name -> class map which maps remote models to local anonymous classes Remote models are always identified by their name.
Instance Method Summary collapse
-
#==(other) ⇒ Object
True if the two objects reference the same model.
-
#_dump(lvl) ⇒ Object
:nodoc:.
-
#initialize(ancestors) ⇒ DRobyModel
constructor
Initialize a DRobyModel object with the given set of ancestors.
-
#proxy(peer) ⇒ Object
Returns a local Class object which maps the given model.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(ancestors) ⇒ DRobyModel
Initialize a DRobyModel object with the given set of ancestors
377 |
# File 'lib/roby/distributed/protocol.rb', line 377 def initialize(ancestors); @ancestors = ancestors end |
Instance Attribute Details
#ancestors ⇒ Object (readonly)
The set of ancestors for the model, as a [name, remote_id] array
374 375 376 |
# File 'lib/roby/distributed/protocol.rb', line 374 def ancestors @ancestors end |
Class Method Details
._load(str) ⇒ Object
:nodoc:
381 382 383 |
# File 'lib/roby/distributed/protocol.rb', line 381 def self._load(str) # :nodoc: DRobyModel.new(Marshal.load(str)) end |
.local_model(ancestors) ⇒ Object
Returns a local representation of the given model. If the model itself is known to us (i.e. there is a constant with the same name), it is returned. Otherwise, the model hierarchy is re-created using anonymous classes, branching the inheritance chain at a point commonly known between the local plan manager and the remote one.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/roby/distributed/protocol.rb', line 403 def self.local_model(ancestors) name, id = ancestors.shift if !id.kind_of?(Distributed::RemoteID) # this is a local task model id elsif !name.empty? && model = (constant(name) rescue nil) model elsif model = @@remote_to_local[id] model elsif !ancestors.empty? parent_model = local_model(ancestors) model = Class.new(parent_model) do singleton_class.class_eval do define_method(:remote_name) { name } define_method(:name) { "AnonModel(#{remote_name})" } end end @@remote_to_local[id] = model @@local_to_remote[model] = [name, id] model else raise ArgumentError, "cannot find a root class" end end |
.local_to_remote ⇒ Object
A class => ID object which maps the anonymous classes we built for remote models to the remote ID of these remote models
343 |
# File 'lib/roby/distributed/protocol.rb', line 343 def self.local_to_remote; @@local_to_remote end |
.remote_to_local ⇒ Object
A name -> class map which maps remote models to local anonymous classes Remote models are always identified by their name
340 |
# File 'lib/roby/distributed/protocol.rb', line 340 def self.remote_to_local; @@remote_to_local end |
Instance Method Details
#==(other) ⇒ Object
True if the two objects reference the same model
392 393 394 395 |
# File 'lib/roby/distributed/protocol.rb', line 392 def ==(other) other.kind_of?(DRobyModel) && ancestors == other.ancestors end |
#_dump(lvl) ⇒ Object
:nodoc:
378 379 380 |
# File 'lib/roby/distributed/protocol.rb', line 378 def _dump(lvl) # :nodoc: @__droby_marshalled__ ||= Marshal.dump(@ancestors) end |
#proxy(peer) ⇒ Object
Returns a local Class object which maps the given model.
See DRobyModel.local_model
387 388 389 |
# File 'lib/roby/distributed/protocol.rb', line 387 def proxy(peer) DRobyModel.local_model(ancestors.map { |name, id| [name, id.local_object] }) end |
#to_s ⇒ Object
:nodoc:
344 345 346 |
# File 'lib/roby/distributed/protocol.rb', line 344 def to_s # :nodoc: "#<dRoby:Model #{ancestors.first.first}" end |