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 namespace RTC
29 {
30 
31  // The list of default configuration file path.
32  const char* ManagerConfig::config_file_path[] =
33  {
34  "./rtc.conf",
35  "/etc/rtc.conf",
36  "/etc/rtc/rtc.conf",
37  "/usr/local/etc/rtc.conf",
38  "/usr/local/etc/rtc/rtc.conf",
39  NULL
40  };
41 
42  // Environment value to specify configuration file
43  const char* ManagerConfig::config_file_env = "RTC_MANAGER_CONFIG";
44 
53  : m_isMaster(false)
54  {
55  }
56 
64  ManagerConfig::ManagerConfig(int argc, char** argv)
65  : m_isMaster(false)
66  {
67  init(argc, argv);
68  }
69 
78  {
79  }
80 
88  void ManagerConfig::init(int argc, char** argv)
89  {
90  parseArgs(argc, argv);
91  }
92 
101  {
103  if (findConfigFile())
104  {
105  std::ifstream f(m_configFile.c_str());
106  if (f.is_open())
107  {
108  prop.load(f);
109  f.close();
110  }
111  }
112  setSystemInformation(prop);
113  if (m_isMaster) { prop["manager.is_master"] = "YES"; }
114 
115  // Properties from arguments are marged finally
116  prop << m_argprop;
117  prop["config_file"] = m_configFile;
118  }
119 
127  void ManagerConfig::parseArgs(int argc, char** argv)
128  {
129  coil::GetOpt get_opts(argc, argv, "af:l:o:p:d", 0);
130  int opt;
131 
132  // if (argc == 0) return true;
133 
134  while ((opt = get_opts()) > 0)
135  {
136  switch (opt)
137  {
138  case 'a':
139  {
140  m_argprop["manager.corba_servant"] = "NO";
141  }
142  break;
143  // Specify configuration file not default
144  case 'f':
145  m_configFile = get_opts.optarg;
146  break;
147  case 'l':
148  // m_configFile = get_opts.optarg;
149  break;
150  case 'o':
151  {
152  std::string optarg(get_opts.optarg);
153  std::string::size_type pos(optarg.find(":"));
154  if (pos != std::string::npos)
155  {
156  m_argprop[optarg.substr(0, pos)] = optarg.substr(pos + 1);
157  }
158  }
159  break;
160  case 'p': // ORB's port number
161  {
162  int portnum;
163  if (coil::stringTo(portnum, get_opts.optarg))
164  {
165  std::string arg(":"); arg += get_opts.optarg;
166  m_argprop["corba.endpoints"] = arg;
167  }
168  }
169  break;
170  case 'd':
171  m_isMaster = true;
172  break;
173  default:
174  ;
175  }
176  }
177  return;
178  }
179 
188  {
189  // Check existance of configuration file given command arg
190  if (m_configFile != "")
191  {
192  if (fileExist(m_configFile))
193  {
194  return true;
195  }
196  }
197 
198  // Search rtc configuration file from environment variable
199  char* env = getenv(config_file_env);
200  if (env != NULL)
201  {
202  if (fileExist(env))
203  {
204  m_configFile = env;
205  return true;
206  }
207  }
208  // Search rtc configuration file from default search path
209  int i = 0;
210  while (config_file_path[i] != NULL)
211  {
212  if (fileExist(config_file_path[i]))
213  {
215  return true;
216  }
217  ++i;
218  }
219  return false;
220  }
221 
230  {
231  //
232  // Get system information by using ACE_OS::uname (UNIX/Windows)
233  //
234  coil::utsname sysinfo;
235  if (coil::uname(&sysinfo) != 0)
236  {
237  return;
238  }
239 
240  //
241  // Getting current proccess pid by using ACE_OS::getpid() (UNIX/Windows)
242  //
243  coil::pid_t pid = coil::getpid();
244  char pidc[8];
245  sprintf(pidc, "%d", pid);
246 
247  prop.setProperty("os.name", sysinfo.sysname);
248  prop.setProperty("os.release", sysinfo.release);
249  prop.setProperty("os.version", sysinfo.version);
250  prop.setProperty("os.arch", sysinfo.machine);
251  prop.setProperty("os.hostname", sysinfo.nodename);
252  prop.setProperty("manager.pid", pidc);
253 
254  return;
255  }
256 
264  bool ManagerConfig::fileExist(const std::string& filename)
265  {
266  std::ifstream infile;
267  infile.open(filename.c_str(), std::ios::in);
268  // fial() 0: ok, !0: fail
269  if (infile.fail() != 0)
270  {
271  infile.close();
272  return false;
273  }
274  else
275  {
276  infile.close();
277  return true;
278  }
279  return false;
280  }
281 }
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:295
std::string setProperty(const std::string &key, const std::string &value)
Set a value associated with key in the property list.
Definition: Properties.cpp:236
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:334
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:593
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 Thu Jun 6 2019 19:25:58