changeset 388:466c992a9a42 lua-scripting

testing filters inside orthanc
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Apr 2013 17:30:38 +0200
parents 7dec4f3c922c
children 3be5837ceb80
files OrthancServer/ServerContext.h OrthancServer/main.cpp
diffstat 2 files changed, 31 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/ServerContext.h	Mon Apr 29 17:14:10 2013 +0200
+++ b/OrthancServer/ServerContext.h	Mon Apr 29 17:30:38 2013 +0200
@@ -36,6 +36,7 @@
 #include "../Core/FileStorage/CompressedFileStorageAccessor.h"
 #include "../Core/FileStorage/FileStorage.h"
 #include "../Core/RestApi/RestApiOutput.h"
+#include "../Core/Lua/LuaContext.h"
 #include "ServerIndex.h"
 #include "FromDcmtkBridge.h"
 
@@ -70,6 +71,8 @@
     DicomCacheProvider provider_;
     MemoryCache dicomCache_;
 
+    LuaContext lua_;
+
   public:
     ServerContext(const boost::filesystem::path& storagePath,
                   const boost::filesystem::path& indexPath);
@@ -129,5 +132,10 @@
 
     // TODO IMPLEMENT MULTITHREADING FOR THIS METHOD
     ParsedDicomFile& GetDicomFile(const std::string& instancePublicId);
+
+    LuaContext& GetLuaContext()
+    {
+      return lua_;
+    }
   };
 }
--- a/OrthancServer/main.cpp	Mon Apr 29 17:14:10 2013 +0200
+++ b/OrthancServer/main.cpp	Mon Apr 29 17:30:38 2013 +0200
@@ -38,7 +38,7 @@
 
 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h"
 #include "../Core/HttpServer/FilesystemHttpHandler.h"
-#include "../Core/HttpServer/MongooseServer.h"
+#include "../Core/Lua/LuaFunctionCall.h"
 #include "DicomProtocol/DicomServer.h"
 #include "OrthancInitialization.h"
 #include "ServerContext.h"
@@ -50,12 +50,15 @@
 class MyDicomStore : public IStoreRequestHandler
 {
 private:
-  ServerContext& context_;
+  ServerContext& server_;
 
 public:
   MyDicomStore(ServerContext& context) :
-    context_(context)
+    server_(context)
   {
+    LuaContext& lua = server_.GetLuaContext();
+    lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
+    lua.Execute("function NewInstanceFilter(dicom, aet) print(dicom['0010,0020'].Value); return true end");
   }
 
   virtual void Handle(const std::string& dicomFile,
@@ -65,7 +68,20 @@
   {
     if (dicomFile.size() > 0)
     {
-      context_.Store(&dicomFile[0], dicomFile.size(), dicomSummary, dicomJson, remoteAet);
+      LuaContext& lua = server_.GetLuaContext();
+      // TODO : Is existing trigger ?
+      LuaFunctionCall call(lua, "NewInstanceFilter");
+      call.PushJSON(dicomJson);
+      call.PushString(remoteAet);
+
+      if (call.ExecutePredicate())
+      {
+        server_.Store(&dicomFile[0], dicomFile.size(), dicomSummary, dicomJson, remoteAet);
+      }
+      else
+      {
+        LOG(WARNING) << "An instance has been discarded by a filter";
+      }
     }
   }
 };
@@ -74,16 +90,16 @@
 class MyDicomStoreFactory : public IStoreRequestHandlerFactory
 {
 private:
-  ServerContext& context_;
+  ServerContext& server_;
 
 public:
-  MyDicomStoreFactory(ServerContext& context) : context_(context)
+  MyDicomStoreFactory(ServerContext& context) : server_(context)
   {
   }
 
   virtual IStoreRequestHandler* ConstructStoreRequestHandler()
   {
-    return new MyDicomStore(context_);
+    return new MyDicomStore(server_);
   }
 
   void Done()