changeset 2814:7d1d3136f6cf

more generic handling of content and serialization in plugin jobs
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 07 Sep 2018 10:09:17 +0200
parents 6d5b20af216f
children 925d8dc03a23
files Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/PluginsJob.cpp Plugins/Engine/PluginsJob.h Plugins/Include/orthanc/OrthancCPlugin.h
diffstat 4 files changed, 112 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Engine/OrthancPlugins.cpp	Thu Sep 06 18:24:44 2018 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Sep 07 10:09:17 2018 +0200
@@ -2884,9 +2884,9 @@
         std::string uuid;
 
         PImpl::ServerContextLock lock(*pimpl_);
-        lock.GetContext().GetJobsEngine().GetRegistry().Submit(uuid, new PluginsJob(p), p.priority_);
+        lock.GetContext().GetJobsEngine().GetRegistry().Submit(uuid, new PluginsJob(p), p.priority);
         
-        *p.resultId_ = CopyString(uuid);
+        *p.resultId = CopyString(uuid);
 
         return true;
       }
--- a/Plugins/Engine/PluginsJob.cpp	Thu Sep 06 18:24:44 2018 +0200
+++ b/Plugins/Engine/PluginsJob.cpp	Fri Sep 07 10:09:17 2018 +0200
@@ -39,6 +39,7 @@
 #endif
 
 
+#include "../../Core/Logging.h"
 #include "../../Core/OrthancException.h"
 
 #include <json/reader.h>
@@ -47,70 +48,39 @@
 namespace Orthanc
 {
   PluginsJob::PluginsJob(const _OrthancPluginSubmitJob& parameters) :
-    job_(parameters.job_),
-    free_(parameters.free_),
-    getProgress_(parameters.getProgress_),
-    step_(parameters.step_),
-    stop_(parameters.stop_),
-    reset_(parameters.reset_)
+    parameters_(parameters)
   {
-    if (job_ == NULL ||
-        parameters.type_ == NULL ||
-        free_ == NULL ||
-        getProgress_ == NULL ||
-        step_ == NULL ||
-        stop_ == NULL ||
-        reset_ == NULL)
+    if (parameters_.job == NULL)
     {
       throw OrthancException(ErrorCode_NullPointer);
     }
+    
+    if (parameters_.resultId == NULL ||
+        parameters_.freeJob == NULL ||
+        parameters_.type == NULL ||
+        parameters_.getProgress == NULL ||
+        parameters_.getContent == NULL ||
+        parameters_.getSerialized == NULL ||
+        parameters_.step == NULL ||
+        parameters_.stop == NULL ||
+        parameters_.reset == NULL)
+    {
+      parameters_.freeJob(parameters.job);
+      throw OrthancException(ErrorCode_NullPointer);
+    }
 
-    type_.assign(parameters.type_);
-
-    if (parameters.content_ == NULL)
-    {
-      publicContent_ = Json::objectValue;
-    }
-    else
-    {
-      Json::Reader reader;
-      if (!reader.parse(parameters.content_, publicContent_) ||
-          publicContent_.type() != Json::objectValue)
-      {
-        free_(job_);
-        throw OrthancException(ErrorCode_BadFileFormat);
-      }
-    }
-
-    if (parameters.serialized_ == NULL)
-    {
-      hasSerialized_ = false;
-    }
-    else
-    {
-      hasSerialized_ = true;
-
-      Json::Reader reader;
-      if (!reader.parse(parameters.serialized_, serialized_) ||
-          serialized_.type() != Json::objectValue ||
-          !serialized_.isMember("Type") ||
-          serialized_["Type"].type() != Json::stringValue)
-      {
-        free_(job_);
-        throw OrthancException(ErrorCode_BadFileFormat);
-      }
-    }
+    type_.assign(parameters.type);
   }
 
   PluginsJob::~PluginsJob()
   {
-    assert(job_ != NULL);
-    free_(job_);
+    assert(parameters_.job != NULL);
+    parameters_.freeJob(parameters_.job);
   }
 
   JobStepResult PluginsJob::Step()
   {
-    OrthancPluginJobStepStatus status = step_(job_);
+    OrthancPluginJobStepStatus status = parameters_.step(parameters_.job);
 
     switch (status)
     {
@@ -130,7 +100,7 @@
 
   void PluginsJob::Reset()
   {
-    reset_(job_);
+    parameters_.reset(parameters_.job);
   }
 
   void PluginsJob::Stop(JobStopReason reason)
@@ -138,19 +108,19 @@
     switch (reason)
     {
       case JobStopReason_Success:
-        stop_(job_, OrthancPluginJobStopReason_Success);
+        parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success);
         break;
 
       case JobStopReason_Failure:
-        stop_(job_, OrthancPluginJobStopReason_Failure);
+        parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure);
         break;
 
       case JobStopReason_Canceled:
-        stop_(job_, OrthancPluginJobStopReason_Canceled);
+        parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled);
         break;
 
       case JobStopReason_Paused:
-        stop_(job_, OrthancPluginJobStopReason_Paused);
+        parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused);
         break;
 
       default:
