Skip to content

Commit fb8ece7

Browse files
authored
Resource env var, with transport interface (#172)
Signed-off-by: Louise Poubel <[email protected]>
1 parent 4a1e1b5 commit fb8ece7

21 files changed

+1019
-8
lines changed

include/ignition/gazebo/Server.hh

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ namespace ignition
8282
/// 1. /world/<world_name>/scene/info(none) : ignition::msgs::Scene
8383
/// + Returns the current scene information.
8484
///
85+
/// 2. /gazebo/resource_paths/get : ignition::msgs::StringMsg_V
86+
/// + Get list of resource paths.
87+
///
88+
/// 3. /gazebo/resource_paths/add : ignition::msgs::Empty
89+
/// + Add new resource paths.
90+
///
8591
/// ## Topics
8692
///
8793
/// The following are topics provided by the Server.
@@ -95,6 +101,9 @@ namespace ignition
95101
/// 2. /world/<world_name>/stats : ignition::msgs::WorldStatistics
96102
/// + This topic is throttled to 5Hz.
97103
///
104+
/// 3. /gazebo/resource_paths : ignition::msgs::StringMsg_V
105+
/// + Updated list of resource paths.
106+
///
98107
class IGNITION_GAZEBO_VISIBLE Server
99108
{
100109
/// \brief Construct the server using the parameters specified in a

include/ignition/gazebo/Util.hh

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define IGNITION_GAZEBO_UTIL_HH_
1919

2020
#include <string>
21+
#include <vector>
2122

2223
#include <ignition/math/Pose3.hh>
2324
#include "ignition/gazebo/config.hh"
@@ -66,8 +67,26 @@ namespace ignition
6667
/// paths.
6768
/// \param[in] _filePath The path to a file in disk.
6869
/// \return The full path URI.
69-
std::string asFullPath(const std::string &_uri,
70+
std::string IGNITION_GAZEBO_VISIBLE asFullPath(const std::string &_uri,
7071
const std::string &_filePath);
72+
73+
/// \brief Get resource paths based on latest environment variables.
74+
/// \return All paths in the IGN_GAZEBO_RESOURCE_PATH variable.
75+
std::vector<std::string> IGNITION_GAZEBO_VISIBLE resourcePaths();
76+
77+
/// \brief Add resource paths based on latest environment variables.
78+
/// This will update the SDF and Ignition environment variables, and
79+
/// optionally add more paths to the list.
80+
/// \param[in] _paths Optional paths to add.
81+
void IGNITION_GAZEBO_VISIBLE addResourcePaths(
82+
const std::vector<std::string> &_paths = {});
83+
84+
/// \brief Environment variable holding resource paths.
85+
const std::string kResourcePathEnv{"IGN_GAZEBO_RESOURCE_PATH"};
86+
87+
/// \brief Environment variable used by SDFormat to find URIs inside
88+
/// `<include>`
89+
const std::string kSdfPathEnv{"SDF_PATH"};
7190
}
7291
}
7392
}

src/SdfGenerator_TEST.cc

