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