@@ -160,19 +130,60 @@
 
   float PluginsJob::GetProgress()
   {
-    return getProgress_(job_);
+    return parameters_.getProgress(parameters_.job);
+  }
+
+  void PluginsJob::GetPublicContent(Json::Value& value)
+  {
+    const char* content = parameters_.getContent(parameters_.job);
+
+    if (content == NULL)
+    {
+      value = Json::objectValue;
+    }
+    else
+    {
+      Json::Reader reader;
+      
+      if (!reader.parse(content, value) ||
+          value.type() != Json::objectValue)
+      {
+        LOG(ERROR) << "A job plugin must provide a JSON object as its public content";
+        throw OrthancException(ErrorCode_Plugin);
+      }
+    }
   }
 
   bool PluginsJob::Serialize(Json::Value& value)
   {
-    if (hasSerialized_)
+    const char* serialized = parameters_.getSerialized(parameters_.job);
+
+    if (serialized == NULL)
     {
-      value = serialized_;
-      return true;
+      return false;
     }
     else
     {
-      return false;
+      Json::Reader reader;
+      
+      if (!reader.parse(serialized, value) ||
+          value.type() != Json::objectValue)
+      {
+        LOG(ERROR) << "A job plugin must provide a JSON object as its serialized content";
+        throw OrthancException(ErrorCode_Plugin);
+      }
+
+
+      static const char* KEY_TYPE = "Type";
+      
+      if (value.isMember(KEY_TYPE))
+      {
+        LOG(ERROR) << "The \"Type\" field is for reserved use for serialized job";
+        throw OrthancException(ErrorCode_Plugin);
+      }
+
+      value[KEY_TYPE] = type_;
+      return true;
     }
   }
 }
--- a/Plugins/Engine/PluginsJob.h	Thu Sep 06 18:24:44 2018 +0200
+++ b/Plugins/Engine/PluginsJob.h	Fri Sep 07 10:09:17 2018 +0200
@@ -43,16 +43,8 @@
   class PluginsJob : public IJob
   {
   private:
-    void                        *job_;
-    std::string                  type_;
-    Json::Value                  publicContent_;
-    bool                         hasSerialized_;
-    Json::Value                  serialized_;
-    OrthancPluginJobFree         free_;
-    OrthancPluginJobGetProgress  getProgress_;
-    OrthancPluginJobStep         step_;
-    OrthancPluginJobStop         stop_;
-    OrthancPluginJobReset        reset_;
+    _OrthancPluginSubmitJob  parameters_;
+    std::string              type_;
 
   public:
     PluginsJob(const _OrthancPluginSubmitJob& parameters);
@@ -76,10 +68,7 @@
       target = type_;
     }
     
