Changeset a06e6af in nscp
- Timestamp:
- 02/02/12 07:02:05 (17 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- a8c6e93
- Parents:
- c5ec0c8
- Files:
-
- 8 edited
-
changelog (modified) (1 diff)
-
modules/LUAScript/CMakeLists.txt (modified) (1 diff)
-
modules/LUAScript/LUAScript.cpp (modified) (2 diffs)
-
modules/LUAScript/LUAScript.h (modified) (1 diff)
-
modules/LUAScript/lua_wrappers.hpp (modified) (10 diffs)
-
modules/LUAScript/script_wrapper.hpp (modified) (8 diffs)
-
version.hpp (modified) (1 diff)
-
version.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
rc5ec0c8 ra06e6af 5 5 * Fixa dependonservice LanManWorkStation (old win) 6 6 * Fix RtlStringFromGUID problem on NT4 7 8 2012-02-01 MickeM 9 * Implemented full settings API for LuaScript (next RC will have to wait till next weekend) 7 10 8 11 2012-01-31 MickeM -
modules/LUAScript/CMakeLists.txt
rd66ccee ra06e6af 19 19 "${TARGET}.h" 20 20 "${TARGET}.def" 21 lua_wrappers.hpp 22 script_wrapper.hpp 21 23 22 24 ${NSCP_DEF_PLUGIN_HPP} -
modules/LUAScript/LUAScript.cpp
rc5ec0c8 ra06e6af 68 68 BOOST_FOREACH(script_container &script, scripts_) { 69 69 try { 70 boost::shared_ptr<script_wrapper::lua_script> instance = script_wrapper::lua_script::create_instance(get_core(), get_id(), registry, script.alias, script.script.string()); 71 instance->pre_load(); 72 instances_.push_back(instance); 70 instances_.push_back(script_wrapper::lua_script::create_instance(get_core(), get_id(), registry, script.alias, script.script.string())); 73 71 } catch (const lua_wrappers::LUAException &e) { 74 72 NSC_LOG_ERROR_STD(_T("Could not load script ") + script.to_wstring() + _T(": ") + e.getMessage()); … … 149 147 bool error = false; 150 148 registry->clear(); 151 for (script_list::const_iterator cit = instances_.begin(); cit != instances_.end() ; ++cit) {149 BOOST_FOREACH(script_instance i, instances_) { 152 150 try { 153 (*cit)->reload();151 i->reload(); 154 152 } catch (const lua_wrappers::LUAException &e) { 155 153 error = true; 156 message += _T("Exception when reloading script: ") + (*cit)->get_wscript() + _T(": ") + e.getMessage();157 NSC_LOG_ERROR_STD(_T("Exception when reloading script: ") + (*cit)->get_wscript() + _T(": ") + e.getMessage());154 message += _T("Exception when reloading script: ") + i->get_wscript() + _T(": ") + e.getMessage(); 155 NSC_LOG_ERROR_STD(_T("Exception when reloading script: ") + i->get_wscript() + _T(": ") + e.getMessage()); 158 156 } catch (...) { 159 157 error = true; 160 message += _T("Unhandeled Exception when reloading script: ") + (*cit)->get_wscript();161 NSC_LOG_ERROR_STD(_T("Unhandeled Exception when reloading script: ") + (*cit)->get_wscript());158 message += _T("Unhandeled Exception when reloading script: ") + i->get_wscript(); 159 NSC_LOG_ERROR_STD(_T("Unhandeled Exception when reloading script: ") + i->get_wscript()); 162 160 } 163 161 } -
modules/LUAScript/LUAScript.h
rc5ec0c8 ra06e6af 36 36 boost::shared_ptr<lua_wrappers::lua_registry> registry; 37 37 script_container::list_type scripts_; 38 typedef std::list<boost::shared_ptr<script_wrapper::lua_script> > script_list; 38 typedef boost::shared_ptr<script_wrapper::lua_script> script_instance; 39 typedef std::list<script_instance> script_list; 39 40 script_list instances_; 40 41 boost::filesystem::wpath root_; -
modules/LUAScript/lua_wrappers.hpp
rc5ec0c8 ra06e6af 27 27 } 28 28 inline lua_State* get_state() const { 29 int i = lua_gettop(L); 29 30 return L; 30 31 } … … 53 54 return strEx::itos(lua_tonumber(L, pos)); 54 55 return _T("<NOT_A_STRING>"); 56 } 57 int get_int(int pos = -1) { 58 if (pos == -1) 59 pos = lua_gettop(L); 60 if (pos == 0) 61 return 0; 62 if (is_string(pos)) 63 return strEx::stoi(utf8::cvt<std::wstring>(lua_tostring(L, pos))); 64 if (is_number(pos)) 65 return lua_tonumber(L, pos); 66 return 0; 55 67 } 56 68 boolean get_boolean(int pos = -1) { … … 110 122 return _T("<EMPTY>"); 111 123 ret = get_string(top); 124 pop(); 125 return ret; 126 } 127 int pop_int() { 128 int ret; 129 int top = lua_gettop(L); 130 if (top == 0) 131 return 0; 132 ret = get_int(top); 112 133 pop(); 113 134 return ret; … … 160 181 return type; 161 182 } 183 std::wstring get_type_as_string(int pos = -1) { 184 if (pos == -1) 185 pos = lua_gettop(L); 186 if (pos == 0) 187 return _T("<EMPTY>"); 188 switch (lua_type(L, pos)) { 189 case LUA_TNUMBER: 190 return _T("<NUMBER>"); 191 case LUA_TSTRING: 192 return _T("<STRING>"); 193 case LUA_TBOOLEAN: 194 return _T("<TABLE>"); 195 case LUA_TLIGHTUSERDATA: 196 return _T("<LIGHTUSERDATA>"); 197 case LUA_TTABLE: 198 return _T("<TABLE>"); 199 } 200 return _T("<UNKNOWN>"); 201 } 202 162 203 inline bool is_string(int pos = -1) { 163 204 return type(pos) == LUA_TSTRING; … … 198 239 lua_pushboolean(L, b?TRUE:FALSE); 199 240 } 241 void push_int(int b) { 242 lua_pushinteger(L, b); 243 } 200 244 void push_string(std::string s) { 201 245 lua_pushstring(L, s.c_str()); … … 215 259 inline bool empty() { 216 260 return size() == 0; 261 } 262 void log_stack() { 263 int args = size(); 264 NSC_DEBUG_MSG_STD(_T("Invalid lua stack state, dumping stack")); 265 for (int i=1;i<args+1;i++) { 266 NSC_DEBUG_MSG_STD(get_type_as_string(i) +_T(": ") + get_string(i)); 267 } 217 268 } 218 269 … … 243 294 } 244 295 296 inline void openlibs() { 297 luaL_openlibs(L); 298 } 299 300 inline int loadfile(std::string script) { 301 return luaL_loadfile(L, script.c_str()); 302 } 303 304 int pcall(int nargs, int nresults, int errfunc) { 305 return lua_pcall(L, nargs, nresults, errfunc); 306 } 307 308 309 std::string inline op_string(int pos, std::string def = "") { 310 return luaL_optstring(L, pos, def.c_str()); 311 } 312 std::wstring inline op_wstring(int pos, std::string def = "") { 313 return utf8::cvt<std::wstring>(op_string(pos, def)); 314 } 315 std::wstring inline op_wstring(int pos, std::wstring def) { 316 return op_wstring(pos, utf8::cvt<std::string>(def)); 317 } 318 std::string inline string(int pos) { 319 return luaL_checkstring(L, pos); 320 } 321 std::wstring inline wstring(int pos) { 322 return utf8::cvt<std::wstring>(string(pos)); 323 } 324 325 boolean inline checkbool(int pos) { 326 return lua_toboolean(L, pos); 327 } 328 int inline op_int(int pos, int def = 0) { 329 return luaL_optinteger(L, pos, def); 330 } 331 int inline checkint(int pos) { 332 return luaL_checkint(L, pos); 333 } 245 334 }; 246 335 … … 314 403 typedef std::map<std::wstring,function_container> function_map; 315 404 function_map functions; 405 function_map channels; 316 406 public: 317 407 NSCAPI::nagiosReturn on_query(const std::wstring & target, const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & message, std::wstring & perf) … … 362 452 } 363 453 364 bool has_command(const std::wstring & command) 365 { 366 NSC_DEBUG_MSG_STD(_T("Looking for command: ") + command); 454 bool has_command(const std::wstring & command) { 367 455 return functions.find(command) != functions.end(); 368 456 } 369 457 void register_query(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 370 NSC_DEBUG_MSG_STD(_T("Registring command: ") + command);371 458 function_container c; 372 459 c.func_ref = func_ref; … … 375 462 } 376 463 377 void clear() 378 { 379 throw std::exception("The method or operation is not implemented."); 380 } 464 void clear() { 465 functions.clear(); 466 // DO we need to release reference here? 467 } 468 469 void register_subscription(const std::wstring &channel, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 470 function_container c; 471 c.func_ref = func_ref; 472 c.instance = instance; 473 channels[channel] = c; 474 } 475 476 void register_cmdline(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 477 function_container c; 478 c.func_ref = func_ref; 479 c.instance = instance; 480 channels[command] = c; 481 } 482 483 381 484 382 485 -
modules/LUAScript/script_wrapper.hpp
rc5ec0c8 ra06e6af 117 117 class registry_wrapper : public base_script_object { 118 118 private: 119 //registry_wrapper(const registry_wrapper &other) {} 120 //registry_wrapper& operator=(const registry_wrapper &other) {} 121 122 public: 123 124 registry_wrapper(lua_State *L) : base_script_object(L) { 125 NSC_DEBUG_MSG(_T("create (from LUA)")); 126 } 127 /* 128 registry_wrapper(nscapi::core_wrapper* core, unsigned int plugin_id) : core(core), plugin_id(plugin_id) { 129 NSC_DEBUG_MSG(_T("create (from c++)")); 130 } 131 static boost::shared_ptr<registry_wrapper> create(unsigned int plugin_id) { 132 return boost::shared_ptr<registry_wrapper>(new registry_wrapper(nscapi::plugin_singleton->get_core(), plugin_id)); 133 } 134 */ 119 120 public: 121 122 registry_wrapper(lua_State *L) : base_script_object(L) {} 135 123 136 124 static const char className[]; … … 138 126 139 127 int register_function(lua_State *L) { 140 NSC_DEBUG_MSG(_T("register_function")); 141 return 0; 128 lua_wrappers::lua_wrapper lua(L); 129 NSC_LOG_ERROR_STD(_T("Unsupported API called: exec")); 130 return lua.error("Unsupported API called: exec"); 142 131 } 143 132 int register_simple_function(lua_State *L) { 144 NSC_DEBUG_MSG(_T("register_simple_function"));145 133 lua_wrapper lua(L); 146 134 std::wstring description; … … 167 155 } 168 156 int register_cmdline(lua_State *L) { 169 NSC_DEBUG_MSG(_T("register_cmdline")); 170 return 0; 157 lua_wrappers::lua_wrapper lua(L); 158 NSC_LOG_ERROR_STD(_T("Unsupported API called: exec")); 159 return lua.error("Unsupported API called: exec"); 171 160 } 172 161 int register_simple_cmdline(lua_State *L) { 173 NSC_DEBUG_MSG(_T("register_simple_cmdline")); 162 lua_wrapper lua(L); 163 std::wstring name; 164 if (lua.is_string()) { 165 name = lua.pop_string(); 166 lua_getglobal(L, utf8::cvt<std::string>(name).c_str()); 167 } 168 if (!lua.is_function()) 169 return lua.error("Invalid argument not a function: " + utf8::cvt<std::string>(name)); 170 171 int func_ref = luaL_ref(L, LUA_REGISTRYINDEX); 172 173 if (func_ref == 0) 174 return lua.error("Invalid function: " + utf8::cvt<std::string>(name)); 175 std::wstring script = lua.pop_string(); 176 get_instance()->get_registry()->register_cmdline(script, get_instance(), func_ref); 174 177 return 0; 175 178 } 176 179 int subscription(lua_State *L) { 177 NSC_DEBUG_MSG(_T("subscription")); 178 return 0; 180 lua_wrappers::lua_wrapper lua(L); 181 NSC_LOG_ERROR_STD(_T("Unsupported API called: exec")); 182 return lua.error("Unsupported API called: exec"); 179 183 } 180 184 int simple_subscription(lua_State *L) { 181 NSC_DEBUG_MSG(_T("simple_subscription")); 185 lua_wrapper lua(L); 186 std::wstring name; 187 if (lua.is_string()) { 188 name = lua.pop_string(); 189 lua_getglobal(L, utf8::cvt<std::string>(name).c_str()); 190 } 191 if (!lua.is_function()) 192 return lua.error("Invalid argument not a function: " + utf8::cvt<std::string>(name)); 193 194 int func_ref = luaL_ref(L, LUA_REGISTRYINDEX); 195 196 if (func_ref == 0) 197 return lua.error("Invalid function: " + utf8::cvt<std::string>(name)); 198 std::wstring channel = lua.pop_string(); 199 get_instance()->get_core()->registerSubmissionListener(get_instance()->get_plugin_id(), channel); 200 get_instance()->get_registry()->register_subscription(channel, get_instance(), func_ref); 182 201 return 0; 183 202 } … … 207 226 static const Luna<settings_wrapper>::RegType methods[]; 208 227 209 210 211 228 int get_section(lua_State *L) { 212 229 lua_wrapper lua(L); 213 int nargs = lua.size(); 214 if (nargs > 1) 215 return lua.error("Incorrect syntax: get_section([<section>]);"); 216 std::wstring v; 217 if (nargs > 0) 218 v = lua.pop_string(); 230 std::wstring v = lua.op_wstring(1); 219 231 try { 220 232 lua.push_array(get_instance()->get_core()->getSettingsSection(v)); … … 226 238 int get_string(lua_State *L) { 227 239 lua_wrapper lua(L); 228 int nargs = lua.size(); 229 // if (nargs < 2 || nargs > 3) { 230 // return lua.error("Incorrect syntax: get_string(<section>, <key>[, <default value>]);" + utf8::cvt<std::string>(lua.dump_stack())); 231 // } 232 std::wstring v; 233 if (nargs > 2) 234 v = lua.pop_string(); 235 std::wstring k = lua.pop_string(); 236 std::wstring s = lua.pop_string(); 237 if (nargs > 1) 238 lua.pop_string(); 240 std::wstring s = lua.wstring(1); 241 std::wstring k = lua.wstring(2); 242 std::wstring v = lua.op_wstring(3); 239 243 lua.push_string(get_instance()->get_core()->getSettingsString(s, k, v)); 240 244 return 1; 241 245 } 242 246 int set_string(lua_State *L) { 243 NSC_DEBUG_MSG(_T("set_string")); 247 lua_wrapper lua(L); 248 std::wstring s = lua.wstring(1); 249 std::wstring k = lua.wstring(2); 250 std::wstring v = lua.wstring(3); 251 get_instance()->get_core()->SetSettingsString(s, k, v); 244 252 return 0; 245 253 } 246 254 int get_bool(lua_State *L) { 247 255 lua_wrapper lua(L); 248 int nargs = lua.size(); 249 if (nargs < 2 || nargs > 3) 250 return lua.error("Incorrect syntax: get_string(<section>, <key>[, <default value>]);"); 251 bool v; 252 if (nargs > 2) 253 v = lua.pop_boolean(); 254 std::wstring k = lua.pop_string(); 255 std::wstring s = lua.pop_string(); 256 std::wstring s = lua.wstring(1); 257 std::wstring k = lua.wstring(2); 258 bool v = lua.checkbool(3); 256 259 lua.push_boolean(get_instance()->get_core()->getSettingsInt(s, k, v?1:0)==1); 257 260 return 1; 258 261 } 259 262 int set_bool(lua_State *L) { 260 NSC_DEBUG_MSG(_T("set_bool")); 263 lua_wrapper lua(L); 264 std::wstring s = lua.wstring(1); 265 std::wstring k = lua.wstring(2); 266 bool v = lua.checkbool(3); 267 get_instance()->get_core()->SetSettingsInt(s, k, v?1:0); 261 268 return 0; 262 269 } 263 270 int get_int(lua_State *L) { 264 NSC_DEBUG_MSG(_T("get_int")); 265 return 0; 271 lua_wrapper lua(L); 272 std::wstring s = lua.wstring(1); 273 std::wstring k = lua.wstring(2); 274 int v = lua.checkint(3); 275 lua.push_int(get_instance()->get_core()->getSettingsInt(s, k, v)); 276 return 1; 266 277 } 267 278 int set_int(lua_State *L) { 268 NSC_DEBUG_MSG(_T("set_int")); 279 lua_wrapper lua(L); 280 std::wstring s = lua.wstring(1); 281 std::wstring k = lua.wstring(2); 282 int v = lua.checkint(3); 283 get_instance()->get_core()->SetSettingsInt(s, k, v); 269 284 return 0; 270 285 } 271 286 int save(lua_State *L) { 272 NSC_DEBUG_MSG(_T("save"));287 get_instance()->get_core()->settings_save(); 273 288 return 0; 274 289 } 275 290 int register_path(lua_State *L) { 276 NSC_DEBUG_MSG(_T("register_path")); 277 return 0; 278 } 291 lua_wrapper lua(L); 292 std::wstring path = lua.wstring(1); 293 std::wstring title = lua.wstring(1); 294 std::wstring description = lua.wstring(1); 295 get_instance()->get_core()->settings_register_path(path, title, description, false); 296 return 0; 297 } 298 NSCAPI::settings_type script_wrapper::settings_wrapper::get_type(std::string stype) { 299 if (stype == "string" || stype == "str" || stype == "s") 300 return NSCAPI::key_string; 301 if (stype == "integer" || stype == "int" || stype == "i") 302 return NSCAPI::key_integer; 303 if (stype == "bool" || stype == "b") 304 return NSCAPI::key_bool; 305 NSC_LOG_ERROR_STD(_T("Invalid settings type")); 306 return NSCAPI::key_string; 307 } 308 279 309 int register_key(lua_State *L) { 280 NSC_DEBUG_MSG(_T("register_key")); 310 lua_wrapper lua(L); 311 std::wstring path = lua.wstring(1); 312 std::wstring key = lua.wstring(1); 313 std::wstring stype = lua.wstring(1); 314 NSCAPI::settings_type type = get_type(stype); 315 std::wstring title = lua.wstring(1); 316 std::wstring description = lua.wstring(1); 317 std::wstring defaultValue = lua.wstring(1); 318 get_instance()->get_core()->settings_register_key(path, key, type, title, description, defaultValue, false); 281 319 return 0; 282 320 } … … 380 418 class lua_script : public script_instance, public boost::enable_shared_from_this<lua_script> { 381 419 lua_script(nscapi::core_wrapper* core, const int plugin_id, boost::shared_ptr<lua_wrappers::lua_registry> registry, const std::string alias, const std::string script) 382 : script_instance(core, plugin_id, registry, alias, script) { 383 load(); 384 } 420 : script_instance(core, plugin_id, registry, alias, script) {} 385 421 public: 386 422 387 423 static boost::shared_ptr<lua_script> create_instance(nscapi::core_wrapper* core, const int plugin_id, boost::shared_ptr<lua_wrappers::lua_registry> registry, const std::wstring alias, const std::wstring script) { 388 424 boost::shared_ptr<lua_script> instance(new lua_script(core, plugin_id, registry, utf8::cvt<std::string>(alias), utf8::cvt<std::string>(script))); 389 if (instance) 425 if (instance) { 390 426 instance->init(); 427 instance->load(); 428 } 391 429 return instance; 392 430 } … … 396 434 397 435 void load() { 398 int i = lua_gettop(get_lua_state()); 399 luaL_openlibs(get_lua_state()); 400 i = lua_gettop(get_lua_state()); 436 lua_wrappers::lua_wrapper lua(get_lua_state()); 437 lua.openlibs(); 401 438 nsclient_wrapper::luaopen(get_lua_state()); 402 i = lua_gettop(get_lua_state());403 if (luaL_loadfile(get_lua_state(), get_script().c_str()) != 0) {404 throw lua_wrappers::LUAException(_T("Failed to load script: ") + get_wscript() + _T(": ") + utf8::cvt<std::wstring>(lua_tostring(get_lua_state(), -1)));405 }439 if (lua.loadfile(get_script()) != 0) 440 throw lua_wrappers::LUAException(_T("Failed to load script: ") + get_wscript() + _T(": ") + lua.pop_string()); 441 if (lua.pcall(0, 0, 0) != 0) 442 throw lua_wrappers::LUAException(_T("Failed to execute script: ") + get_wscript() + _T(": ") + lua.pop_string()); 406 443 } 407 444 std::wstring get_wscript() const { … … 412 449 unload(); 413 450 load(); 414 pre_load(); 415 } 416 void pre_load() { 417 if (lua_pcall(get_lua_state(), 0, 0, 0) != 0) { 418 throw lua_wrappers::LUAException(_T("Failed to parse script: ") + get_wscript() + _T(": ") + utf8::cvt<std::wstring>(lua_tostring(get_lua_state(), -1))); 419 } 420 } 421 422 423 /* 424 NSCAPI::nagiosReturn handleCommand(lua_handler *handler, std::wstring function, std::wstring cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) { 425 lua_manager::set_handler(L, handler); 426 lua_manager::set_script(L, this); 427 lua_wrapper lua(L); 428 int nargs = lua.size(); 429 lua_getglobal(L, utf8::cvt<std::string>(function).c_str()); 430 if (!lua_isfunction(L, -1)) { 431 lua_pop(L, 1); // remove function from LUA stack 432 throw lua_wrappers::LUAException(_T("Failed to run script: ") + get_wscript() + _T(": Function not found: handle")); 433 } 434 lua.push_string(cmd); 435 lua.push_array(arguments); 436 437 if (lua_pcall(L, 2, LUA_MULTRET, 0) != 0) { 438 std::wstring err = lua.pop_string(); 439 NSC_LOG_ERROR_STD(_T("Failed to call main function in script: ") + get_wscript() + _T(": ") + err); 440 return NSCAPI::returnUNKNOWN; 441 } 442 return extract_return(L, lua.size(), msg, perf); 443 } 444 */ 445 }; 446 447 448 449 450 451 } 452 }; 451 453 } -
version.hpp
rc5ec0c8 ra06e6af 1 1 #ifndef VERSION_HPP 2 2 #define VERSION_HPP 3 #define PRODUCTVER 0,4,0,13 04 #define STRPRODUCTVER "0,4,0,13 0"5 #define STRPRODUCTDATE "2012-01- 26"3 #define PRODUCTVER 0,4,0,131 4 #define STRPRODUCTVER "0,4,0,131" 5 #define STRPRODUCTDATE "2012-01-31" 6 6 #endif // VERSION_HPP -
version.txt
rc5ec0c8 ra06e6af 1 1 version=0.4.0 2 build=13 03 date=2012-01- 262 build=131 3 date=2012-01-31
Note: See TracChangeset
for help on using the changeset viewer.








