Skip to content

Commit

Permalink
moved common parts in superclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kraynyukhov committed Sep 1, 2018
1 parent 0806051 commit 8c2cc87
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 115 deletions.
56 changes: 0 additions & 56 deletions include/ApplicationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ extern "C" {
#include <abstract/Worker.h>
#include <abstract/ApplicationContext.h>

#include <modules/nljson.h>
#include <modules/wsSend.h>
#include <modules/bcast.h>
#include <modules/pam_auth.h>
#include <modules/murmur.h>
#include <modules/time_now.h>
#include <modules/mqr.h>
#include <modules/cws.h>
#include <modules/nap.h>

#include <Config.h>


using json = nlohmann::json;


Expand Down Expand Up @@ -310,49 +297,6 @@ namespace LAppS
explicit ApplicationContext(const std::string& appname, abstract::Application* parent)
: ::abstract::ApplicationContext(appname), mParent(parent),mustStop{false}
{
luaL_openlibs(mLState);

luaopen_nljson(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"nljson");

luaopen_wssend(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"ws");

try{
json& modules=LAppSConfig::getInstance()->getLAppSConfig()["services"][appname]["preload"];

for(const std::string& module: modules)
{
if(module == "pam_auth")
{
luaopen_pam_auth(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"pam_auth");

}else if(module == "time")
{
luaopen_time(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"time");
}else if(module == "murmur")
{
luaopen_murmur(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"murmur");
}else if(module == "mqr")
{
luaopen_mqr(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"mqr");
}else if(module == "cws")
{
luaopen_cws(mLState) ;
lua_setfield(mLState,LUA_GLOBALSINDEX,"cws");
}else{
itc::getLog()->error(__FILE__,__LINE__,"No such module %s", module.c_str());
}
}
}catch(const std::exception& e)
{
itc::getLog()->error(__FILE__,__LINE__,"An exception %s is risen on modules preload for service %s", e.what(), appname.c_str());
}

add_bcast(mProtocol);

if(require(mName))
Expand Down
59 changes: 1 addition & 58 deletions include/InternalAppContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ extern "C" {
#include <stdexcept>
}

#include <modules/nljson.h>
#include <modules/wsSend.h>
#include <modules/bcast.h>
#include <modules/pam_auth.h>
#include <modules/murmur.h>
#include <modules/time_now.h>
#include <modules/mqr.h>
#include <modules/cws.h>
#include <modules/nap.h>

static thread_local std::atomic<bool>* mustStop=nullptr; // per instance

extern "C" {
Expand Down Expand Up @@ -106,62 +96,15 @@ namespace LAppS
InternalAppContext(const std::string& name):
::abstract::ApplicationContext(name), mCanStop{false}, mMustStop{false}
{
luaL_openlibs(mLState);

luaopen_nljson(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"nljson");
cleanLuaStack();


luaopen_bcast(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"bcast");
cleanLuaStack();

luaopen_nap(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"nap");
cleanLuaStack();

try{
json& modules=LAppSConfig::getInstance()->getLAppSConfig()["services"][name]["preload"];

for(const std::string& module: modules)
{
if(module == "pam_auth")
{
luaopen_pam_auth(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"pam_auth");

}else if(module == "time")
{
luaopen_time(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"time");
}else if(module == "murmur")
{
luaopen_murmur(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"murmur");
}else if(module == "mqr")
{
luaopen_mqr(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"mqr");
}else if(module == "cws")
{
luaopen_cws(mLState) ;
lua_setfield(mLState,LUA_GLOBALSINDEX,"cws");
}else{
itc::getLog()->error(__FILE__,__LINE__,"No such module %s", module.c_str());
}
}
}catch(const std::exception& e)
{
itc::getLog()->error(__FILE__,__LINE__,"An exception %s is risen on modules preload for service %s", e.what(), name.c_str());
}

cleanLuaStack();

lua_pushcfunction(mLState,must_stop);
lua_setglobal(mLState,"must_stop");



if(require(mName))
{
itc::getLog()->info(__FILE__,__LINE__,"Lua module %s is registered",mName.c_str());
Expand Down
66 changes: 65 additions & 1 deletion include/abstract/ApplicationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@ extern "C" {
#include <lualib.h>
#include <lauxlib.h>
#include <stdexcept>

}

#include <modules/nljson.h>
#include <modules/wsSend.h>
#include <modules/bcast.h>
#include <modules/pam_auth.h>
#include <modules/murmur.h>
#include <modules/time_now.h>
#include <modules/mqr.h>
#include <modules/cws.h>
#include <modules/nap.h>
//#include <modules/cppstring.h>
//#include <modules/cppsvector.h>

#include <Config.h>

namespace abstract
{
class ApplicationContext
Expand Down Expand Up @@ -96,7 +111,56 @@ namespace abstract
ApplicationContext(ApplicationContext&)=delete;
explicit ApplicationContext(const std::string& name)
: mName(name),mLState(luaL_newstate())
{}
{
luaL_openlibs(mLState);

luaopen_nljson(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"nljson");
cleanLuaStack();

luaopen_nap(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"nap");
cleanLuaStack();

try{
json& modules=LAppSConfig::getInstance()->getLAppSConfig()["services"][name]["preload"];

for(const std::string& module: modules)
{
if(module == "pam_auth")
{
luaopen_pam_auth(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"pam_auth");
cleanLuaStack();
}else if(module == "time")
{
luaopen_time(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"time");
cleanLuaStack();
}else if(module == "murmur")
{
luaopen_murmur(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"murmur");
cleanLuaStack();
}else if(module == "mqr")
{
luaopen_mqr(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"mqr");
cleanLuaStack();
}else if(module == "cws")
{
luaopen_cws(mLState);
lua_setfield(mLState,LUA_GLOBALSINDEX,"cws");
cleanLuaStack();
}else{
itc::getLog()->error(__FILE__,__LINE__,"No such module %s", module.c_str());
}
}
}catch(const std::exception& e)
{
itc::getLog()->error(__FILE__,__LINE__,"An exception %s is risen on modules preload for service %s", e.what(), name.c_str());
}
}
const std::string& getName() const {
return mName;
}
Expand Down

0 comments on commit 8c2cc87

Please # to comment.