From 7fa460e9dc44c9d8c1e11add3547d7b772157f0a Mon Sep 17 00:00:00 2001 From: Joakim Kilby Date: Wed, 24 Jun 2015 19:41:59 +0200 Subject: [PATCH] changed definition of Lua function slightly to incorporate a boolean defining if the script should be shared or not. added a constructor with the sharing variable as default to zero so every script doesn't have to be changed. added functionality in runScript function to check if a script should be shared and if so send it. --- include/openspace/scripting/scriptengine.h | 16 +++-- src/scripting/scriptengine.cpp | 78 +++++++++++++++++++--- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index 3e90dcdd92..94f52796d5 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -46,6 +46,17 @@ public: lua_CFunction function; std::string argumentText; std::string helpText; + bool parallelShared; + + Function(std::string n, lua_CFunction f, std::string a, std::string h , bool ps = false): + name(n), + function(f), + argumentText(a), + helpText(h), + parallelShared(ps) + { + + } }; std::string name; std::vector functions; @@ -78,8 +89,6 @@ public: void preSynchronization(); void queueScript(const std::string &script); - - std::vector executedScripts(); std::vector allLuaFunctions() const; @@ -101,9 +110,6 @@ private: std::vector _receivedScripts; std::string _currentSyncedScript; - //parallel variables @TODO make a more permanent solution to this - JK - std::vector _executedScripts; - std::mutex _executedScriptsMutex; }; } // namespace scripting diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 466c75cbf0..ef7d0dac81 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -141,7 +141,7 @@ bool ScriptEngine::runScript(const std::string& script) { LWARNING("Script was empty"); return false; } - + int status = luaL_loadstring(_state, script.c_str()); if (status != LUA_OK) { LERROR("Error loading script: '" << lua_tostring(_state, -1) << "'"); @@ -155,6 +155,72 @@ bool ScriptEngine::runScript(const std::string& script) { return false; } + //if we're currently hosting the parallel session find out if script should be synchronized. + if(OsEng.parallelConnection()->isHost()){ + + //"deconstruct the script to find library and function name + //assuming a script looks like: "openspace.library.function()" + //or openspace.funcion() + std::string sub; + std::string lib; + std::string func; + //find first "." + std::size_t pos = script.find("."); + if(pos != std::string::npos){ + //strip "openspace." + sub = script.substr(pos + 1, script.size()); + pos = sub.find("."); + //one more "." was found, we have a library name + if(pos != std::string::npos){ + //assing library name + lib = sub.substr(0, pos); + //strip "library." + sub = sub.substr(pos + 1, sub.size()); + + pos = sub.find("("); + if(pos != std::string::npos && pos > 0){ + //strip the () and we're left with function name + func = sub.substr(0, pos); + } + } + else{ + //no more "." was found, we have the case of "openspace.funcion()" + pos = sub.find("("); + if(pos != std::string::npos && pos > 0){ + //strip the () and we're left with function name + func = sub.substr(0, pos); + } + } + } + + LuaLibrary *library = nullptr; + std::set::const_iterator libit; + for(libit = _registeredLibraries.cbegin(); + libit != _registeredLibraries.cend(); + ++libit){ + if(libit->name.compare(lib) == 0){ + break; + } + } + + std::vector::const_iterator funcit; + //library was found + if(libit != _registeredLibraries.cend()){ + for( funcit = libit->functions.cbegin(); + funcit != libit->functions.cend(); + ++funcit){ + //function was found! + if(funcit->name.compare(func) == 0){ + //is the function of a type that should be shared via parallel connection? + //and are we currently hosting the session? + if(funcit->parallelShared ){ + OsEng.parallelConnection()->sendScript(script); + } + } + } + } + } + return true; } @@ -498,10 +564,6 @@ void ScriptEngine::deserialize(SyncBuffer* syncBuffer){ _mutex.lock(); _receivedScripts.push_back(_currentSyncedScript); _mutex.unlock(); - - _executedScriptsMutex.lock(); - _executedScripts.push_back(_currentSyncedScript); - _executedScriptsMutex.unlock(); } } @@ -539,12 +601,6 @@ void ScriptEngine::queueScript(const std::string &script){ _mutex.unlock(); } - -std::vector ScriptEngine::executedScripts(){ - std::lock_guard lockGuard(_executedScriptsMutex); - return _executedScripts; -} - } // namespace scripting } // namespace openspace