diff OrthancServer/ServerContext.cpp @ 1436:0a3e3be59094

uncoupling of SignalChange for Lua scripts
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2015 17:19:26 +0200
parents f9cd40166269
children 5ba7471780ae
line wrap: on
line diff
--- a/OrthancServer/ServerContext.cpp	Tue Jun 30 16:46:23 2015 +0200
+++ b/OrthancServer/ServerContext.cpp	Tue Jun 30 17:19:26 2015 +0200
@@ -67,6 +67,34 @@
 
 namespace Orthanc
 {
+  void ServerContext::ChangeThread(ServerContext* that)
+  {
+    while (!that->done_)
+    {
+      std::auto_ptr<IDynamicObject> obj(that->pendingChanges_.Dequeue(500));
+        
+      if (obj.get() != NULL)
+      {
+        const ServerIndexChange& change = dynamic_cast<const ServerIndexChange&>(*obj.get());
+
+        for (ServerListeners::iterator it = that->listeners_.begin(); 
+             it != that->listeners_.end(); ++it)
+        {
+          try
+          {
+            it->GetListener().SignalChange(change);
+          }
+          catch (OrthancException& e)
+          {
+            LOG(ERROR) << "Error in the " << it->GetDescription() 
+                       << " callback while signaling a change: " << e.What();
+          }
+        }
+      }
+    }
+  }
+
+
   ServerContext::ServerContext(IDatabaseWrapper& database) :
     index_(*this, database),
     compressionEnabled_(false),
@@ -75,6 +103,7 @@
     scheduler_(Configuration::GetGlobalIntegerParameter("LimitJobs", 10)),
     lua_(*this),
     plugins_(NULL),
+    done_(false),
     queryRetrieveArchive_(Configuration::GetGlobalIntegerParameter("QueryRetrieveSize", 10)),
     defaultLocalAet_(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"))
   {
@@ -82,8 +111,23 @@
     scu_.SetMillisecondsBeforeClose(s * 1000);  // Milliseconds are expected here
 
     listeners_.push_back(ServerListener(lua_, "Lua"));
+
+    changeThread_ = boost::thread(ChangeThread, this);
   }
 
+
+  
+  ServerContext::~ServerContext()
+  {
+    done_ = true;
+
+    if (changeThread_.joinable())
+    {
+      changeThread_.join();
+    }
+  }
+
+
   void ServerContext::SetCompressionEnabled(bool enabled)
   {
     if (enabled)
@@ -360,18 +404,7 @@
 
   void ServerContext::SignalChange(const ServerIndexChange& change)
   {
-    for (ServerListeners::iterator it = listeners_.begin(); it != listeners_.end(); ++it)
-    {
-      try
-      {
-        it->GetListener().SignalChange(change);
-      }
-      catch (OrthancException& e)
-      {
-        LOG(ERROR) << "Error in the " << it->GetDescription() 
-                   << " callback while signaling a change: " << e.What();
-      }
-    }
+    pendingChanges_.Enqueue(change.Clone());
   }