Skip to content

Commit 1e22d2c

Browse files
committed
use new LoadPlugin function
Signed-off-by: Ian Chen <[email protected]>
1 parent 278579d commit 1e22d2c

File tree

5 files changed

+64
-74
lines changed

5 files changed

+64
-74
lines changed

src/SimulationRunner.cc

+1-54
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,6 @@ SimulationRunner::SimulationRunner(const sdf::World *_world,
237237

238238
ignmsg << "Serving world SDF generation service on [" << opts.NameSpace()
239239
<< "/" << genWorldSdfService << "]" << std::endl;
240-
241-
std::string entitySystemService{"entity/system/add"};
242-
this->node->Advertise(entitySystemService,
243-
&SimulationRunner::EntitySystemAddService, this);
244-
ignmsg << "Serving entity system service on [" << opts.NameSpace()
245-
<< "/" << entitySystemService << "]" << std::endl;
246240
}
247241

248242
//////////////////////////////////////////////////
@@ -814,7 +808,7 @@ void SimulationRunner::Step(const UpdateInfo &_info)
814808
this->ProcessRecreateEntitiesRemove();
815809

816810
// handle systems that need to be added
817-
this->ProcessEntitySystems();
811+
this->systemMgr->ProcessPendingEntitySystems();
818812

819813
// Update all the systems.
820814
this->UpdateSystems();
@@ -1446,53 +1440,6 @@ bool SimulationRunner::GenerateWorldSdf(const msgs::SdfGeneratorConfig &_req,
14461440
return false;
14471441
}
14481442

1449-
//////////////////////////////////////////////////
1450-
bool SimulationRunner::EntitySystemAddService(const msgs::EntityPlugin_V &_req,
1451-
msgs::Boolean &_res)
1452-
{
1453-
std::lock_guard<std::mutex> lock(this->systemsMsgMutex);
1454-
this->systemsToAdd.push_back(_req);
1455-
_res.set_data(true);
1456-
return true;
1457-
}
1458-
1459-
//////////////////////////////////////////////////
1460-
void SimulationRunner::ProcessEntitySystems()
1461-
{
1462-
std::lock_guard<std::mutex> lock(this->systemsMsgMutex);
1463-
for (auto &req : this->systemsToAdd)
1464-
{
1465-
Entity entity = req.entity().id();
1466-
1467-
for (auto &pluginMsg : req.plugins())
1468-
{
1469-
std::string fname = pluginMsg.filename();
1470-
std::string name = pluginMsg.name();
1471-
std::string innerxml = pluginMsg.innerxml();
1472-
1473-
// Use the SDF parser to read all the inner xml.
1474-
sdf::ElementPtr pluginSDF(new sdf::Element);
1475-
sdf::initFile("plugin.sdf", pluginSDF);
1476-
std::stringstream tmp;
1477-
tmp << "<sdf version='" << SDF_VERSION << "'>";
1478-
tmp << "<plugin name='" << name << "' filename='" << fname << "'>";
1479-
tmp << innerxml;
1480-
tmp << "</plugin></sdf>";
1481-
1482-
if (sdf::readString(tmp.str(), pluginSDF))
1483-
{
1484-
this->LoadPlugin(entity, fname, name, pluginSDF);
1485-
}
1486-
else
1487-
{
1488-
ignerr << "Error reading Plugin SDF. Unable to parse Innerxml:\n"
1489-
<< innerxml << std::endl;
1490-
}
1491-
}
1492-
}
1493-
this->systemsToAdd.clear();
1494-
}
1495-
14961443

14971444
//////////////////////////////////////////////////
14981445
void SimulationRunner::SetFuelUriMap(

src/SimulationRunner.hh

-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef IGNITION_GAZEBO_SIMULATIONRUNNER_HH_
1818
#define IGNITION_GAZEBO_SIMULATIONRUNNER_HH_
1919

20-
#include <ignition/msgs/entity_plugin_v.pb.h>
2120
#include <ignition/msgs/gui.pb.h>
2221
#include <ignition/msgs/log_playback_control.pb.h>
2322
#include <ignition/msgs/sdf_generator_config.pb.h>
@@ -317,15 +316,6 @@ namespace ignition
317316
/// \return True if successful.
318317
private: bool GuiInfoService(ignition::msgs::GUI &_res);
319318

320-
/// \brief Callback for entity add system service.
321-
/// \param[in] _req Request message containing the entity id and plugins
322-
/// to add to that entity
323-
/// \param[out] _res Response containing a boolean value indicating if
324-
/// service request is received or not
325-
/// \return True if request received.
326-
private: bool EntitySystemAddService(const msgs::EntityPlugin_V &_req,
327-
msgs::Boolean &_res);
328-
329319
/// \brief Calculate real time factor and populate currentInfo.
330320
private: void UpdateCurrentInfo();
331321

@@ -387,9 +377,6 @@ namespace ignition
387377
/// See the newWorldControlState variable below.
388378
private: void ProcessNewWorldControlState();
389379

390-
/// \brief Process system messages and add systems to entities
391-
private: void ProcessEntitySystems();
392-
393380
/// \brief This is used to indicate that a stop event has been received.
394381
private: std::atomic<bool> stopReceived{false};
395382

@@ -540,12 +527,6 @@ namespace ignition
540527
/// at the appropriate time.
541528
private: std::unique_ptr<msgs::WorldControlState> newWorldControlState;
542529

543-
/// \brief A list of entity systems to add
544-
private: std::vector<msgs::EntityPlugin_V> systemsToAdd;
545-
546-
/// \brief Mutex to protect systemsToAdd list
547-
private: std::mutex systemsMsgMutex;
548-
549530
friend class LevelManager;
550531
};
551532
}

