ManagerConfig.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <rtm/RTC.h>
21 #include <rtm/ManagerConfig.h>
22 #include <fstream>
23 #include <iostream>
24 #include <coil/OS.h>
25 #include <coil/stringutil.h>
27 
28 #ifdef __QNX__
29 using std::sprintf;
30 #endif
31 
32 namespace RTC
33 {
34 
35  // The list of default configuration file path.
36  const char* ManagerConfig::config_file_path[] =
37  {
38  "./rtc.conf",
39  "/etc/rtc.conf",
40  "/etc/rtc/rtc.conf",
41  "/usr/local/etc/rtc.conf",
42  "/usr/local/etc/rtc/rtc.conf",
43  NULL
44  };
45 
46  // Environment value to specify configuration file
47  const char* ManagerConfig::config_file_env = "RTC_MANAGER_CONFIG";
48 
57  : m_isMaster(false)
58  {
59  }
60 
68  ManagerConfig::ManagerConfig(int argc, char** argv)
69  : m_isMaster(false)
70  {
71  init(argc, argv);
72  }
73 
82  {
83  }
84 
92  void ManagerConfig::init(int argc, char** argv)
93  {
94  parseArgs(argc, argv);
95  }
96 
105  {
107  if (findConfigFile())
108  {
109  std::ifstream f(m_configFile.c_str());
110  if (f.is_open())
111  {
112  prop.load(f);
113  f.close();
114  }
115  }
116  setSystemInformation(prop);
117  if (m_isMaster) { prop["manager.is_master"] = "YES"; }
118 
119  // Properties from arguments are marged finally
120  prop << m_argprop;
121  prop["config_file"] = m_configFile;
122  }
123 
131  void ManagerConfig::parseArgs(int argc, char** argv)
132  {
133  coil::GetOpt get_opts(argc, argv, "af:l:o:p:d", 0);
134  int opt;
135 
136  // if (argc == 0) return true;
137 
138  while ((opt = get_opts()) > 0)
139  {
140  switch (opt)
141  {
142  case 'a':
143  {
144  m_argprop["manager.corba_servant"] = "NO";
145  }
146  break;
147  // Specify configuration file not default
148  case 'f':
149  m_configFile = get_opts.optarg;
150  break;
151  case 'l':
152  // m_configFile = get_opts.optarg;
153  break;
154  case 'o':
155  {
156  std::string optarg(get_opts.optarg);
157  std::string::size_type pos(optarg.find(":"));
158  if (pos != std::string::npos)
159  {
160  m_argprop[optarg.substr(0, pos)] = optarg.substr(pos + 1);
161  }
162  }
163  break;
164  case 'p': // ORB's port number
165  {
166  int portnum;
167  if (coil::stringTo(portnum, get_opts.optarg))
168  {
169  std::string arg(":"); arg += get_opts.optarg;
170  m_argprop["corba.endpoints"] = arg;
171  }
172  }
173  break;
174  case 'd':
175  m_isMaster = true;
176  break;
177  default:
178  ;
179  }
180  }
181  return;
182  }
183 
192  {
193  // Check existance of configuration file given command arg
194  if (m_configFile != "")
195  {
196  if (fileExist(m_configFile))
197  {
198  return true;
199  }
200  }
201 
202  // Search rtc configuration file from environment variable
203  char* env = getenv(config_file_env);
204  if (env != NULL)
205  {
206  if (fileExist(env))
207  {
208  m_configFile = env;
209  return true;
210  }
211  }
212  // Search rtc configuration file from default search path
213  int i = 0;
214  while (config_file_path[i] != NULL)
215  {
216  if (fileExist(config_file_path[i]))
217  {
219  return true;
220  }
221  ++i;
222  }
223  return false;
224  }
225 
234  {
235  //
236  // Get system information by using ACE_OS::uname (UNIX/Windows)
237  //
238  coil::utsname sysinfo;
239  if (coil::uname(&sysinfo) != 0)
240  {
241  return;
242  }
243 
244  //
245  // Getting current proccess pid by using ACE_OS::getpid() (UNIX/Windows)
246  //
247  coil::pid_t pid = coil::getpid();
248  char pidc[8];
249  sprintf(pidc, "%d", pid);
250 
251  prop.setProperty("os.name", sysinfo.sysname);
252  prop.setProperty("os.release", sysinfo.release);
253  prop.setProperty("os.version", sysinfo.version);
254  prop.setProperty("os.arch", sysinfo.machine);
255  prop.setProperty("os.hostname", sysinfo.nodename);
256  prop.setProperty("manager.pid", pidc);
257 
258  return;
259  }
260 
268  bool ManagerConfig::fileExist(const std::string& filename)
269  {
270  std::ifstream infile;
271  infile.open(filename.c_str(), std::ios::in);
272  // fial() 0: ok, !0: fail
273  if (infile.fail() != 0)
274  {
275  infile.close();
276  return false;
277  }
278  else
279  {
280  infile.close();
281  return true;
282  }
283  return false;
284  }
285 }
char version[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:54
RT-Component.
std::string m_configFile
Manager&#39;s configuration file path.
bool stringTo(To &val, const char *str)
Convert the given std::string to object.
Definition: stringutil.h:597
virtual ~ManagerConfig(void)
Destructor.
GetOpt class.
bool fileExist(const std::string &filename)
Check the file existence.
RTC manager configuration.
char sysname[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:51
void configure(coil::Properties &prop)
Specify the configuration information to the Property.
void parseArgs(int argc, char **argv)
Parse the command arguments.
static const char * default_config[]
Default configuration for ORBManager.
char nodename[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:52
env
ネームサーバー定義 env = RtmEnv(sys.argv, ["localhost:2809"]) list0 = env.name_space["localhost:2809"].list_obj() env.name_space[&#39;localhost:2809&#39;].rtc_handles.keys() ns = env.name_space[&#39;localhost:2809&#39;]
Definition: ConnectTest.py:25
::pid_t pid_t
Get process ID of the caller process.
Definition: ace/coil/OS.h:38
void init(int argc, char **argv)
Initialization.
void setDefaults(const char *defaults[], long num=LONG_MAX)
Set a default value together in the property list.
Definition: Properties.cpp:299
std::string setProperty(const std::string &key, const std::string &value)
Set a value associated with key in the property list.
Definition: Properties.cpp:240
int uname(utsname *name)
Get System information.
Definition: ace/coil/OS.h:33
void load(std::istream &inStream)
Loads property list that consists of key:value from input stream.
Definition: Properties.cpp:338
prop
Organization::get_organization_property ();.
pid_t getpid()
Definition: ace/coil/OS.h:39
Class represents a set of properties.
Definition: Properties.h:101
char * getenv(const char *name)
Get environment variable.
Definition: ace/coil/OS.h:48
static const char * config_file_path[]
The default configuration file path for manager.
Definition: ManagerConfig.h:95
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:598
RTComponent header.
ManagerConfig()
Constructor.
char machine[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:55
char * optarg
bool m_isMaster
Manager master flag.
static const char * config_file_env
The environment variable to distinguish the default configuration file path.
coil::Properties m_argprop
configuration properties from arguments
char release[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:53
bool findConfigFile()
Find the configuration file.
void setSystemInformation(coil::Properties &prop)
Set system information.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43