launch_config.h
Go to the documentation of this file.
1 // Aggregates all information needed to start and monitor nodes
2 // Author: Max Schwarz <max.schwarz@uni-bonn.de>
3 
4 #ifndef ROSMON_LAUNCH_LAUNCH_CONFIG_H
5 #define ROSMON_LAUNCH_LAUNCH_CONFIG_H
6 
7 #include "node.h"
8 
9 #include <map>
10 #include <vector>
11 #include <stdexcept>
12 #include <future>
13 #include <random>
14 
15 #include <XmlRpc.h>
16 #include <tinyxml.h>
17 #include <yaml-cpp/yaml.h>
18 
19 #include <fmt/format.h>
20 
21 namespace rosmon
22 {
23 namespace launch
24 {
25 
26 class LaunchConfig;
27 
28 constexpr double DEFAULT_CPU_LIMIT = 0.9f;
29 constexpr uint64_t DEFAULT_MEMORY_LIMIT = 500*1024*1024;
30 constexpr double DEFAULT_STOP_TIMEOUT = 5.0f;
31 
32 class ParseException : public std::exception
33 {
34 public:
35  explicit ParseException(const std::string& msg)
36  : m_msg(msg)
37  {}
38 
39  virtual ~ParseException() throw()
40  {}
41 
42  virtual const char* what() const noexcept
43  { return m_msg.c_str(); }
44 
45  template<typename... Args>
46  ParseException format(const char* format, const Args& ... args)
47  {
48  return ParseException(fmt::format(format, args...));
49  }
50 private:
51  std::string m_msg;
52 };
53 
54 extern const char* UNSET_MARKER;
55 
57 {
58 public:
60  : m_config(config)
61  , m_prefix("/")
62  {}
63 
64  const std::string& prefix() const
65  { return m_prefix; }
66 
67  const std::string& filename() const
68  { return m_filename; }
69 
70  void setFilename(const std::string& filename)
71  { m_filename = filename; }
72 
73  void setCurrentElement(TiXmlElement* e)
74  {
75  // NOTE: We should not keep a reference to the TiXmlElement here,
76  // since the ParseContext might be around longer than the DOM tree.
77  // See evaluateParameters().
78  if(e)
79  m_currentLine = e->Row();
80  else
81  m_currentLine = -1;
82  }
83 
84  ParseContext enterScope(const std::string& prefix);
85  void parseScopeAttributes(TiXmlElement* e, ParseContext& attr_ctx);
86 
87  std::string evaluate(const std::string& tpl, bool simplifyWhitespace = true);
88 
89  bool parseBool(const std::string& value, int line);
90 
92  {
93  m_args.clear();
94  }
95 
96  inline const std::map<std::string, std::string>& arguments() const
97  { return m_args; }
98 
99  void setArg(const std::string& name, const std::string& value, bool override);
100 
101  void setEnvironment(const std::string& name, const std::string& value);
102 
103  inline const std::map<std::string, std::string> environment() const
104  { return m_environment; }
105 
106  bool shouldSkip(TiXmlElement* e);
107 
109  { return m_config; }
110 
111  void setRemap(const std::string& from, const std::string& to);
112  const std::map<std::string, std::string>& remappings()
113  { return m_remappings; }
114 
115  std::string anonName(const std::string& base);
116 
117  template<typename... Args>
118  ParseException error(const char* fmt, const Args& ... args) const
119  {
120  std::string msg = fmt::format(fmt, args...);
121 
122  if(m_currentLine >= 0)
123  {
124  return ParseException(fmt::format("{}:{}: {}",
126  ));
127  }
128  else
129  {
130  return ParseException(fmt::format("{}: {}", m_filename, msg));
131  }
132  }
133 
134  template<typename... Args>
135  void warning(const char* fmt, const Args& ... args) const;
136 
137 
138  double cpuLimit() const
139  { return m_cpuLimit; }
140  void setCPULimit(double limit)
141  { m_cpuLimit = limit; }
142 
143  uint64_t memoryLimit() const
144  { return m_memoryLimit; }
145  void setMemoryLimit(uint64_t limit)
146  { m_memoryLimit = limit; }
147 
148  double stopTimeout() const
149  { return m_stopTimeout; }
150  void setStopTimeout(double timeout)
151  { m_stopTimeout = timeout; }
152 
153  bool coredumpsEnabled() const
154  { return m_coredumpsEnabled; }
155  void setCoredumpsEnabled(bool enabled)
156  { m_coredumpsEnabled = enabled; }
157 
158 private:
160 
161  std::string m_prefix;
162  std::string m_filename;
163  int m_currentLine = -1;
164  std::map<std::string, std::string> m_args;
165  std::map<std::string, std::string> m_environment;
166  std::map<std::string, std::string> m_remappings;
167  std::map<std::string, std::string> m_anonNames;
168 
172  bool m_coredumpsEnabled = true;
173 };
174 
176 {
177 public:
178  typedef std::shared_ptr<LaunchConfig> Ptr;
179  typedef std::shared_ptr<const LaunchConfig> ConstPtr;
180 
181  LaunchConfig();
182 
183  enum class OutputAttr
184  {
185  Obey,
186  Ignore
187  };
188 
189  void setWarningOutput(std::ostream* warningStream);
190  std::ostream& warningOutput()
191  { return *m_warningOutput; }
192 
193  void setArgument(const std::string& name, const std::string& value);
194 
195  void setDefaultStopTimeout(double timeout);
196  void setDefaultCPULimit(double CPULimit);
197  void setDefaultMemoryLimit(uint64_t memoryLimit);
198 
199  void setOutputAttrMode(OutputAttr mode);
200 
201  void setNodeLogDir(const std::string& logDir);
202  std::string nodeLogDir() const
203  { return m_nodeLogDir; }
204 
205  void parse(const std::string& filename, bool onlyArguments = false);
206  void parseString(const std::string& input, bool onlyArguments = false);
207 
208  void applyAutoIncrementSpawnDelayToAll(const ros::WallDuration& autoIncrementSpawnDelay);
209 
210  void evaluateParameters();
211 
212  inline const std::map<std::string, XmlRpc::XmlRpcValue>& parameters() const
213  { return m_params; }
214 
215  inline const std::vector<Node::Ptr>& nodes() const
216  { return m_nodes; }
217 
218  inline const std::map<std::string, std::string>& arguments() const
219  { return m_rootContext.arguments(); }
220 
221  std::string anonName(const std::string& base);
222 
223  std::string rosmonNodeName() const
224  { return m_rosmonNodeName; }
225 
226  std::string windowTitle() const
227  { return m_windowTitle; }
228 
229  std::string generateAnonHash();
230 
231  bool disableUI() const
232  { return m_disableUI; }
233 private:
235  {
238  };
239 
240  void parseTopLevelAttributes(TiXmlElement* element);
241 
242  void parse(TiXmlElement* element, ParseContext* ctx, bool onlyArguments = false);
243  void parseNode(TiXmlElement* element, ParseContext& ctx);
244  void parseParam(TiXmlElement* element, ParseContext& ctx, ParamContext paramContext = PARAM_GENERAL);
245  void parseROSParam(TiXmlElement* element, ParseContext& ctx);
246  void parseInclude(TiXmlElement* element, ParseContext& ctx);
247  void parseArgument(TiXmlElement* element, ParseContext& ctx);
248  void parseEnv(TiXmlElement* element, ParseContext& ctx);
249  void parseRemap(TiXmlElement* element, ParseContext& ctx);
250 
251  void loadYAMLParams(const ParseContext& ctx, const YAML::Node& n, const std::string& prefix);
252 
253  XmlRpc::XmlRpcValue paramToXmlRpc(const ParseContext& ctx, const std::string& value, const std::string& type = "");
254 
256 
257  std::vector<Node::Ptr> m_nodes;
258 
259  using ParameterList = std::map<std::string, XmlRpc::XmlRpcValue>;
260  using ParameterFuture = std::future<XmlRpc::XmlRpcValue>;
261 
262  struct YAMLResult
263  {
264  std::string name;
265  YAML::Node yaml;
266  };
267 
269  std::map<std::string, ParameterFuture> m_paramJobs;
270  std::vector<std::future<YAMLResult>> m_yamlParamJobs;
271 
272  std::mt19937_64 m_anonGen;
273 
274  std::string m_rosmonNodeName;
275 
276  std::string m_windowTitle;
277 
279 
280  bool m_disableUI = false;
281 
282  std::ostream* m_warningOutput = &std::cerr;
283 
284  std::string m_nodeLogDir;
285 };
286 
287 template<typename... Args>
288 void ParseContext::warning(const char* fmt, const Args& ... args) const
289 {
290  std::string msg = fmt::format(fmt, args...);
291 
292  if(m_currentLine >= 0)
293  {
294  m_config->warningOutput() << fmt::format(
295  "{}:{}: Warning: {}\n", m_filename, m_currentLine, msg
296  );
297  }
298  else
299  {
300  m_config->warningOutput() << fmt::format(
301  "{}: Warning: {}\n", m_filename, msg
302  );
303  }
304 }
305 
306 }
307 }
308 
309 #endif
310 
rosmon::launch::ParseContext::memoryLimit
uint64_t memoryLimit() const
Definition: launch_config.h:143
rosmon::launch::ParseException
Definition: launch_config.h:32
rosmon::launch::ParseContext
Definition: launch_config.h:56
rosmon::launch::ParseContext::m_anonNames
std::map< std::string, std::string > m_anonNames
Definition: launch_config.h:167
rosmon::launch::LaunchConfig::OutputAttr::Ignore
@ Ignore
rosmon::launch::ParseContext::shouldSkip
bool shouldSkip(TiXmlElement *e)
Definition: launch_config.cpp:137
rosmon::launch::LaunchConfig::parseParam
void parseParam(TiXmlElement *element, ParseContext &ctx, ParamContext paramContext=PARAM_GENERAL)
Definition: launch_config.cpp:572
rosmon::launch::LaunchConfig::PARAM_GENERAL
@ PARAM_GENERAL
tag inside <node>
Definition: launch_config.h:236
rosmon::launch::ParseContext::ParseContext
ParseContext(LaunchConfig *config)
Definition: launch_config.h:59
rosmon::launch::LaunchConfig::parseTopLevelAttributes
void parseTopLevelAttributes(TiXmlElement *element)
Definition: launch_config.cpp:288
rosmon
Definition: diagnostics_publisher.cpp:34
rosmon::launch::LaunchConfig::parseRemap
void parseRemap(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:1158
rosmon::launch::LaunchConfig::PARAM_IN_NODE
@ PARAM_IN_NODE
tag everywhere else
Definition: launch_config.h:237
rosmon::launch::LaunchConfig::m_yamlParamJobs
std::vector< std::future< YAMLResult > > m_yamlParamJobs
Definition: launch_config.h:270
rosmon::launch::LaunchConfig::parseString
void parseString(const std::string &input, bool onlyArguments=false)
Definition: launch_config.cpp:249
rosmon::launch::ParseContext::anonName
std::string anonName(const std::string &base)
Definition: launch_config.cpp:179
rosmon::launch::LaunchConfig::setDefaultMemoryLimit
void setDefaultMemoryLimit(uint64_t memoryLimit)
Definition: launch_config.cpp:221
rosmon::launch::ParseContext::setEnvironment
void setEnvironment(const std::string &name, const std::string &value)
Definition: launch_config.cpp:169
rosmon::launch::LaunchConfig::m_windowTitle
std::string m_windowTitle
Definition: launch_config.h:276
rosmon::launch::LaunchConfig::parseROSParam
void parseROSParam(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:905
rosmon::launch::ParseContext::setFilename
void setFilename(const std::string &filename)
Definition: launch_config.h:70
rosmon::launch::ParseContext::setArg
void setArg(const std::string &name, const std::string &value, bool override)
Definition: launch_config.cpp:160
rosmon::launch::LaunchConfig::setArgument
void setArgument(const std::string &name, const std::string &value)
Definition: launch_config.cpp:206
rosmon::launch::ParseContext::enterScope
ParseContext enterScope(const std::string &prefix)
Definition: launch_config.cpp:34
rosmon::launch::LaunchConfig::nodes
const std::vector< Node::Ptr > & nodes() const
Definition: launch_config.h:215
rosmon::launch::LaunchConfig::applyAutoIncrementSpawnDelayToAll
void applyAutoIncrementSpawnDelayToAll(const ros::WallDuration &autoIncrementSpawnDelay)
Definition: launch_config.cpp:274
rosmon::launch::LaunchConfig::OutputAttr::Obey
@ Obey
rosmon::launch::LaunchConfig::m_nodes
std::vector< Node::Ptr > m_nodes
Definition: launch_config.h:257
rosmon::launch::ParseContext::m_filename
std::string m_filename
Definition: launch_config.h:162
rosmon::launch::ParseContext::parseScopeAttributes
void parseScopeAttributes(TiXmlElement *e, ParseContext &attr_ctx)
Definition: launch_config.cpp:44
rosmon::launch::ParseException::~ParseException
virtual ~ParseException()
Definition: launch_config.h:39
rosmon::launch::ParseContext::m_cpuLimit
double m_cpuLimit
Definition: launch_config.h:169
rosmon::launch::LaunchConfig
Definition: launch_config.h:175
rosmon::launch::ParseContext::error
ParseException error(const char *fmt, const Args &... args) const
Definition: launch_config.h:118
rosmon::launch::ParseContext::m_coredumpsEnabled
bool m_coredumpsEnabled
Definition: launch_config.h:172
rosmon::launch::LaunchConfig::m_rosmonNodeName
std::string m_rosmonNodeName
Definition: launch_config.h:274
rosmon::launch::ParseContext::config
LaunchConfig * config()
Definition: launch_config.h:108
rosmon::launch::LaunchConfig::m_anonGen
std::mt19937_64 m_anonGen
Definition: launch_config.h:272
rosmon::launch::ParseException::what
virtual const char * what() const noexcept
Definition: launch_config.h:42
rosmon::launch::LaunchConfig::YAMLResult::yaml
YAML::Node yaml
Definition: launch_config.h:265
rosmon::launch::ParseContext::filename
const std::string & filename() const
Definition: launch_config.h:67
rosmon::launch::ParseContext::m_prefix
std::string m_prefix
Definition: launch_config.h:161
rosmon::launch::LaunchConfig::m_outputAttrMode
OutputAttr m_outputAttrMode
Definition: launch_config.h:278
rosmon::launch::ParseContext::m_environment
std::map< std::string, std::string > m_environment
Definition: launch_config.h:165
rosmon::launch::LaunchConfig::setNodeLogDir
void setNodeLogDir(const std::string &logDir)
Definition: launch_config.cpp:1268
rosmon::launch::LaunchConfig::parseNode
void parseNode(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:364
rosmon::launch::LaunchConfig::OutputAttr
OutputAttr
Definition: launch_config.h:183
rosmon::launch::LaunchConfig::setDefaultStopTimeout
void setDefaultStopTimeout(double timeout)
Definition: launch_config.cpp:211
rosmon::launch::LaunchConfig::m_warningOutput
std::ostream * m_warningOutput
Definition: launch_config.h:282
rosmon::launch::ParseContext::m_config
LaunchConfig * m_config
Definition: launch_config.h:159
rosmon::launch::LaunchConfig::paramToXmlRpc
XmlRpc::XmlRpcValue paramToXmlRpc(const ParseContext &ctx, const std::string &value, const std::string &type="")
Definition: launch_config.cpp:865
rosmon::launch::ParseContext::m_args
std::map< std::string, std::string > m_args
Definition: launch_config.h:164
rosmon::launch::LaunchConfig::setOutputAttrMode
void setOutputAttrMode(OutputAttr mode)
Definition: launch_config.cpp:1258
rosmon::launch::LaunchConfig::parse
void parse(const std::string &filename, bool onlyArguments=false)
Definition: launch_config.cpp:226
rosmon::launch::LaunchConfig::nodeLogDir
std::string nodeLogDir() const
Definition: launch_config.h:202
rosmon::launch::LaunchConfig::m_nodeLogDir
std::string m_nodeLogDir
Definition: launch_config.h:284
rosmon::launch::ParseContext::clearArguments
void clearArguments()
Definition: launch_config.h:91
rosmon::launch::LaunchConfig::ConstPtr
std::shared_ptr< const LaunchConfig > ConstPtr
Definition: launch_config.h:179
rosmon::launch::LaunchConfig::loadYAMLParams
void loadYAMLParams(const ParseContext &ctx, const YAML::Node &n, const std::string &prefix)
Definition: launch_config.cpp:986
rosmon::launch::LaunchConfig::disableUI
bool disableUI() const
Definition: launch_config.h:231
node.h
rosmon::launch::ParseException::format
ParseException format(const char *format, const Args &... args)
Definition: launch_config.h:46
rosmon::launch::ParseContext::arguments
const std::map< std::string, std::string > & arguments() const
Definition: launch_config.h:96
rosmon::launch::ParseContext::setCPULimit
void setCPULimit(double limit)
Definition: launch_config.h:140
rosmon::launch::LaunchConfig::m_disableUI
bool m_disableUI
Definition: launch_config.h:280
rosmon::launch::LaunchConfig::m_rootContext
ParseContext m_rootContext
Definition: launch_config.h:255
rosmon::launch::ParseContext::coredumpsEnabled
bool coredumpsEnabled() const
Definition: launch_config.h:153
XmlRpc.h
rosmon::launch::UNSET_MARKER
const char * UNSET_MARKER
Definition: launch_config.cpp:32
rosmon::launch::string_utils::simplifyWhitespace
std::string simplifyWhitespace(const std::string &input)
Compress any sequence of whitespace to single spaces.
Definition: string_utils.cpp:13
rosmon::launch::LaunchConfig::m_paramJobs
std::map< std::string, ParameterFuture > m_paramJobs
Definition: launch_config.h:269
rosmon::launch::ParseException::ParseException
ParseException(const std::string &msg)
Definition: launch_config.h:35
rosmon::launch::LaunchConfig::parseInclude
void parseInclude(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:1038
rosmon::launch::LaunchConfig::windowTitle
std::string windowTitle() const
Definition: launch_config.h:226
rosmon::launch::ParseContext::stopTimeout
double stopTimeout() const
Definition: launch_config.h:148
rosmon::launch::ParseContext::evaluate
std::string evaluate(const std::string &tpl, bool simplifyWhitespace=true)
Definition: launch_config.cpp:100
rosmon::launch::ParseContext::m_currentLine
int m_currentLine
Definition: launch_config.h:163
rosmon::launch::LaunchConfig::ParameterFuture
std::future< XmlRpc::XmlRpcValue > ParameterFuture
Definition: launch_config.h:260
rosmon::launch::ParseException::m_msg
std::string m_msg
Definition: launch_config.h:51
rosmon::launch::ParseContext::prefix
const std::string & prefix() const
Definition: launch_config.h:64
rosmon::launch::LaunchConfig::YAMLResult::name
std::string name
Definition: launch_config.h:264
rosmon::launch::LaunchConfig::ParameterList
std::map< std::string, XmlRpc::XmlRpcValue > ParameterList
Definition: launch_config.h:259
rosmon::launch::LaunchConfig::ParamContext
ParamContext
Definition: launch_config.h:234
rosmon::launch::LaunchConfig::setDefaultCPULimit
void setDefaultCPULimit(double CPULimit)
Definition: launch_config.cpp:216
rosmon::launch::ParseContext::m_stopTimeout
double m_stopTimeout
Definition: launch_config.h:171
rosmon::launch::ParseContext::setCurrentElement
void setCurrentElement(TiXmlElement *e)
Definition: launch_config.h:73
rosmon::launch::DEFAULT_CPU_LIMIT
constexpr double DEFAULT_CPU_LIMIT
Definition: launch_config.h:28
rosmon::launch::LaunchConfig::generateAnonHash
std::string generateAnonHash()
Definition: launch_config.cpp:1169
rosmon::launch::ParseContext::setMemoryLimit
void setMemoryLimit(uint64_t limit)
Definition: launch_config.h:145
rosmon::launch::ParseContext::cpuLimit
double cpuLimit() const
Definition: launch_config.h:138
rosmon::launch::ParseContext::remappings
const std::map< std::string, std::string > & remappings()
Definition: launch_config.h:112
rosmon::launch::ParseContext::setStopTimeout
void setStopTimeout(double timeout)
Definition: launch_config.h:150
rosmon::launch::DEFAULT_STOP_TIMEOUT
constexpr double DEFAULT_STOP_TIMEOUT
Definition: launch_config.h:30
rosmon::launch::LaunchConfig::YAMLResult
Definition: launch_config.h:262
rosmon::launch::ParseContext::environment
const std::map< std::string, std::string > environment() const
Definition: launch_config.h:103
rosmon::launch::ParseContext::setRemap
void setRemap(const std::string &from, const std::string &to)
Definition: launch_config.cpp:174
rosmon::launch::ParseContext::m_remappings
std::map< std::string, std::string > m_remappings
Definition: launch_config.h:166
rosmon::launch::LaunchConfig::warningOutput
std::ostream & warningOutput()
Definition: launch_config.h:190
rosmon::launch::ParseContext::warning
void warning(const char *fmt, const Args &... args) const
Definition: launch_config.h:288
rosmon::launch::LaunchConfig::evaluateParameters
void evaluateParameters()
Definition: launch_config.cpp:1185
ros::WallDuration
rosmon::launch::LaunchConfig::setWarningOutput
void setWarningOutput(std::ostream *warningStream)
Definition: launch_config.cpp:1263
rosmon::launch::LaunchConfig::Ptr
std::shared_ptr< LaunchConfig > Ptr
Definition: launch_config.h:178
rosmon::launch::LaunchConfig::m_params
ParameterList m_params
Definition: launch_config.h:268
rosmon::launch::LaunchConfig::parameters
const std::map< std::string, XmlRpc::XmlRpcValue > & parameters() const
Definition: launch_config.h:212
rosmon::launch::ParseContext::m_memoryLimit
uint64_t m_memoryLimit
Definition: launch_config.h:170
rosmon::launch::LaunchConfig::anonName
std::string anonName(const std::string &base)
rosmon::launch::ParseContext::parseBool
bool parseBool(const std::string &value, int line)
Definition: launch_config.cpp:118
rosmon::launch::LaunchConfig::parseArgument
void parseArgument(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:1122
rosmon::launch::LaunchConfig::arguments
const std::map< std::string, std::string > & arguments() const
Definition: launch_config.h:218
rosmon::launch::ParseContext::setCoredumpsEnabled
void setCoredumpsEnabled(bool enabled)
Definition: launch_config.h:155
rosmon::launch::LaunchConfig::parseEnv
void parseEnv(TiXmlElement *element, ParseContext &ctx)
Definition: launch_config.cpp:1147
rosmon::launch::LaunchConfig::rosmonNodeName
std::string rosmonNodeName() const
Definition: launch_config.h:223
XmlRpc::XmlRpcValue
rosmon::launch::DEFAULT_MEMORY_LIMIT
constexpr uint64_t DEFAULT_MEMORY_LIMIT
Definition: launch_config.h:29
rosmon::launch::LaunchConfig::LaunchConfig
LaunchConfig()
Definition: launch_config.cpp:193


rosmon_core
Author(s): Max Schwarz
autogenerated on Wed Feb 21 2024 04:01:14