-    virtual void GetPublicContent(Json::Value& value)
-    {
-      value = publicContent_;
-    }
+    virtual void GetPublicContent(Json::Value& value);
 
     virtual bool Serialize(Json::Value& value);
   };
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Sep 06 18:24:44 2018 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Sep 07 10:09:17 2018 +0200
@@ -1274,6 +1274,8 @@
 
   typedef void (*OrthancPluginJobFree) (void* job);
   typedef float (*OrthancPluginJobGetProgress) (void* job);
+  typedef const char* (*OrthancPluginJobGetContent) (void* job);
+  typedef const char* (*OrthancPluginJobGetSerialized) (void* job);
   typedef OrthancPluginJobStepStatus (*OrthancPluginJobStep) (void* job);
   typedef OrthancPluginErrorCode (*OrthancPluginJobStop) (void* job, 
                                                           OrthancPluginJobStopReason reason);
@@ -6041,48 +6043,48 @@
 
   typedef struct
   {
-    char**                        resultId_;
-    void                         *job_;
-    int                           priority_;
-    const char                   *type_;
-    const char                   *content_;
-    const char                   *serialized_;
-    OrthancPluginJobFree          free_;
-    OrthancPluginJobGetProgress   getProgress_;
-    OrthancPluginJobStep          step_;
-    OrthancPluginJobStop          stop_;
-    OrthancPluginJobReset         reset_;
+    char**                          resultId;
+    void                           *job;
+    OrthancPluginJobFree            freeJob;
+    int                             priority;
+    const char                     *type;
+    OrthancPluginJobGetProgress     getProgress;
+    OrthancPluginJobGetContent      getContent;
+    OrthancPluginJobGetSerialized   getSerialized;
+    OrthancPluginJobStep            step;
+    OrthancPluginJobStop            stop;
+    OrthancPluginJobReset           reset;
   } _OrthancPluginSubmitJob;
 
   ORTHANC_PLUGIN_INLINE char *OrthancPluginSubmitJob(
-    OrthancPluginContext         *context,
-    void                         *job,
-    int                           priority,
-    const char                   *type,
-    const char                   *content,
-    const char                   *serialized,
-    OrthancPluginJobFree          freeJob,
-    OrthancPluginJobGetProgress   getProgress,
-    OrthancPluginJobStep          step,
-    OrthancPluginJobStop          stop,
-    OrthancPluginJobReset         reset)
+    OrthancPluginContext           *context,
+    void                           *job,
+    OrthancPluginJobFree            freeJob,
+    int                             priority,
+    const char                     *type,
+    OrthancPluginJobGetProgress     getProgress,
+    OrthancPluginJobGetContent      getContent,
+    OrthancPluginJobGetSerialized   getSerialized,
+    OrthancPluginJobStep            step,
+    OrthancPluginJobStop            stop,
+    OrthancPluginJobReset           reset)
   {
     char* resultId = NULL;
 
     _OrthancPluginSubmitJob params;
     memset(&params, 0, sizeof(params));
 
-    params.resultId_ = &resultId;
-    params.job_ = job;
-    params.priority_ = priority;
-    params.free_ = freeJob;
-    params.type_ = type;
-    params.content_ = content;
-    params.serialized_ = serialized;
-    params.getProgress_ = getProgress;
-    params.step_ = step;
-    params.stop_ = stop;
-    params.reset_ = reset;
+    params.resultId = &resultId;
+    params.job = job;
+    params.freeJob = freeJob;
+    params.priority = priority;
+    params.type = type;
+    params.getProgress = getProgress;
+    params.getContent = getContent;
+    params.getSerialized = getSerialized;
+    params.step = step;
+    params.stop = stop;
+    params.reset = reset;
 
     if (context->InvokeService(context, _OrthancPluginService_SubmitJob, &params) != OrthancPluginErrorCode_Success ||
         resultId == NULL)