changeset 1191:d49505e377e3 db-changes

demo of OnChangeCallback in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 25 Sep 2014 17:50:06 +0200
parents 6b9b02a16e99
children 669bb978d52e
files OrthancServer/ServerIndex.cpp Plugins/Samples/Basic/Plugin.c
diffstat 2 files changed, 53 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/ServerIndex.cpp	Thu Sep 25 17:02:28 2014 +0200
+++ b/OrthancServer/ServerIndex.cpp	Thu Sep 25 17:50:06 2014 +0200
@@ -122,10 +122,19 @@
       std::list<FileToRemove> pendingFilesToRemove_;
       std::list<Change> pendingChanges_;
       uint64_t sizeOfFilesToRemove_;
+      bool insideTransaction_;
+
+      void Reset()
+      {
+        sizeOfFilesToRemove_ = 0;
+        hasRemainingLevel_ = false;
+        pendingFilesToRemove_.clear();
+        pendingChanges_.clear();
+      }
 
     public:
-      ServerIndexListener(ServerContext& context) : 
-        context_(context)
+      ServerIndexListener(ServerContext& context) : context_(context),
+                                                    insideTransaction_(false)      
       {
         Reset();
         assert(ResourceType_Patient < ResourceType_Study &&
@@ -133,12 +142,15 @@
                ResourceType_Series < ResourceType_Instance);
       }
 
-      void Reset()
+      void StartTransaction()
       {
-        sizeOfFilesToRemove_ = 0;
-        hasRemainingLevel_ = false;
-        pendingFilesToRemove_.clear();
-        pendingChanges_.clear();
+        Reset();
+        insideTransaction_ = true;
+      }
+
+      void EndTransaction()
+      {
+        insideTransaction_ = false;
       }
 
       uint64_t GetSizeOfFilesToRemove()
@@ -201,7 +213,14 @@
         LOG(INFO) << "Change related to resource " << publicId << " of type " 
                   << EnumerationToString(resourceType) << ": " << EnumerationToString(changeType);
 
-        pendingChanges_.push_back(Change(changeType, resourceType, publicId));
+        if (insideTransaction_)
+        {
+          pendingChanges_.push_back(Change(changeType, resourceType, publicId));
+        }
+        else
+        {
+          context_.SignalChange(changeType, resourceType, publicId);
+        }
       }
 
       bool HasRemainingLevel() const
@@ -238,9 +257,15 @@
     {
       assert(index_.currentStorageSize_ == index_.db_->GetTotalCompressedSize());
 
-      index_.listener_->Reset();
       transaction_.reset(index_.db_->StartTransaction());
       transaction_->Begin();
+
+      index_.listener_->StartTransaction();
+    }
+
+    ~Transaction()
+    {
+      index_.listener_->EndTransaction();
     }
 
     void Commit(uint64_t sizeOfAddedFiles)
@@ -312,7 +337,6 @@
                                    ResourceType expectedType)
   {
     boost::mutex::scoped_lock lock(mutex_);
-    listener_->Reset();
 
     Transaction t(*this);
 
@@ -489,7 +513,6 @@
                                  const MetadataMap& metadata)
   {
     boost::mutex::scoped_lock lock(mutex_);
-    listener_->Reset();
 
     instanceMetadata.clear();
 
@@ -1826,7 +1849,6 @@
                                      FileContentType type)
   {
     boost::mutex::scoped_lock lock(mutex_);
-    listener_->Reset();
 
     Transaction t(*this);
 
--- a/Plugins/Samples/Basic/Plugin.c	Thu Sep 25 17:02:28 2014 +0200
+++ b/Plugins/Samples/Basic/Plugin.c	Thu Sep 25 17:50:06 2014 +0200
@@ -215,6 +215,7 @@
   char buffer[256];
   FILE* fp;
   char* json;
+  static int first = 1;
 
   sprintf(buffer, "Just received a DICOM instance of size %d and ID %s from AET %s", 
           (int) OrthancPluginGetInstanceSize(context, instance), instanceId, 
@@ -228,7 +229,12 @@
   fclose(fp);
 
   json = OrthancPluginGetInstanceSimplifiedJson(context, instance);
-  printf("[%s]\n", json);
+  if (first)
+  {
+    /* Only print the first DICOM instance */
+    printf("[%s]\n", json);
+    first = 0;
+  }
   OrthancPluginFreeString(context, json);
 
   if (OrthancPluginHasInstanceMetadata(context, instance, "ReceptionDate"))
@@ -244,6 +250,16 @@
 }
 
 
+ORTHANC_PLUGINS_API int32_t OnChangeCallback(OrthancPluginChangeType changeType,
+                                             OrthancPluginResourceType resourceType,
+                                             const char* resourceId)
+{
+  char info[1024];
+  sprintf(info, "Change %d on resource %s of type %d", changeType, resourceId, resourceType);
+  OrthancPluginLogWarning(context, info);
+  return 0;
+}
+
 
 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
 {
@@ -293,6 +309,8 @@
 
   OrthancPluginRegisterOnStoredInstanceCallback(context, OnStoredCallback);
 
+  OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
+
   /* Make REST requests to the built-in Orthanc API */
   OrthancPluginRestApiGet(context, &tmp, "/changes");
   OrthancPluginFreeMemoryBuffer(context, &tmp);