diff Plugins/Engine/OrthancPlugins.cpp @ 2808:37583cd183ed

primitives to create jobs from plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Sep 2018 17:57:06 +0200
parents 6e3a60b85da6
children 6d5b20af216f
line wrap: on
line diff
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Sep 04 15:58:26 2018 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 05 17:57:06 2018 +0200
@@ -44,6 +44,7 @@
 #include "../../Core/HttpServer/HttpToolbox.h"
 #include "../../Core/Logging.h"
 #include "../../Core/OrthancException.h"
+#include "../../Core/SerializationToolbox.h"
 #include "../../Core/Toolbox.h"
 #include "../../Core/DicomParsing/FromDcmtkBridge.h"
 #include "../../Core/DicomParsing/ToDcmtkBridge.h"
@@ -62,6 +63,7 @@
 #include "../../Core/Images/ImageProcessing.h"
 #include "../../OrthancServer/DefaultDicomImageDecoder.h"
 #include "PluginsEnumerations.h"
+#include "PluginsJob.h"
 
 #include <boost/regex.hpp> 
 #include <dcmtk/dcmdata/dcdict.h>
@@ -402,6 +404,7 @@
     typedef std::list<OrthancPluginIncomingHttpRequestFilter>  IncomingHttpRequestFilters;
     typedef std::list<OrthancPluginIncomingHttpRequestFilter2>  IncomingHttpRequestFilters2;
     typedef std::list<OrthancPluginDecodeImageCallback>  DecodeImageCallbacks;
+    typedef std::list<OrthancPluginJobsUnserializer>  JobsUnserializers;
     typedef std::map<Property, std::string>  Properties;
 
     PluginsManager manager_;
@@ -412,6 +415,7 @@
     OrthancPluginFindCallback  findCallback_;
     OrthancPluginWorklistCallback  worklistCallback_;
     DecodeImageCallbacks  decodeImageCallbacks_;
+    JobsUnserializers  jobsUnserializers_;
     _OrthancPluginMoveCallback moveCallbacks_;
     IncomingHttpRequestFilters  incomingHttpRequestFilters_;
     IncomingHttpRequestFilters2 incomingHttpRequestFilters2_;
@@ -423,6 +427,7 @@
     boost::mutex findCallbackMutex_;
     boost::mutex worklistCallbackMutex_;
     boost::mutex decodeImageCallbackMutex_;
+    boost::mutex jobsUnserializersMutex_;
     boost::recursive_mutex invokeServiceMutex_;
 
     Properties properties_;
@@ -809,6 +814,7 @@
         sizeof(int32_t) != sizeof(_OrthancPluginDatabaseAnswerType) ||
         sizeof(int32_t) != sizeof(OrthancPluginIdentifierConstraint) ||
         sizeof(int32_t) != sizeof(OrthancPluginInstanceOrigin) ||
+        sizeof(int32_t) != sizeof(OrthancPluginJobStepStatus) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludeBinary) != static_cast<int>(DicomToJsonFlags_IncludeBinary) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludePrivateTags) != static_cast<int>(DicomToJsonFlags_IncludePrivateTags) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludeUnknownTags) != static_cast<int>(DicomToJsonFlags_IncludeUnknownTags) ||
@@ -1168,6 +1174,19 @@
   }
 
 
+  void OrthancPlugins::RegisterJobsUnserializer(const void* parameters)
+  {
+    const _OrthancPluginJobsUnserializer& p = 
+      *reinterpret_cast<const _OrthancPluginJobsUnserializer*>(parameters);
+
+    boost::mutex::scoped_lock lock(pimpl_->jobsUnserializersMutex_);
+
+    pimpl_->jobsUnserializers_.push_back(p.unserializer);
+    LOG(INFO) << "Plugin has registered a callback to unserialize jobs (" 
+              << pimpl_->jobsUnserializers_.size() << " unserializer(s) now active)";
+  }
+
+
   void OrthancPlugins::RegisterIncomingHttpRequestFilter(const void* parameters)
   {
     const _OrthancPluginIncomingHttpRequestFilter& p = 
@@ -2857,6 +2876,19 @@
         CallPeerApi(parameters);
         return true;
 
+      case _OrthancPluginService_SubmitJob:
+      {
+        const _OrthancPluginSubmitJob& p =
+          *reinterpret_cast<const _OrthancPluginSubmitJob*>(parameters);
+
+        std::string uuid;
+
+        PImpl::ServerContextLock lock(*pimpl_);
+        lock.GetContext().GetJobsEngine().GetRegistry().Submit(uuid, new PluginsJob(p), p.priority_);
+        
+        return true;
+      }
+
       default:
         return false;
     }
@@ -2906,6 +2938,10 @@
         RegisterDecodeImageCallback(parameters);
         return true;
 
+      case _OrthancPluginService_RegisterJobsUnserializer:
+        RegisterJobsUnserializer(parameters);
+        return true;
+
       case _OrthancPluginService_RegisterIncomingHttpRequestFilter:
         RegisterIncomingHttpRequestFilter(parameters);
         return true;
@@ -3319,7 +3355,7 @@
     }
   }
 
-
+  
   bool OrthancPlugins::IsAllowed(HttpMethod method,
                                  const char* uri,
                                  const char* ip,
@@ -3394,4 +3430,25 @@
 
     return true;
   }
+
+
+  bool OrthancPlugins::UnserializeJob(const Json::Value& value)
+  {
+    const std::string type = SerializationToolbox::ReadString(value, "Type");
+    const std::string serialized = value.toStyledString();
+
+    boost::mutex::scoped_lock lock(pimpl_->jobsUnserializersMutex_);
+
+    for (PImpl::JobsUnserializers::iterator 
+           unserializer = pimpl_->jobsUnserializers_.begin();
+         unserializer != pimpl_->jobsUnserializers_.end(); ++unserializer)
+    {
+      if ((*unserializer) (type.c_str(), serialized.c_str()) == OrthancPluginErrorCode_Success)
+      {
+        return true;
+      }
+    }
+
+    return false;
+  }
 }