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