+16
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,35 @@ static bool isSubset(const sdf::ElementPtr &_elemA,
5252
const sdf::ElementPtr &_elemB)
5353
{
5454
if (_elemA->GetName() != _elemB->GetName())
55+
{
56+
igndbg << "[" << _elemA->GetName() << "] different from ["
57+
<< _elemB->GetName() << "]" << std::endl;
5558
return false;
59+
}
5660
if (_elemA->GetAttributeCount() != _elemB->GetAttributeCount())
61+
{
62+
igndbg << "[" << _elemA->GetAttributeCount() << "] different from ["
63+
<< _elemB->GetAttributeCount() << "]" << std::endl;
5764
return false;
65+
}
5866

5967
// Compare attributes
6068
for (std::size_t i = 0; i < _elemA->GetAttributeCount(); ++i)
6169
{
6270
sdf::ParamPtr attrA = _elemA->GetAttribute(i);
6371
sdf::ParamPtr attrB = _elemB->GetAttribute(attrA->GetKey());
6472
if (attrA->GetTypeName() != attrB->GetTypeName())
73+
{
74+
igndbg << "[" << attrA->GetTypeName() << "] different from ["
75+
<< attrB->GetTypeName() << "]" << std::endl;
6576
return false;
77+
}
6678
if (attrA->GetAsString() != attrB->GetAsString())
79+
{
80+
igndbg << "[" << attrA->GetAsString() << "] different from ["
81+
<< attrB->GetAsString() << "]" << std::endl;
6782
return false;
83+
}
6884
}
6985
// Compare values
7086
{

src/Server.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
#include "ignition/gazebo/Server.hh"
1817

1918
#include <ignition/common/SystemPaths.hh>
2019
#include <ignition/fuel_tools/Interface.hh>
@@ -23,6 +22,9 @@
2322
#include <sdf/Error.hh>
2423

2524
#include "ignition/gazebo/config.hh"
25+
#include "ignition/gazebo/Server.hh"
26+
#include "ignition/gazebo/Util.hh"
27+
2628
#include "ServerPrivate.hh"
2729
#include "SimulationRunner.hh"
2830

@@ -100,6 +102,8 @@ Server::Server(const ServerConfig &_config)
100102
common::addFindFileURICallback(std::bind(&ServerPrivate::FetchResourceUri,
101103
this->dataPtr.get(), std::placeholders::_1));
102104

105+
addResourcePaths();
106+
103107
sdf::Errors errors;
104108

105109
// Load a world if specified. Check SDF string first, then SDF file
@@ -120,7 +124,7 @@ Server::Server(const ServerConfig &_config)
120124
else if (!_config.SdfFile().empty())
121125
{
122126
common::SystemPaths systemPaths;
123-
systemPaths.SetFilePathEnv("IGN_GAZEBO_RESOURCE_PATH");
127+
systemPaths.SetFilePathEnv(kResourcePathEnv);
124128
systemPaths.AddFilePaths(IGN_GAZEBO_WORLD_INSTALL_DIR);
125129
std::string filePath = systemPaths.FindFile(_config.SdfFile());
126130
ignmsg << "Loading SDF world file[" << filePath << "].\n";

src/ServerPrivate.cc

+65-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#include <sdf/World.hh>
2323

2424
#include <ignition/common/Console.hh>
25+
#include <ignition/common/Util.hh>
2526

2627
#include <ignition/fuel_tools/Interface.hh>
2728

2829
#include <ignition/gui/Application.hh>
2930

31+
#include "ignition/gazebo/Util.hh"
3032
#include "SimulationRunner.hh"
3133

3234
using namespace ignition;
@@ -384,7 +386,26 @@ void ServerPrivate::CreateEntities()
384386
void ServerPrivate::SetupTransport()
385387
{
386388
// Advertise available worlds.
387-
this->node.Advertise("/gazebo/worlds", &ServerPrivate::WorldsService, this);
389+
std::string worldsService{"/gazebo/worlds"};
390+
this->node.Advertise(worldsService, &ServerPrivate::WorldsService, this);
391+
392+
ignmsg << "Serving world names on [" << worldsService << "]" << std::endl;
393+
394+
// Resource path management
395+
std::string addPathService{"/gazebo/resource_paths/add"};
396+
this->node.Advertise(addPathService,
397+
&ServerPrivate::AddResourcePathsService, this);
398+
399+
std::string getPathService{"/gazebo/resource_paths/get"};
400+
this->node.Advertise(getPathService,
401+
&ServerPrivate::ResourcePathsService, this);
402+
403+
std::string pathTopic{"/gazebo/resource_paths"};
404+
this->pathPub = this->node.Advertise<msgs::StringMsg_V>(pathTopic);
405+
406+
ignmsg << "Resource path interfaces on [" << addPathService
407+
<< "], [" << getPathService << "], and [" << pathTopic << "]."
408+
<< std::endl;
388409
}
389410

390411
//////////////////////////////////////////////////
@@ -402,6 +423,49 @@ bool ServerPrivate::WorldsService(ignition::msgs::StringMsg_V &_res)
402423
return true;
403424
}
404425

426+
//////////////////////////////////////////////////
427+
void ServerPrivate::AddResourcePathsService(
428+
const ignition::msgs::StringMsg_V &_req)
429+
{
430+
std::vector<std::string> paths;
431+
for (int i = 0; i < _req.data_size(); ++i)
432+
{
433+
paths.push_back(_req.data(i));
434+
}
435+
addResourcePaths(paths);
436+
437+
// Notify new paths
438+
msgs::StringMsg_V msg;
439+
auto gzPaths = resourcePaths();
440+
for (const auto &path : gzPaths)
441+
{
442+
if (!path.empty())
443+
msg.add_data(path);
444+
}
445+
446+
this->pathPub.Publish(msg);
447+
}
448+
449+
//////////////////////////////////////////////////
450+
bool ServerPrivate::ResourcePathsService(
451+
ignition::msgs::StringMsg_V &_res)
452+
{
453+
_res.Clear();
454+
455+
// Update paths
456+
addResourcePaths();
457+
458+
// Get paths
459+
auto gzPaths = resourcePaths();
460+
for (const auto &path : gzPaths)
461+
{
462+
if (!path.empty())
463+
_res.add_data(path);
464+
}
465+
466+
return true;
467+
}
468+
405469
//////////////////////////////////////////////////
406470
std::string ServerPrivate::FetchResource(const std::string &_uri)
407471
{

src/ServerPrivate.hh

+13
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ namespace ignition
103103
/// \return True if successful.
104104
private: bool WorldsService(ignition::msgs::StringMsg_V &_res);
105105

106+
/// \brief Callback for add resource paths service.
107+
/// \param[out] _req Request containing the paths to be added.
108+
private: void AddResourcePathsService(
109+
const ignition::msgs::StringMsg_V &_req);
110+
111+
/// \brief Callback for get resource paths service.
112+
/// \param[out] _res Response filled with all current paths.
113+
/// \return True if successful.
114+
private: bool ResourcePathsService(ignition::msgs::StringMsg_V &_res);
115+
106116
/// \brief A pool of worker threads.
107117
public: common::WorkerPool workerPool{2};
108118

@@ -148,6 +158,9 @@ namespace ignition
148158

149159
/// \brief Node for transport.
150160
private: transport::Node node;
161+
162+
/// \brief Publisher of resrouce paths.
163+
private: transport::Node::Publisher pathPub;
151164
};
152165
}
153166
}

0 commit comments

Comments
 (0)