Changeset 291548e in nscp
- Timestamp:
- 08/22/10 08:49:56 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 2a1411e
- Parents:
- de7ec29
- Files:
-
- 2 added
- 28 edited
-
include/NSCAPI.h (modified) (1 diff)
-
include/nrpe/server/server.cpp (modified) (1 diff)
-
include/nscapi/nscapi_core_wrapper.cpp (modified) (2 diffs)
-
include/nscapi/nscapi_core_wrapper.hpp (modified) (3 diffs)
-
include/nscapi/settings.hpp (modified) (3 diffs)
-
include/settings/settings_core.hpp (modified) (8 diffs)
-
include/settings/settings_core_impl.hpp (modified) (1 diff)
-
include/settings/settings_handler_impl.hpp (added)
-
include/settings/settings_ini.hpp (modified) (2 diffs)
-
include/settings/settings_interface_impl.hpp (added)
-
include/settings/settings_old.hpp (modified) (1 diff)
-
include/settings/settings_registry.hpp (modified) (3 diffs)
-
include/strEx.h (modified) (1 diff)
-
modules/CheckEventLog/CheckEventLog.def (modified) (1 diff)
-
modules/CheckHelpers/CheckHelpers.def (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.def (modified) (1 diff)
-
modules/FileLogger/FileLogger.def (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.def (modified) (1 diff)
-
modules/NRPEServer/NRPEServer.cpp (modified) (2 diffs)
-
modules/NSCAAgent/NSCAAgent.def (modified) (1 diff)
-
modules/Scheduler/Scheduler.def (modified) (1 diff)
-
service/CMakeLists.txt (modified) (1 diff)
-
service/NSClient++.cpp (modified) (15 diffs)
-
service/NSClient++.h (modified) (6 diffs)
-
service/cli_parser.hpp (modified) (3 diffs)
-
service/core_api.cpp (modified) (8 diffs)
-
service/core_api.h (modified) (1 diff)
-
service/settings_client.hpp (modified) (4 diffs)
-
service/settings_manager_impl.cpp (modified) (4 diffs)
-
service/settings_manager_impl.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/NSCAPI.h
r497b779 r291548e 124 124 typedef NSCAPI::errorReturn (*lpNSAPIExpandPath)(const wchar_t*,wchar_t*,unsigned int); 125 125 typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsInt)(const wchar_t*, const wchar_t*, int); 126 typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsBool)(const wchar_t*, const wchar_t*, int); 126 127 typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsSection)(const wchar_t*, wchar_t***, unsigned int *); 127 128 typedef NSCAPI::errorReturn (*lpNSAPIReleaseSettingsSectionBuffer)(wchar_t***, unsigned int *); -
include/nrpe/server/server.cpp
r497b779 r291548e 51 51 acceptor_.open(endpoint.protocol()); 52 52 acceptor_.set_option(ip::tcp::acceptor::reuse_address(true)); 53 request_handler_->log_debug(__FILEW__, __LINE__, _T("Attempting to bind to: ") + info.get_endpoint_str()); 53 54 acceptor_.bind(endpoint); 54 55 if (info.back_log == connection_info::backlog_default) -
include/nscapi/nscapi_core_wrapper.cpp
r497b779 r291548e 328 328 return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); 329 329 } 330 bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) { 331 if (!fNSAPIGetSettingsBool) 332 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 333 return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1; 334 } 330 335 void nscapi::core_wrapper::settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) { 331 336 if (!fNSAPISettingsRegKey) … … 558 563 fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr")); 559 564 fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt")); 565 fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool")); 560 566 fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString")); 561 567 fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection")); -
include/nscapi/nscapi_core_wrapper.hpp
r497b779 r291548e 44 44 nscapi::core_api::lpNSAPIExpandPath fNSAPIExpandPath; 45 45 nscapi::core_api::lpNSAPIGetSettingsInt fNSAPIGetSettingsInt; 46 nscapi::core_api::lpNSAPIGetSettingsBool fNSAPIGetSettingsBool; 46 47 nscapi::core_api::lpNSAPIMessage fNSAPIMessage; 47 48 nscapi::core_api::lpNSAPIStopServer fNSAPIStopServer; … … 89 90 , fNSAPIGetSettingsString(NULL) 90 91 , fNSAPIGetSettingsInt(NULL) 92 , fNSAPIGetSettingsBool(NULL) 91 93 , fNSAPIMessage(NULL) 92 94 , fNSAPIStopServer(NULL) … … 124 126 std::wstring expand_path(std::wstring value); 125 127 int getSettingsInt(std::wstring section, std::wstring key, int defaultValue); 128 bool getSettingsBool(std::wstring section, std::wstring key, bool defaultValue); 126 129 void settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced); 127 130 void settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced); -
include/nscapi/settings.hpp
rde7ec29 r291548e 91 91 } 92 92 virtual void notify(nscapi::core_wrapper* core_, std::wstring path, std::wstring key) const { 93 T value = static_cast<T>(core_->getSettings Int(path, key, typed_int_value<T>::default_value_as_int_)==1);93 T value = static_cast<T>(core_->getSettingsBool(path, key, typed_int_value<T>::default_value_as_int_==1)); 94 94 update_target(&value); 95 95 } … … 405 405 else 406 406 alias_ = cur; 407 407 } 408 void set_alias(std::wstring prefix, std::wstring cur, std::wstring def) { 409 if (!prefix.empty()) 410 prefix += _T("/"); 411 if (cur.empty()) 412 alias_ = prefix + def; 413 else 414 alias_ = prefix + cur; 408 415 } 409 416 … … 420 427 void notify() { 421 428 BOOST_FOREACH(key_list::value_type v, keys_) { 422 if (v->key) 423 v->key->notify(core_, v->path, v->key_name); 429 try { 430 if (v->key) 431 v->key->notify(core_, v->path, v->key_name); 432 } catch (...) { 433 core_->Message(NSCAPI::error, __FILEW__, __LINE__, _T("Failed to register: ") + v->key_name); 434 } 424 435 } 425 436 BOOST_FOREACH(path_list::value_type v, paths_) { 426 if (v->path) 427 v->path->notify(core_, v->path_name); 437 try { 438 if (v->path) 439 v->path->notify(core_, v->path_name); 440 } catch (...) { 441 core_->Message(NSCAPI::error, __FILEW__, __LINE__, _T("Failed to register: ") + v->path_name); 442 } 428 443 } 429 444 -
include/settings/settings_core.hpp
r497b779 r291548e 34 34 35 35 namespace settings { 36 37 38 template<class T> 39 struct instance_ptr_helper { 40 boost::shared_ptr<T> ptr_; 41 boost::unique_lock<boost::timed_mutex> *mutex_; 42 instance_ptr_helper(boost::shared_ptr<T> ptr, boost::timed_mutex &mutex, int timeout) : ptr_(ptr), mutex_(NULL) { 43 mutex_ = new boost::unique_lock<boost::timed_mutex>(mutex, boost::get_system_time() + boost::posix_time::seconds(timeout)); 44 std::wcout << _T("creating (safe)...") << std::endl; 45 if (!mutex_->owns_lock()) 46 throw settings_exception(_T("Failed to get mutex, cant get settings instance")); 47 } 48 instance_ptr_helper(boost::shared_ptr<T> ptr) : ptr_(ptr) { 49 std::wcout << _T("creating (unsafe)...") << std::endl; 50 delete mutex_; 51 } 52 ~instance_ptr_helper() { 53 std::wcout << _T("destroying...") << std::endl; 54 } 55 boost::shared_ptr<T> operator* () const { 56 return ptr_; 57 } 58 59 boost::shared_ptr<T> operator-> () const { 60 return ptr_; 61 } 62 bool operator! () const { 63 return !ptr_; 64 } 65 }; 36 66 class settings_exception { 37 67 std::wstring error_; … … 104 134 105 135 class settings_interface; 136 typedef boost::shared_ptr<settings_interface> instance_ptr; 137 typedef boost::shared_ptr<settings_interface> instance_raw_ptr; 106 138 107 139 class settings_core { … … 207 239 /// 208 240 /// @author mickem 209 virtual boost::shared_ptr<settings_interface> get() = 0; 241 virtual instance_ptr get() = 0; 242 virtual instance_ptr get_no_wait() = 0; 243 210 244 ////////////////////////////////////////////////////////////////////////// 211 245 /// Get a settings interface … … 217 251 //virtual settings_interface* get(settings_core::settings_type type) = 0; 218 252 // Conversion Functions 219 virtual void migrate( boost::shared_ptr<settings_interface> from, boost::shared_ptr<settings_interface>to) = 0;220 virtual void migrate_to( boost::shared_ptr<settings_interface>to) = 0;221 virtual void migrate_from( boost::shared_ptr<settings_interface>from) = 0;253 virtual void migrate(instance_ptr from, instance_ptr to) = 0; 254 virtual void migrate_to(instance_ptr to) = 0; 255 virtual void migrate_from(instance_ptr from) = 0; 222 256 virtual void migrate(std::wstring from, std::wstring to) = 0; 223 257 virtual void migrate_to(std::wstring to) = 0; … … 263 297 /// 264 298 /// @author mickem 265 virtual boost::shared_ptr<settings_interface>create_instance(std::wstring context) = 0;299 virtual instance_raw_ptr create_instance(std::wstring context) = 0; 266 300 267 301 ////////////////////////////////////////////////////////////////////////// … … 485 519 /// 486 520 /// @author mickem 487 virtual void save_to( boost::shared_ptr<settings_interface>other) = 0;521 virtual void save_to(instance_ptr other) = 0; 488 522 virtual void save_to(std::wstring other) = 0; 489 523 ////////////////////////////////////////////////////////////////////////// … … 498 532 /// 499 533 /// @author mickem 500 virtual void load_from( boost::shared_ptr<settings_interface>other) = 0;534 virtual void load_from(instance_ptr other) = 0; 501 535 virtual void load_from(std::wstring other) = 0; 502 536 ////////////////////////////////////////////////////////////////////////// … … 510 544 virtual std::wstring get_info() = 0; 511 545 546 static bool string_to_bool(std::wstring str) { 547 return str == _T("true")||str == _T("1"); 548 } 549 550 512 551 }; 513 552 -
include/settings/settings_core_impl.hpp
r497b779 r291548e 21 21 #pragma once 22 22 23 #include <types.hpp> 24 #include <Singleton.h> 25 #include <string> 26 #include <map> 27 #include <set> 28 #include <boost/thread/thread.hpp> 29 #include <boost/thread/locks.hpp> 30 #include <boost/filesystem/path.hpp> 31 #include <boost/regex.hpp> 32 #include <strEx.h> 33 #include <settings/settings_core.hpp> 23 #include <settings/settings_handler_impl.hpp> 24 #include <settings/settings_interface_impl.hpp> 34 25 35 namespace settings {36 typedef boost::shared_ptr<settings_interface> instance_ptr;37 class settings_handler_impl : public settings_core {38 private:39 //typedef std::map<settings_core::settings_type,settings_interface*> instance_list;40 //typedef std::list<settings_interface*> instance_list;41 instance_ptr instance_;42 //instance_list instances_;43 boost::timed_mutex mutexHandler_;44 /*45 struct key_description : public settings_core::key_description {46 std::wstring title;47 std::wstring description;48 settings_core::key_type type;49 std::wstring defValue;50 bool advanced;51 key_description(std::wstring title_, std::wstring description_, settings_core::key_type type_, std::wstring defValue_, bool advanced_)52 : title(title_), description(description_), type(type_), defValue(defValue_), advanced(advanced_) {}53 key_description() : advanced(false), type(settings_core::key_string) {}54 };55 */56 class dummy_logger : public logger_interface {57 void err(std::wstring file, int line, std::wstring message) {}58 void warn(std::wstring file, int line, std::wstring message) {}59 void info(std::wstring file, int line, std::wstring message) {}60 void debug(std::wstring file, int line, std::wstring message) {}61 };62 typedef std::map<std::wstring, std::wstring> path_map;63 //path_map path_mappings_;64 //path_map reversed_path_mappings_;65 boost::filesystem::wpath base_path_;66 logger_interface *logger_;67 typedef std::map<std::wstring,settings_core::path_description> reg_paths_type;68 reg_paths_type registred_paths_;69 typedef std::map<key_path_type,key_path_type> mapped_paths_type;70 //mapped_paths_type mapped_paths_;71 //mapped_paths_type reversed_mapped_paths_;72 typedef settings_interface::string_list string_list;73 public:74 settings_handler_impl() : logger_(new dummy_logger()) {}75 ~settings_handler_impl() {76 destroy_all_instances();77 set_logger(NULL);78 }79 80 //////////////////////////////////////////////////////////////////////////81 /// Set the basepath for the settings subsystem.82 /// In other words set where the settings files reside83 ///84 /// @param path the path to the settings files85 ///86 /// @author mickem87 void set_base(boost::filesystem::wpath path) {88 base_path_ = path;89 }90 91 //////////////////////////////////////////////////////////////////////////92 /// Set the logging interface (will receive log messages)93 ///94 /// @param logger the new logger to use95 ///96 /// @author mickem97 void set_logger(logger_interface *logger) {98 logger_interface *old_logger = logger_;99 logger_ = logger;100 delete old_logger;101 }102 103 //////////////////////////////////////////////////////////////////////////104 /// Get the logging interface (will receive log messages)105 ///106 /// @return the logger to use107 ///108 /// @author mickem109 logger_interface* get_logger() {110 if (!logger_)111 throw settings_exception(_T("Failed to log message, no logger defined!"));112 return logger_;113 }114 115 //////////////////////////////////////////////////////////////////////////116 /// Get the basepath for the settings subsystem.117 /// In other words get where the settings files reside118 ///119 /// @return the path to the settings files120 ///121 /// @author mickem122 boost::filesystem::wpath get_base() {123 return base_path_;124 }125 126 127 instance_ptr get() {128 boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));129 if (!mutex.owns_lock())130 throw settings_exception(_T("Failed to get mutext, cant get settings instance"));131 if (!instance_)132 throw settings_exception(_T("Failed initialize settings instance"));133 return instance_;134 }135 //////////////////////////////////////////////////////////////////////////136 /// Overwrite the (current) settings store with default values.137 ///138 /// @author mickem139 void update_defaults() {140 get_logger()->warn(__FILEW__, __LINE__, _T("Updating settings with default values!"));141 BOOST_FOREACH(std::wstring path, get_reg_sections()) {142 get()->add_path(path);143 BOOST_FOREACH(std::wstring key, get_reg_keys(path)) {144 settings_core::key_description desc = get_registred_key(path, key);145 if (!desc.advanced) {146 if (!get()->has_key(path, key)) {147 get_logger()->debug(__FILEW__, __LINE__, _T("Adding: ") + path + _T(".") + key);148 if (desc.type == key_string)149 get()->set_string(path, key, desc.defValue);150 else if (desc.type == key_bool)151 get()->set_bool(path, key, desc.defValue==_T("true"));152 else if (desc.type == key_integer)153 get()->set_int(path, key, strEx::stoi(desc.defValue));154 else155 get_logger()->err(__FILEW__, __LINE__, _T("Unknown keytype for: ") + path + _T(".") + key);156 } else {157 std::wstring val = get()->get_string(path, key);158 get_logger()->debug(__FILEW__, __LINE__, _T("Setting old (already exists): ") + path + _T(".") + key + _T(" = ") + val);159 if (desc.type == key_string)160 get()->set_string(path, key, val);161 else if (desc.type == key_bool)162 get()->set_bool(path, key, val==_T("true"));163 else if (desc.type == key_integer)164 get()->set_int(path, key, strEx::stoi(val));165 else166 get_logger()->err(__FILEW__, __LINE__, _T("Unknown keytype for: ") + path + _T(".") + key);167 }168 } else {169 get_logger()->debug(__FILEW__, __LINE__, _T("Skipping (advanced): ") + path + _T(".") + key);170 }171 }172 }173 get_logger()->info(__FILEW__, __LINE__, _T("DONE Updating settings with default values!"));174 }175 void migrate(instance_ptr from, instance_ptr to) {176 if (!from || !to)177 throw new settings_exception(_T("Source or target is null"));178 {179 #ifdef _DEBUG180 get_logger()->debug(__FILEW__, __LINE__, _T("Starting to migrate..."));181 #endif182 boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));183 if (!mutex.owns_lock())184 throw settings_exception(_T("migrate_type: Failed to get mutex, cant get settings instance"));185 from->save_to(to);186 #ifdef _DEBUG187 get_logger()->debug(__FILEW__, __LINE__, _T("Done migrating..."));188 #endif189 }190 }191 void migrate_to(instance_ptr to) {192 migrate(instance_, to);193 }194 void migrate_from(instance_ptr from) {195 migrate(from, instance_);196 }197 void migrate_to(std::wstring to) {198 instance_ptr i = create_instance(to);199 migrate_to(i);200 }201 void migrate_from(std::wstring from) {202 instance_ptr i = create_instance(from);203 migrate_from(i);204 }205 void migrate(std::wstring from, std::wstring to) {206 instance_ptr ifrom = create_instance(from);207 instance_ptr ito = create_instance(to);208 migrate(ifrom, ito);209 }210 211 // settings_core::settings_type get_settings_type() {212 // boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));213 // if (!mutex.owns_lock())214 // throw settings_exception(_T("Failed to get mutext, cant get load settings"));215 // if (instance_ == NULL)216 // throw settings_exception(_T("No settings subsystem selected"));217 // return instance_->get_type();218 // }219 // std::wstring get_settings_type_desc() {220 // return get_type_desc(get_settings_type());221 // }222 // std::wstring get_type_desc(settings_core::settings_type type) {223 // if (type == settings_core::ini_file)224 // return _T(".INI file (nsc.ini)");225 // if (type == settings_core::registry)226 // return _T("registry");227 // if (type == settings_core::xml_file)228 // return _T(".XML file (nsc.xml)");229 // return _T("Unknown settings type");230 // }231 232 //////////////////////////////////////////////////////////////////////////233 /// Resolve a path or key to any potential mappings.234 ///235 /// @param path the path to resolve236 /// @return the resolved new key237 ///238 /// @author mickem239 /*240 std::wstring map_path(std::wstring path) {241 MutexLock mutex(mutexHandler_);242 if (!mutex.hasMutex())243 throw settings_exception(_T("map_path Failed to get mutext, cant get access settings"));244 path_map::const_iterator cit = path_mappings_.find(path);245 if (cit == path_mappings_.end())246 return path;247 return (*cit).second;248 }249 */250 251 //////////////////////////////////////////////////////////////////////////252 /// Resolve a path or key to any potential mappings.253 ///254 /// @param path the path to resolve255 /// @param key the key to resolve256 /// @return the resolved new path and key257 ///258 /// @author mickem259 /*260 key_path_type map_key(key_path_type key) {261 MutexLock mutex(mutexHandler_);262 if (!mutex.hasMutex())263 throw settings_exception(_T("map_path Failed to get mutext, cant get access settings"));264 mapped_paths_type::const_iterator cit = mapped_paths_.find(key);265 if (cit != mapped_paths_.end())266 return (*cit).second;267 key.first = map_path(key.first);268 return key;269 }270 */271 //////////////////////////////////////////////////////////////////////////272 /// Find all mapped keys given a path273 ///274 /// @param path the path to resolve275 /// @return A list of resolved keys276 ///277 /// @author mickem278 /*279 mapped_key_list_type find_maped_keys(std::wstring path) {280 mapped_key_list_type ret;281 MutexLock mutex(mutexHandler_);282 if (!mutex.hasMutex())283 throw settings_exception(_T("find_maped_keys Failed to get mutext, cant get access settings"));284 for (mapped_paths_type::const_iterator cit = mapped_paths_.begin(); cit != mapped_paths_.end();++cit) {285 if ((*cit).first.first == path) {286 ret.push_back(mapped_key((*cit).first, (*cit).second));287 }288 }289 return ret;290 }291 */292 293 //////////////////////////////////////////////////////////////////////////294 /// Reverse resolve a path or key to any potential mappings.295 ///296 /// @param path the path to resolve297 /// @return the resolved new key298 ///299 /// @author mickem300 /*301 virtual std::wstring reverse_map_path(std::wstring path) {302 MutexLock mutex(mutexHandler_);303 if (!mutex.hasMutex())304 throw settings_exception(_T("map_path Failed to get mutext, cant get access settings"));305 path_map::const_iterator cit = reversed_path_mappings_.find(path);306 if (cit == reversed_path_mappings_.end())307 return path;308 return (*cit).second;309 }310 inline void int_add_mapped(std::wstring::size_type &plen, const std::wstring &path, const std::wstring &string, string_list &list) {311 if (string.length() > plen && string.substr(0,plen) == path) {312 if (path[plen] == '/')313 plen++;314 std::wstring rest = string.substr(plen);315 std::wstring::size_type pos = rest.find('/');316 if (pos != std::wstring::npos)317 rest = rest.substr(pos);318 list.push_back(rest);319 }320 }321 inline void int_add_mapped_2(const std::wstring &string, string_list &list) {322 if (string.length() > 1) {323 std::wstring::size_type pos = string.find('/');324 if (pos != std::wstring::npos)325 list.push_back(string);326 else327 list.push_back(string.substr(0, pos));328 }329 }330 331 332 //////////////////////////////////////////////////////////////////////////333 /// Get all mapped sections.334 ///335 /// @param path path to get mapped ssections from336 /// @return337 ///338 /// @author mickem339 virtual string_list get_mapped_sections(std::wstring path) {340 string_list ret;341 if (path.empty() || path == _T("//")) {342 for (mapped_paths_type::const_iterator cit = mapped_paths_.begin(); cit != mapped_paths_.end(); ++cit) {343 int_add_mapped_2((*cit).first.first, ret);344 }345 for (path_map::const_iterator cit = path_mappings_.begin(); cit != path_mappings_.end(); ++cit) {346 int_add_mapped_2((*cit).first, ret);347 }348 } else {349 std::wstring::size_type plen = path.length();350 for (mapped_paths_type::const_iterator cit = mapped_paths_.begin(); cit != mapped_paths_.end(); ++cit) {351 int_add_mapped(plen, path, (*cit).first.first, ret);352 }353 for (path_map::const_iterator cit = path_mappings_.begin(); cit != path_mappings_.end(); ++cit) {354 int_add_mapped(plen, path, (*cit).first, ret);355 }356 }357 ret.sort();358 ret.unique();359 return ret;360 }361 362 //////////////////////////////////////////////////////////////////////////363 /// Reverse resolve a path or key to any potential mappings.364 ///365 /// @param key the key to resolve366 /// @return the resolved new path and key367 ///368 /// @author mickem369 virtual key_path_type reverse_map_key(key_path_type key) {370 MutexLock mutex(mutexHandler_);371 if (!mutex.hasMutex())372 throw settings_exception(_T("map_path Failed to get mutext, cant get access settings"));373 mapped_paths_type::const_iterator cit = reversed_mapped_paths_.find(key);374 if (cit == reversed_mapped_paths_.end())375 return key;376 return (*cit).second;377 }378 379 380 381 382 383 //////////////////////////////////////////////////////////////////////////384 /// Map a path/key to another path/key.385 /// ie add_mapping("hej","san","foo","bar"); means looking up hej/san will get you the value from foo/bar386 ///387 /// @param source_path The path to map (look here)388 /// @param source_key The node to map389 /// @param destination_path The path to bind to (to end up here)390 /// @param destination_key The key to bind to391 ///392 /// @author mickem393 void add_mapping(std::wstring source_path, std::wstring source_key, std::wstring destination_path, std::wstring destination_key) {394 key_path_type src(source_path, source_key);395 key_path_type dst(destination_path, destination_key);396 MutexLock mutex(mutexHandler_);397 if (!mutex.hasMutex())398 throw settings_exception(_T("add_mapping Failed to get mutext, cant get access settings"));399 mapped_paths_[src] = dst;400 mapped_paths_type::iterator cit = reversed_mapped_paths_.find(dst);401 if (cit == reversed_mapped_paths_.end())402 reversed_mapped_paths_[dst] = src;403 else404 reversed_mapped_paths_.erase(cit);405 }406 407 //////////////////////////////////////////////////////////////////////////408 ///Map a path to another path.409 ///ie add_mapping("hej","foo"); means looking up hej/xxx will get you the value from foo/xxx410 ///411 ///@param source_path The path to map (look here)412 ///@param destination_path The path to bind to (to end up here)413 ///414 ///@author mickem415 void add_mapping(std::wstring source_path, std::wstring destination_path) {416 MutexLock mutex(mutexHandler_);417 if (!mutex.hasMutex())418 throw settings_exception(_T("add_mapping Failed to get mutext, cant get access settings"));419 path_mappings_[source_path] = destination_path;420 path_map::iterator cit = reversed_path_mappings_.find(destination_path);421 if (cit == reversed_path_mappings_.end())422 reversed_path_mappings_[destination_path] = source_path;423 else424 reversed_path_mappings_.erase(cit);425 }426 */427 428 429 430 //////////////////////////////////////////////////////////////////////////431 /// Register a path with the settings module.432 /// A registered key or path will be nicely documented in some of the settings files when converted.433 ///434 /// @param path The path to register435 /// @param type The type of value436 /// @param title The title to use437 /// @param description the description to use438 /// @param defValue the default value439 /// @param advanced advanced options will only be included if they are changed440 ///441 /// @author mickem442 void register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced = false) {443 reg_paths_type::iterator it = registred_paths_.find(path);444 if (it == registred_paths_.end()) {445 registred_paths_[path] = path_description(title, description, advanced);446 } else {447 (*it).second.update(title, description, advanced);448 }449 }450 451 //////////////////////////////////////////////////////////////////////////452 /// Register a key with the settings module.453 /// A registered key or path will be nicely documented in some of the settings files when converted.454 ///455 /// @param path The path to register456 /// @param key The key to register457 /// @param title The title to use458 /// @param description the description to use459 /// @param defValue the default value460 /// @param advanced advanced options will only be included if they are changed461 ///462 /// @author mickem463 void register_key(std::wstring path, std::wstring key, settings_core::key_type type, std::wstring title, std::wstring description, std::wstring defValue, bool advanced = false) {464 reg_paths_type::iterator it = registred_paths_.find(path);465 if (it == registred_paths_.end()) {466 registred_paths_[path] = path_description();467 registred_paths_[path].keys[key] = key_description(title, description, type, defValue, advanced);468 } else {469 (*it).second.keys[key] = key_description(title, description, type, defValue, advanced);470 }471 }472 //////////////////////////////////////////////////////////////////////////473 /// Get info about a registered key.474 /// Used when writing settings files.475 ///476 /// @param path The path of the key477 /// @param key The key of the key478 /// @return the key description479 ///480 /// @author mickem481 settings_core::key_description get_registred_key(std::wstring path, std::wstring key) {482 reg_paths_type::const_iterator cit = registred_paths_.find(path);483 if (cit != registred_paths_.end()) {484 path_description::keys_type::const_iterator cit2 = (*cit).second.keys.find(key);485 if (cit2 != (*cit).second.keys.end()) {486 settings_core::key_description ret = (*cit2).second;487 return ret;488 }489 }490 throw KeyNotFoundException(path, key);491 }492 settings_core::path_description get_registred_path(std::wstring path) {493 reg_paths_type::const_iterator cit = registred_paths_.find(path);494 if (cit != registred_paths_.end()) {495 return (*cit).second;496 }497 throw KeyNotFoundException(path);498 }499 500 501 502 //////////////////////////////////////////////////////////////////////////503 /// Get all registered sections504 ///505 /// @return a list of section paths506 ///507 /// @author mickem508 string_list get_reg_sections() {509 string_list ret;510 for (reg_paths_type::const_iterator cit = registred_paths_.begin(); cit != registred_paths_.end(); ++cit) {511 ret.push_back((*cit).first);512 }513 return ret;514 }515 //////////////////////////////////////////////////////////////////////////516 /// Get all keys for a registered section.517 ///518 /// @param path the path to find keys under519 /// @return a list of key names520 ///521 /// @author mickem522 virtual string_list get_reg_keys(std::wstring path) {523 string_list ret;524 reg_paths_type::const_iterator cit = registred_paths_.find(path);525 if (cit != registred_paths_.end()) {526 for (path_description::keys_type::const_iterator cit2 = (*cit).second.keys.begin();cit2 != (*cit).second.keys.end(); ++cit2) {527 ret.push_back((*cit2).first);528 }529 return ret;530 }531 throw KeyNotFoundException(path, _T(""));532 }533 534 535 void set_instance(std::wstring key) {536 boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));537 if (!mutex.owns_lock())538 throw settings_exception(_T("set_instance Failed to get mutext, cant get access settings"));539 instance_ = create_instance(key);540 if (!instance_)541 throw settings_exception(_T("set_instance Failed to create instance for: ") + key);542 // instance_list::iterator it = instances_.find(instance->get_type());543 // if (it == instances_.end())544 // instances_[instance->get_type()] = instance;545 // else {546 // settings_interface *old = (*it).second;547 // instances_[instance->get_type()] = instance;548 // delete old;549 // }550 instance_->set_core(this);551 }552 553 // void add_instance(settings_interface *instance) {554 // boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));555 // if (!mutex.owns_lock())556 // throw settings_exception(_T("load_all_instance Failed to get mutext, cant get access settings"));557 // instance_list::iterator it = instances_.find(instance->get_type());558 // if (it == instances_.end())559 // instances_[instance->get_type()] = instance;560 // else {561 // settings_interface *old = (*it).second;562 // instances_[instance->get_type()] = instance;563 // delete old;564 // }565 // instance->set_core(this);566 // }567 568 // void copy_type(settings_core::settings_type from, settings_core::settings_type to) {569 // throw settings_exception(_T("copy_type not implemented"));570 // }571 572 573 574 private:575 void destroy_all_instances() {576 boost::unique_lock<boost::timed_mutex> mutex(mutexHandler_, boost::get_system_time() + boost::posix_time::seconds(5));577 if (!mutex.owns_lock())578 throw settings_exception(_T("destroy_all_instances Failed to get mutext, cant get access settings"));579 instance_.reset();580 // for (instance_list::iterator it = instances_.begin(); it != instances_.end(); ++it) {581 // settings_interface *tmp = (*it).second;582 // if (tmp != NULL)583 // tmp->set_core(NULL);584 // (*it).second = NULL;585 // delete tmp;586 // }587 // instances_.clear();588 }589 // settings_interface* instance_unsafe(settings_core::settings_type type) {590 // instance_list::const_iterator cit = instances_.find(type);591 // if (cit == instances_.end())592 // throw settings_exception(_T("Failed to find settings for: ") + get_type_desc(type));593 // return (*cit).second;594 // }595 596 597 virtual std::wstring to_string() {598 if (instance_)599 return instance_->to_string();600 return _T("<NULL>");601 }602 603 };604 //typedef Singleton<SettingsHandlerImpl> SettingsHandler;605 typedef settings_interface::string_list string_list;606 607 /*608 // Alias to make handling "compatible" with old syntax609 settings_interface* get_settings() {610 return SettingsHandler::getInstance()->get();611 }612 settings_interface* getInstance() {613 return SettingsHandler::getInstance()->get();614 }615 settings_core* get_core() {616 return SettingsHandler::getInstance();617 }618 void destroyInstance() {619 SettingsHandler::destroyInstance();620 }621 */622 623 class SettingsInterfaceImpl : public settings_interface {624 protected:625 settings_core *core_;626 typedef std::list<instance_ptr > parent_list_type;627 parent_list_type children_;628 public:629 static bool string_to_bool(std::wstring str) {630 return str == _T("true")||str == _T("1");631 }632 struct conainer {633 settings_core::key_type type;634 int int_val;635 std::wstring string_val;636 conainer(int value) : type(settings_core::key_integer), int_val(value) {}637 conainer(bool value) : type(settings_core::key_bool), int_val(value?1:0) {}638 conainer(std::wstring value) : type(settings_core::key_string), string_val(value) {}639 conainer() : type(settings_core::key_string) {}640 641 std::wstring get_string() const {642 if (type==settings_core::key_string)643 return string_val;644 if (type==settings_core::key_integer)645 return strEx::itos(int_val);646 if (type==settings_core::key_bool)647 return int_val==1?_T("true"):_T("false");648 return _T("UNKNOWN TYPE");649 }650 int get_int() const {651 if (type==settings_core::key_string)652 return strEx::stoi(string_val);653 if (type==settings_core::key_integer)654 return int_val;655 if (type==settings_core::key_bool)656 return int_val==1?1:0;657 return -1;658 }659 bool get_bool() const {660 if (type==settings_core::key_string)661 return string_to_bool(string_val);662 if (type==settings_core::key_integer)663 return int_val==1?true:false;664 if (type==settings_core::key_bool)665 return int_val==1?true:false;666 return false;667 }668 };669 typedef settings_core::key_path_type cache_key_type;670 typedef std::map<cache_key_type,conainer> cache_type;671 typedef std::set<std::wstring> path_cache_type;672 cache_type settings_cache_;673 path_cache_type path_cache_;674 std::wstring context_;675 net::url url_;676 677 //SettingsInterfaceImpl() : core_(NULL) {}678 SettingsInterfaceImpl(settings_core *core, std::wstring context) : core_(core), context_(context), url_(net::parse(context_)) {}679 680 //////////////////////////////////////////////////////////////////////////681 /// Empty all cached settings values and force a reload.682 /// Notice this does not save so anhy "active" values will be flushed and new ones read from file.683 ///684 /// @author mickem685 void clear_cache() {686 settings_cache_.clear();687 }688 689 //////////////////////////////////////////////////////////////////////////690 /// Set the core module to use691 ///692 /// @param core The core to use693 ///694 /// @author mickem695 virtual void set_core(settings_core *core) {696 core_ = core;697 }698 settings_core* get_core() const {699 if (core_ == NULL)700 throw settings_exception(_T("FATAL ERROR: Settings subsystem not initialized"));701 return core_;702 }703 704 void add_child(instance_ptr child) {705 children_.push_back(child);706 }707 void add_child(std::wstring context) {708 add_child(get_core()->create_instance(context));709 }710 711 //////////////////////////////////////////////////////////////////////////712 /// Get a string value if it does not exist exception will be thrown713 ///714 /// @param path the path to look up715 /// @param key the key to lookup716 /// @return the string value717 ///718 /// @author mickem719 virtual std::wstring get_string(std::wstring path, std::wstring key) {720 settings_core::key_path_type lookup(path,key);721 cache_type::const_iterator cit = settings_cache_.find(lookup);722 if (cit == settings_cache_.end()) {723 std::wstring val;724 try {725 val = get_real_string(lookup);726 } catch (KeyNotFoundException e) {727 val = get_string_from_child(lookup);728 }729 settings_cache_[lookup] = val;730 return val;731 }732 return (*cit).second.get_string();733 }734 std::wstring get_string_from_child(settings_core::key_path_type key) {735 for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {736 try {737 return (*it)->get_string(key.first, key.second);738 } catch (KeyNotFoundException e) {739 continue;740 }741 }742 throw KeyNotFoundException(key);743 }744 //////////////////////////////////////////////////////////////////////////745 /// Get a string value if it does not exist the default value will be returned746 ///747 /// @param path the path to look up748 /// @param key the key to lookup749 /// @param def the default value to use when no value is found750 /// @return the string value751 ///752 /// @author mickem753 virtual std::wstring get_string(std::wstring path, std::wstring key, std::wstring def) {754 try {755 return get_string(path, key);756 } catch (KeyNotFoundException e) {757 settings_cache_[cache_key_type(path,key)] = def;758 return def;759 }760 }761 //////////////////////////////////////////////////////////////////////////762 /// Set or update a string value763 ///764 /// @param path the path to look up765 /// @param key the key to lookup766 /// @param value the value to set767 ///768 /// @author mickem769 virtual void set_string(std::wstring path, std::wstring key, std::wstring value) {770 settings_cache_[cache_key_type(path,key)] = value;771 }772 773 virtual void add_path(std::wstring path) {774 path_cache_.insert(path);775 }776 777 //////////////////////////////////////////////////////////////////////////778 /// Get an integer value if it does not exist exception will be thrown779 ///780 /// @param path the path to look up781 /// @param key the key to lookup782 /// @return the string value783 ///784 /// @author mickem785 virtual int get_int(std::wstring path, std::wstring key) {786 settings_core::key_path_type lookup(path,key);787 cache_type::const_iterator cit = settings_cache_.find(lookup);788 if (cit == settings_cache_.end()) {789 int val;790 try {791 val = get_real_int(lookup);792 } catch (KeyNotFoundException e) {793 val = get_int_from_child(path, key);794 }795 settings_cache_[lookup] = val;796 return val;797 }798 return (*cit).second.get_int();799 }800 int get_int_from_child(std::wstring path, std::wstring key) {801 for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {802 try {803 return (*it)->get_int(path, key);804 } catch (KeyNotFoundException e) {805 continue;806 }807 }808 throw KeyNotFoundException(path, key);809 }810 //////////////////////////////////////////////////////////////////////////811 /// Get an integer value if it does not exist the default value will be returned812 ///813 /// @param path the path to look up814 /// @param key the key to lookup815 /// @param def the default value to use when no value is found816 /// @return the string value817 ///818 /// @author mickem819 virtual int get_int(std::wstring path, std::wstring key, int def) {820 try {821 return get_int(path, key);822 } catch (KeyNotFoundException e) {823 settings_cache_[cache_key_type(path,key)] = def;824 return def;825 }826 }827 //////////////////////////////////////////////////////////////////////////828 /// Set or update an integer value829 ///830 /// @param path the path to look up831 /// @param key the key to lookup832 /// @param value the value to set833 ///834 /// @author mickem835 virtual void set_int(std::wstring path, std::wstring key, int value) {836 settings_cache_[cache_key_type(path,key)] = value;837 }838 839 //////////////////////////////////////////////////////////////////////////840 /// Get the type of a key (String, int, bool)841 ///842 /// @param path the path to get type for843 /// @param key the key to get the type for844 /// @return the type of the key845 ///846 /// @author mickem847 virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) {848 throw settings_exception(_T("TODO: FIX ME: get_key_type"));849 }850 851 //////////////////////////////////////////////////////////////////////////852 /// Get a boolean value if it does not exist exception will be thrown853 ///854 /// @param path the path to look up855 /// @param key the key to lookup856 /// @return the string value857 ///858 /// @author mickem859 virtual bool get_bool(std::wstring path, std::wstring key) {860 settings_core::key_path_type lookup(path,key);861 cache_type::const_iterator cit = settings_cache_.find(lookup);862 if (cit == settings_cache_.end()) {863 bool val;864 try {865 val = get_real_bool(lookup);866 } catch (KeyNotFoundException e) {867 val = get_bool_from_child(path, key);868 }869 settings_cache_[lookup] = val;870 return val;871 }872 return (*cit).second.get_bool();873 }874 bool get_bool_from_child(std::wstring path, std::wstring key) {875 for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {876 try {877 return (*it)->get_bool(path, key);878 } catch (KeyNotFoundException e) {879 continue;880 }881 }882 throw KeyNotFoundException(path, key);883 }884 //////////////////////////////////////////////////////////////////////////885 /// Get a boolean value if it does not exist the default value will be returned886 ///887 /// @param path the path to look up888 /// @param key the key to lookup889 /// @param def the default value to use when no value is found890 /// @return the string value891 ///892 /// @author mickem893 virtual bool get_bool(std::wstring path, std::wstring key, bool def) {894 try {895 return get_bool(path, key);896 } catch (KeyNotFoundException e) {897 settings_cache_[cache_key_type(path,key)] = def;898 return def;899 }900 }901 //////////////////////////////////////////////////////////////////////////902 /// Set or update a boolean value903 ///904 /// @param path the path to look up905 /// @param key the key to lookup906 /// @param value the value to set907 ///908 /// @author mickem909 virtual void set_bool(std::wstring path, std::wstring key, bool value) {910 settings_cache_[cache_key_type(path,key)] = value;911 }912 913 // Meta Functions914 //////////////////////////////////////////////////////////////////////////915 /// Get all (sub) sections (given a path).916 /// If the path is empty all root sections will be returned917 ///918 /// @param path The path to get sections from (if empty root sections will be returned)919 /// @return a list of sections920 ///921 /// @author mickem922 virtual string_list get_sections(std::wstring path) {923 get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("Get sections for: ")) + path);924 string_list ret;925 get_real_sections(path, ret);926 get_section_from_child(path, ret);927 ret.sort();928 ret.unique();929 return ret;930 }931 void get_section_from_child(std::wstring path, string_list &list) {932 for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {933 string_list itm = (*it)->get_sections(path);934 list.insert(list.end(), itm.begin(), itm.end());935 }936 }937 //////////////////////////////////////////////////////////////////////////938 /// Get all keys for a path.939 ///940 /// @param path The path to get keys under941 /// @return a list of keys942 ///943 /// @author mickem944 virtual string_list get_keys(std::wstring path) {945 string_list ret;946 get_real_keys(path, ret);947 get_keys_from_child(path, ret);948 ret.sort();949 ret.unique();950 return ret;951 }952 void get_keys_from_child(std::wstring path, string_list &list) {953 for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {954 string_list itm = (*it)->get_keys(path);955 list.insert(list.end(), itm.begin(), itm.end());956 }957 }958 //////////////////////////////////////////////////////////////////////////959 /// Does the section exists?960 ///961 /// @param path The path of the section962 /// @return true/false963 ///964 /// @author mickem965 virtual bool has_section(std::wstring path) {966 throw settings_exception(_T("TODO: FIX ME: has_section"));967 }968 //////////////////////////////////////////////////////////////////////////969 /// Does the key exists?970 ///971 /// @param path The path of the section972 /// @param key The key to check973 /// @return true/false974 ///975 /// @author mickem976 virtual bool has_key(std::wstring path, std::wstring key) {977 settings_core::key_path_type lookup(path,key);978 cache_type::const_iterator cit = settings_cache_.find(lookup);979 if (cit != settings_cache_.end())980 return true;981 return has_real_key(lookup);982 }983 984 // Misc Functions985 //////////////////////////////////////////////////////////////////////////986 /// Get a context.987 /// The context is an identifier for the settings store for INI/XML it is the filename.988 ///989 /// @return the context990 ///991 /// @author mickem992 virtual std::wstring get_context() {993 return context_;994 }995 virtual std::wstring get_file_from_context() {996 return core_->find_file(url_.host + url_.path, DEFAULT_CONF_OLD_LOCATION);997 }998 //////////////////////////////////////////////////////////////////////////999 /// Set the context.1000 /// The context is an identifier for the settings store for INI/XML it is the filename.1001 ///1002 /// @param context the new context1003 ///1004 /// @author mickem1005 virtual void set_context(std::wstring context) {1006 context_ = context;1007 }1008 1009 // Save/Load Functions1010 //////////////////////////////////////////////////////////////////////////1011 /// Reload the settings store1012 ///1013 /// @author mickem1014 virtual void reload() {1015 throw settings_exception(_T("TODO: FIX ME: reload"));1016 }1017 //////////////////////////////////////////////////////////////////////////1018 /// Copy the settings store to another settings store1019 ///1020 /// @param other the settings store to save to1021 ///1022 /// @author mickem1023 virtual void save_to(std::wstring other) {1024 instance_ptr i = get_core()->create_instance(other);1025 save_to(i);1026 }1027 virtual void save_to(instance_ptr other) {1028 #ifdef _DEBUG1029 get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("Migrating settings...")));1030 #endif1031 if (other == NULL)1032 throw settings_exception(_T("Cant migrate to NULL instance!"));1033 other->clear_cache();1034 st_copy_section(_T(""), other);1035 other->save();1036 }1037 void st_copy_section(std::wstring path, instance_ptr other) {1038 #ifdef _DEBUG1039 get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("st_copy_section: ") + path));1040 #endif1041 if (other == NULL)1042 throw settings_exception(_T("Failed to create new instance!"));1043 string_list list = get_sections(path);1044 std::wstring subpath = path;1045 // TODO: check trailing / instead!1046 if (!subpath.empty())1047 subpath += _T("/");1048 for (string_list::const_iterator cit = list.begin();cit != list.end(); ++cit) {1049 st_copy_section(subpath + *cit, other);1050 }1051 list = get_keys(path);1052 for (string_list::const_iterator cit = list.begin();cit != list.end(); ++cit) {1053 settings_core::key_path_type key(path, *cit);1054 settings_core::key_type type = get_key_type(key.first, key.second);1055 if (type ==settings_core::key_string) {1056 try {1057 other->set_string(key.first, key.second, get_string(key.first, key.second));1058 } catch (KeyNotFoundException e) {1059 other->set_string(key.first, key.second, _T(""));1060 }1061 } else if (type ==settings_core::key_integer)1062 other->set_int(key.first, key.second, get_int(key.first, key.second));1063 else if (type ==settings_core::key_bool)1064 other->set_bool(key.first, key.second, get_bool(key.first, key.second));1065 else1066 throw settings_exception(_T("Invalid type for key: ") + key.first + _T(".") + key.second);1067 }1068 }1069 //////////////////////////////////////////////////////////////////////////1070 /// Save the settings store1071 ///1072 /// @author mickem1073 virtual void save() {1074 BOOST_FOREACH(std::wstring path, path_cache_) {1075 set_real_path(path);1076 }1077 std::set<std::wstring> sections;1078 for (cache_type::const_iterator cit = settings_cache_.begin(); cit != settings_cache_.end(); ++cit) {1079 set_real_value((*cit).first, (*cit).second);1080 sections.insert((*cit).first.first);1081 }1082 BOOST_FOREACH(std::wstring str, get_core()->get_reg_sections()) {1083 set_real_path(str);1084 }1085 }1086 /////////////////////////////////////////////////////////////////////////1087 /// Load from another settings store1088 ///1089 /// @param other the other settings store to load from1090 ///1091 /// @author mickem1092 virtual void load_from(instance_ptr other) {1093 throw settings_exception(_T("TODO: FIX ME: load_from"));1094 }1095 //////////////////////////////////////////////////////////////////////////1096 /// Load from another context.1097 /// The context is an identifier for the settings store for INI/XML it is the filename.1098 ///1099 /// @param context the context to load from1100 ///1101 /// @author mickem1102 virtual void load_from(std::wstring context) {1103 throw settings_exception(_T("TODO: FIX ME: load_from"));1104 }1105 //////////////////////////////////////////////////////////////////////////1106 /// Load settings from the context.1107 ///1108 /// @author mickem1109 virtual void load() {1110 throw settings_exception(_T("TODO: FIX ME: load"));1111 }1112 1113 //////////////////////////////////////////////////////////////////////////1114 /// VIRTUAL FUNCTIONS ////1115 //////////////////////////////////////////////////////////////////////////1116 1117 //////////////////////////////////////////////////////////////////////////1118 /// Get all (sub) sections (given a path).1119 /// If the path is empty all root sections will be returned1120 ///1121 /// @param path The path to get sections from (if empty root sections will be returned)1122 /// @param list The list to append nodes to1123 /// @return a list of sections1124 ///1125 /// @author mickem1126 virtual void get_real_sections(std::wstring path, string_list &list) = 0;1127 //////////////////////////////////////////////////////////////////////////1128 /// Get all keys given a path/section.1129 /// If the path is empty all root sections will be returned1130 ///1131 /// @param path The path to get sections from (if empty root sections will be returned)1132 /// @param list The list to append nodes to1133 /// @return a list of sections1134 ///1135 /// @author mickem1136 virtual void get_real_keys(std::wstring path, string_list &list) = 0;1137 1138 //////////////////////////////////////////////////////////////////////////1139 /// Get a string value if it does not exist exception will be thrown1140 ///1141 /// @param key the key to lookup1142 /// @return the string value1143 ///1144 /// @author mickem1145 virtual std::wstring get_real_string(settings_core::key_path_type key) = 0;1146 //////////////////////////////////////////////////////////////////////////1147 /// Get an integer value if it does not exist exception will be thrown1148 ///1149 /// @param key the key to lookup1150 /// @return the int value1151 ///1152 /// @author mickem1153 virtual int get_real_int(settings_core::key_path_type key) = 0;1154 //////////////////////////////////////////////////////////////////////////1155 /// Get a boolean value if it does not exist exception will be thrown1156 ///1157 /// @param key the key to lookup1158 /// @return the boolean value1159 ///1160 /// @author mickem1161 virtual bool get_real_bool(settings_core::key_path_type key) = 0;1162 1163 //////////////////////////////////////////////////////////////////////////1164 /// Write a value to the resulting context.1165 ///1166 /// @param key The key to write to1167 /// @param value The value to write1168 ///1169 /// @author mickem1170 virtual void set_real_value(settings_core::key_path_type key, conainer value) = 0;1171 1172 //////////////////////////////////////////////////////////////////////////1173 /// Write a value to the resulting context.1174 ///1175 /// @param key The key to write to1176 /// @param value The value to write1177 ///1178 /// @author mickem1179 virtual void set_real_path(std::wstring path) = 0;1180 1181 //////////////////////////////////////////////////////////////////////////1182 /// Check if a key exists1183 ///1184 /// @param key the key to lookup1185 /// @return true/false if the key exists.1186 ///1187 /// @author mickem1188 virtual bool has_real_key(settings_core::key_path_type key) = 0;1189 //////////////////////////////////////////////////////////////////////////1190 /// Get the type this settings store represent.1191 ///1192 /// @return the type of settings store1193 ///1194 /// @author mickem1195 // virtual settings_core::settings_type get_type() = 0;1196 //////////////////////////////////////////////////////////////////////////1197 /// Is this the active settings store1198 ///1199 /// @return1200 ///1201 /// @author mickem1202 // virtual bool is_active() = 0;1203 1204 //////////////////////////////////////////////////////////////////////////1205 /// Create a new settings interface of "this kind"1206 ///1207 /// @param context the context to use1208 /// @return the newly created settings interface1209 ///1210 /// @author mickem1211 virtual SettingsInterfaceImpl* create_new_context(std::wstring context) = 0;1212 1213 1214 virtual std::wstring to_string() {1215 std::wstring ret = get_info();1216 if (!children_.empty()) {1217 ret += _T("parents = [");1218 BOOST_FOREACH(parent_list_type::value_type i, children_) {1219 ret += i->to_string();1220 }1221 ret += _T("]");1222 }1223 return ret + _T("}");1224 }1225 };1226 }1227 typedef settings::settings_exception settings_exception;1228 -
include/settings/settings_ini.hpp
r497b779 r291548e 197 197 virtual void get_real_keys(std::wstring path, string_list &list) { 198 198 load_data(); 199 get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("Looking for: ")) + path);200 199 CSimpleIni::TNamesDepend lst; 201 200 ini.GetAllKeys(path.c_str(), lst); … … 214 213 throw_SI_error(rc, _T("Failed to save file")); 215 214 } 216 virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) { 217 return settings_core::key_string; 218 } 215 // virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) { 216 // return SettingsInterfaceImpl::get_key_type(path, key); 217 // return settings_core::key_string; 218 // } 219 219 private: 220 220 void load_data() { -
include/settings/settings_old.hpp
r497b779 r291548e 391 391 */ 392 392 } 393 virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) {394 return settings_core::key_string;395 }393 // virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) { 394 // return settings_core::key_string; 395 // } 396 396 private: 397 397 bool has_key(std::wstring section, std::wstring key) { -
include/settings/settings_registry.hpp
r497b779 r291548e 93 93 virtual void set_real_value(settings_core::key_path_type key, conainer value) { 94 94 if (value.type == settings_core::key_string) { 95 if (!setString_(get_reg_key(key), key.second, value.get_string())) 96 throw settings_exception(_T("Failed to write key: ") + key.first + _T(".") + key.second); 95 setString_(get_reg_key(key), key.second, value.get_string()); 97 96 } else if (value.type == settings_core::key_integer) { 98 if (!setInt_(get_reg_key(key), key.second, value.get_int())) 99 throw settings_exception(_T("Failed to write key: ") + key.first + _T(".") + key.second); 97 setInt_(get_reg_key(key), key.second, value.get_int()); 100 98 } else if (value.type == settings_core::key_bool) { 101 if (!setInt_(get_reg_key(key), key.second, value.get_bool()?1:0)) 102 throw settings_exception(_T("Failed to write key: ") + key.first + _T(".") + key.second); 99 setInt_(get_reg_key(key), key.second, value.get_bool()?1:0); 103 100 } else { 104 101 throw settings_exception(_T("Invalid settings type.")); … … 133 130 getValues_(get_reg_key(path), list); 134 131 } 135 virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) {136 return settings_core::key_string;137 }132 // virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) { 133 // return SettingsInterfaceImpl::get_key_type(path, key); 134 // } 138 135 private: 139 136 reg_key get_reg_key(settings_core::key_path_type key) { … … 150 147 return ret; 151 148 } 152 153 static bool setString_(reg_key path, std::wstring key, std::wstring value) { 154 HKEY hTemp; 155 if (RegCreateKeyEx(path.hKey, path.path.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hTemp, NULL) != ERROR_SUCCESS) { 156 return false; 157 } 149 struct reg_buffer { 150 wchar_t *buf; 151 reg_buffer(int len) : buf(new wchar_t[len]) {} 152 ~reg_buffer() { 153 delete [] buf; 154 } 155 TCHAR* operator* () { 156 return buf; 157 } 158 int str_len() { 159 return (wcslen(buf)+1)*sizeof(wchar_t); 160 } 161 }; 162 163 struct tmp_reg_key { 164 HKEY hTemp; 165 tmp_reg_key(HKEY hKey, std::wstring path) : hTemp(NULL) { 166 DWORD err = RegCreateKeyEx(hKey, path.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hTemp, NULL); 167 if (err != ERROR_SUCCESS) { 168 throw settings_exception(_T("Failed to open key: ") + path + _T(" (") + error::format::from_system(err) + _T(")")); 169 } 170 } 171 HKEY operator*() { 172 return hTemp; 173 } 174 ~tmp_reg_key() { 175 if (hTemp) 176 RegCloseKey(hTemp); 177 } 178 179 }; 180 181 static void setString_(reg_key path, std::wstring key, std::wstring value) { 182 tmp_reg_key hTemp(path.hKey, path.path); 158 183 DWORD bDataLen = value.length()+2; 159 TCHAR *bData = new TCHAR[bDataLen]; 160 wcsncpy_s(bData, bDataLen, value.c_str(), value.length()); 161 BOOL bRet = RegSetValueExW(hTemp, key.c_str(), 0, REG_EXPAND_SZ, reinterpret_cast<LPBYTE>(bData), (wcslen(bData)+1)*sizeof(TCHAR)); 162 RegCloseKey(hTemp); 163 delete [] bData; 164 return (bRet == ERROR_SUCCESS); 165 } 166 167 static bool setInt_(reg_key path, std::wstring key, DWORD value) { 168 HKEY hTemp; 169 if (RegCreateKeyEx(path.hKey, path.path.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hTemp, NULL) != ERROR_SUCCESS) { 170 return false; 171 } 172 BOOL bRet = RegSetValueEx(hTemp, key.c_str(), NULL, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(DWORD)); 173 RegCloseKey(hTemp); 174 return (bRet == ERROR_SUCCESS); 184 reg_buffer buffer(bDataLen); 185 wcsncpy_s(*buffer, bDataLen, value.c_str(), value.length()); 186 DWORD err = RegSetValueExW(*hTemp, key.c_str(), 0, REG_EXPAND_SZ, reinterpret_cast<LPBYTE>(*buffer), buffer.str_len()); 187 if (err != ERROR_SUCCESS) { 188 throw settings_exception(_T("Failed to write string: ") + path.to_string() + _T(".") + key + _T(" (") + error::format::from_system(err) + _T(")")); 189 } 190 } 191 192 static void setInt_(reg_key path, std::wstring key, DWORD value) { 193 tmp_reg_key hTemp(path.hKey, path.path); 194 DWORD err = RegSetValueEx(*hTemp, key.c_str(), NULL, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(DWORD)); 195 if (err != ERROR_SUCCESS) { 196 throw settings_exception(_T("Failed to int string: ") + path.to_string() + _T(".") + key + _T(" (") + error::format::from_system(err) + _T(")")); 197 } 175 198 } 176 199 -
include/strEx.h
rde7ec29 r291548e 865 865 return format_time_delta(static_cast<time_t>(filetime), format); 866 866 } 867 endif867 #endif 868 868 869 869 #ifdef _DEBUG -
modules/CheckEventLog/CheckEventLog.def
re26cfe0 r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/CheckHelpers/CheckHelpers.def
r2018659 r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/CheckSystem/CheckSystem.def
r294b37b r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/FileLogger/FileLogger.def
rcad08fb r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/NRPEClient/NRPEClient.def
rcad08fb r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/NRPEServer/NRPEServer.cpp
r497b779 r291548e 60 60 61 61 sh::settings_registry settings(nscapi::plugin_singleton->get_core()); 62 settings.set_alias( alias, _T("NRPE/server"));62 settings.set_alias(_T("NRPE"), alias, _T("server")); 63 63 64 64 settings.add_path_to_settings() … … 140 140 NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what()); 141 141 return false; 142 } catch (std::exception &e) { 143 NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 144 return false; 142 145 } catch (...) { 143 146 NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>")); -
modules/NSCAAgent/NSCAAgent.def
r40970de r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
modules/Scheduler/Scheduler.def
r2018659 r291548e 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion -
service/CMakeLists.txt
r497b779 r291548e 39 39 simple_client.hpp 40 40 settings_client.hpp 41 cli_parser.hpp 41 42 42 43 commands.hpp -
service/NSClient++.cpp
r497b779 r291548e 451 451 } 452 452 453 bool contains_plugin(NSClientT::plugin_alias_list_type &ret, std::wstring alias, std::wstring plugin) { 454 std::pair<std::wstring,std::wstring> v; 455 BOOST_FOREACH(v, ret.equal_range(alias)) { 456 if (v.second == plugin) 457 return true; 458 } 459 return false; 460 } 461 NSClientT::plugin_alias_list_type NSClientT::find_all_plugins(bool active) { 462 plugin_alias_list_type ret; 463 464 settings::string_list list = settings_manager::get_settings()->get_keys(MAIN_MODULES_SECTION); 465 BOOST_FOREACH(std::wstring key, list) { 466 std::wstring val = settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, key); 467 if (val.empty() || val == _T("enabled")) { 468 ret.insert(plugin_alias_list_type::value_type(_T(""), key)); 469 } else if (val == _T("disabled") && !active) { 470 ret.insert(plugin_alias_list_type::value_type(_T("disabled"), key)); 471 } else if (val == _T("disabled")) { 472 } else { 473 ret.insert(plugin_alias_list_type::value_type(key, val)); 474 } 475 } 476 if (!active) { 477 boost::filesystem::wpath pluginPath = expand_path(_T("${module-path}")); 478 boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 479 for ( boost::filesystem::wdirectory_iterator itr( pluginPath ); itr != end_itr; ++itr ) { 480 if ( !is_directory(itr->status()) ) { 481 boost::filesystem::wpath file= itr->leaf(); 482 if (is_module(pluginPath / file) && !contains_plugin(ret, _T(""), file.string())) 483 ret.insert(plugin_alias_list_type::value_type(_T(""), file.string())); 484 } 485 } 486 } 487 return ret; 488 } 489 453 490 NSClientT::plugin_info_list NSClientT::get_all_plugins() { 491 boost::filesystem::wpath pluginPath = expand_path(_T("${module-path}")); 492 plugin_alias_list_type plugins = find_all_plugins(false); 454 493 plugin_info_list ret; 455 boost::filesystem::wpath pluginPath = getBasePath() / boost::filesystem::wpath(_T("modules")); 456 boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 457 for ( boost::filesystem::wdirectory_iterator itr( pluginPath ); itr != end_itr; ++itr ) { 458 if ( !is_directory(itr->status()) ) { 459 boost::filesystem::wpath file= itr->leaf(); 460 if (is_module(pluginPath / file)) { 461 plugin_info_type info; 462 info.dll = itr->leaf(); 463 try { 464 std::wstring alias = settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, file.string()); 465 LOG_DEBUG_STD(_T("Attempting to fake load: ") + file.string() + _T(" as ") + alias); 466 NSCPlugin plugin(next_plugin_id_++, pluginPath / file, alias); 467 plugin.load_dll(); 468 plugin.load_plugin(NSCAPI::dontStart); 469 info.name = plugin.getName(); 470 info.description = plugin.getDescription(); 471 plugin.unload(); 472 } catch (NSPluginException e) { 473 LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 474 } catch (...) { 475 LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file.string()); 476 } 477 ret.push_back(info); 478 } 479 } 494 std::pair<std::wstring,std::wstring> v; 495 496 BOOST_FOREACH(v, plugins) { 497 plugin_info_type info; 498 info.dll = v.second; 499 try { 500 LOG_DEBUG_STD(_T("Attempting to fake load: ") + v.second + _T(" as ") + v.first); 501 NSCPlugin plugin(next_plugin_id_++, pluginPath / v.second, v.first); 502 plugin.load_dll(); 503 plugin.load_plugin(NSCAPI::dontStart); 504 info.name = plugin.getName(); 505 info.description = plugin.getDescription(); 506 plugin.unload(); 507 } catch (NSPluginException e) { 508 LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 509 } catch (...) { 510 LOG_CRITICAL_STD(_T("Unknown Error loading: ") + info.dll); 511 } 512 ret.push_back(info); 480 513 } 481 514 return ret; … … 484 517 485 518 void NSClientT::load_all_plugins(int mode) { 486 boost::filesystem::wpath modPath = getBasePath() / boost::filesystem::wpath(_T("modules")); 487 488 boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 489 for ( boost::filesystem::wdirectory_iterator itr( modPath ); itr != end_itr; ++itr ) { 490 if ( !is_directory(itr->status()) ) { 491 boost::filesystem::wpath file= itr->leaf(); 492 if (is_module(modPath / file)) { 493 if (settings_manager::get_settings()->has_key(MAIN_MODULES_SECTION, file.string())) { 494 std::wstring alias = settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, file.string()); 495 if (alias == _T("disabled")) { 496 try { 497 LOG_DEBUG_STD(_T("Attempting to fake load: ") + file.string()); 498 NSCPlugin plugin(next_plugin_id_++, modPath / file, alias); 499 plugin.load_dll(); 500 plugin.load_plugin(mode); 501 plugin.unload(); 502 } catch (NSPluginException e) { 503 LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 504 } catch (...) { 505 LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file.string()); 506 } 507 } 508 } else { 509 std::wstring desc; 510 std::wstring name = file.string(); 511 try { 512 NSCPlugin plugin(next_plugin_id_++, modPath / file, _T("")); 513 name = plugin.getModule(); 514 plugin.load_dll(); 515 plugin.load_plugin(mode); 516 desc = plugin.getName() + _T(" - "); 517 desc += plugin.getDescription(); 518 plugin.unload(); 519 } catch (NSPluginException e) { 520 desc += _T("unknown module"); 521 LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 522 } catch (...) { 523 desc += _T("unknown module"); 524 LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file.string()); 525 } 526 settings_manager::get_core()->register_key(MAIN_MODULES_SECTION, name, settings::settings_core::key_string, desc, desc, _T("disabled"), false); 527 } 528 } 529 } 519 boost::filesystem::wpath pluginPath = expand_path(_T("${module-path}")); 520 plugin_alias_list_type plugins = find_all_plugins(false); 521 std::pair<std::wstring,std::wstring> v; 522 523 BOOST_FOREACH(v, plugins) { 524 std::wstring desc; 525 std::wstring name = v.second; 526 try { 527 NSCPlugin plugin(next_plugin_id_++, pluginPath / v.second, v.first); 528 name = plugin.getModule(); 529 plugin.load_dll(); 530 plugin.load_plugin(mode); 531 desc = plugin.getName() + _T(" - "); 532 desc += plugin.getDescription(); 533 plugin.unload(); 534 } catch (NSPluginException e) { 535 desc += _T("unknown module"); 536 LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 537 } catch (...) { 538 desc += _T("unknown module"); 539 LOG_CRITICAL_STD(_T("Unknown Error loading: ") + name); 540 } 541 try { 542 if (v.first.empty() && !settings_manager::get_settings()->has_key(MAIN_MODULES_SECTION, v.second)) 543 settings_manager::get_core()->register_key(MAIN_MODULES_SECTION, name, settings::settings_core::key_string, desc, desc, _T("disabled"), false); 544 } catch (...) { 545 LOG_CRITICAL_STD(_T("Failed to register plugin key: ") + name); 546 } 530 547 } 531 548 } … … 554 571 bool NSClientT::initCore(bool boot) { 555 572 LOG_MESSAGE(_T("Attempting to start NSCLient++ - ") SZVERSION); 556 if (!settings_manager::init_settings( )) {573 if (!settings_manager::init_settings(context_)) { 557 574 return false; 558 575 } … … 563 580 settings_manager::get_settings()->set_int(_T("Settings"), _T("shared_Session"), 1); 564 581 enable_shared_session_ = SETTINGS_GET_BOOL_CORE(settings_def::SHARED_SESSION); 565 } catch (settings _exception e) {582 } catch (settings::settings_exception e) { 566 583 LOG_ERROR_CORE_STD(_T("Could not find settings: ") + e.getMessage()); 567 584 } … … 606 623 } 607 624 } 608 /*609 try {610 simpleSocket::WSAStartup();611 } catch (simpleSocket::SocketException e) {612 LOG_ERROR_STD(_T("Socket exception: ") + e.getMessage());613 return false;614 } catch (...) {615 LOG_ERROR_STD(_T("Unknown exception iniating socket..."));616 return false;617 }618 */619 625 #ifdef WIN32 620 626 try { … … 628 634 } 629 635 #endif 630 if (boot) { 631 try { 632 settings::string_list list = settings_manager::get_settings()->get_keys(MAIN_MODULES_SECTION); 633 for (settings::string_list::const_iterator cit = list.begin(); cit != list.end(); ++cit) { 634 std::wstring file = NSCPlugin::get_plugin_file(*cit); 635 std::wstring alias; 636 try { 637 alias = settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, *cit); 638 if (alias == _T("disabled")) { 639 LOG_DEBUG_STD(_T("Not booting: ") + file + _T(" since it is disabled.")); 640 continue; 641 } else if (alias == _T("enabled")) 642 alias = _T(""); 643 644 } catch (...) { 645 // If we except we load the plugin in as-is 646 } 647 LOG_DEBUG_STD(_T("Processing plugin: " + *cit) + _T(" in ") + file + _T(" as ") + alias); 648 try { 649 loadPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(file), alias); 650 } catch(const NSPluginException& e) { 651 LOG_ERROR_CORE_STD(_T("Exception raised: '") + e.error_ + _T("' in module: ") + e.file_); 652 //return false; 653 } catch (std::exception e) { 654 LOG_ERROR_CORE_STD(_T("exception loading plugin: ") + file + strEx::string_to_wstring(e.what())); 655 return false; 656 } catch (...) { 657 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugin: ") + file); 658 return false; 659 } 660 } 661 } catch (settings_exception e) { 662 LOG_ERROR_CORE_STD(_T("Failed to set settings file") + e.getMessage()); 663 } catch (...) { 664 LOG_ERROR_CORE_STD(_T("Unknown exception when loading plugins")); 665 return false; 666 } 667 try { 668 loadPlugins(boot?NSCAPI::normalStart:NSCAPI::dontStart); 669 } catch (...) { 670 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugins")); 671 return false; 672 } 673 LOG_DEBUG_STD(_T("NSCLient++ - ") SZVERSION _T(" Started!")); 674 } 675 LOG_MESSAGE_STD(_T("NSCLient++ - ") SZVERSION _T(" Started!")); 636 try { 637 boost::filesystem::wpath pluginPath = expand_path(_T("${module-path}")); 638 plugin_alias_list_type plugins = find_all_plugins(true); 639 std::pair<std::wstring,std::wstring> v; 640 BOOST_FOREACH(v, plugins) { 641 std::wstring file = NSCPlugin::get_plugin_file(v.second); 642 std::wstring alias = v.first; 643 LOG_DEBUG_STD(_T("Processing plugin: ") + file + _T(" as ") + alias); 644 try { 645 addPlugin(pluginPath / boost::filesystem::wpath(file), alias); 646 } catch(const NSPluginException& e) { 647 LOG_ERROR_CORE_STD(_T("Exception raised: '") + e.error_ + _T("' in module: ") + e.file_); 648 } catch (std::exception e) { 649 LOG_ERROR_CORE_STD(_T("exception loading plugin: ") + file + strEx::string_to_wstring(e.what())); 650 return false; 651 } catch (...) { 652 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugin: ") + file); 653 return false; 654 } 655 } 656 } catch (settings::settings_exception e) { 657 LOG_ERROR_CORE_STD(_T("Failed to set settings file") + e.getMessage()); 658 } catch (...) { 659 LOG_ERROR_CORE_STD(_T("Unknown exception when loading plugins")); 660 return false; 661 } 662 try { 663 loadPlugins(boot?NSCAPI::normalStart:NSCAPI::dontStart); 664 } catch (...) { 665 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugins")); 666 return false; 667 } 668 LOG_DEBUG_STD(_T("NSCLient++ - ") SZVERSION _T(" Started!")); 676 669 return true; 677 670 } … … 847 840 } 848 841 try { 849 plugin_type plugin = loadPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(module), _T(""));842 plugin_type plugin = addPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(module), _T("")); 850 843 LOG_DEBUG_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 851 844 plugin->load_plugin(NSCAPI::dontStart); … … 944 937 } 945 938 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 946 LOG_DEBUG_STD(_T(" * * * (FIX THIS)Loading plugin: ") + (*it)->getName() + _T("..."));939 LOG_DEBUG_STD(_T("Loading plugin: ") + (*it)->getName() + _T("...")); 947 940 try { 948 if (!(*it)->load_plugin( NSCAPI::normalStart)) {941 if (!(*it)->load_plugin(mode)) { 949 942 LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 950 943 it = plugins_.erase(it); … … 960 953 } 961 954 } 962 /*963 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) {964 LOG_DEBUG_STD(_T("Loading plugin: ") + (*it)->getName() + _T("..."));965 try {966 (*it)->load_plugin(mode);967 ++it;968 } catch(NSPluginException e) {969 it = plugins_.erase(it);970 LOG_ERROR_CORE_STD(_T("Exception raised when loading plugin: ") + e.error_ + _T(" in module: ") + e.file_ + _T(" plugin has been removed."));971 } catch(...) {972 it = plugins_.erase(it);973 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin plugin has been removed"));974 }975 }976 */977 955 plugins_loaded_ = true; 978 }979 /**980 * Load a single plug-in using a DLL filename981 * @param file The DLL file982 */983 NSClientT::plugin_type NSClientT::loadPlugin(const boost::filesystem::wpath file, std::wstring alias) {984 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias));985 return addPlugin(plugin);986 956 } 987 957 /** … … 989 959 * @param plugin The plug-in instance to load. The pointer is managed by the 990 960 */ 991 NSClientT::plugin_type NSClientT::addPlugin(plugin_type plugin) { 961 NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 962 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 992 963 plugin->load_dll(); 993 964 { … … 1026 997 try { 1027 998 len = settings_manager::get_settings()->get_int(SETTINGS_KEY(settings_def::PAYLOAD_LEN)); 1028 } catch (settings _exception &e) {999 } catch (settings::settings_exception &e) { 1029 1000 LOG_DEBUG_STD(_T("Failed to get length: ") + e.getMessage()); 1030 1001 return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; … … 1197 1168 debug_ = log_looking; 1198 1169 try { 1199 if (settings_manager::get_settings ()->get_int(_T("log"), _T("debug"), 0) == 1)1170 if (settings_manager::get_settings_no_wait()->get_bool(_T("log"), _T("debug"), false) == 1) 1200 1171 debug_ = log_debug; 1201 1172 else 1202 1173 debug_ = log_nodebug; 1203 } catch (settings_exception e) { 1204 return true; 1174 } catch (settings::settings_exception e) { 1175 debug_ = log_unknown; 1176 return false; 1205 1177 } 1206 1178 } else if (debug_ == log_looking) 1207 return true;1179 return false; 1208 1180 return (debug_ == log_debug); 1209 1181 } … … 1226 1198 void NSClientT::reportMessage(int msgType, const wchar_t* file, const int line, std::wstring message) { 1227 1199 try { 1200 if ((msgType == NSCAPI::debug)&&(!logDebug())) { 1201 return; 1202 } 1228 1203 strEx::replace(message, _T("\n"), _T(" ")); 1229 1204 strEx::replace(message, _T("\r"), _T(" ")); 1230 if ((msgType == NSCAPI::debug)&&(!logDebug())) {1231 return;1232 }1233 1205 // if (shared_server_.get() != NULL && shared_server_->hasClients()) { 1234 1206 // try { … … 1335 1307 try { 1336 1308 settings_manager::get_core()->set_base(basePath); 1337 } catch (settings _exception e) {1309 } catch (settings::settings_exception e) { 1338 1310 LOG_ERROR_CORE_STD(_T("Failed to set settings file: ") + e.getMessage()); 1339 1311 } catch (...) { … … 1398 1370 std::wstring NSClientT::expand_path(std::wstring file) { 1399 1371 strEx::replace(file, _T("${certificate-path}"), _T("${shared-path}/security")); 1372 strEx::replace(file, _T("${module-path}"), _T("${shared-path}/modules")); 1373 1400 1374 strEx::replace(file, _T("${base-path}"), getBasePath().string()); 1401 1375 #ifdef WIN32 -
service/NSClient++.h
r497b779 r291548e 112 112 typedef enum log_status {log_unknown, log_looking, log_debug, log_nodebug }; 113 113 log_status debug_; 114 std::wstring context_; 114 115 #ifdef WIN32 115 116 com_helper::initialize_com com_helper_; … … 128 129 129 130 public: 131 typedef std::multimap<std::wstring,std::wstring> plugin_alias_list_type; 130 132 // c-tor, d-tor 131 133 NSClientT(void) : debug_(log_unknown), plugins_loaded_(false), enable_shared_session_(false), commands_(this), channels_(this), next_plugin_id_(0) {} … … 141 143 bool initCore(bool boot); 142 144 bool exitCore(bool boot); 145 void set_settings_context(std::wstring context) { context_ = context; } 143 146 #ifdef WIN32x 144 147 static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); … … 174 177 int commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args); 175 178 176 plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias);179 //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias); 177 180 void loadPlugins(NSCAPI::moduleLoadMode mode); 178 181 void unloadPlugins(bool unloadLoggers); … … 187 190 void listPlugins(); 188 191 plugin_info_list get_all_plugins(); 192 plugin_alias_list_type find_all_plugins(bool active); 189 193 std::list<std::wstring> list_commands(); 190 194 … … 210 214 211 215 private: 212 plugin_type addPlugin( plugin_type plugin);216 plugin_type addPlugin(boost::filesystem::wpath file, std::wstring alias); 213 217 }; 214 218 -
service/cli_parser.hpp
r497b779 r291548e 34 34 ("migrate-to", po::value<std::wstring>(), "Migrate (copy) settings from current store to target store") 35 35 ("migrate-from", po::value<std::wstring>(), "Migrate (copy) settings from current store to target store") 36 ("generate", po::value<std::wstring>(), "(re)Generate settings store and add comments and such")36 ("generate", po::value<std::wstring>(), "(re)Generate a commented settings store or similar KEY can be trac, settings or the target store.") 37 37 ("add-defaults", "Add all default (if missing) values.") 38 ("path", po::value<std::wstring>()->default_value(_T("")), "Path of key to work with.") 39 ("key", po::value<std::wstring>()->default_value(_T("")), "Key to work with.") 40 ("set", po::value<std::wstring>(), "Set a key and path to a given value.") 41 ("show", "Set a value given a key and path.") 42 ("list", "Set all keys below the path (or root).") 38 43 ; 39 44 … … 84 89 return 1; 85 90 } catch (...) { 86 std::cerr << "Un able to parse command line" << std::endl;91 std::cerr << "Unhanded Exception" << std::endl; 87 92 return 1; 88 93 } 89 94 return 0; 90 95 } 91 96 … … 131 136 132 137 138 client.set_current(current); 139 client.set_update_defaults(def); 140 133 141 client.boot(); 134 142 143 int ret = -1; 144 135 145 if (vm.count("migrate-to")) { 136 std::wcout << _T("Migrating to: ") << vm["migrate-to"].as<std::wstring>() << std::endl; 137 client.migrate_to(current, vm["migrate-to"].as<std::wstring>(), def); 138 return 1; 146 ret = client.migrate_to(vm["migrate-to"].as<std::wstring>()); 139 147 } 140 148 if (vm.count("migrate-from")) { 141 std::wcout << _T("Migrating from: ") << vm["migrate-from"].as<std::wstring>() << std::endl; 142 client.migrate_from(vm["migrate-from"].as<std::wstring>(), current, def); 143 return 1; 149 ret = client.migrate_from(vm["migrate-from"].as<std::wstring>()); 144 150 } 151 if (vm.count("generate")) { 152 ret = client.generate(vm["generate"].as<std::wstring>()); 153 } 154 if (vm.count("set")) { 155 ret = client.set(vm["path"].as<std::wstring>(), vm["key"].as<std::wstring>(), vm["set"].as<std::wstring>()); 156 } 157 if (vm.count("list")) { 158 ret = client.list(vm["path"].as<std::wstring>()); 159 } 160 if (vm.count("show")) { 161 ret = client.show(vm["path"].as<std::wstring>(), vm["key"].as<std::wstring>()); 162 } 163 client.exit(); 164 165 return ret; 145 166 } catch(std::exception & e) { 146 167 std::cerr << "Unable to parse command line (settings): " << e.what() << std::endl; 147 168 return 1; 148 169 } 149 150 170 } 151 171 -
service/core_api.cpp
r497b779 r291548e 64 64 try { 65 65 return settings_manager::get_settings()->get_int(section, key, defaultValue); 66 } catch (settings_exception e) { 66 } catch (settings::settings_exception e) { 67 LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 68 return defaultValue; 69 } 70 } 71 int NSAPIGetSettingsBool(const wchar_t* section, const wchar_t* key, int defaultValue) { 72 try { 73 return settings_manager::get_settings()->get_bool(section, key, defaultValue==1); 74 } catch (settings::settings_exception e) { 67 75 LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 68 76 return defaultValue; … … 104 112 *bufLen = len; 105 113 return NSCAPI::isSuccess; 106 } catch (settings _exception e) {114 } catch (settings::settings_exception e) { 107 115 LOG_ERROR_STD(_T("Failed to get section: ") + e.getMessage()); 108 116 } catch (...) { … … 236 244 } 237 245 settings_manager::get_core()->migrate_to(inst); 238 } catch (settings _exception e) {246 } catch (settings::settings_exception e) { 239 247 LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage()); 240 248 return NSCAPI::hasFailed; … … 254 262 settings_manager::get_core()->migrate_from(inst); 255 263 settings_manager::get_settings()->reload(); 256 } catch (settings _exception e) {264 } catch (settings::settings_exception e) { 257 265 LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage()); 258 266 return NSCAPI::hasFailed; … … 302 310 settings_manager::get_core()->register_key(path, key, settings::settings_core::key_integer, title, description, defVal, advanced); 303 311 return NSCAPI::hasFailed; 304 } catch (settings _exception e) {312 } catch (settings::settings_exception e) { 305 313 LOG_ERROR_STD(_T("Failed register key: ") + e.getMessage()); 306 314 return NSCAPI::hasFailed; … … 316 324 try { 317 325 settings_manager::get_core()->register_path(path, title, description, advanced); 318 } catch (settings _exception e) {326 } catch (settings::settings_exception e) { 319 327 LOG_ERROR_STD(_T("Failed register path: ") + e.getMessage()); 320 328 return NSCAPI::hasFailed; … … 362 370 try { 363 371 settings_manager::get_settings()->save(); 364 } catch (settings _exception e) {372 } catch (settings::settings_exception e) { 365 373 LOG_ERROR_STD(_T("Failed to save: ") + e.getMessage()); 366 374 return NSCAPI::hasFailed; … … 387 395 if (wcscasecmp(buffer, _T("NSAPIGetSettingsInt")) == 0) 388 396 return reinterpret_cast<LPVOID>(&NSAPIGetSettingsInt); 397 if (wcscasecmp(buffer, _T("NSAPIGetSettingsBool")) == 0) 398 return reinterpret_cast<LPVOID>(&NSAPIGetSettingsBool); 389 399 if (wcscasecmp(buffer, _T("NSAPIMessage")) == 0) 390 400 return reinterpret_cast<LPVOID>(&NSAPIMessage); -
service/core_api.h
r497b779 r291548e 46 46 NSCAPI::errorReturn NSAPISetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* value); 47 47 NSCAPI::errorReturn NSAPISetSettingsInt(const wchar_t* section, const wchar_t* key, int value); 48 NSCAPI::errorReturn NSAPISetSettingsBool(const wchar_t* section, const wchar_t* key, int value); 48 49 NSCAPI::errorReturn NSAPIWriteSettings(const wchar_t* key); 49 50 NSCAPI::errorReturn NSAPIReadSettings(const wchar_t* key); -
service/settings_client.hpp
r497b779 r291548e 1 1 #pragma once 2 2 #include <settings/settings_core.hpp> 3 3 4 4 class NSClientT; … … 6 6 class settings_client { 7 7 NSClient* core_; 8 std::wstring current_; 9 bool default_; 10 11 8 12 public: 9 13 settings_client(NSClient* core) : core_(core) {} … … 14 18 15 19 void boot() { 16 if (!core_->initCore(true)) { 20 if (!current_.empty()) 21 core_->set_settings_context(current_); 22 if (!core_->initCore(false)) { 17 23 std::wcout << _T("Service *NOT* started!") << std::endl; 18 24 return; 19 25 } 26 if (default_) 27 settings_manager::get_core()->update_defaults(); 20 28 } 21 29 22 void migrate_from(std::wstring src, std::wstring target, bool def) { 30 void exit() { 31 core_->exitCore(true); 32 } 33 34 void set_current(std::wstring current) { current_ = current; } 35 void set_update_defaults(bool def) { default_ = def; } 36 37 int migrate_from(std::wstring src) { 23 38 try { 24 if (def) 25 settings_manager::get_core()->update_defaults(); 26 if (!src.empty() && !target.empty()) { 27 debug_msg(_T("Migrating ") + src + _T(" to ") + target); 28 settings_manager::get_core()->migrate(src, target); 29 } else { 30 debug_msg(_T("Migrating ") + src + _T(" to ") + target); 31 settings_manager::get_core()->migrate_from(src); 32 } 33 core_->exitCore(true); 34 } catch (settings_exception e) { 39 debug_msg(_T("Migrating from: ") + src); 40 settings_manager::get_core()->migrate_from(src); 41 return 1; 42 } catch (settings::settings_exception e) { 35 43 error_msg(_T("Failed to initialize settings: ") + e.getError()); 36 44 } catch (...) { 37 45 error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 38 46 } 47 return -1; 39 48 } 40 void migrate_to(std::wstring src, std::wstring target, bool def) {49 int migrate_to(std::wstring target) { 41 50 try { 42 if (def) 43 settings_manager::get_core()->update_defaults(); 44 if (!src.empty() && !target.empty()) { 45 debug_msg(_T("Migrating ") + src + _T(" to ") + target); 46 settings_manager::get_core()->migrate(src, target); 47 } else { 48 debug_msg(_T("Migrating ") + src + _T(" to ") + target); 49 settings_manager::get_core()->migrate_to(target); 50 } 51 core_->exitCore(true); 52 } catch (settings_exception e) { 51 debug_msg(_T("Migrating to: ") + target); 52 settings_manager::get_core()->migrate_to(target); 53 return 1; 54 } catch (settings::settings_exception e) { 53 55 error_msg(_T("Failed to initialize settings: ") + e.getError()); 54 56 } catch (...) { 55 57 error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 56 58 } 59 return -1; 57 60 } 58 61 59 void generate(std::wstring target) { 62 void dump_path(std::wstring root) { 63 BOOST_FOREACH(std::wstring path, settings_manager::get_core()->get()->get_sections(root)) { 64 if (!root.empty()) 65 dump_path(root + _T("/") + path); 66 else 67 dump_path(path); 68 } 69 BOOST_FOREACH(std::wstring key, settings_manager::get_core()->get()->get_keys(root)) { 70 std::wcout << root << _T(".") << key << _T("=") << settings_manager::get_core()->get()->get_string(root, key) << std::endl; 71 } 72 } 73 74 75 int generate(std::wstring target) { 60 76 try { 61 77 core_->load_all_plugins(NSCAPI::dontStart); 62 if (target == _T("default") || target.empty()) {63 settings_manager::get_core()->update_defaults();78 settings_manager::get_core()->update_defaults(); 79 if (target == _T("settings") || target.empty()) { 64 80 settings_manager::get_core()->get()->save(); 65 81 } else if (target == _T("trac")) { 66 82 settings::string_list s = settings_manager::get_core()->get_reg_sections(); 67 for (settings::string_list::const_iterator cit = s.begin(); cit != s.end(); ++cit) {68 std::wcout << _T("== ") << (*cit)<< _T(" ==") << std::endl;69 settings::string_list k = settings_manager::get_core()->get_reg_keys( *cit);83 BOOST_FOREACH(std::wstring path, s) { 84 std::wcout << _T("== ") << path << _T(" ==") << std::endl; 85 settings::string_list k = settings_manager::get_core()->get_reg_keys(path); 70 86 bool first = true; 71 for (settings::string_list::const_iterator citk = k.begin(); citk != k.end(); ++citk) {72 settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key( *cit, *citk);87 BOOST_FOREACH(std::wstring key, k) { 88 settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key); 73 89 if (!desc.advanced) { 74 90 if (first) 75 91 std::wcout << _T("'''Normal settings'''") << std::endl; 76 92 first = false; 77 std::wcout << _T("||") << (*citk)<< _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;93 std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl; 78 94 } 79 95 } 80 96 first = true; 81 for (settings::string_list::const_iterator citk = k.begin(); citk != k.end(); ++citk) {82 settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key( *cit, *citk);97 BOOST_FOREACH(std::wstring key, k) { 98 settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key); 83 99 if (desc.advanced) { 84 100 if (first) 85 101 std::wcout << _T("'''Advanced settings'''") << std::endl; 86 102 first = false; 87 std::wcout << _T("||") << (*citk)<< _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;103 std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl; 88 104 } 89 105 } … … 93 109 settings_manager::get_core()->get()->save_to(target); 94 110 } 95 } catch (settings_exception e) { 111 return 1; 112 } catch (settings::settings_exception e) { 96 113 error_msg(_T("Failed to initialize settings: ") + e.getError()); 97 114 } catch (...) { 98 115 error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 99 116 } 100 core_->exitCore(true);117 return -1; 101 118 } 119 120 121 int set(std::wstring path, std::wstring key, std::wstring val) { 122 core_->load_all_plugins(NSCAPI::dontStart); 123 settings::settings_core::key_type type = settings_manager::get_core()->get()->get_key_type(path, key); 124 if (type == settings::settings_core::key_string) { 125 settings_manager::get_core()->get()->set_string(path, key, val); 126 } else if (type == settings::settings_core::key_integer) { 127 settings_manager::get_core()->get()->set_int(path, key, strEx::stoi(val)); 128 } else if (type == settings::settings_core::key_bool) { 129 settings_manager::get_core()->get()->set_bool(path, key, settings::settings_interface::string_to_bool(val)); 130 } else { 131 error_msg(_T("Failed to set key (not found)")); 132 return -1; 133 } 134 settings_manager::get_core()->get()->save(); 135 return 0; 136 } 137 int show(std::wstring path, std::wstring key) { 138 //core_->load_all_plugins(NSCAPI::dontStart); 139 std::wcout << settings_manager::get_core()->get()->get_string(path, key); 140 return 0; 141 } 142 int list(std::wstring path) { 143 144 try { 145 dump_path(path); 146 } catch (settings::settings_exception e) { 147 error_msg(_T("Settings error: ") + e.getError()); 148 } catch (...) { 149 error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 150 } 151 152 return 0; 153 } 154 102 155 void error_msg(std::wstring msg) { 103 156 core_->reportMessage(NSCAPI::error, __FILEW__, __LINE__, msg.c_str()); -
service/settings_manager_impl.cpp
r497b779 r291548e 13 13 namespace settings_manager { 14 14 // Alias to make handling "compatible" with old syntax 15 boost::shared_ptr<settings::settings_interface>get_settings() {15 settings::instance_ptr get_settings() { 16 16 return SettingsHandler::getInstance()->get(); 17 } 18 settings::instance_ptr get_settings_no_wait() { 19 return SettingsHandler::getInstance()->get_no_wait(); 17 20 } 18 21 settings::settings_core* get_core() { … … 39 42 /// 40 43 /// @author mickem 41 settings::instance_ ptr NSCSettingsImpl::create_instance(std::wstring key) {44 settings::instance_raw_ptr NSCSettingsImpl::create_instance(std::wstring key) { 42 45 net::url url = net::parse(key); 43 get_logger()->debug(__FILEW__, __LINE__, _T("Trying to create: ") + url.protocol + _T(": ") + key);44 46 #ifdef WIN32 45 47 if (url.protocol == _T("old")) { 46 48 old_ = true; 47 return settings::instance_ ptr(new settings::OLDSettings(this, key));49 return settings::instance_raw_ptr(new settings::OLDSettings(this, key)); 48 50 } 49 51 if (url.protocol == _T("registry")) 50 return settings::instance_ ptr(new settings::REGSettings(this, key));52 return settings::instance_raw_ptr(new settings::REGSettings(this, key)); 51 53 #endif 52 54 if (url.protocol == _T("ini")) 53 return settings::instance_ ptr(new settings::INISettings(this, key));54 throw settings _exception(_T("Undefined settings protocol: ") + url.protocol);55 return settings::instance_raw_ptr(new settings::INISettings(this, key)); 56 throw settings::settings_exception(_T("Undefined settings protocol: ") + url.protocol); 55 57 } 56 58 … … 62 64 /// @author mickem 63 65 void NSCSettingsImpl::boot(std::wstring file) { 64 boot_ = get_base() / file; 65 std::wstring key = get_boot_string(_T("settings"), _T("location"), DEFAULT_CONF_LOCATION); 66 get_logger()->debug(__FILEW__, __LINE__, _T("Trying to boot: ") + key + _T(" from base: ") + boot_.string()); 66 std::wstring key = file; 67 if (file.empty()) { 68 boot_ = mainClient.expand_path(_T("${base-path}/boot.ini")); 69 key = get_boot_string(_T("settings"), _T("location"), DEFAULT_CONF_LOCATION); 70 } 67 71 set_instance(key); 68 72 } 69 73 70 bool init_settings( ) {74 bool init_settings(std::wstring context) { 71 75 try { 72 76 get_core()->set_logger(new settings_logger()); 73 77 get_core()->set_base(mainClient.expand_path(_T("${base-path}"))); 74 get_core()->boot( _T("boot.ini"));78 get_core()->boot(context); 75 79 get_core()->register_key(SETTINGS_REG_KEY_I_GEN(settings_def::PAYLOAD_LEN, settings::settings_core::key_integer)); 76 80 get_core()->register_key(SETTINGS_REG_KEY_S_GEN(protocol_def::ALLOWED_HOSTS, settings::settings_core::key_string)); … … 79 83 get_core()->register_key(SETTINGS_REG_KEY_S_GEN(protocol_def::PWD, settings::settings_core::key_string)); 80 84 get_core()->register_key(SETTINGS_REG_KEY_S_GEN(protocol_def::OBFUSCATED_PWD, settings::settings_core::key_string)); 81 LOG_CRITICAL_STD(_T("Loaded: ") + get_core()->to_string()); 82 83 } catch (settings_exception e) { 85 } catch (settings::settings_exception e) { 84 86 LOG_CRITICAL_STD(_T("Failed to initialize settings: ") + e.getError()); 85 87 return false; -
service/settings_manager_impl.h
r497b779 r291548e 40 40 std::wstring find_file(std::wstring file, std::wstring fallback = _T("")); 41 41 std::wstring expand_path(std::wstring file); 42 settings::instance_ ptr create_instance(std::wstring key);42 settings::instance_raw_ptr create_instance(std::wstring key); 43 43 }; 44 44 … … 47 47 // Alias to make handling "compatible" with old syntax 48 48 settings::instance_ptr get_settings(); 49 settings::instance_ptr get_settings_no_wait(); 49 50 settings::settings_core* get_core(); 50 51 void destroy_settings(); 51 bool init_settings( );52 bool init_settings(std::wstring context = _T("")); 52 53 }
Note: See TracChangeset
for help on using the changeset viewer.








