Changeset 47b843a in nscp for modules/LUAScript
- Timestamp:
- 12/16/07 21:49:16 (6 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- 5ac88c0a
- Parents:
- 3f69109
- Location:
- modules/LUAScript
- Files:
-
- 1 added
- 3 edited
-
LUAScript-2005.vcproj (modified) (1 diff)
-
LUAScript.cpp (modified) (3 diffs)
-
LUAScript.h (modified) (3 diffs)
-
script_wrapper.hpp (added)
Legend:
- Unmodified
- Added
- Removed
-
modules/LUAScript/LUAScript-2005.vcproj
r3f69109 r47b843a 1802 1802 </File> 1803 1803 <File 1804 RelativePath=".\script_wrapper.hpp" 1805 > 1806 </File> 1807 <File 1804 1808 RelativePath=".\stdafx.h" 1805 1809 > -
modules/LUAScript/LUAScript.cpp
r3f69109 r47b843a 26 26 #include <error.hpp> 27 27 28 extern "C" {29 #include "lua.h"30 #include "lauxlib.h"31 #include "lualib.h"32 33 }34 #include "luna.h"35 28 36 29 LUAScript gLUAScript; … … 49 42 50 43 bool LUAScript::loadModule() { 44 //LUA Scripts 45 std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(LUA_SCRIPT_SECTION_TITLE); 46 std::list<std::wstring>::const_iterator it; 47 for (it = commands.begin(); it != commands.end(); ++it) { 48 loadScript((*it)); 49 } 51 50 return true; 52 51 } 52 53 void LUAScript::register_command(script_wrapper::lua_script* script, std::wstring command, std::wstring function) { 54 NSC_LOG_MESSAGE(_T("Script loading: ") + script->get_script() + _T(": ") + command); 55 strEx::blindstr bstr = command.c_str(); 56 commands_[bstr] = lua_func(script, function); 57 } 58 59 bool LUAScript::loadScript(const std::wstring file) { 60 try { 61 script_wrapper::lua_script *script = new script_wrapper::lua_script(file); 62 script->pre_load(this); 63 scripts_.push_back(script); 64 return true; 65 } catch (script_wrapper::LUAException e) { 66 NSC_LOG_ERROR_STD(_T("Could not load script: ") + file + _T(", ") + e.getMessage()); 67 } catch (...) { 68 NSC_LOG_ERROR_STD(_T("Could not load script: (Unknown exception) ") + file); 69 assert(false); 70 } 71 return false; 72 } 73 74 53 75 bool LUAScript::unloadModule() { 76 for (script_list::const_iterator cit = scripts_.begin(); cit != scripts_.end() ; ++cit) { 77 delete (*cit); 78 } 79 scripts_.clear(); 54 80 return true; 55 81 } … … 62 88 } 63 89 64 class Lua_State65 {66 lua_State *L;67 public:68 Lua_State() : L(lua_open()) { }69 90 70 ~Lua_State() { 71 lua_close(L); 91 bool LUAScript::reload(std::wstring &message) { 92 bool error = false; 93 commands_.clear(); 94 for (script_list::const_iterator cit = scripts_.begin(); cit != scripts_.end() ; ++cit) { 95 try { 96 (*cit)->reload(this); 97 } catch (script_wrapper::LUAException e) { 98 error = true; 99 message += _T("Exception when reloading script: ") + (*cit)->get_script() + _T(": ") + e.getMessage(); 100 NSC_LOG_ERROR_STD(_T("Exception when reloading script: ") + (*cit)->get_script() + _T(": ") + e.getMessage()); 101 } catch (...) { 102 error = true; 103 message += _T("Unhandeled Exception when reloading script: ") + (*cit)->get_script(); 104 NSC_LOG_ERROR_STD(_T("Unhandeled Exception when reloading script: ") + (*cit)->get_script()); 105 } 72 106 } 73 74 // implicitly act as a lua_State pointer 75 inline operator lua_State*() { 76 return L; 77 } 78 }; 79 80 NSCAPI::nagiosReturn LUAScript::RunLUA(const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { 81 Lua_State L; 82 std::wstring script = _T("scripts\\test.lua"); 83 luaL_openlibs(L); 84 85 //Luna<Account>::Register(L); 86 87 if (luaL_loadfile(L, strEx::wstring_to_string(script).c_str()) != 0) { 88 std::wstring err = strEx::string_to_wstring(lua_tostring(L, -1)); 89 NSC_LOG_ERROR_STD(_T("Failed to load script: ") + script + _T(": ") + err); 90 return NSCAPI::returnUNKNOWN; 91 } 92 93 if (lua_pcall(L, 0, 0, 0) != 0) { 94 std::wstring err = strEx::string_to_wstring(lua_tostring(L, -1)); 95 NSC_LOG_ERROR_STD(_T("Failed to parse script: ") + script + _T(": ") + err); 96 return NSCAPI::returnUNKNOWN; 97 } 98 99 int nargs = lua_gettop( L ); 100 lua_getglobal(L, "main"); 101 if (!lua_isfunction(L, -1)) { 102 lua_pop(L, 1); // remove function from LUA stack 103 NSC_LOG_ERROR_STD(_T("main was not found in script: ") + script); 104 return NSCAPI::returnUNKNOWN; 105 } 106 lua_pushstring(L, "test"); 107 108 if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) { 109 std::wstring err = strEx::string_to_wstring(lua_tostring(L, -1)); 110 NSC_LOG_ERROR_STD(_T("Failed to call main function in script: ") + script + _T(": ") + err); 111 lua_pop(L, 1); // remove error message 112 return NSCAPI::returnUNKNOWN; 113 } 114 //extract_args(L, lua_gettop( L )-nargs) 115 116 std::cout << "[C++] These values were returned from the script" << std::endl; 117 while (lua_gettop( L )) 118 { 119 switch (lua_type( L, lua_gettop( L ) )) 120 { 121 case LUA_TNUMBER: std::cout << lua_gettop( L ) << " script returned " << lua_tonumber( L, lua_gettop( L ) ) << std::endl; break; 122 case LUA_TTABLE: std::cout << lua_gettop( L ) << " script returned a table" << std::endl; break; 123 case LUA_TSTRING: std::cout << lua_gettop( L ) << " script returned " << lua_tostring( L, lua_gettop( L ) ) << std::endl; break; 124 case LUA_TBOOLEAN:std::cout << lua_gettop( L ) << " script returned " << lua_toboolean( L, lua_gettop( L ) ) << std::endl; break; 125 default: std::cout << "script returned unknown param" << std::endl; break; 126 } 127 lua_pop( L, 1 ); 128 } 129 NSC_LOG_ERROR_STD(_T("We got:") + strEx::itos(lua_gettop(L))); 107 if (!error) 108 message = _T("LUA scripts Reloaded..."); 109 return !error; 110 } 130 111 131 112 132 /*133 std::wstring ret1 = strEx::string_to_wstring(lua_tostring(L, -3));134 NSC_LOG_ERROR_STD(_T("Return values are:") + ret1);135 std::wstring ret2 = strEx::string_to_wstring(lua_tostring(L, -2));136 NSC_LOG_ERROR_STD(_T("Return values are:") + ret2);137 std::wstring ret3 = strEx::string_to_wstring(lua_tostring(L, -1));138 NSC_LOG_ERROR_STD(_T("Return values are:") + ret3);139 */140 //lua_setgcthreshold(L, 0); // collected garbage141 std::wcout << _T("humm 001") << std::endl;142 return 0;143 }144 113 145 114 NSCAPI::nagiosReturn LUAScript::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { 146 if (command == _T("RunLUA")) { 147 return RunLUA(argLen, char_args, msg, perf); 148 } 149 return NSCAPI::returnIgnored; 115 if (command == _T("LuaReload")) { 116 return reload(msg)?NSCAPI::returnOK:NSCAPI::returnCRIT; 117 } 118 cmd_list::const_iterator cit = commands_.find(command); 119 if (cit == commands_.end()) 120 return NSCAPI::returnIgnored; 121 return (*cit).second.handleCommand(this, command, argLen, char_args, msg, perf); 150 122 } 151 123 -
modules/LUAScript/LUAScript.h
r3f69109 r47b843a 24 24 #include <utils.h> 25 25 #include <checkHelpers.hpp> 26 #include "script_wrapper.hpp" 26 27 27 class LUAScript {28 class LUAScript : script_wrapper::lua_handler { 28 29 private: 30 31 class lua_func { 32 public: 33 lua_func(script_wrapper::lua_script* script_, std::wstring function_) : script(script_), function(function_) {} 34 lua_func() : script(NULL) {} 35 script_wrapper::lua_script* script; 36 std::wstring function; 37 38 NSCAPI::nagiosReturn handleCommand(lua_handler *handler, strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) const { 39 return script->handleCommand(handler, function, command, argLen, char_args, msg, perf); 40 } 41 }; 42 43 typedef std::map<strEx::blindstr,lua_func> cmd_list; 44 typedef std::list<script_wrapper::lua_script*> script_list; 45 46 cmd_list commands_; 47 script_list scripts_; 29 48 30 49 public: … … 34 53 bool loadModule(); 35 54 bool unloadModule(); 55 bool reload(std::wstring &msg); 36 56 37 57 std::wstring getModuleName() { … … 48 68 bool hasCommandHandler(); 49 69 bool hasMessageHandler(); 70 bool loadScript(const std::wstring script); 50 71 NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 51 NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 72 //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 73 //NSCAPI::nagiosReturn extract_return(Lua_State &L, int arg_count, std::wstring &message, std::wstring &perf); 74 75 //script_wrapper::lua_handler 76 void register_command(script_wrapper::lua_script* script, std::wstring command, std::wstring function); 52 77 53 78 private:
Note: See TracChangeset
for help on using the changeset viewer.








