Subversion Repositories HomeAutomation

Rev

Rev 2107 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1594 runge 1
/*
2216 runge 2
 *
1594 runge 3
 *  Copyright (C) 2010  Mattias Runge
2216 runge 4
 *
1594 runge 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.
2216 runge 9
 *
1594 runge 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.
2216 runge 14
 *
1594 runge 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.
2216 runge 18
 *
1594 runge 19
 */
20
 
21
#include "Manager.h"
22
 
23
#include <iostream>
24
#include <fstream>
25
 
26
#include <boost/filesystem.hpp>
27
 
1961 runge 28
#include "common/log.h"
29
 
1594 runge 30
namespace atom {
31
namespace config {
32
 
1961 runge 33
static const std::string log_module_ = "config";
1599 runge 34
Manager::Pointer Manager::instance_;
1594 runge 35
 
36
Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options")
37
{
1961 runge 38
  std::string default_config_file = "/etc/atom/atom.conf";
2216 runge 39
 
1961 runge 40
  if (!boost::filesystem::exists(default_config_file))
41
  {
42
      default_config_file = "atom.conf";
43
  }
1594 runge 44
 
1961 runge 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")
2107 runge 48
  ("JsPipePort",    boost::program_options::value<int>()->default_value(1203), "TCP port to open for the JsPipe")
1961 runge 49
  ("DaemonPort",    boost::program_options::value<int>()->default_value(1200), "TCP port to open for canDaemon input/output")
2216 runge 50
  ("CanForwardPort",boost::program_options::value<int>()->default_value(1101), "TCP port to open for CAN traffic forward")
1961 runge 51
  ("MbbPort",       boost::program_options::value<int>()->default_value(1212), "TCP port to open for MBB clients")
52
  ("LogFile",       boost::program_options::value<std::string>(),              "File to log output to")
53
  ("LogLevel",      boost::program_options::value<int>()->default_value(4),    "Level of logging")
54
  ("LogLevelMask",  boost::program_options::value<std::string>(),              "Level of logging")
55
  ("ScriptPath",    boost::program_options::value<std::string>(),              "Path to where the scripts are")
56
  ("UserScriptPath",boost::program_options::value<std::string>(),              "Path to where the user scripts are")
57
  ("StoragePath",   boost::program_options::value<std::string>(),              "Path to where the storage files are")
58
  ("ProtocolFile",  boost::program_options::value<std::string>(),              "File to read the protocol form")
59
  ("CanNet",        boost::program_options::value<common::StringList>(),       "Information on where to locate the CAN networks")
60
  ("Legacy",                                                                   "Start legacy VM environment");
1594 runge 61
 
1961 runge 62
  this->command_line_.add_options()
63
  ("help,h",    "produce help message")
64
  ("daemon,d",  "start in daemon mode")
65
  ("file,f",    boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file");
2216 runge 66
 
1961 runge 67
  this->command_line_.add(this->configuration_file_);
1594 runge 68
}
69
 
70
Manager::~Manager()
71
{
72
}
73
 
74
Manager::Pointer Manager::Instance()
75
{
1961 runge 76
  return Manager::instance_;
1594 runge 77
}
78
 
1599 runge 79
void Manager::Create()
80
{
1961 runge 81
  Manager::instance_ = Manager::Pointer(new Manager());
1599 runge 82
}
83
 
1594 runge 84
void Manager::Delete()
85
{
1961 runge 86
  Manager::instance_.reset();
1594 runge 87
}
88
 
89
bool Manager::Set(int argument_count, char** argument_vector)
90
{
1961 runge 91
  try
92
  {
93
    boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_);
94
  }
95
  catch (boost::program_options::unknown_option e)
2216 runge 96
  {
1961 runge 97
    log::Error(log_module_, e.what());
98
    std::cout << this->command_line_ << std::endl;
99
    return false;
100
  }
101
  catch (boost::program_options::invalid_syntax e)
102
  {
103
    log::Error(log_module_, e.what());
104
    std::cout << this->command_line_ << std::endl;
105
    return false;
106
  }
2216 runge 107
 
1961 runge 108
  std::ifstream ifs(this->GetAsString("file").data());
2216 runge 109
 
1961 runge 110
  if (!ifs.is_open())
111
  {
112
    log::Error(log_module_, "Could not find %s!", this->GetAsString("file").c_str());
113
    return false;
114
  }
2216 runge 115
 
1961 runge 116
  try
117
  {
118
      boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_);
119
  }
120
  catch (boost::program_options::unknown_option e)
121
  {
122
      ifs.close();
123
      log::Error(log_module_, e.what());
124
      std::cout << this->configuration_file_ << std::endl;
125
      return false;
126
  }
127
  catch (boost::program_options::invalid_syntax e)
128
  {
129
    log::Error(log_module_, "%s found in %s!", e.what(), this->GetAsString("file").c_str());
130
    std::cout << this->command_line_ << std::endl;
131
    return false;
132
  }
2216 runge 133
 
1961 runge 134
  ifs.close();
2216 runge 135
 
1961 runge 136
  if (this->Exist("help"))
137
  {
138
    std::cout << this->command_line_ << std::endl;
139
    return false;
140
  }
2216 runge 141
 
1989 runge 142
  log::Info(log_module_, "Using %s as configuration file.", this->GetAsString("file").c_str());
2216 runge 143
 
1961 runge 144
  return true;
1594 runge 145
}
146
 
147
bool Manager::Exist(std::string name)
148
{
1961 runge 149
  return this->variable_map_.count(name) != 0;
1594 runge 150
}
151
 
152
int Manager::GetAsInt(std::string name)
153
{
1961 runge 154
  return this->variable_map_[name].as<int>();
1594 runge 155
}
156
 
157
std::string Manager::GetAsString(std::string name)
158
{
1961 runge 159
  return this->variable_map_[name].as<std::string>();
1594 runge 160
}
161
 
1642 runge 162
common::StringList Manager::GetAsStringVector(std::string name)
1594 runge 163
{
1961 runge 164
  return this->variable_map_[name].as<common::StringList>();
1594 runge 165
}
166
 
167
 
168
}; // namespace config
1675 arune 169
}; // namespace atom