Rev 1599 | Rev 1609 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1594 | runge | 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 | namespace atom { |
||
| 29 | namespace config { |
||
| 30 | |||
| 1599 | runge | 31 | Manager::Pointer Manager::instance_; |
| 1594 | runge | 32 | |
| 33 | Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options") |
||
| 34 | { |
||
| 35 | std::string default_config_file = "/etc/atom.conf"; |
||
| 36 | |||
| 37 | if (!boost::filesystem::exists(default_config_file)) |
||
| 38 | { |
||
| 39 | default_config_file = "atom.conf"; |
||
| 40 | } |
||
| 41 | |||
| 42 | this->configuration_file_.add_options() |
||
| 43 | ("MonitorPort", boost::program_options::value<int>()->default_value(1201), "TCP port to open for monitor output") |
||
| 44 | ("CommandPort", boost::program_options::value<int>()->default_value(1202), "TCP port to open for command input") |
||
| 45 | ("LogFile", boost::program_options::value<std::string>(), "File to log output to") |
||
| 1599 | runge | 46 | ("LogLevel", boost::program_options::value<int>()->default_value(4), "Level of logging") |
| 1601 | runge | 47 | ("ScriptPath", boost::program_options::value<std::string>(), "Path to where the scripts are") |
| 1594 | runge | 48 | ("ProtocolFile",boost::program_options::value<std::string>(), "File to read the protocol form") |
| 1596 | runge | 49 | ("CanNet", boost::program_options::value<type::StringList>(), "Information on where to locate the CAN networks"); |
| 1594 | runge | 50 | |
| 51 | this->command_line_.add_options() |
||
| 52 | ("version,v", "print version string") |
||
| 53 | ("help,h", "produce help message") |
||
| 54 | ("daemon,d", "start in daemon mode") |
||
| 55 | ("file,f", boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file"); |
||
| 56 | |||
| 57 | this->command_line_.add(this->configuration_file_); |
||
| 58 | } |
||
| 59 | |||
| 60 | Manager::~Manager() |
||
| 61 | { |
||
| 62 | } |
||
| 63 | |||
| 64 | Manager::Pointer Manager::Instance() |
||
| 65 | { |
||
| 66 | return Manager::instance_; |
||
| 67 | } |
||
| 68 | |||
| 1599 | runge | 69 | void Manager::Create() |
| 70 | { |
||
| 71 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 72 | } |
||
| 73 | |||
| 1594 | runge | 74 | void Manager::Delete() |
| 75 | { |
||
| 76 | Manager::instance_.reset(); |
||
| 77 | } |
||
| 78 | |||
| 79 | bool Manager::Set(int argument_count, char** argument_vector) |
||
| 80 | { |
||
| 81 | try |
||
| 82 | { |
||
| 83 | boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_); |
||
| 84 | } |
||
| 85 | catch (boost::program_options::unknown_option e) |
||
| 86 | { |
||
| 87 | std::cerr << e.what() << std::endl; |
||
| 88 | std::cout << this->command_line_ << std::endl; |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | catch (boost::program_options::invalid_syntax e) |
||
| 92 | { |
||
| 93 | std::cerr << e.what() << std::endl; |
||
| 94 | std::cout << this->command_line_ << std::endl; |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | std::ifstream ifs(this->GetAsString("file").c_str()); |
||
| 99 | |||
| 100 | if (!ifs.is_open()) |
||
| 101 | { |
||
| 102 | std::cerr << "Error: Could not find " << this->GetAsString("file") << std::endl; |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | try |
||
| 107 | { |
||
| 108 | boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_); |
||
| 109 | } |
||
| 110 | catch (boost::program_options::unknown_option e) |
||
| 111 | { |
||
| 112 | ifs.close(); |
||
| 113 | std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl; |
||
| 114 | std::cout << this->configuration_file_ << std::endl; |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | catch (boost::program_options::invalid_syntax e) |
||
| 118 | { |
||
| 119 | std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl; |
||
| 120 | std::cout << this->command_line_ << std::endl; |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | ifs.close(); |
||
| 125 | |||
| 126 | if (this->Exist("help")) |
||
| 127 | { |
||
| 128 | std::cout << this->command_line_ << std::endl; |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | return true; |
||
| 133 | } |
||
| 134 | |||
| 135 | bool Manager::Exist(std::string name) |
||
| 136 | { |
||
| 137 | return this->variable_map_.count(name) != 0; |
||
| 138 | } |
||
| 139 | |||
| 140 | int Manager::GetAsInt(std::string name) |
||
| 141 | { |
||
| 142 | return this->variable_map_[name].as<int>(); |
||
| 143 | } |
||
| 144 | |||
| 145 | std::string Manager::GetAsString(std::string name) |
||
| 146 | { |
||
| 147 | return this->variable_map_[name].as<std::string>(); |
||
| 148 | } |
||
| 149 | |||
| 1596 | runge | 150 | type::StringList Manager::GetAsStringVector(std::string name) |
| 1594 | runge | 151 | { |
| 1596 | runge | 152 | return this->variable_map_[name].as<type::StringList>(); |
| 1594 | runge | 153 | } |
| 154 | |||
| 155 | |||
| 156 | }; // namespace config |
||
| 157 | }; // namespace atom |