Changeset 283


Ignore:
Timestamp:
08/22/10 08:49:56 (18 months ago)
Author:
mickem
Message:

Tweaks to the settings subsystem (thread safeness) as well as additions to the command line client and such...

Files:
2 added
29 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/modules/CheckDisk/CheckDisk.def

    r32 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/include/NSCAPI.h

    r281 r283  
    124124    typedef NSCAPI::errorReturn (*lpNSAPIExpandPath)(const wchar_t*,wchar_t*,unsigned int); 
    125125    typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsInt)(const wchar_t*, const wchar_t*, int); 
     126    typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsBool)(const wchar_t*, const wchar_t*, int); 
    126127    typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsSection)(const wchar_t*, wchar_t***, unsigned int *); 
    127128    typedef NSCAPI::errorReturn (*lpNSAPIReleaseSettingsSectionBuffer)(wchar_t***, unsigned int *); 
  • trunk/include/nrpe/server/server.cpp

    r281 r283  
    5151      acceptor_.open(endpoint.protocol()); 
    5252      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()); 
    5354      acceptor_.bind(endpoint); 
    5455      if (info.back_log == connection_info::backlog_default) 
  • trunk/include/nscapi/nscapi_core_wrapper.cpp

    r281 r283  
    328328  return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); 
    329329} 
     330bool 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} 
    330335void 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) { 
    331336  if (!fNSAPISettingsRegKey) 
     
    558563  fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr")); 
    559564  fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt")); 
     565  fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool")); 
    560566  fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString")); 
    561567  fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection")); 
  • trunk/include/nscapi/nscapi_core_wrapper.hpp

    r281 r283  
    4444    nscapi::core_api::lpNSAPIExpandPath fNSAPIExpandPath; 
    4545    nscapi::core_api::lpNSAPIGetSettingsInt fNSAPIGetSettingsInt; 
     46    nscapi::core_api::lpNSAPIGetSettingsBool fNSAPIGetSettingsBool; 
    4647    nscapi::core_api::lpNSAPIMessage fNSAPIMessage; 
    4748    nscapi::core_api::lpNSAPIStopServer fNSAPIStopServer; 
     
    8990      , fNSAPIGetSettingsString(NULL) 
    9091      , fNSAPIGetSettingsInt(NULL) 
     92      , fNSAPIGetSettingsBool(NULL) 
    9193      , fNSAPIMessage(NULL) 
    9294      , fNSAPIStopServer(NULL) 
     
    124126    std::wstring expand_path(std::wstring value); 
    125127    int getSettingsInt(std::wstring section, std::wstring key, int defaultValue); 
     128    bool getSettingsBool(std::wstring section, std::wstring key, bool defaultValue); 
    126129    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); 
    127130    void settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced); 
  • trunk/include/nscapi/settings.hpp

    r282 r283  
    9191      } 
    9292      virtual void notify(nscapi::core_wrapper* core_, std::wstring path, std::wstring key) const { 
    93         T value = static_cast<T>(core_->getSettingsInt(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)); 
    9494        update_target(&value); 
    9595      } 
     
    405405        else 
    406406          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; 
    408415      } 
    409416 
     
    420427      void notify() { 
    421428        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          } 
    424435        } 
    425436        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          } 
    428443        } 
    429444 
  • trunk/include/settings/settings_core.hpp

    r281 r283  
    3434 
    3535namespace 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  }; 
    3666  class settings_exception { 
    3767    std::wstring error_; 
     
    104134 
    105135  class settings_interface; 
     136  typedef boost::shared_ptr<settings_interface> instance_ptr; 
     137  typedef boost::shared_ptr<settings_interface> instance_raw_ptr; 
    106138 
    107139  class settings_core { 
     
    207239    /// 
    208240    /// @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     
    210244    ////////////////////////////////////////////////////////////////////////// 
    211245    /// Get a settings interface 
     
    217251    //virtual settings_interface* get(settings_core::settings_type type) = 0; 
    218252    // 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; 
    222256    virtual void migrate(std::wstring from, std::wstring to) = 0; 
    223257    virtual void migrate_to(std::wstring to) = 0; 
     
    263297    /// 
    264298    /// @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; 
    266300 
    267301    ////////////////////////////////////////////////////////////////////////// 
     
    485519    /// 
    486520    /// @author mickem 
    487     virtual void save_to(boost::shared_ptr<settings_interface> other) = 0; 
     521    virtual void save_to(instance_ptr other) = 0; 
    488522    virtual void save_to(std::wstring other) = 0; 
    489523    ////////////////////////////////////////////////////////////////////////// 
     
    498532    /// 
    499533    /// @author mickem 
    500     virtual void load_from(boost::shared_ptr<settings_interface> other) = 0; 
     534    virtual void load_from(instance_ptr other) = 0; 
    501535    virtual void load_from(std::wstring other) = 0; 
    502536    ////////////////////////////////////////////////////////////////////////// 
     
    510544    virtual std::wstring get_info() = 0; 
    511545 
     546    static bool string_to_bool(std::wstring str) { 
     547      return str == _T("true")||str == _T("1"); 
     548    } 
     549 
     550 
    512551  }; 
    513552 
  • trunk/include/settings/settings_core_impl.hpp

    r281 r283  
    2121#pragma once 
    2222 
    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> 
    3425 
    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 reside 
    83     /// 
    84     /// @param path the path to the settings files 
    85     /// 
    86     /// @author mickem 
    87     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 use 
    95     /// 
    96     /// @author mickem 
    97     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 use 
    107     /// 
    108     /// @author mickem 
    109     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 reside 
    118     /// 
    119     /// @return the path to the settings files 
    120     /// 
    121     /// @author mickem 
    122     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 mickem 
    139     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               else 
    155                 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               else 
    166                 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 _DEBUG 
    180         get_logger()->debug(__FILEW__, __LINE__, _T("Starting to migrate...")); 
    181 #endif 
    182         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 _DEBUG 
    187         get_logger()->debug(__FILEW__, __LINE__, _T("Done migrating...")); 
    188 #endif 
    189       } 
    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 resolve 
    236     /// @return the resolved new key 
    237     /// 
    238     /// @author mickem 
    239     /* 
    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 resolve 
    255     /// @param key the key to resolve 
    256     /// @return the resolved new path and key 
    257     /// 
    258     /// @author mickem 
    259     /* 
    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 path 
    273     /// 
    274     /// @param path the path to resolve 
    275     /// @return A list of resolved keys 
    276     /// 
    277     /// @author mickem 
    278     /* 
    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 resolve 
    297     /// @return the resolved new key 
    298     /// 
    299     /// @author mickem 
    300     /* 
    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         else 
    327           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 from 
    336     /// @return 
    337     /// 
    338     /// @author mickem 
    339     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 resolve 
    366     /// @return the resolved new path and key 
    367     /// 
    368     /// @author mickem 
    369     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/bar 
    386     /// 
    387     /// @param source_path The path to map (look here) 
    388     /// @param source_key The node to map 
    389     /// @param destination_path The path to bind to (to end up here) 
    390     /// @param destination_key The key to bind to 
    391     /// 
    392     /// @author mickem 
    393     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       else 
    404         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/xxx 
    410     /// 
    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 mickem 
    415     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       else 
    424         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 register 
    435     /// @param type The type of value 
    436     /// @param title The title to use 
    437     /// @param description the description to use 
    438     /// @param defValue the default value 
    439     /// @param advanced advanced options will only be included if they are changed 
    440     /// 
    441     /// @author mickem 
    442     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 register 
    456     /// @param key The key to register 
    457     /// @param title The title to use 
    458     /// @param description the description to use 
    459     /// @param defValue the default value 
    460     /// @param advanced advanced options will only be included if they are changed 
    461     /// 
    462     /// @author mickem 
    463     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 key 
    477     /// @param key The key of the key 
    478     /// @return the key description 
    479     /// 
    480     /// @author mickem 
    481     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 sections 
    504     /// 
    505     /// @return a list of section paths 
    506     /// 
    507     /// @author mickem 
    508     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 under 
    519     /// @return a list of key names 
    520     /// 
    521     /// @author mickem 
    522     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 syntax 
    609   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 mickem 
    685     void clear_cache() { 
    686       settings_cache_.clear(); 
    687     } 
    688  
    689     ////////////////////////////////////////////////////////////////////////// 
    690     /// Set the core module to use 
    691     /// 
    692     /// @param core The core to use 
    693     /// 
    694     /// @author mickem 
    695     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 thrown 
    713     /// 
    714     /// @param path the path to look up 
    715     /// @param key the key to lookup 
    716     /// @return the string value 
    717     /// 
    718     /// @author mickem 
    719     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 returned 
    746     ///  
    747     /// @param path the path to look up 
    748     /// @param key the key to lookup 
    749     /// @param def the default value to use when no value is found 
    750     /// @return the string value 
    751     /// 
    752     /// @author mickem 
    753     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 value 
    763     /// 
    764     /// @param path the path to look up 
    765     /// @param key the key to lookup 
    766     /// @param value the value to set 
    767     /// 
    768     /// @author mickem 
    769     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 thrown 
    779     /// 
    780     /// @param path the path to look up 
    781     /// @param key the key to lookup 
    782     /// @return the string value 
    783     /// 
    784     /// @author mickem 
    785     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 returned 
    812     ///  
    813     /// @param path the path to look up 
    814     /// @param key the key to lookup 
    815     /// @param def the default value to use when no value is found 
    816     /// @return the string value 
    817     /// 
    818     /// @author mickem 
    819     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 value 
    829     /// 
    830     /// @param path the path to look up 
    831     /// @param key the key to lookup 
    832     /// @param value the value to set 
    833     /// 
    834     /// @author mickem 
    835     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 for 
    843     /// @param key the key to get the type for 
    844     /// @return the type of the key 
    845     /// 
    846     /// @author mickem 
    847     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 thrown 
    853     /// 
    854     /// @param path the path to look up 
    855     /// @param key the key to lookup 
    856     /// @return the string value 
    857     /// 
    858     /// @author mickem 
    859     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 returned 
    886     ///  
    887     /// @param path the path to look up 
    888     /// @param key the key to lookup 
    889     /// @param def the default value to use when no value is found 
    890     /// @return the string value 
    891     /// 
    892     /// @author mickem 
    893     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 value 
    903     /// 
    904     /// @param path the path to look up 
    905     /// @param key the key to lookup 
    906     /// @param value the value to set 
    907     /// 
    908     /// @author mickem 
    909     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 Functions 
    914     ////////////////////////////////////////////////////////////////////////// 
    915     /// Get all (sub) sections (given a path). 
    916     /// If the path is empty all root sections will be returned 
    917     /// 
    918     /// @param path The path to get sections from (if empty root sections will be returned) 
    919     /// @return a list of sections 
    920     /// 
    921     /// @author mickem 
    922     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 under 
    941     /// @return a list of keys 
    942     /// 
    943     /// @author mickem 
    944     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 section 
    962     /// @return true/false 
    963     /// 
    964     /// @author mickem 
    965     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 section 
    972     /// @param key The key to check 
    973     /// @return true/false 
    974     /// 
    975     /// @author mickem 
    976     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 Functions 
    985     ////////////////////////////////////////////////////////////////////////// 
    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 context 
    990     /// 
    991     /// @author mickem 
    992     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 context 
    1003     /// 
    1004     /// @author mickem 
    1005     virtual void set_context(std::wstring context) { 
    1006       context_ = context; 
    1007     } 
    1008  
    1009     // Save/Load Functions 
    1010     ////////////////////////////////////////////////////////////////////////// 
    1011     /// Reload the settings store 
    1012     /// 
    1013     /// @author mickem 
    1014     virtual void reload() { 
    1015       throw settings_exception(_T("TODO: FIX ME: reload")); 
    1016     } 
    1017     ////////////////////////////////////////////////////////////////////////// 
    1018     /// Copy the settings store to another settings store 
    1019     /// 
    1020     /// @param other the settings store to save to 
    1021     /// 
    1022     /// @author mickem 
    1023     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 _DEBUG 
    1029       get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("Migrating settings..."))); 
    1030 #endif 
    1031       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 _DEBUG 
    1039       get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("st_copy_section: ") + path)); 
    1040 #endif 
    1041       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         else 
    1066           throw settings_exception(_T("Invalid type for key: ") + key.first + _T(".") + key.second); 
    1067       } 
    1068     } 
    1069     ////////////////////////////////////////////////////////////////////////// 
    1070     /// Save the settings store 
    1071     /// 
    1072     /// @author mickem 
    1073     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 store 
    1088     /// 
    1089     /// @param other the other settings store to load from 
    1090     /// 
    1091     /// @author mickem 
    1092     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 from 
    1100     /// 
    1101     /// @author mickem 
    1102     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 mickem 
    1109     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 returned 
    1120     /// 
    1121     /// @param path The path to get sections from (if empty root sections will be returned) 
    1122     /// @param list The list to append nodes to 
    1123     /// @return a list of sections 
    1124     /// 
    1125     /// @author mickem 
    1126     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 returned 
    1130     /// 
    1131     /// @param path The path to get sections from (if empty root sections will be returned) 
    1132     /// @param list The list to append nodes to 
    1133     /// @return a list of sections 
    1134     /// 
    1135     /// @author mickem 
    1136     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 thrown 
    1140     /// 
    1141     /// @param key the key to lookup 
    1142     /// @return the string value 
    1143     /// 
    1144     /// @author mickem 
    1145     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 thrown 
    1148     /// 
    1149     /// @param key the key to lookup 
    1150     /// @return the int value 
    1151     /// 
    1152     /// @author mickem 
    1153     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 thrown 
    1156     /// 
    1157     /// @param key the key to lookup 
    1158     /// @return the boolean value 
    1159     /// 
    1160     /// @author mickem 
    1161     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 to 
    1167     /// @param value The value to write 
    1168     /// 
    1169     /// @author mickem 
    1170     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 to 
    1176     /// @param value The value to write 
    1177     /// 
    1178     /// @author mickem 
    1179     virtual void set_real_path(std::wstring path) = 0; 
    1180  
    1181     ////////////////////////////////////////////////////////////////////////// 
    1182     /// Check if a key exists 
    1183     /// 
    1184     /// @param key the key to lookup 
    1185     /// @return true/false if the key exists. 
    1186     /// 
    1187     /// @author mickem 
    1188     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 store 
    1193     /// 
    1194     /// @author mickem 
    1195 //    virtual settings_core::settings_type get_type() = 0; 
    1196     ////////////////////////////////////////////////////////////////////////// 
    1197     /// Is this the active settings store 
    1198     /// 
    1199     /// @return 
    1200     /// 
    1201     /// @author mickem 
    1202 //    virtual bool is_active() = 0; 
    1203  
    1204     ////////////////////////////////////////////////////////////////////////// 
    1205     /// Create a new settings interface of "this kind" 
    1206     /// 
    1207     /// @param context the context to use 
    1208     /// @return the newly created settings interface 
    1209     /// 
    1210     /// @author mickem 
    1211     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  
  • trunk/include/settings/settings_ini.hpp

    r281 r283  
    197197    virtual void get_real_keys(std::wstring path, string_list &list) { 
    198198      load_data(); 
    199       get_core()->get_logger()->debug(__FILEW__, __LINE__, std::wstring(_T("Looking for: ")) + path); 
    200199      CSimpleIni::TNamesDepend lst; 
    201200      ini.GetAllKeys(path.c_str(), lst); 
     
    214213        throw_SI_error(rc, _T("Failed to save file")); 
    215214    } 
    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//    } 
    219219  private: 
    220220    void load_data() { 
  • trunk/include/settings/settings_old.hpp

    r281 r283  
    391391      */ 
    392392    } 
    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//    } 
    396396  private: 
    397397    bool has_key(std::wstring section, std::wstring key) { 
  • trunk/include/settings/settings_registry.hpp

    r281 r283  
    9393  virtual void set_real_value(settings_core::key_path_type key, conainer value) { 
    9494    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()); 
    9796    } 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()); 
    10098    } 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); 
    103100    } else { 
    104101      throw settings_exception(_T("Invalid settings type.")); 
     
    133130    getValues_(get_reg_key(path), list); 
    134131  } 
    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// } 
    138135private: 
    139136  reg_key get_reg_key(settings_core::key_path_type key) { 
     
    150147    return ret; 
    151148  } 
    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); 
    158183    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    } 
    175198  } 
    176199 
  • trunk/include/strEx.h

    r282 r283  
    865865    return format_time_delta(static_cast<time_t>(filetime), format); 
    866866  } 
    867 endif 
     867#endif 
    868868 
    869869#ifdef _DEBUG 
  • trunk/modules/CheckEventLog/CheckEventLog.def

    r32 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/CheckHelpers/CheckHelpers.def

    r241 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/CheckSystem/CheckSystem.def

    r278 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/FileLogger/FileLogger.def

    r257 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/NRPEClient/NRPEClient.def

    r257 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/NRPEServer/NRPEServer.cpp

    r281 r283  
    6060 
    6161    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")); 
    6363 
    6464    settings.add_path_to_settings() 
     
    140140    NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what()); 
    141141    return false; 
     142  } catch (std::exception &e) { 
     143    NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 
     144    return false; 
    142145  } catch (...) { 
    143146    NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>")); 
  • trunk/modules/NSCAAgent/NSCAAgent.def

    r254 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/modules/Scheduler/Scheduler.def

    r241 r283  
    44  NSModuleHelperInit 
    55  NSLoadModule 
     6  NSLoadModuleEx 
    67  NSGetModuleName 
    78  NSGetModuleVersion 
  • trunk/service/CMakeLists.txt

    r281 r283  
    3939    simple_client.hpp 
    4040    settings_client.hpp 
     41    cli_parser.hpp 
    4142     
    4243    commands.hpp 
  • trunk/service/NSClient++.cpp

    r281 r283  
    451451} 
    452452 
     453bool 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} 
     461NSClientT::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 
    453490NSClientT::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); 
    454493  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); 
    480513  } 
    481514  return ret; 
     
    484517 
    485518void 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    } 
    530547  } 
    531548} 
     
    554571bool NSClientT::initCore(bool boot) { 
    555572  LOG_MESSAGE(_T("Attempting to start NSCLient++ - ") SZVERSION); 
    556   if (!settings_manager::init_settings()) { 
     573  if (!settings_manager::init_settings(context_)) { 
    557574    return false; 
    558575  } 
     
    563580      settings_manager::get_settings()->set_int(_T("Settings"), _T("shared_Session"), 1); 
    564581    enable_shared_session_ = SETTINGS_GET_BOOL_CORE(settings_def::SHARED_SESSION); 
    565   } catch (settings_exception e) { 
     582  } catch (settings::settings_exception e) { 
    566583    LOG_ERROR_CORE_STD(_T("Could not find settings: ") + e.getMessage()); 
    567584  } 
     
    606623    } 
    607624  } 
    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 */ 
    619625#ifdef WIN32 
    620626  try { 
     
    628634  } 
    629635#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!")); 
    676669  return true; 
    677670} 
     
    847840  } 
    848841  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("")); 
    850843    LOG_DEBUG_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 
    851844    plugin->load_plugin(NSCAPI::dontStart); 
     
    944937    } 
    945938    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("...")); 
    947940      try { 
    948         if (!(*it)->load_plugin(NSCAPI::normalStart)) { 
     941        if (!(*it)->load_plugin(mode)) { 
    949942          LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 
    950943          it = plugins_.erase(it); 
     
    960953    } 
    961954  } 
    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   */ 
    977955  plugins_loaded_ = true; 
    978 } 
    979 /** 
    980  * Load a single plug-in using a DLL filename 
    981  * @param file The DLL file 
    982  */ 
    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); 
    986956} 
    987957/** 
     
    989959 * @param plugin The plug-in instance to load. The pointer is managed by the  
    990960 */ 
    991 NSClientT::plugin_type NSClientT::addPlugin(plugin_type plugin) { 
     961NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 
     962  plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 
    992963  plugin->load_dll(); 
    993964  { 
     
    1026997    try { 
    1027998      len = settings_manager::get_settings()->get_int(SETTINGS_KEY(settings_def::PAYLOAD_LEN)); 
    1028     } catch (settings_exception &e) { 
     999    } catch (settings::settings_exception &e) { 
    10291000      LOG_DEBUG_STD(_T("Failed to get length: ") + e.getMessage()); 
    10301001      return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; 
     
    11971168    debug_ = log_looking; 
    11981169    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) 
    12001171        debug_ = log_debug; 
    12011172      else 
    12021173        debug_ = log_nodebug; 
    1203     } catch (settings_exception e) { 
    1204       return true; 
     1174    } catch (settings::settings_exception e) { 
     1175      debug_ = log_unknown; 
     1176      return false; 
    12051177    } 
    12061178  } else if (debug_ == log_looking)  
    1207     return true; 
     1179    return false; 
    12081180  return (debug_ == log_debug); 
    12091181} 
     
    12261198void NSClientT::reportMessage(int msgType, const wchar_t* file, const int line, std::wstring message) { 
    12271199  try { 
     1200    if ((msgType == NSCAPI::debug)&&(!logDebug())) { 
     1201      return; 
     1202    } 
    12281203    strEx::replace(message, _T("\n"), _T(" ")); 
    12291204    strEx::replace(message, _T("\r"), _T(" ")); 
    1230     if ((msgType == NSCAPI::debug)&&(!logDebug())) { 
    1231       return; 
    1232     } 
    12331205//    if (shared_server_.get() != NULL && shared_server_->hasClients()) { 
    12341206//      try { 
     
    13351307  try { 
    13361308    settings_manager::get_core()->set_base(basePath); 
    1337   } catch (settings_exception e) { 
     1309  } catch (settings::settings_exception e) { 
    13381310    LOG_ERROR_CORE_STD(_T("Failed to set settings file: ") + e.getMessage()); 
    13391311  } catch (...) { 
     
    13981370std::wstring NSClientT::expand_path(std::wstring file) { 
    13991371  strEx::replace(file, _T("${certificate-path}"), _T("${shared-path}/security")); 
     1372  strEx::replace(file, _T("${module-path}"), _T("${shared-path}/modules")); 
     1373   
    14001374  strEx::replace(file, _T("${base-path}"), getBasePath().string()); 
    14011375#ifdef WIN32 
  • trunk/service/NSClient++.h

    r281 r283  
    112112  typedef enum log_status {log_unknown, log_looking, log_debug, log_nodebug }; 
    113113  log_status debug_; 
     114  std::wstring context_; 
    114115#ifdef WIN32 
    115116  com_helper::initialize_com com_helper_; 
     
    128129 
    129130public: 
     131  typedef std::multimap<std::wstring,std::wstring> plugin_alias_list_type; 
    130132  // c-tor, d-tor 
    131133  NSClientT(void) : debug_(log_unknown), plugins_loaded_(false), enable_shared_session_(false), commands_(this), channels_(this), next_plugin_id_(0) {} 
     
    141143  bool initCore(bool boot); 
    142144  bool exitCore(bool boot); 
     145  void set_settings_context(std::wstring context) { context_ = context; } 
    143146#ifdef WIN32x 
    144147  static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); 
     
    174177  int commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args); 
    175178 
    176   plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias); 
     179  //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias); 
    177180  void loadPlugins(NSCAPI::moduleLoadMode mode); 
    178181  void unloadPlugins(bool unloadLoggers); 
     
    187190  void listPlugins(); 
    188191  plugin_info_list get_all_plugins(); 
     192  plugin_alias_list_type find_all_plugins(bool active); 
    189193  std::list<std::wstring> list_commands(); 
    190194 
     
    210214 
    211215  private: 
    212     plugin_type addPlugin(plugin_type plugin); 
     216    plugin_type addPlugin(boost::filesystem::wpath file, std::wstring alias); 
    213217}; 
    214218 
  • trunk/service/cli_parser.hpp

    r281 r283  
    3434      ("migrate-to", po::value<std::wstring>(), "Migrate (copy) settings from current store to target store") 
    3535      ("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.") 
    3737      ("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).") 
    3843      ; 
    3944 
     
    8489      return 1; 
    8590    } catch (...) { 
    86       std::cerr << "Unable to parse command line" << std::endl; 
     91      std::cerr << "Unhanded Exception" << std::endl; 
    8792      return 1; 
    8893    } 
    89  
     94    return 0; 
    9095  } 
    9196 
     
    131136 
    132137 
     138      client.set_current(current); 
     139      client.set_update_defaults(def); 
     140 
    133141      client.boot(); 
    134142 
     143      int ret = -1; 
     144 
    135145      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>()); 
    139147      } 
    140148      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>()); 
    144150      } 
     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; 
    145166    } catch(std::exception & e) { 
    146167      std::cerr << "Unable to parse command line (settings): " << e.what() << std::endl; 
    147168      return 1; 
    148169    } 
    149  
    150170  } 
    151171 
  • trunk/service/core_api.cpp

    r281 r283  
    6464  try { 
    6565    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} 
     71int 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) { 
    6775    LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 
    6876    return defaultValue; 
     
    104112    *bufLen = len; 
    105113    return NSCAPI::isSuccess; 
    106   } catch (settings_exception e) { 
     114  } catch (settings::settings_exception e) { 
    107115    LOG_ERROR_STD(_T("Failed to get section: ") + e.getMessage()); 
    108116  } catch (...) { 
     
    236244    } 
    237245    settings_manager::get_core()->migrate_to(inst); 
    238   } catch (settings_exception e) { 
     246  } catch (settings::settings_exception e) { 
    239247    LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage()); 
    240248    return NSCAPI::hasFailed; 
     
    254262    settings_manager::get_core()->migrate_from(inst); 
    255263    settings_manager::get_settings()->reload(); 
    256   } catch (settings_exception e) { 
     264  } catch (settings::settings_exception e) { 
    257265    LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage()); 
    258266    return NSCAPI::hasFailed; 
     
    302310      settings_manager::get_core()->register_key(path, key, settings::settings_core::key_integer, title, description, defVal, advanced); 
    303311    return NSCAPI::hasFailed; 
    304   } catch (settings_exception e) { 
     312  } catch (settings::settings_exception e) { 
    305313    LOG_ERROR_STD(_T("Failed register key: ") + e.getMessage()); 
    306314    return NSCAPI::hasFailed; 
     
    316324  try { 
    317325    settings_manager::get_core()->register_path(path, title, description, advanced); 
    318   } catch (settings_exception e) { 
     326  } catch (settings::settings_exception e) { 
    319327    LOG_ERROR_STD(_T("Failed register path: ") + e.getMessage()); 
    320328    return NSCAPI::hasFailed; 
     
    362370  try { 
    363371    settings_manager::get_settings()->save(); 
    364   } catch (settings_exception e) { 
     372  } catch (settings::settings_exception e) { 
    365373    LOG_ERROR_STD(_T("Failed to save: ") + e.getMessage()); 
    366374    return NSCAPI::hasFailed; 
     
    387395  if (wcscasecmp(buffer, _T("NSAPIGetSettingsInt")) == 0) 
    388396    return reinterpret_cast<LPVOID>(&NSAPIGetSettingsInt); 
     397  if (wcscasecmp(buffer, _T("NSAPIGetSettingsBool")) == 0) 
     398    return reinterpret_cast<LPVOID>(&NSAPIGetSettingsBool); 
    389399  if (wcscasecmp(buffer, _T("NSAPIMessage")) == 0) 
    390400    return reinterpret_cast<LPVOID>(&NSAPIMessage); 
  • trunk/service/core_api.h

    r281 r283  
    4646NSCAPI::errorReturn NSAPISetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* value); 
    4747NSCAPI::errorReturn NSAPISetSettingsInt(const wchar_t* section, const wchar_t* key, int value); 
     48NSCAPI::errorReturn NSAPISetSettingsBool(const wchar_t* section, const wchar_t* key, int value); 
    4849NSCAPI::errorReturn NSAPIWriteSettings(const wchar_t* key); 
    4950NSCAPI::errorReturn NSAPIReadSettings(const wchar_t* key); 
  • trunk/service/settings_client.hpp

    r281 r283  
    11#pragma once 
    2  
     2#include <settings/settings_core.hpp> 
    33 
    44class NSClientT; 
     
    66  class settings_client { 
    77    NSClient* core_; 
     8    std::wstring current_; 
     9    bool default_; 
     10 
     11 
    812  public: 
    913    settings_client(NSClient* core) : core_(core) {} 
     
    1418 
    1519    void boot() { 
    16       if (!core_->initCore(true)) { 
     20      if (!current_.empty()) 
     21        core_->set_settings_context(current_); 
     22      if (!core_->initCore(false)) { 
    1723        std::wcout << _T("Service *NOT* started!") << std::endl; 
    1824        return; 
    1925      } 
     26      if (default_) 
     27        settings_manager::get_core()->update_defaults(); 
    2028    } 
    2129 
    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) { 
    2338      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) { 
    3543        error_msg(_T("Failed to initialize settings: ") + e.getError()); 
    3644      } catch (...) { 
    3745        error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 
    3846      } 
     47      return -1; 
    3948    } 
    40     void migrate_to(std::wstring src, std::wstring target, bool def) { 
     49    int migrate_to(std::wstring target) { 
    4150      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) { 
    5355        error_msg(_T("Failed to initialize settings: ") + e.getError()); 
    5456      } catch (...) { 
    5557        error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 
    5658      } 
     59      return -1; 
    5760    } 
    5861 
    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) { 
    6076      try { 
    6177        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()) { 
    6480          settings_manager::get_core()->get()->save(); 
    6581        } else if (target == _T("trac")) { 
    6682          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); 
    7086            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); 
    7389              if (!desc.advanced) { 
    7490                if (first) 
    7591                  std::wcout << _T("'''Normal settings'''") << std::endl; 
    7692                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; 
    7894              } 
    7995            } 
    8096            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); 
    8399              if (desc.advanced) { 
    84100                if (first) 
    85101                  std::wcout << _T("'''Advanced settings'''") << std::endl; 
    86102                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; 
    88104              } 
    89105            } 
     
    93109          settings_manager::get_core()->get()->save_to(target); 
    94110        } 
    95       } catch (settings_exception e) { 
     111        return 1; 
     112      } catch (settings::settings_exception e) { 
    96113        error_msg(_T("Failed to initialize settings: ") + e.getError()); 
    97114      } catch (...) { 
    98115        error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM")); 
    99116      } 
    100       core_->exitCore(true); 
     117      return -1; 
    101118    } 
     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 
    102155    void error_msg(std::wstring msg) { 
    103156      core_->reportMessage(NSCAPI::error, __FILEW__, __LINE__, msg.c_str()); 
  • trunk/service/settings_manager_impl.cpp

    r281 r283  
    1313namespace settings_manager { 
    1414  // Alias to make handling "compatible" with old syntax 
    15   boost::shared_ptr<settings::settings_interface> get_settings() { 
     15  settings::instance_ptr get_settings() { 
    1616    return SettingsHandler::getInstance()->get(); 
     17  } 
     18  settings::instance_ptr get_settings_no_wait() { 
     19    return SettingsHandler::getInstance()->get_no_wait(); 
    1720  } 
    1821  settings::settings_core* get_core() { 
     
    3942  /// 
    4043  /// @author mickem 
    41   settings::instance_ptr NSCSettingsImpl::create_instance(std::wstring key) { 
     44  settings::instance_raw_ptr NSCSettingsImpl::create_instance(std::wstring key) { 
    4245    net::url url = net::parse(key); 
    43     get_logger()->debug(__FILEW__, __LINE__, _T("Trying to create: ") + url.protocol + _T(": ") + key); 
    4446#ifdef WIN32 
    4547    if (url.protocol == _T("old")) { 
    4648      old_ = true; 
    47       return settings::instance_ptr(new settings::OLDSettings(this, key)); 
     49      return settings::instance_raw_ptr(new settings::OLDSettings(this, key)); 
    4850    }  
    4951    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)); 
    5153#endif 
    5254    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); 
    5557  } 
    5658 
     
    6264  /// @author mickem 
    6365  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    } 
    6771    set_instance(key); 
    6872  } 
    6973 
    70   bool init_settings() { 
     74  bool init_settings(std::wstring context) { 
    7175    try { 
    7276      get_core()->set_logger(new settings_logger()); 
    7377      get_core()->set_base(mainClient.expand_path(_T("${base-path}"))); 
    74       get_core()->boot(_T("boot.ini")); 
     78      get_core()->boot(context); 
    7579      get_core()->register_key(SETTINGS_REG_KEY_I_GEN(settings_def::PAYLOAD_LEN, settings::settings_core::key_integer)); 
    7680      get_core()->register_key(SETTINGS_REG_KEY_S_GEN(protocol_def::ALLOWED_HOSTS, settings::settings_core::key_string)); 
     
    7983      get_core()->register_key(SETTINGS_REG_KEY_S_GEN(protocol_def::PWD, settings::settings_core::key_string)); 
    8084      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) { 
    8486      LOG_CRITICAL_STD(_T("Failed to initialize settings: ") + e.getError()); 
    8587      return false; 
  • trunk/service/settings_manager_impl.h

    r281 r283  
    4040    std::wstring find_file(std::wstring file, std::wstring fallback = _T("")); 
    4141    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); 
    4343  }; 
    4444 
     
    4747  // Alias to make handling "compatible" with old syntax 
    4848  settings::instance_ptr get_settings(); 
     49   settings::instance_ptr get_settings_no_wait(); 
    4950  settings::settings_core* get_core(); 
    5051  void destroy_settings(); 
    51   bool init_settings(); 
     52  bool init_settings(std::wstring context = _T("")); 
    5253} 
Note: See TracChangeset for help on using the changeset viewer.