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