Subversion Repositories HomeAutomation

Rev

Rev 1939 | Rev 1989 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Manager.h"
  22.  
  23. #include <iostream>
  24. #include <fstream>
  25.  
  26. #include <boost/filesystem.hpp>
  27.  
  28. #include "common/log.h"
  29.  
  30. namespace atom {
  31. namespace config {
  32.  
  33. static const std::string log_module_ = "config";
  34. Manager::Pointer Manager::instance_;
  35.  
  36. Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options")
  37. {
  38.   std::string default_config_file = "/etc/atom/atom.conf";
  39.  
  40.   if (!boost::filesystem::exists(default_config_file))
  41.   {
  42.       default_config_file = "atom.conf";
  43.   }
  44.  
  45.   this->configuration_file_.add_options()
  46.   ("MonitorPort",   boost::program_options::value<int>()->default_value(1201), "TCP port to open for monitor output")
  47.   ("CommandPort",   boost::program_options::value<int>()->default_value(1202), "TCP port to open for command input")
  48.   ("DaemonPort",    boost::program_options::value<int>()->default_value(1200), "TCP port to open for canDaemon input/output")
  49.   ("MbbPort",       boost::program_options::value<int>()->default_value(1212), "TCP port to open for MBB clients")
  50.   ("LogFile",       boost::program_options::value<std::string>(),              "File to log output to")
  51.   ("LogLevel",      boost::program_options::value<int>()->default_value(4),    "Level of logging")
  52.   ("LogLevelMask",  boost::program_options::value<std::string>(),              "Level of logging")
  53.   ("ScriptPath",    boost::program_options::value<std::string>(),              "Path to where the scripts are")
  54.   ("UserScriptPath",boost::program_options::value<std::string>(),              "Path to where the user scripts are")
  55.   ("StoragePath",   boost::program_options::value<std::string>(),              "Path to where the storage files are")
  56.   ("ProtocolFile",  boost::program_options::value<std::string>(),              "File to read the protocol form")
  57.   ("CanNet",        boost::program_options::value<common::StringList>(),       "Information on where to locate the CAN networks")
  58.   ("Legacy",                                                                   "Start legacy VM environment");
  59.  
  60.   this->command_line_.add_options()
  61.   ("help,h",    "produce help message")
  62.   ("daemon,d",  "start in daemon mode")
  63.   ("file,f",    boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file");
  64.  
  65.   this->command_line_.add(this->configuration_file_);
  66. }
  67.  
  68. Manager::~Manager()
  69. {
  70. }
  71.  
  72. Manager::Pointer Manager::Instance()
  73. {
  74.   return Manager::instance_;
  75. }
  76.  
  77. void Manager::Create()
  78. {
  79.   Manager::instance_ = Manager::Pointer(new Manager());
  80. }
  81.  
  82. void Manager::Delete()
  83. {
  84.   Manager::instance_.reset();
  85. }
  86.  
  87. bool Manager::Set(int argument_count, char** argument_vector)
  88. {
  89.   try
  90.   {
  91.     boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_);
  92.   }
  93.   catch (boost::program_options::unknown_option e)
  94.   {
  95.     log::Error(log_module_, e.what());
  96.     std::cout << this->command_line_ << std::endl;
  97.     return false;
  98.   }
  99.   catch (boost::program_options::invalid_syntax e)
  100.   {
  101.     log::Error(log_module_, e.what());
  102.     std::cout << this->command_line_ << std::endl;
  103.     return false;
  104.   }
  105.  
  106.   std::ifstream ifs(this->GetAsString("file").data());
  107.      
  108.   if (!ifs.is_open())
  109.   {
  110.     log::Error(log_module_, "Could not find %s!", this->GetAsString("file").c_str());
  111.     return false;
  112.   }
  113.  
  114.   try
  115.   {
  116.       boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_);
  117.   }
  118.   catch (boost::program_options::unknown_option e)
  119.   {
  120.       ifs.close();
  121.       log::Error(log_module_, e.what());
  122.       std::cout << this->configuration_file_ << std::endl;
  123.       return false;
  124.   }
  125.   catch (boost::program_options::invalid_syntax e)
  126.   {
  127.     log::Error(log_module_, "%s found in %s!", e.what(), this->GetAsString("file").c_str());
  128.     std::cout << this->command_line_ << std::endl;
  129.     return false;
  130.   }
  131.      
  132.   ifs.close();
  133.  
  134.   if (this->Exist("help"))
  135.   {
  136.     std::cout << this->command_line_ << std::endl;
  137.     return false;
  138.   }
  139.  
  140.   log::Info(log_module_, "Using %s as configuraiton file.", this->GetAsString("file").c_str());
  141.  
  142.   return true;
  143. }
  144.  
  145. bool Manager::Exist(std::string name)
  146. {
  147.   return this->variable_map_.count(name) != 0;
  148. }
  149.  
  150. int Manager::GetAsInt(std::string name)
  151. {
  152.   return this->variable_map_[name].as<int>();
  153. }
  154.  
  155. std::string Manager::GetAsString(std::string name)
  156. {
  157.   return this->variable_map_[name].as<std::string>();
  158. }
  159.  
  160. common::StringList Manager::GetAsStringVector(std::string name)
  161. {
  162.   return this->variable_map_[name].as<common::StringList>();
  163. }
  164.  
  165.  
  166. }; // namespace config
  167. }; // namespace atom
  168.