changeset 2617:912a767911b0 jobs

back to a single Lua context
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 May 2018 12:45:34 +0200
parents 2f3007bf0708
children ef5b45884972
files OrthancServer/IServerListener.h OrthancServer/LuaScripting.h OrthancServer/OrthancFindRequestHandler.cpp OrthancServer/OrthancRestApi/OrthancRestSystem.cpp OrthancServer/QueryRetrieveHandler.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/main.cpp
diffstat 8 files changed, 27 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/IServerListener.h	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/IServerListener.h	Tue May 22 12:45:34 2018 +0200
@@ -40,7 +40,7 @@
 
 namespace Orthanc
 {
-  class IServerListener
+  class IServerListener : public boost::noncopyable
   {
   public:
     virtual ~IServerListener()
--- a/OrthancServer/LuaScripting.h	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/LuaScripting.h	Tue May 22 12:45:34 2018 +0200
@@ -88,7 +88,7 @@
       boost::mutex::scoped_lock  lock_;
 
     public:
-      Lock(LuaScripting& that) : 
+      explicit Lock(LuaScripting& that) : 
         that_(that), 
         lock_(that.mutex_)
       {
--- a/OrthancServer/OrthancFindRequestHandler.cpp	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/OrthancFindRequestHandler.cpp	Tue May 22 12:45:34 2018 +0200
@@ -47,25 +47,6 @@
 
 namespace Orthanc
 {
-  static LuaScripting& GetLuaScripting(ServerContext& context)
-  {
-    // Returns a singleton Lua context
-    static boost::mutex mutex_;
-    static std::auto_ptr<LuaScripting>  lua_;
-    
-    boost::mutex::scoped_lock lock(mutex_);
-
-    if (lua_.get() == NULL)
-    {
-      LOG(INFO) << "Initializing Lua for OrthancFindRequestHandler";
-      lua_.reset(new LuaScripting(context));
-      lua_->LoadGlobalConfiguration();
-    }
-
-    return *lua_;
-  }
-
-
   static void GetChildren(std::list<std::string>& target,
                           ServerIndex& index,
                           const std::list<std::string>& source)
@@ -505,7 +486,8 @@
   {
     static const char* LUA_CALLBACK = "IncomingFindRequestFilter";
     
-    LuaScripting::Lock lock(GetLuaScripting(context_));
+    LuaScripting::Lock lock(context_.GetLuaScripting());
+
     if (!lock.GetLua().IsExistingFunction(LUA_CALLBACK))
     {
       return false;
--- a/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Tue May 22 12:45:34 2018 +0200
@@ -124,7 +124,7 @@
     call.BodyToString(command);
 
     {
-      LuaScripting::Lock lock(context.GetLuaEventHandler());
+      LuaScripting::Lock lock(context.GetLuaScripting());
       lock.GetLua().Execute(result, command);
     }
 
--- a/OrthancServer/QueryRetrieveHandler.cpp	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/QueryRetrieveHandler.cpp	Tue May 22 12:45:34 2018 +0200
@@ -42,32 +42,13 @@
 
 namespace Orthanc
 {
-  static LuaScripting& GetLuaScripting(ServerContext& context)
-  {
-    // Returns a singleton Lua context
-    static boost::mutex mutex_;
-    static std::auto_ptr<LuaScripting>  lua_;
-    
-    boost::mutex::scoped_lock lock(mutex_);
-
-    if (lua_.get() == NULL)
-    {
-      LOG(INFO) << "Initializing Lua for QueryRetrieveHandler";
-      lua_.reset(new LuaScripting(context));
-      lua_->LoadGlobalConfiguration();
-    }
-
-    return *lua_;
-  }
-
-
   static void FixQueryLua(DicomMap& query,
                           ServerContext& context,
                           const std::string& modality)
   {
     static const char* LUA_CALLBACK = "OutgoingFindRequestFilter";
 
-    LuaScripting::Lock lock(GetLuaScripting(context));
+    LuaScripting::Lock lock(context.GetLuaScripting());
 
     if (lock.GetLua().IsExistingFunction(LUA_CALLBACK))
     {
--- a/OrthancServer/ServerContext.cpp	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/ServerContext.cpp	Tue May 22 12:45:34 2018 +0200
@@ -114,7 +114,7 @@
     storeMD5_(true),
     provider_(*this),
     dicomCache_(provider_, DICOM_CACHE_SIZE),
-    luaEventHandler_(*this),
+    lua_(*this),
 #if ORTHANC_ENABLE_PLUGINS == 1
     plugins_(NULL),
 #endif
@@ -122,7 +122,7 @@
     queryRetrieveArchive_(Configuration::GetGlobalUnsignedIntegerParameter("QueryRetrieveSize", 10)),
     defaultLocalAet_(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"))
   {
-    listeners_.push_back(ServerListener(luaEventHandler_, "Lua"));
+    listeners_.push_back(ServerListener(lua_, "Lua"));
 
     jobsEngine_.SetWorkersCount(Configuration::GetGlobalUnsignedIntegerParameter("ConcurrentJobs", 2));
     //jobsEngine_.SetMaxCompleted   // TODO
@@ -580,7 +580,7 @@
 
     // TODO REFACTOR THIS
     listeners_.clear();
-    listeners_.push_back(ServerListener(luaEventHandler_, "Lua"));
+    listeners_.push_back(ServerListener(lua_, "Lua"));
     listeners_.push_back(ServerListener(plugins, "plugin"));
   }
 
@@ -593,7 +593,7 @@
 
     // TODO REFACTOR THIS
     listeners_.clear();
-    listeners_.push_back(ServerListener(luaEventHandler_, "Lua"));
+    listeners_.push_back(ServerListener(lua_, "Lua"));
   }
 
 
--- a/OrthancServer/ServerContext.h	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/ServerContext.h	Tue May 22 12:45:34 2018 +0200
@@ -119,7 +119,7 @@
     MemoryCache dicomCache_;
     JobsEngine jobsEngine_;
 
-    LuaScripting luaEventHandler_;
+    LuaScripting lua_;
 
 #if ORTHANC_ENABLE_PLUGINS == 1
     OrthancPlugins* plugins_;
@@ -257,9 +257,9 @@
       return defaultLocalAet_;
     }
 
-    LuaScripting& GetLuaEventHandler()
+    LuaScripting& GetLuaScripting()
     {
-      return luaEventHandler_;
+      return lua_;
     }
 
     OrthancHttpHandler& GetHttpHandler()
--- a/OrthancServer/main.cpp	Tue May 22 12:25:37 2018 +0200
+++ b/OrthancServer/main.cpp	Tue May 22 12:45:34 2018 +0200
@@ -167,19 +167,16 @@
 class OrthancApplicationEntityFilter : public IApplicationEntityFilter
 {
 private:
-  LuaScripting  lua_;
-  bool          alwaysAllowEcho_;
-  bool          alwaysAllowStore_;
+  ServerContext&  context_;
+  bool            alwaysAllowEcho_;
+  bool            alwaysAllowStore_;
 
 public:
   OrthancApplicationEntityFilter(ServerContext& context) :
-    lua_(context)
+    context_(context)
   {
     alwaysAllowEcho_ = Configuration::GetGlobalBoolParameter("DicomAlwaysAllowEcho", true);
     alwaysAllowStore_ = Configuration::GetGlobalBoolParameter("DicomAlwaysAllowStore", true);
-
-    LOG(INFO) << "Initializing Lua for OrthancApplicationEntityFilter";
-    lua_.LoadGlobalConfiguration();
   }
 
   virtual bool IsAllowedConnection(const std::string& remoteIp,
@@ -268,7 +265,7 @@
     {
       std::string name = "Is" + configuration;
 
-      LuaScripting::Lock lock(lua_);
+      LuaScripting::Lock lock(context_.GetLuaScripting());
       
       if (lock.GetLua().IsExistingFunction(name.c_str()))
       {
@@ -293,7 +290,7 @@
     {
       std::string lua = "Is" + std::string(configuration);
 
-      LuaScripting::Lock lock(lua_);
+      LuaScripting::Lock lock(context_.GetLuaScripting());
       
       if (lock.GetLua().IsExistingFunction(lua.c_str()))
       {
@@ -313,17 +310,15 @@
 class MyIncomingHttpRequestFilter : public IIncomingHttpRequestFilter
 {
 private:
-  LuaScripting     lua_;
+  ServerContext&   context_;
   OrthancPlugins*  plugins_;
 
 public:
   MyIncomingHttpRequestFilter(ServerContext& context,
                               OrthancPlugins* plugins) : 
-    lua_(context),
+    context_(context),
     plugins_(plugins)
   {
-    LOG(INFO) << "Initializing Lua for MyIncomingHttpRequestFilter";
-    lua_.LoadGlobalConfiguration();
   }
 
   virtual bool IsAllowed(HttpMethod method,
@@ -341,7 +336,7 @@
 
     static const char* HTTP_FILTER = "IncomingHttpRequestFilter";
 
-    LuaScripting::Lock lock(lua_);
+    LuaScripting::Lock lock(context_.GetLuaScripting());
 
     // Test if the instance must be filtered out
     if (lock.GetLua().IsExistingFunction(HTTP_FILTER))
@@ -675,8 +670,8 @@
   }
 #endif
 
-  context.GetLuaEventHandler().Start();
-  context.GetLuaEventHandler().Execute("Initialize");
+  context.GetLuaScripting().Start();
+  context.GetLuaScripting().Execute("Initialize");
 
   bool restart;
 
@@ -710,8 +705,8 @@
     }
   }
 
-  context.GetLuaEventHandler().Execute("Finalize");
-  context.GetLuaEventHandler().Stop();
+  context.GetLuaScripting().Execute("Finalize");
+  context.GetLuaScripting().Stop();
 
 #if ORTHANC_ENABLE_PLUGINS == 1
   if (context.HasPlugins())
@@ -1000,7 +995,7 @@
   }
 
   LOG(INFO) << "Initializing Lua for the event handler";
-  context.GetLuaEventHandler().LoadGlobalConfiguration();
+  context.GetLuaScripting().LoadGlobalConfiguration();
 
 #if ORTHANC_ENABLE_PLUGINS == 1
   if (plugins)