2727#include < gz/common/Profiler.hh>
2828
2929#include " gz/sim/components/ParentEntity.hh"
30+ #include " gz/sim/components/PerformerAffinity.hh"
3031#include " gz/sim/Conversions.hh"
3132#include " gz/sim/Entity.hh"
3233#include " gz/sim/EntityComponentManager.hh"
@@ -95,6 +96,10 @@ bool NetworkManagerSecondary::OnControl(const private_msgs::PeerControl &_req,
9596{
9697 this ->enableSim = _req.enable_sim ();
9798 _resp.set_enable_sim (this ->enableSim );
99+
100+ gzdbg << " Received msg PeerControl: enable_sim: "
101+ << this ->enableSim << std::endl;
102+
98103 return true ;
99104}
100105
@@ -104,74 +109,130 @@ void NetworkManagerSecondary::OnStep(
104109{
105110 GZ_PROFILE (" NetworkManagerSecondary::OnStep" );
106111
112+ // Do not print debug message if paused.
113+ if (!_msg.stats ().paused ())
114+ {
115+ gzdbg << " NetworkManagerSecondary::OnStep [" << this ->Namespace () << " ]"
116+ << std::endl;
117+ if (_msg.affinity_size () > 0 )
118+ {
119+ gzdbg << " Secondary: SimulationStep:\n "
120+ << _msg.DebugString () << std::endl;
121+ }
122+ }
123+
107124 // Throttle the number of step messages going to the debug output.
108125 if (!_msg.stats ().paused () && _msg.stats ().iterations () % 1000 == 0 )
109126 {
110127 gzdbg << " Network iterations: " << _msg.stats ().iterations ()
111- << std::endl;
128+ << std::endl;
112129 }
113130
114131 // Update affinities
115132 for (int i = 0 ; i < _msg.affinity_size (); ++i)
116133 {
117134 const auto &affinityMsg = _msg.affinity (i);
118- const auto &entityId = affinityMsg.entity ().id ();
135+ const auto &perfEntity = affinityMsg.entity ().id ();
119136
120137 if (affinityMsg.secondary_prefix () == this ->Namespace ())
121138 {
122- this ->performers .insert (entityId);
139+ this ->performers .insert (perfEntity);
140+
141+ // Set performer affinity to this secondary
142+ this ->dataPtr ->ecm ->RemoveComponent <components::PerformerAffinity>(
143+ perfEntity);
144+ auto newAffinity = components::PerformerAffinity (this ->Namespace ());
145+ this ->dataPtr ->ecm ->CreateComponent (perfEntity, newAffinity);
123146
124147 gzmsg << " Secondary [" << this ->Namespace ()
125- << " ] assigned affinity to performer [" << entityId << " ]."
148+ << " ] assigned affinity to performer ["
149+ << perfEntity << " ]."
126150 << std::endl;
127151 }
128- // If performer has been assigned to another secondary, remove it
129152 else
130153 {
131- auto parent =
132- this ->dataPtr ->ecm ->Component <components::ParentEntity>(entityId);
133- if (parent != nullptr )
134- {
135- this ->dataPtr ->ecm ->RequestRemoveEntity (parent->Data ());
136- }
137-
138- if (this ->performers .find (entityId) != this ->performers .end ())
154+ // If performer assigned to this secondary is also assigned to another
155+ // secondary, remove it from performers and the parent model from the ECM.
156+ if (this ->performers .find (perfEntity) != this ->performers .end ())
139157 {
140158 gzmsg << " Secondary [" << this ->Namespace ()
141- << " ] unassigned affinity to performer [" << entityId << " ]."
159+ << " ] unassigned affinity to performer ["
160+ << perfEntity << " ]."
142161 << std::endl;
143- this ->performers .erase (entityId);
162+ this ->performers .erase (perfEntity);
163+
164+ // Remove performer affinity from this this secondary
165+ this ->dataPtr ->ecm ->RemoveComponent <components::PerformerAffinity>(
166+ perfEntity);
167+
168+ auto parent =
169+ this ->dataPtr ->ecm ->Component <components::ParentEntity>(perfEntity);
170+ if (parent != nullptr )
171+ {
172+ this ->dataPtr ->ecm ->RequestRemoveEntity (parent->Data ());
173+ gzmsg << " Secondary [" << this ->Namespace ()
174+ << " ] request remove model entity ["
175+ << parent->Data () << " ]."
176+ << std::endl;
177+ }
178+ else
179+ {
180+ gzerr << " Secondary [" << this ->Namespace ()
181+ << " ] should remove parent for performer ["
182+ << perfEntity << " ]."
183+ << std::endl;
184+ }
144185 }
145186 }
146187 }
147188
148189 // Update info
149190 auto info = convert<UpdateInfo>(_msg.stats ());
150191
192+ // ! @todo must set the secondaryNamespace here...
193+ info.secondaryNamespace = this ->Namespace ();
194+
151195 // Step runner
152196 this ->dataPtr ->stepFunction (info);
153197
154198 // Update state with all the performer's entities
155199 std::unordered_set<Entity> entities;
156- for (const auto &perf : this ->performers )
200+ if (!_msg.stats ().paused ())
201+ {
202+ gzdbg << " Secondary: performer: size: "
203+ << this ->performers .size () << std::endl;
204+ }
205+ for (const auto &perfEntity : this ->performers )
157206 {
158207 // Performer model
159- auto parent = this ->dataPtr ->ecm ->Component <components::ParentEntity>(perf);
208+ auto parent =
209+ this ->dataPtr ->ecm ->Component <components::ParentEntity>(perfEntity);
160210 if (parent == nullptr )
161211 {
162- gzerr << " Failed to get parent for performer [" << perf << " ]"
163- << std::endl;
212+ gzerr << " Secondary: failed to get parent for performer ["
213+ << perfEntity << " ]"
214+ << std::endl;
164215 continue ;
165216 }
166- auto modelEntity = parent->Data ();
167217
218+ // ! @todo why was the performers model entity not also included?
219+ auto modelEntity = parent->Data ();
220+ entities.insert (modelEntity);
221+ if (!_msg.stats ().paused ())
222+ {
223+ gzdbg << " Secondary: performer [" << perfEntity << " ], "
224+ << " parent: [" << modelEntity << " ]"
225+ << std:: endl;
226+ }
168227 auto children = this ->dataPtr ->ecm ->Descendants (modelEntity);
169228 entities.insert (children.begin (), children.end ());
170229 }
171230
172231 msgs::SerializedStateMap stateMsg;
173232 if (!entities.empty ())
233+ {
174234 this ->dataPtr ->ecm ->State (stateMsg, entities);
235+ }
175236 stateMsg.set_has_one_time_component_changes (
176237 this ->dataPtr ->ecm ->HasOneTimeComponentChanges ());
177238
0 commit comments