Changeset 76540c3 in nscp


Ignore:
Timestamp:
02/12/12 18:16:53 (16 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
2906cda
Parents:
0f7b655
Message:
  • Added support for specifying targets on schedules: settings/scheduler/schedules/foo target = foo-host Will re-target the NSCA (or whatever you use) towards a given target.
  • Fixed is schedule manager uses new standard object reader
  • Fixed some issues with reading schedule
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r0f7b655 r76540c3  
    55 * Fixa dependonservice LanManWorkStation (old win) 
    66 * Fix RtlStringFromGUID problem on NT4 
     7 
     82012-02-12 MickeM 
     9 * Added support for specifying targets on schedules: 
     10   [/settings/scheduler/schedules/foo] 
     11   target = foo-host 
     12   Will re-target the NSCA (or whatever you use) towards a given target. 
     13 * Fixed is schedule manager uses new standard object reader 
     14 * Fixed some issues with reading schedule 
    715 
    8162012-02-05 MickeM 
  • include/nscapi/targets.hpp

    re396b2f r76540c3  
    1010#include <settings/client/settings_client.hpp> 
    1111#include <nscapi/settings_proxy.hpp> 
     12#include <nscapi/settings_object.hpp> 
    1213#include <nscapi/functions.hpp> 
    1314 
     
    1516 
    1617namespace nscapi { 
    17  
    18   namespace settings_object_helper { 
    19     class default_object_type { 
    20       std::wstring path; 
    21       std::wstring alias; 
    22       std::wstring value; 
    23       std::wstring parent; 
    24     }; 
    25  
    26     template<class object_type> 
    27     class default_object_reader { 
    28       static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) {} 
    29       static void apply_parent(object_type &object, object_type &parent) {} 
    30       static void post_process_object(object_type &object) {} 
    31       static void init_default(object_type &object) {} 
    32     }; 
    33  
    34  
    35     template<class object_type = default_object_type, class object_reader = default_object_reader<default_object_type> > 
    36     struct target_handler : boost::noncopyable { 
    37       typedef boost::optional<object_type> optional_object; 
    38       typedef optional_object optarget; 
    39       typedef object_type target; 
    40       typedef std::map<std::wstring, object_type> object_list_type; 
    41       typedef target_handler<object_type, object_reader> my_type; 
    42  
    43       object_list_type object_list; 
    44       object_list_type template_list; 
    45  
    46       void add_missing(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) { 
    47         if (has_object(alias)) 
    48           return; 
    49         add(proxy, path, alias, value, is_template); 
    50       } 
    51  
    52       object_type add(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) { 
    53         object_type object; 
    54         object.alias = alias; 
    55         object.value = value; 
    56         object.path = path + _T("/") + alias; 
    57         object.parent = _T("default"); 
    58  
    59         object_reader::read_target(proxy, object); 
    60  
    61         if (!object.parent.empty() && object.parent != alias & object.parent != object.alias) { 
    62           object_type parent; 
    63           optional_object tmp = find_object(object.parent); 
    64           if (!tmp) { 
    65             parent = add(proxy, path, object.parent, _T("")); 
    66             add_template(parent); 
    67           } else { 
    68             parent = *tmp; 
    69           } 
    70           object_reader::apply_parent(object, parent); 
    71         } 
    72         if (is_template) 
    73           add_template(object); 
    74         else 
    75           add_object(object); 
    76         return object; 
    77       } 
    78  
    79       void rebuild(boost::shared_ptr<nscapi::settings_proxy> proxy) { 
    80         std::list<object_type> tmp; 
    81         BOOST_FOREACH(object_list_type::value_type t, object_list) { 
    82           tmp.push_back(t.second); 
    83         } 
    84         object_list.clear(); 
    85         BOOST_FOREACH(const &object_item o, tmp) { 
    86           std::wstring::size_type pos = o.path.find_last_of(_T("/")); 
    87           if (pos == std::wstring::npos) 
    88             continue; 
    89           add(proxy, o.path.substr(0, pos-1), o.path.substr(pos), o.host); 
    90         } 
    91       } 
    92  
    93  
    94       optional_object find_object(std::wstring alias) { 
    95         object_list_type::const_iterator cit = object_list.find(alias); 
    96         if (cit != object_list.end()) 
    97           return optional_object(cit->second); 
    98         cit = template_list.find(alias); 
    99         if (cit != template_list.end()) 
    100           return optional_object(cit->second); 
    101         return optional_object(); 
    102       } 
    103  
    104       bool has_object(std::wstring alias) { 
    105         object_list_type::const_iterator cit = object_list.find(alias); 
    106         if (cit != object_list.end()) 
    107           return true; 
    108         cit = template_list.find(alias); 
    109         if (cit != template_list.end()) 
    110           return true; 
    111         return false; 
    112       } 
    113  
    114  
    115       std::wstring to_wstring() { 
    116         std::wstringstream ss; 
    117         ss << _T("Objects: "); 
    118         BOOST_FOREACH(object_list_type::value_type t, object_list) { 
    119           ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} "); 
    120         } 
    121         ss << _T("Templates: "); 
    122         BOOST_FOREACH(object_list_type::value_type t, template_list) { 
    123           ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} "); 
    124         } 
    125         return ss.str(); 
    126       } 
    127  
    128       void add_object(object_type object) { 
    129         object_reader::post_process_object(object); 
    130         object_list_type::iterator cit = template_list.find(object.alias); 
    131         if (cit != template_list.end()) 
    132           template_list.erase(cit); 
    133         object_list[object.alias] = object; 
    134       } 
    135       void add_template(object_type object) { 
    136         object_list_type::const_iterator cit = object_list.find(object.alias); 
    137         if (cit != object_list.end()) 
    138           return; 
    139         object_reader::post_process_object(object); 
    140         template_list[object.alias] = object; 
    141       } 
    142     }; 
    143   } 
    144  
    145  
    14618  namespace targets { 
    14719    struct target_object { 
     
    15123      std::wstring value; 
    15224      std::wstring parent; 
     25      bool is_template; 
    15326 
    15427      net::wurl address; 
     
    16134        ss << _T(", address: ") << get_address(); 
    16235        ss << _T(", parent: ") << parent; 
     36        ss << _T(", is_template: ") << is_template; 
    16337        BOOST_FOREACH(options_type::value_type o, options) { 
    16438          ss << _T(", option[") << o.first << _T("]: ") << o.second; 
     
    17751      } 
    17852      void set_port(int value) { 
    179         address.host = strEx::itos(value); 
     53        address.port = value; 
    18054      } 
    18155      bool has_option(std::wstring key) const { 
     
    21892    struct target_object_reader { 
    21993      typedef target_object object_type; 
    220       static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object); 
     94      static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object); 
    22195      static void apply_parent(object_type &object, object_type &parent); 
    22296    }; 
     
    236110      } 
    237111 
    238       static void read_target(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     112      static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
    239113        object.address = net::parse(object.value, 0); 
    240114        if (object.alias == _T("default")) 
     
    267141          _T("TARGET PARENT"), _T("The parent the target inherits from")) 
    268142 
     143          (_T("is template"), nscapi::settings_helper::bool_key(&object.is_template, false), 
     144          _T("IS TEMPLATE"), _T("Declare this object as a template (this means it will not be avalible as a separate object)")) 
     145 
    269146          ; 
    270147        custom_reader::add_custom_keys(settings, proxy, object); 
     
    290167    }; 
    291168    template<class custom_reader> 
    292     struct handler : public nscapi::settings_object_helper::target_handler<target_object, split_object_reader<custom_reader > > {}; 
    293  
    294 /* 
    295     template<class custom_reader> 
    296     */ 
    297     //typedef nscapi::settings_object_helper::target_handler<target_object, split_object_reader<dummy_custom_reader > > handler; 
    298  
     169    struct handler : public nscapi::settings_objects::object_handler<target_object, split_object_reader<custom_reader > > {}; 
    299170  } 
    300171} 
  • include/settings/client/settings_client.cpp

    r8013c0c r76540c3  
    1212*/ 
    1313    boost::shared_ptr<wstring_key_type> wstring_key(std::wstring *val, std::wstring def) { 
    14       boost::shared_ptr<wstring_key_type> r(new wstring_key_type(val, def)); 
     14      boost::shared_ptr<wstring_key_type> r(new wstring_key_type(val, def, true)); 
     15      return r; 
     16    } 
     17    boost::shared_ptr<wstring_key_type> wstring_key(std::wstring *val) { 
     18      boost::shared_ptr<wstring_key_type> r(new wstring_key_type(val, _T(""), false)); 
    1519      return r; 
    1620    } 
    1721    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val, std::wstring def) { 
    18       boost::shared_ptr<wpath_key_type> r(new wpath_key_type(val, def)); 
     22      boost::shared_ptr<wpath_key_type> r(new wpath_key_type(val, def, true)); 
     23      return r; 
     24    } 
     25    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val) { 
     26      boost::shared_ptr<wpath_key_type> r(new wpath_key_type(val, _T(""), false)); 
    1927      return r; 
    2028    } 
    2129    boost::shared_ptr<string_key_type> string_key(std::string *val, std::string def) { 
    22       boost::shared_ptr<string_key_type> r(new string_key_type(val, def)); 
     30      boost::shared_ptr<string_key_type> r(new string_key_type(val, def, true)); 
     31      return r; 
     32    } 
     33    boost::shared_ptr<string_key_type> string_key(std::string *val) { 
     34      boost::shared_ptr<string_key_type> r(new string_key_type(val, "", false)); 
    2335      return r; 
    2436    } 
    2537    boost::shared_ptr<int_key_type> int_key(int *val, int def) { 
    26       boost::shared_ptr<int_key_type> r(new int_key_type(val, def)); 
     38      boost::shared_ptr<int_key_type> r(new int_key_type(val, def, true)); 
     39      return r; 
     40    } 
     41    boost::shared_ptr<int_key_type> int_key(int *val) { 
     42      boost::shared_ptr<int_key_type> r(new int_key_type(val, 0, false)); 
    2743      return r; 
    2844    } 
    2945    boost::shared_ptr<uint_key_type> uint_key(unsigned int *val, unsigned int def) { 
    30       boost::shared_ptr<uint_key_type> r(new uint_key_type(val, def)); 
     46      boost::shared_ptr<uint_key_type> r(new uint_key_type(val, def, true)); 
    3147      return r; 
    3248    } 
    3349    boost::shared_ptr<bool_key_type> bool_key(bool *val, bool def) { 
    34       boost::shared_ptr<bool_key_type> r(new bool_key_type(val, def)); 
     50      boost::shared_ptr<bool_key_type> r(new bool_key_type(val, def, true)); 
     51      return r; 
     52    } 
     53    boost::shared_ptr<bool_key_type> bool_key(bool *val) { 
     54      boost::shared_ptr<bool_key_type> r(new bool_key_type(val, false, false)); 
    3555      return r; 
    3656    } 
  • include/settings/client/settings_client.hpp

    re396b2f r76540c3  
    3232    class typed_key : public key_interface { 
    3333    public: 
    34       typed_key(const T& v)  : default_value_(boost::any(v)), default_value_as_text_(boost::lexical_cast<std::wstring>(v)) {} 
     34      typed_key(const T& v, bool has_default)  : has_default_(has_default), default_value_(boost::any(v)), default_value_as_text_(boost::lexical_cast<std::wstring>(v)) {} 
     35      typed_key(bool has_default)  : has_default_(has_default) {} 
    3536 
    3637      virtual typed_key* default_value(const T& v) { 
     
    4546      virtual void update_target(T *value) const = 0; 
    4647    protected: 
     48      bool has_default_; 
    4749      boost::any default_value_; 
    4850      std::wstring default_value_as_text_; 
     
    5254    class typed_string_value : public typed_key<T> { 
    5355    public: 
    54       typed_string_value(const T& v) : typed_key<T>(v) {} 
     56      typed_string_value(const T& v, bool has_default) : typed_key<T>(v, has_default) {} 
     57      //typed_string_value() : typed_key<T>() {} 
    5558      virtual NSCAPI::settings_type get_type() const { 
    5659        return NSCAPI::key_string; 
    5760      } 
    5861      virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    59         T value = boost::lexical_cast<T>(core_->get_string(path, key, typed_key<T>::default_value_as_text_)); 
    60         update_target(&value); 
     62        std::wstring dummy(_T("$$DUMMY_VALUE_DO_NOT_USE$$")); 
     63        if (typed_key<T>::has_default_) 
     64          dummy = typed_key<T>::default_value_as_text_; 
     65        std::wstring data = core_->get_string(path, key, dummy); 
     66        if (typed_key<T>::has_default_ || data != dummy) { 
     67          T value = boost::lexical_cast<T>(data); 
     68          update_target(&value); 
     69        } 
    6170      } 
    6271      virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 
    63         std::wstring default_value = core_->get_string(parent, key, typed_key<T>::default_value_as_text_); 
    64         T value = boost::lexical_cast<T>(core_->get_string(path, key, default_value)); 
    65         update_target(&value); 
    66       } 
    67     }; 
    68     template<class T> 
    69     class typed_string_value_no_default : public typed_key<T> { 
    70     public: 
    71       typed_string_value_no_default(const T& v) : typed_key<T>(v) {} 
     72        std::wstring dummy(_T("$$DUMMY_VALUE_DO_NOT_USE$$")); 
     73        if (typed_key<T>::has_default_) 
     74          dummy = typed_key<T>::default_value_as_text_; 
     75        std::wstring data = core_->get_string(parent, key, dummy); 
     76        if (typed_key<T>::has_default_ || data != dummy)  
     77          dummy = data; 
     78        data = core_->get_string(path, key, dummy); 
     79        if (typed_key<T>::has_default_ || data != dummy) { 
     80          T value = boost::lexical_cast<T>(data); 
     81          update_target(&value); 
     82        } 
     83      } 
     84    }; 
     85    template<class T> 
     86    class typed_path_value : public typed_key<T> { 
     87    public: 
     88      typed_path_value(const T& v, bool has_default) : typed_key<T>(v, has_default) {} 
    7289      virtual NSCAPI::settings_type get_type() const { 
    7390        return NSCAPI::key_string; 
    7491      } 
    7592      virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    76         std::wstring dummy = _T("$$DUMMY_VALUE_DO_NOT_USE$$"); 
     93        std::wstring dummy(_T("$$DUMMY_VALUE_DO_NOT_USE$$")); 
     94        if (typed_key<T>::has_default_) 
     95          dummy = typed_key<T>::default_value_as_text_; 
    7796        std::wstring data = core_->get_string(path, key, dummy); 
    78         if (data != dummy) { 
    79           T value = boost::lexical_cast<T>(data); 
     97        if (typed_key<T>::has_default_ || data != dummy) { 
     98          T value = boost::lexical_cast<T>(core_->expand_path(data)); 
    8099          update_target(&value); 
    81100        } 
    82101      } 
    83102      virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 
    84         std::wstring dummy = _T("$$DUMMY_VALUE_DO_NOT_USE$$"); 
     103        std::wstring dummy(_T("$$DUMMY_VALUE_DO_NOT_USE$$")); 
     104        if (typed_key<T>::has_default_) 
     105          dummy = typed_key<T>::default_value_as_text_; 
    85106        std::wstring data = core_->get_string(parent, key, dummy); 
    86         if (data != dummy)  
     107        if (typed_key<T>::has_default_ || data != dummy)  
    87108          dummy = data; 
    88109        data = core_->get_string(path, key, dummy); 
    89         if (data != dummy) { 
    90           T value = boost::lexical_cast<T>(data); 
     110        if (typed_key<T>::has_default_ || data != dummy) { 
     111          T value = boost::lexical_cast<T>(core_->expand_path(data)); 
    91112          update_target(&value); 
    92113        } 
    93114      } 
    94115    }; 
    95     template<class T> 
    96     class typed_path_value : public typed_key<T> { 
    97     public: 
    98       typed_path_value(const T& v) : typed_key<T>(v) {} 
    99       virtual NSCAPI::settings_type get_type() const { 
    100         return NSCAPI::key_string; 
    101       } 
    102       virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    103         std::wstring val = core_->get_string(path, key, typed_key<T>::default_value_as_text_); 
    104         T value = boost::lexical_cast<T>(core_->expand_path(val)); 
    105         update_target(&value); 
    106       } 
    107       virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 
    108         std::wstring def_val = core_->get_string(parent, key, typed_key<T>::default_value_as_text_); 
    109         std::wstring val = core_->get_string(path, key, def_val); 
    110         T value = boost::lexical_cast<T>(core_->expand_path(val)); 
    111         update_target(&value); 
    112       } 
    113     }; 
    114116     
    115117    template<class T> 
    116118    class typed_int_value : public typed_key<T> { 
    117119    public: 
    118       typed_int_value(const T& v) : typed_key<T>(v), default_value_as_int_(boost::lexical_cast<int>(v)) {} 
     120      typed_int_value(const T& v, bool has_default) : typed_key<T>(v, has_default), default_value_as_int_(boost::lexical_cast<int>(v)) {} 
    119121      typed_key<T>* default_value(const T& v) { 
    120122        typed_key<T>::default_value(v); 
     
    126128      } 
    127129      virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    128         T value = static_cast<T>(core_->get_int(path, key, default_value_as_int_)); 
    129         update_target(&value); 
    130       } 
    131       virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 
    132         T default_value = static_cast<T>(core_->get_int(parent, key, default_value_as_int_)); 
    133         T value = static_cast<T>(core_->get_int(path, key, default_value)); 
    134         update_target(&value); 
    135       } 
    136     protected: 
    137       int default_value_as_int_; 
    138     }; 
    139     template<class T> 
    140     class typed_int_value_no_default : public typed_key<T> { 
    141     public: 
    142       typed_int_value_no_default(const T& v) : typed_key<T>(v), default_value_as_int_(0) {} 
    143       typed_key<T>* default_value(const T& v) { 
    144         typed_key<T>::default_value(v); 
    145         default_value_as_int_ = boost::lexical_cast<int>(v); 
    146         return this; 
    147       } 
    148       virtual NSCAPI::settings_type get_type() const { 
    149         return NSCAPI::key_integer; 
    150       } 
    151       virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    152130        int dummy = -1; 
     131        if (typed_key<T>::has_default_) 
     132          dummy = default_value_as_int_; 
    153133        int val = core_->get_int(path, key, dummy); 
    154         if (val == dummy) { 
     134        if (!typed_key<T>::has_default_ && val == dummy) { 
    155135          dummy = -2; 
    156136          val = core_->get_int(path, key, dummy); 
     
    162142      } 
    163143      virtual void notify(settings_impl_interface_ptr core_, std::wstring parent, std::wstring path, std::wstring key) const { 
    164         int dummy = -1; 
    165         int defval = core_->get_int(path, key, dummy); 
    166         if (defval == dummy) { 
    167           dummy = -2; 
    168           defval = core_->get_int(path, key, dummy); 
    169         } 
    170         if (defval != dummy) { 
    171           T value = static_cast<T>(core_->get_int(path, key, defval)); 
     144        if (typed_key<T>::has_default_) { 
     145          T default_value = static_cast<T>(core_->get_int(parent, key, default_value_as_int_)); 
     146          T value = static_cast<T>(core_->get_int(path, key, default_value)); 
    172147          update_target(&value); 
    173         } 
    174         dummy = -1; 
    175         int val = core_->get_int(path, key, dummy); 
    176         if (val == dummy) { 
    177           dummy = -2; 
    178           val = core_->get_int(path, key, dummy); 
    179           if (val == dummy) 
    180             return; 
    181         } 
    182         T value = static_cast<T>(val); 
    183         update_target(&value); 
     148        } else { 
     149          int dummy = -1; 
     150          int defval = core_->get_int(path, key, dummy); 
     151          if (defval == dummy) { 
     152            dummy = -2; 
     153            defval = core_->get_int(path, key, dummy); 
     154          } 
     155          if (defval != dummy) { 
     156            T value = static_cast<T>(core_->get_int(path, key, defval)); 
     157            update_target(&value); 
     158          } 
     159          dummy = -1; 
     160          int val = core_->get_int(path, key, dummy); 
     161          if (val == dummy) { 
     162            dummy = -2; 
     163            val = core_->get_int(path, key, dummy); 
     164            if (val == dummy) 
     165              return; 
     166          } 
     167          T value = static_cast<T>(val); 
     168          update_target(&value); 
     169        } 
    184170      } 
    185171    protected: 
     
    189175    class typed_bool_value : public typed_int_value<T> { 
    190176    public: 
    191       typed_bool_value(const T& v) : typed_int_value<T>(v) {} 
     177      typed_bool_value(const T& v, bool has_default) : typed_int_value<T>(v, has_default) {} 
    192178      virtual NSCAPI::settings_type get_type() const { 
    193179        return NSCAPI::key_bool; 
    194180      } 
     181      // TODO: FIXME: Add support for has_default 
    195182      virtual void notify(settings_impl_interface_ptr core_, std::wstring path, std::wstring key) const { 
    196183        T value = static_cast<T>(core_->get_bool(path, key, typed_int_value<T>::default_value_as_int_==1)); 
     
    207194    class typed_key_value : public TBase { 
    208195    public: 
    209       typed_key_value(T* store_to, const T& v) : TBase(v), store_to_(store_to) {} 
     196      typed_key_value(T* store_to, const T& v, bool has_default) : TBase(v, has_default), store_to_(store_to) {} 
    210197 
    211198      virtual void update_target(T *value) const { 
     
    220207    class typed_key_entry_in_vector : public TBase { 
    221208    public: 
    222       typed_key_entry_in_vector(V* store_to, typename V::key_type key, const T& v) : TBase(v), store_to_(store_to), key_(key) {} 
     209      typed_key_entry_in_vector(V* store_to, typename V::key_type key, const T& v, bool has_default) : TBase(v, has_default), store_to_(store_to), key_(key) {} 
    223210 
    224211      virtual void update_target(T *value) const { 
     
    234221    class typed_key_fun : public TBase { 
    235222    public: 
    236       typed_key_fun(boost::function<void (T)> callback, const T& v): TBase(v), callback_(callback) {} 
     223      typed_key_fun(boost::function<void (T)> callback, const T& v, bool has_default): TBase(v, has_default), callback_(callback) {} 
    237224 
    238225      virtual void update_target(T *value) const { 
     
    245232    template<typename T> 
    246233    boost::shared_ptr<typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> > > wstring_vector_key(T *val, typename T::key_type key, std::wstring def) { 
    247       boost::shared_ptr<typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> > > r(new typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> >(val, key, def)); 
     234      boost::shared_ptr<typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> > > r(new typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> >(val, key, def, true)); 
    248235      return r; 
    249236    } 
     
    260247    typed_key_entry_in_vector<std::wstring, T, typed_string_value<std::wstring> >* wstring_vector_key(T *val, typename T::key_type key, std::wstring def); 
    261248    */ 
    262     boost::shared_ptr<wstring_key_type> wstring_key(std::wstring *val, std::wstring def = _T("")); 
    263     boost::shared_ptr<string_key_type> string_key(std::string *val, std::string def = ""); 
     249    boost::shared_ptr<wstring_key_type> wstring_key(std::wstring *val, std::wstring def); 
     250    boost::shared_ptr<wstring_key_type> wstring_key(std::wstring *val); 
     251    boost::shared_ptr<string_key_type> string_key(std::string *val, std::string def); 
     252    boost::shared_ptr<string_key_type> string_key(std::string *val); 
    264253    boost::shared_ptr<int_key_type> int_key(int *val, int def = 0); 
    265     boost::shared_ptr<uint_key_type> uint_key(unsigned int *val, unsigned int def = 0); 
    266     boost::shared_ptr<bool_key_type> bool_key(bool *val, bool def = false); 
    267     boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val, std::wstring def = _T("")); 
     254    boost::shared_ptr<uint_key_type> uint_key(unsigned int *val, unsigned int def); 
     255    boost::shared_ptr<bool_key_type> bool_key(bool *val, bool def); 
     256    boost::shared_ptr<bool_key_type> bool_key(bool *val); 
     257    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val, std::wstring def); 
     258    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val); 
    268259 
    269260    template<class T> 
    270261    boost::shared_ptr<typed_key_fun<T, typed_int_value<T> > > int_fun_key(boost::function<void (T)> fun, T def) { 
    271       boost::shared_ptr<typed_key_fun<T, typed_int_value<T> > > r(new typed_key_fun<T, typed_int_value<T> >(fun, def)); 
     262      boost::shared_ptr<typed_key_fun<T, typed_int_value<T> > > r(new typed_key_fun<T, typed_int_value<T> >(fun, def, true)); 
    272263      return r; 
    273264    } 
    274265    template<class T> 
    275     boost::shared_ptr<typed_key_fun<T, typed_int_value_no_default<T> > > int_fun_key(boost::function<void (T)> fun) { 
    276       boost::shared_ptr<typed_key_fun<T, typed_int_value_no_default<T> > > r(new typed_key_fun<T, typed_int_value_no_default<T> >(fun, 0)); 
     266    boost::shared_ptr<typed_key_fun<T, typed_int_value<T> > > int_fun_key(boost::function<void (T)> fun) { 
     267      boost::shared_ptr<typed_key_fun<T, typed_int_value<T> > > r(new typed_key_fun<T, typed_int_value<T> >(fun, 0, false)); 
    277268      return r; 
    278269    } 
    279270    template<class T> 
    280271    boost::shared_ptr<typed_key_fun<T, typed_bool_value<T> > > bool_fun_key(boost::function<void (T)> fun, T def) { 
    281       boost::shared_ptr<typed_key_fun<T, typed_bool_value<T> > > r(new typed_key_fun<T, typed_bool_value<T> >(fun, def)); 
     272      boost::shared_ptr<typed_key_fun<T, typed_bool_value<T> > > r(new typed_key_fun<T, typed_bool_value<T> >(fun, def, true)); 
    282273      return r; 
    283274    } 
    284275    template<class T> 
    285276    boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > string_fun_key(boost::function<void (T)> fun, T def) { 
    286       boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > r(new typed_key_fun<T, typed_string_value<T> >(fun, def)); 
     277      boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > r(new typed_key_fun<T, typed_string_value<T> >(fun, def, true)); 
    287278      return r; 
    288279    } 
    289280    template<class T> 
    290     boost::shared_ptr<typed_key_fun<T, typed_string_value_no_default<T> > > string_fun_key(boost::function<void (T)> fun) { 
    291       boost::shared_ptr<typed_key_fun<T, typed_string_value_no_default<T> > > r(new typed_key_fun<T, typed_string_value_no_default<T> >(fun, T())); 
     281    boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > string_fun_key(boost::function<void (T)> fun) { 
     282      boost::shared_ptr<typed_key_fun<T, typed_string_value<T> > > r(new typed_key_fun<T, typed_string_value<T> >(fun, T(), false)); 
    292283      return r; 
    293284    } 
  • modules/PythonScript/PythonScript.cpp

    r441a022 r76540c3  
    211211      //PyEval_ReleaseLock(); 
    212212 
    213       PyThreadState *state; 
    214       state = PyEval_SaveThread(); 
    215         //PyThreadState *mainThreadState = PyThreadState_Get(); 
    216         //PyThreadState_Clear 
    217         //PyThreadState_(mainThreadState); 
    218  
     213      PyThreadState *state = PyEval_SaveThread(); 
    219214      do_init = true; 
    220215    } 
  • modules/Scheduler/Scheduler.cpp

    re396b2f r76540c3  
    3838  try { 
    3939 
    40     typedef std::map<std::wstring,std::wstring> schedule_map; 
    41     schedule_map schedules; 
    4240    sh::settings_registry settings(get_settings_proxy()); 
    4341    settings.set_alias(alias, _T("scheduler")); 
     42    schedule_path = settings.alias().get_settings_path(_T("schedules")); 
    4443 
    4544    settings.alias().add_path_to_settings() 
     
    5352      ; 
    5453 
    55     scheduler::target def = read_schedule(settings.alias().get_settings_path(_T("default")), _T("Default schedule")); 
    56  
    57     std::wstring sch_path = settings.alias().get_settings_path(_T("schedules")); 
    58  
    5954    settings.alias().add_path_to_settings() 
    60       (_T("schedules"), sh::fun_values_path(boost::bind(&Scheduler::add_schedule, this, sch_path, _1, _2, def)),  
     55      (_T("schedules"), sh::fun_values_path(boost::bind(&Scheduler::add_schedule, this, _1, _2)),  
    6156      _T("SCHEDULER SECTION"), _T("Section for the Scheduler module.")) 
    6257      ; 
     
    6459    settings.register_all(); 
    6560    settings.notify(); 
     61 
     62    BOOST_FOREACH(const schedules::schedule_handler::object_list_type::value_type &o, schedules_.object_list) { 
     63      NSC_DEBUG_MSG(_T("Adding scheduled item: ") + o.second.to_wstring()); 
     64      scheduler_.add_task(o.second); 
     65    } 
    6666 
    6767  } catch (nscapi::nscapi_exception &e) { 
     
    9494} 
    9595 
    96 scheduler::target Scheduler::read_schedule(std::wstring path, std::wstring schedule_name, scheduler::target *def) { 
    97  
    98   scheduler::target item; 
    99   std::wstring report, duration; 
    100  
    101   sh::settings_registry settings(get_settings_proxy()); 
    102  
    103   settings.path(path).add_path() 
    104     (_T("SCHEDULE DEFENITION"), _T("Schedule defenition for: ") + schedule_name) 
    105     ; 
    106  
    107   settings.path(path).add_key() 
    108     (_T("channel"), sh::wstring_key(&item.channel, def==NULL?_T("NSCA"):def->channel), 
    109     _T("SCHEDULE CHANNEL"), _T("Channel to send results on")) 
    110  
    111     (_T("command"), sh::wstring_key(&item.command, def==NULL?_T("check_ok"):def->command), 
    112     _T("SCHEDULE COMMAND"), _T("Command to execute")) 
    113  
    114     (_T("alias"), sh::wstring_key(&item.alias, def==NULL?_T(""):def->alias), 
    115     _T("SCHEDULE ALIAS"), _T("The alias (service name) to report to server")) 
    116  
    117     (_T("report"), sh::wstring_key(&report, def==NULL?_T("all"):nscapi::report::to_string(def->report)), 
    118     _T("REPORT MODE"), _T("What to report to the server (any of the following: all, critical, warning, unknown, ok)")) 
    119  
    120     (_T("target"), sh::wstring_key(&item.target_id, def==NULL?_T(""):def->target_id), 
    121     _T("TARGET"), _T("")) 
    122  
    123     // TODO: get the proper default value here! 
    124     (_T("interval"), sh::wstring_key(&duration, _T("5s")), 
    125     _T("SCHEDULE INTERAVAL"), _T("Time in seconds between each check")) 
    126  
    127     ; 
    128  
    129   settings.register_all(); 
    130   settings.notify(); 
    131  
    132   item.report = nscapi::report::parse(report); 
    133   item.duration = boost::posix_time::seconds(strEx::stoui_as_time_sec(duration, 1)); 
    134   return item; 
    135 } 
    136 void Scheduler::add_schedule(std::wstring path, std::wstring alias, std::wstring command, scheduler::target def) { 
     96void Scheduler::add_schedule(std::wstring key, std::wstring arg) { 
    13797  try { 
    138     def.alias = alias; 
    139     def.command = command; 
    140     scheduler::target item = read_schedule(path + _T("/") + alias, alias, &def); 
    141     strEx::parse_command(item.command, item.command, item.arguments); 
    142     NSC_DEBUG_MSG_STD(_T("Adding scheduled task: ") + alias); 
    143     scheduler_.add_task(item); 
     98    schedules_.add(get_settings_proxy(), schedule_path, key, arg, key == _T("default")); 
     99  } catch (const std::exception &e) { 
     100    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    144101  } catch (...) { 
    145     NSC_LOG_ERROR_STD(_T("Failed to add schedule: ") + alias); 
     102    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
    146103  } 
    147104} 
     
    157114} 
    158115#include <nscapi/functions.hpp> 
    159 void Scheduler::handle_schedule(scheduler::target item) { 
     116void Scheduler::handle_schedule(schedules::schedule_object item) { 
    160117  try { 
    161118    std::string response; 
    162     NSCAPI::nagiosReturn code = GET_CORE()->simple_query(item.command.c_str(), item.arguments, response); 
     119    NSCAPI::nagiosReturn code = get_core()->simple_query(item.command.c_str(), item.arguments, response); 
    163120    if (code == NSCAPI::returnIgnored) { 
    164121      NSC_LOG_ERROR_STD(_T("Command was not found: ") + item.command.c_str()); 
     
    166123      nscapi::functions::create_simple_submit_request(item.channel, item.command, NSCAPI::returnUNKNOWN, _T("Command was not found: ") + item.command, _T(""), response); 
    167124      std::string result; 
    168       GET_CORE()->submit_message(item.channel, response, result); 
     125      get_core()->submit_message(item.channel, response, result); 
    169126    } else if (nscapi::report::matches(item.report, code)) { 
    170127      // @todo: allow renaming of commands here item.alias,  
     
    172129      nscapi::functions::make_submit_from_query(response, item.channel, item.alias, item.target_id); 
    173130      std::string result; 
    174       GET_CORE()->submit_message(item.channel, response, result); 
     131      get_core()->submit_message(item.channel, response, result); 
    175132    } 
    176133  } catch (nscapi::nscapi_exception &e) { 
  • modules/Scheduler/Scheduler.h

    rf085f9b r76540c3  
    2424 
    2525#include "simple_scheduler.hpp" 
     26#include "schedules.hpp" 
    2627 
    2728 
    2829class Scheduler : public scheduler::schedule_handler, public nscapi::impl::simple_plugin { 
    2930private: 
     31  std::wstring schedule_path; 
    3032  scheduler::simple_scheduler scheduler_; 
     33  schedules::schedule_handler schedules_; 
    3134 
    3235 
    3336public: 
    34   Scheduler() {} 
     37  Scheduler() { 
     38    scheduler_.set_handler(this);  
     39  } 
    3540  virtual ~Scheduler() {} 
    3641  // Module calls 
     
    4045 
    4146 
    42   void add_schedule(std::wstring path, std::wstring alias, std::wstring command, scheduler::target def); 
    43   scheduler::target read_schedule(std::wstring path, std::wstring comment, scheduler::target *def = NULL); 
    44   void handle_schedule(scheduler::target item); 
     47  void add_schedule(std::wstring alias, std::wstring command); 
     48  //scheduler::target read_schedule(std::wstring path, std::wstring comment, scheduler::target *def = NULL); 
     49  void handle_schedule(schedules::schedule_object item); 
    4550  void on_error(std::wstring error); 
    4651 
  • modules/Scheduler/simple_scheduler.cpp

    r8013c0c r76540c3  
    55#include <unicode_char.hpp> 
    66 
     7#include <nscapi/macros.hpp> 
     8 
    79using namespace nscp::helpers; 
    810 
    911namespace scheduler { 
    1012 
    11   int simple_scheduler::add_task(target item) { 
     13  int simple_scheduler::add_task(schedules::schedule_object item) { 
    1214    { 
    1315      boost::mutex::scoped_lock l(mutex_); 
    14       item.id = ++target_id_; 
     16      item.id = ++schedule_id_; 
    1517      targets_[item.id] = item; 
    1618    } 
     
    2325    targets_.erase(it); 
    2426  } 
    25   boost::optional<target> simple_scheduler::get_task(int id) { 
     27  boost::optional<schedules::schedule_object> simple_scheduler::get_task(int id) { 
    2628    boost::mutex::scoped_lock l(mutex_); 
    2729    target_list_type::iterator it = targets_.find(id); 
    2830    if (it == targets_.end()) 
    29       return boost::optional<target>(); 
    30     return boost::optional<target>((*it).second); 
     31      return boost::optional<schedules::schedule_object>(); 
     32    return boost::optional<schedules::schedule_object>((*it).second); 
    3133  } 
    3234 
     
    110112 
    111113        boost::posix_time::ptime now_time = now(); 
    112         boost::optional<target> item = get_task((*instance).schedule_id); 
     114        boost::optional<schedules::schedule_object> item = get_task((*instance).schedule_id); 
    113115        if (item) { 
    114116          try { 
     
    132134  } 
    133135 
    134   void simple_scheduler::reschedule(target item) { 
    135     reschedule_wnext(item, now() + boost::posix_time::seconds(rand()%item.duration.total_seconds())); 
     136  void simple_scheduler::reschedule(const schedules::schedule_object &item) { 
     137    if (item.duration.total_seconds() == 0) 
     138      log_error(_T("Not scheduling since duration is 0: ") + item.to_wstring()); 
     139    else 
     140      reschedule_wnext(item.id, now() + boost::posix_time::seconds(rand()%item.duration.total_seconds())); 
    136141  } 
    137   void simple_scheduler::reschedule(target item, boost::posix_time::ptime now) { 
    138     reschedule_wnext(item, now + item.duration); 
     142  void simple_scheduler::reschedule(const schedules::schedule_object &item, boost::posix_time::ptime now) { 
     143    reschedule_wnext(item.id, now + item.duration); 
    139144  } 
    140   void simple_scheduler::reschedule_wnext(target item, boost::posix_time::ptime next) { 
     145  void simple_scheduler::reschedule_wnext(int id, boost::posix_time::ptime next) { 
    141146    schedule_instance instance; 
    142     instance.schedule_id = item.id; 
     147    instance.schedule_id = id; 
    143148    instance.time = next; 
    144149    if (!queue_.push(instance)) { 
  • modules/Scheduler/simple_scheduler.hpp

    re396b2f r76540c3  
    1414#include <unicode_char.hpp> 
    1515 
     16#include "schedules.hpp" 
     17 
    1618namespace scheduler { 
    17  
    18  
    19   class task_not_found { 
    20     int id_; 
    21   public: 
    22     task_not_found(int id) : id_(id) {} 
    23     int get_id() {return id_; } 
    24   }; 
     19/* 
    2520  class target { 
    2621  public: 
     
    6964    } 
    7065  }; 
     66  */ 
    7167  class schedule_handler { 
    7268  public: 
    73     virtual void handle_schedule(target item) = 0; 
     69    virtual void handle_schedule(schedules::schedule_object item) = 0; 
    7470    virtual void on_error(std::wstring error) = 0; 
    7571  }; 
     
    126122  class simple_scheduler : public boost::noncopyable { 
    127123  private: 
    128     typedef std::map<int,target> target_list_type; 
     124    typedef std::map<int,schedules::schedule_object> target_list_type; 
    129125    typedef safe_schedule_queue<schedule_instance> schedule_queue_type; 
    130126    target_list_type targets_; 
    131     unsigned int target_id_; 
     127    unsigned int schedule_id_; 
    132128    schedule_queue_type queue_; 
    133129    unsigned int thread_count_; 
     
    146142  public: 
    147143 
    148     simple_scheduler() : target_id_(0), stop_requested_(false), running_(false), thread_count_(10), handler_(NULL), error_threshold_(5) {} 
     144    simple_scheduler() : schedule_id_(0), stop_requested_(false), running_(false), thread_count_(10), handler_(NULL), error_threshold_(5) {} 
    149145    ~simple_scheduler() {} 
    150146 
     
    157153    } 
    158154 
    159     int add_task(target item); 
     155    int add_task(schedules::schedule_object item); 
    160156    void remove_task(int id); 
    161     boost::optional<target> get_task(int id); 
     157    boost::optional<schedules::schedule_object> get_task(int id); 
    162158     
    163159    void start(); 
     
    175171    void watch_dog(int id); 
    176172 
    177     void reschedule(target item); 
    178     void reschedule(target item, boost::posix_time::ptime now); 
    179     void reschedule_wnext(target item, boost::posix_time::ptime next); 
     173    void reschedule(const schedules::schedule_object &item); 
     174    void reschedule(const schedules::schedule_object &item, boost::posix_time::ptime now); 
     175    void reschedule_wnext(int id, boost::posix_time::ptime next); 
    180176    void start_thread(); 
    181177 
  • version.hpp

    ra06e6af r76540c3  
    11#ifndef VERSION_HPP 
    22#define VERSION_HPP 
    3 #define PRODUCTVER     0,4,0,131 
    4 #define STRPRODUCTVER  "0,4,0,131" 
    5 #define STRPRODUCTDATE "2012-01-31" 
     3#define PRODUCTVER     0,4,0,132 
     4#define STRPRODUCTVER  "0,4,0,132" 
     5#define STRPRODUCTDATE "2012-02-12" 
    66#endif // VERSION_HPP 
  • version.txt

    ra06e6af r76540c3  
    11version=0.4.0 
    2 build=131 
    3 date=2012-01-31 
     2build=132 
     3date=2012-02-12 
Note: See TracChangeset for help on using the changeset viewer.