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