ManagerConfig.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <rtm/RTC.h>
00021 #include <rtm/ManagerConfig.h>
00022 #include <fstream>
00023 #include <iostream>
00024 #include <coil/OS.h>
00025 #include <coil/stringutil.h>
00026 #include <rtm/DefaultConfiguration.h>
00027 
00028 namespace RTC
00029 {
00030   
00031   // The list of default configuration file path.
00032   const char* ManagerConfig::config_file_path[] = 
00033     {
00034       "./rtc.conf",
00035       "/etc/rtc.conf",
00036       "/etc/rtc/rtc.conf",
00037       "/usr/local/etc/rtc.conf",
00038       "/usr/local/etc/rtc/rtc.conf",
00039       NULL
00040     };
00041   
00042   // Environment value to specify configuration file
00043   const char* ManagerConfig::config_file_env = "RTC_MANAGER_CONFIG";
00044   
00052   ManagerConfig::ManagerConfig()
00053     : m_isMaster(false)
00054   {
00055   }
00056   
00064   ManagerConfig::ManagerConfig(int argc, char** argv)
00065     : m_isMaster(false)
00066   {
00067     init(argc, argv);
00068   }
00069   
00077   ManagerConfig::~ManagerConfig()
00078   {
00079   }
00080   
00088   void ManagerConfig::init(int argc, char** argv)
00089   {
00090     parseArgs(argc, argv);
00091   }
00092   
00100   void ManagerConfig::configure(coil::Properties& prop)
00101   {
00102     prop.setDefaults(default_config);
00103     if (findConfigFile())
00104       {
00105         std::ifstream f(m_configFile.c_str());
00106         if (f.is_open())
00107           {
00108             prop.load(f);
00109             f.close();
00110           }
00111       }
00112     setSystemInformation(prop);
00113     if (m_isMaster) { prop["manager.is_master"] = "YES"; }
00114 
00115     // Properties from arguments are marged finally
00116     prop << m_argprop;
00117     prop["config_file"] = m_configFile;
00118   }
00119   
00127   void ManagerConfig::parseArgs(int argc, char** argv)
00128   {
00129     coil::GetOpt get_opts(argc, argv, "af:l:o:p:d", 0);
00130     int opt;
00131     
00132     //  if (argc == 0) return true;
00133     
00134     while ((opt = get_opts()) > 0)
00135       {
00136         switch (opt)
00137           {
00138           case 'a':
00139             {
00140               m_argprop["manager.corba_servant"] = "NO";
00141             }
00142             break;
00143             // Specify configuration file not default
00144           case 'f':
00145             m_configFile = get_opts.optarg;
00146             break;
00147           case 'l':
00148             //      m_configFile = get_opts.optarg;
00149             break;
00150           case 'o':
00151             {
00152               std::string optarg(get_opts.optarg);
00153               std::string::size_type pos(optarg.find(":"));
00154               if (pos != std::string::npos)
00155                 {
00156                   m_argprop[optarg.substr(0, pos)] = optarg.substr(pos + 1);
00157                 }
00158             }
00159             break;
00160           case 'p': // ORB's port number
00161             {
00162               int portnum;
00163               if (coil::stringTo(portnum, get_opts.optarg))
00164                 {
00165                   std::string arg(":"); arg += get_opts.optarg;
00166                   m_argprop["corba.endpoints"]  = arg;
00167                 }
00168             }
00169             break;
00170           case 'd':
00171             m_isMaster = true;
00172             break;
00173           default:
00174             ;
00175           }
00176       }
00177     return;
00178   }
00179   
00187   bool ManagerConfig::findConfigFile()
00188   {
00189     // Check existance of configuration file given command arg
00190     if (m_configFile != "") 
00191       {
00192         if (fileExist(m_configFile))
00193           {
00194             return true;
00195           }
00196       }
00197     
00198     // Search rtc configuration file from environment variable
00199     char* env = getenv(config_file_env);
00200     if (env != NULL)
00201       {
00202         if (fileExist(env))
00203           {
00204             m_configFile = env;
00205             return true;
00206           }
00207       }
00208     // Search rtc configuration file from default search path
00209     int i = 0;
00210     while (config_file_path[i] != NULL)
00211       {
00212         if (fileExist(config_file_path[i]))
00213           {
00214             m_configFile = config_file_path[i];
00215             return true;
00216           }
00217         ++i;
00218       }
00219     return false;
00220   }
00221   
00229   void ManagerConfig::setSystemInformation(coil::Properties& prop)
00230   { 
00231     //
00232     // Get system information by using ACE_OS::uname (UNIX/Windows)
00233     // 
00234     coil::utsname  sysinfo;
00235     if (coil::uname(&sysinfo) != 0)
00236       {
00237         return;
00238       }
00239     
00240     //
00241     // Getting current proccess pid by using ACE_OS::getpid() (UNIX/Windows)
00242     //
00243     coil::pid_t pid = coil::getpid();
00244     char pidc[8];
00245     sprintf(pidc, "%d", pid);
00246     
00247     prop.setProperty("os.name",     sysinfo.sysname);
00248     prop.setProperty("os.release",  sysinfo.release);
00249     prop.setProperty("os.version",  sysinfo.version);
00250     prop.setProperty("os.arch",     sysinfo.machine);
00251     prop.setProperty("os.hostname", sysinfo.nodename);
00252     prop.setProperty("manager.pid",         pidc);
00253     
00254     return;
00255   }
00256   
00264   bool ManagerConfig::fileExist(const std::string& filename)
00265   {
00266     std::ifstream infile;
00267     infile.open(filename.c_str(), std::ios::in);
00268     // fial() 0: ok, !0: fail
00269     if (infile.fail() != 0) 
00270       {
00271         infile.close();
00272         return false;
00273       }
00274     else
00275       {
00276         infile.close();
00277         return true;
00278       }
00279     return false;
00280   }
00281 }


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:04