src/SystemManager.cc

+38
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ SystemManager::SystemManager(const SystemLoaderPtr &_systemLoader,
3030
entityCompMgr(_entityCompMgr),
3131
eventMgr(_eventMgr)
3232
{
33+
this->node = std::make_unique<transport::Node>();
34+
std::string entitySystemService{"/entity/system/add"};
35+
this->node->Advertise(entitySystemService,
36+
&SystemManager::EntitySystemAddService, this);
37+
ignmsg << "Serving entity system service on ["
38+
<< "/" << entitySystemService << "]" << std::endl;
3339
}
3440

3541
//////////////////////////////////////////////////
@@ -195,3 +201,35 @@ std::vector<SystemInternal> SystemManager::TotalByEntity(Entity _entity)
195201
std::back_inserter(result), checkEntity);
196202
return result;
197203
}
204+
205+
//////////////////////////////////////////////////
206+
bool SystemManager::EntitySystemAddService(const msgs::EntityPlugin_V &_req,
207+
msgs::Boolean &_res)
208+
{
209+
std::lock_guard<std::mutex> lock(this->systemsMsgMutex);
210+
this->systemsToAdd.push_back(_req);
211+
_res.set_data(true);
212+
return true;
213+
}
214+
215+
//////////////////////////////////////////////////
216+
void SystemManager::ProcessPendingEntitySystems()
217+
{
218+
std::lock_guard<std::mutex> lock(this->systemsMsgMutex);
219+
for (auto &req : this->systemsToAdd)
220+
{
221+
Entity entity = req.entity().id();
222+
223+
for (auto &pluginMsg : req.plugins())
224+
{
225+
std::string fname = pluginMsg.filename();
226+
std::string name = pluginMsg.name();
227+
std::string innerxml = pluginMsg.innerxml();
228+
sdf::Plugin pluginSDF(fname, name, innerxml);
229+
this->LoadPlugin(entity, pluginSDF);
230+
}
231+
}
232+
this->systemsToAdd.clear();
233+
}
234+
235+

src/SystemManager.hh

+24
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
#ifndef IGNITION_GAZEBO_SYSTEMMANAGER_HH_
1818
#define IGNITION_GAZEBO_SYSTEMMANAGER_HH_
1919

20+
#include <ignition/msgs/entity_plugin_v.pb.h>
21+
2022
#include <memory>
2123
#include <string>
2224
#include <vector>
2325

2426
#include <sdf/Plugin.hh>
27+
#include <ignition/transport/Node.hh>
2528

2629
#include "ignition/gazebo/config.hh"
2730
#include "ignition/gazebo/EntityComponentManager.hh"
@@ -110,6 +113,9 @@ namespace ignition
110113
/// \return Vector of systems.
111114
public: std::vector<SystemInternal> TotalByEntity(Entity _entity);
112115

116+
/// \brief Process system messages and add systems to entities
117+
public: void ProcessPendingEntitySystems();
118+
113119
/// \brief Implementation for AddSystem functions that takes an SDF
114120
/// element. This calls the AddSystemImpl that accepts an SDF Plugin.
115121
/// \param[in] _system Generic representation of a system.
@@ -125,6 +131,15 @@ namespace ignition
125131
private: void AddSystemImpl(SystemInternal _system,
126132
const sdf::Plugin &_sdf);
127133

134+
/// \brief Callback for entity add system service.
135+
/// \param[in] _req Request message containing the entity id and plugins
136+
/// to add to that entity
137+
/// \param[out] _res Response containing a boolean value indicating if
138+
/// service request is received or not
139+
/// \return True if request received.
140+
private: bool EntitySystemAddService(const msgs::EntityPlugin_V &_req,
141+
msgs::Boolean &_res);
142+
128143
/// \brief All the systems.
129144
private: std::vector<SystemInternal> systems;
130145

@@ -157,6 +172,15 @@ namespace ignition
157172

158173
/// \brief Pointer to associated event manager
159174
private: EventManager *eventMgr;
175+
176+
/// \brief A list of entity systems to add
177+
private: std::vector<msgs::EntityPlugin_V> systemsToAdd;
178+
179+
/// \brief Mutex to protect systemsToAdd list
180+
private: std::mutex systemsMsgMutex;
181+
182+
/// \brief Node for communication.
183+
private: std::unique_ptr<transport::Node> node{nullptr};
160184
};
161185
}
162186
} // namespace gazebo

test/integration/entity_system.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class EntitySystemTest : public InternalFixture<::testing::TestWithParam<int>>
128128
msgs::Boolean res;
129129
bool result;
130130
unsigned int timeout = 5000;
131-
std::string service{"/world/diff_drive/entity/system/add"};
131+
std::string service{"/entity/system/add"};
132132

133133
EXPECT_TRUE(node.Request(service, req, timeout, res, result));
134134
EXPECT_TRUE(result);

0 commit comments

Comments
 (0)