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 class ParseException : public std::exception
29 {
30 public:
31  explicit ParseException(const std::string& msg)
32  : m_msg(msg)
33  {}
34 
35  virtual ~ParseException() throw()
36  {}
37 
38  virtual const char* what() const noexcept
39  { return m_msg.c_str(); }
40 
41  template<typename... Args>
42  ParseException format(const char* format, const Args& ... args)
43  {
44  return ParseException(fmt::format(format, args...));
45  }
46 private:
47  std::string m_msg;
48 };
49 
50 extern const char* UNSET_MARKER;
51 
53 {
54 public:
56  : m_config(config)
57  , m_prefix("/")
58  {}
59 
60  const std::string& prefix() const
61  { return m_prefix; }
62 
63  const std::string& filename() const
64  { return m_filename; }
65 
66  void setFilename(const std::string& filename)
67  { m_filename = filename; }
68 
69  void setCurrentElement(TiXmlElement* e)
70  {
71  // NOTE: We should not keep a reference to the TiXmlElement here,
72  // since the ParseContext might be around longer than the DOM tree.
73  // See evaluateParameters().
74  if(e)
75  m_currentLine = e->Row();
76  else
77  m_currentLine = -1;
78  }
79 
80  ParseContext enterScope(const std::string& prefix);
81 
82  std::string evaluate(const std::string& tpl, bool simplifyWhitespace = true);
83 
84  bool parseBool(const std::string& value, int line);
85 
87  {
88  m_args.clear();
89  }
90 
91  inline const std::map<std::string, std::string>& arguments() const
92  { return m_args; }
93 
94  void setArg(const std::string& name, const std::string& value, bool override);
95 
96  void setEnvironment(const std::string& name, const std::string& value);
97 
98  inline const std::map<std::string, std::string> environment() const
99  { return m_environment; }
100 
101  bool shouldSkip(TiXmlElement* e);
102 
104  { return m_config; }
105 
106  void setRemap(const std::string& from, const std::string& to);
107  const std::map<std::string, std::string>& remappings()
108  { return m_remappings; }
109 
110  template<typename... Args>
111  ParseException error(const char* fmt, const Args& ... args) const
112  {
113  std::string msg = fmt::format(fmt, args...);
114 
115  if(m_currentLine >= 0)
116  {
117  return ParseException(fmt::format("{}:{}: {}",
118  m_filename, m_currentLine, msg
119  ));
120  }
121  else
122  {
123  return ParseException(fmt::format("{}: {}", m_filename, msg));
124  }
125  }
126 
127  template<typename... Args>
128  void warning(const char* fmt, const Args& ... args) const
129  {
130  std::string msg = fmt::format(fmt, args...);
131 
132  if(m_currentLine >= 0)
133  {
134  fmt::print(stderr, "{}:{}: Warning: {}\n", m_filename, m_currentLine, msg);
135  }
136  else
137  {
138  fmt::print(stderr, "{}: Warning: {}\n", m_filename, msg);
139  }
140  }
141 private:
143 
144  std::string m_prefix;
145  std::string m_filename;
146  int m_currentLine = -1;
147  std::map<std::string, std::string> m_args;
148  std::map<std::string, std::string> m_environment;
149  std::map<std::string, std::string> m_remappings;
150 };
151 
153 {
154 public:
155  typedef std::shared_ptr<LaunchConfig> Ptr;
156  typedef std::shared_ptr<const LaunchConfig> ConstPtr;
157 
158  constexpr static float DEFAULT_CPU_LIMIT = 0.9f;
159  constexpr static uint64_t DEFAULT_MEMORY_LIMIT = 500*1024*1024;
160  constexpr static float DEFAULT_STOP_TIMEOUT = 5.0f;
161 
162  LaunchConfig();
163 
164  void setArgument(const std::string& name, const std::string& value);
165 
166  void setDefaultStopTimeout(double timeout);
167  void setDefaultCPULimit(double CPULimit);
168  void setDefaultMemoryLimit(uint64_t memoryLimit);
169 
170  void parse(const std::string& filename, bool onlyArguments = false);
171  void parseString(const std::string& input, bool onlyArguments = false);
172 
173  void evaluateParameters();
174 
175  inline const std::map<std::string, XmlRpc::XmlRpcValue>& parameters() const
176  { return m_params; }
177 
178  inline const std::vector<Node::Ptr>& nodes() const
179  { return m_nodes; }
180 
181  inline const std::map<std::string, std::string>& arguments() const
182  { return m_rootContext.arguments(); }
183 
184  std::string anonName(const std::string& base);
185 
186  std::string rosmonNodeName() const
187  { return m_rosmonNodeName; }
188 
189  std::string windowTitle() const
190  { return m_windowTitle; }
191 private:
193  {
196  };
197 
198  void parseTopLevelAttributes(TiXmlElement* element);
199 
200  void parse(TiXmlElement* element, ParseContext* ctx, bool onlyArguments = false);
201  void parseNode(TiXmlElement* element, ParseContext ctx);
202  void parseParam(TiXmlElement* element, ParseContext ctx, ParamContext paramContext = PARAM_GENERAL);
203  void parseROSParam(TiXmlElement* element, ParseContext ctx);
204  void parseInclude(TiXmlElement* element, ParseContext ctx);
205  void parseArgument(TiXmlElement* element, ParseContext& ctx);
206  void parseEnv(TiXmlElement* element, ParseContext& ctx);
207  void parseRemap(TiXmlElement* element, ParseContext& ctx);
208 
209  void loadYAMLParams(const ParseContext& ctx, const YAML::Node& n, const std::string& prefix);
210 
211  XmlRpc::XmlRpcValue paramToXmlRpc(const ParseContext& ctx, const std::string& value, const std::string& type = "");
212 
214 
215  std::vector<Node::Ptr> m_nodes;
216 
217  using ParameterList = std::map<std::string, XmlRpc::XmlRpcValue>;
218  using ParameterFuture = std::future<XmlRpc::XmlRpcValue>;
219 
220  struct YAMLResult
221  {
222  std::string name;
223  YAML::Node yaml;
224  };
225 
227  std::map<std::string, ParameterFuture> m_paramJobs;
228  std::vector<std::future<YAMLResult>> m_yamlParamJobs;
229 
230  std::map<std::string, std::string> m_anonNames;
231  std::mt19937_64 m_anonGen;
232 
233  std::string m_rosmonNodeName;
234 
235  std::string m_windowTitle;
236 
237  double m_defaultStopTimeout{DEFAULT_STOP_TIMEOUT};
238  uint64_t m_defaultMemoryLimit{DEFAULT_MEMORY_LIMIT};
239  double m_defaultCPULimit{DEFAULT_CPU_LIMIT};
240 };
241 
242 }
243 }
244 
245 #endif
246 
config evaluateParameters()
virtual const char * what() const noexcept
Definition: launch_config.h:38
const std::vector< Node::Ptr > & nodes() const
std::string simplifyWhitespace(const std::string &input)
Compress any sequence of whitespace to single spaces.
std::future< XmlRpc::XmlRpcValue > ParameterFuture
void setFilename(const std::string &filename)
Definition: launch_config.h:66
ParseException(const std::string &msg)
Definition: launch_config.h:31
const std::map< std::string, std::string > environment() const
Definition: launch_config.h:98
ParseContext(LaunchConfig *config)
Definition: launch_config.h:55
const std::map< std::string, XmlRpc::XmlRpcValue > & parameters() const
std::map< std::string, ParameterFuture > m_paramJobs
std::shared_ptr< LaunchConfig > Ptr
std::string rosmonNodeName() const
const std::string & prefix() const
Definition: launch_config.h:60
ParseException format(const char *format, const Args &...args)
Definition: launch_config.h:42
std::vector< std::future< YAMLResult > > m_yamlParamJobs
const char * UNSET_MARKER
std::map< std::string, std::string > m_anonNames
std::map< std::string, std::string > m_args
const std::map< std::string, std::string > & arguments() const
std::map< std::string, std::string > m_environment
const std::string & filename() const
Definition: launch_config.h:63
config parseString(R"EOF( <launch> <group ns="/"> <param name="param1" value="hello" /> </group> <node name="test_node" pkg="rosmon_core" type="abort" ns="/racecar"> <param name="private_param" value="hello again" /> </node> </launch> )EOF")
void setCurrentElement(TiXmlElement *e)
Definition: launch_config.h:69
std::shared_ptr< const LaunchConfig > ConstPtr
void warning(const char *fmt, const Args &...args) const
ParseException error(const char *fmt, const Args &...args) const
std::vector< Node::Ptr > m_nodes
std::string windowTitle() const
const std::map< std::string, std::string > & arguments() const
Definition: launch_config.h:91
std::map< std::string, std::string > m_remappings
const std::map< std::string, std::string > & remappings()
std::map< std::string, XmlRpc::XmlRpcValue > ParameterList


rosmon_core
Author(s): Max Schwarz
autogenerated on Wed Jul 10 2019 03:10:12