changeset 964:91f827272c1f toa2019082701

Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 27 Aug 2019 14:28:19 +0200
parents e70d75f199c5
children 1f9f604d2c34
files Framework/Loaders/DicomStructureSetLoader.cpp Framework/Loaders/LoaderStateMachine.cpp Framework/Oracle/WebAssemblyOracle.cpp Framework/Viewport/WebAssemblyViewport.cpp
diffstat 4 files changed, 245 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Loaders/DicomStructureSetLoader.cpp	Mon Aug 26 11:03:14 2019 +0200
+++ b/Framework/Loaders/DicomStructureSetLoader.cpp	Tue Aug 27 14:28:19 2019 +0200
@@ -24,8 +24,31 @@
 #include "../Scene2D/PolylineSceneLayer.h"
 #include "../Toolbox/GeometryToolbox.h"
 
+#if 0
+bool logbgo233 = false;
+bool logbgo115 = false;
+#endif
+
 namespace OrthancStone
 {
+
+#if 0
+  void DumpDicomMap(std::ostream& o, const Orthanc::DicomMap& dicomMap)
+  {
+    using namespace std;
+    //ios_base::fmtflags state = o.flags();
+    //o.flags(ios::right | ios::hex);
+    //o << "(" << setfill('0') << setw(4) << tag.GetGroup()
+    //  << "," << setw(4) << tag.GetElement() << ")";
+    //o.flags(state);
+    Json::Value val;
+    dicomMap.Serialize(val);
+    o << val;
+    //return o;
+  }
+#endif
+
+
   class DicomStructureSetLoader::AddReferencedInstance : public LoaderStateMachine::State
   {
   private:
@@ -41,6 +64,10 @@
 
     virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
     {
+#if 0
+      if (logbgo115)
+        LOG(TRACE) << "DicomStructureSetLoader::AddReferencedInstance::Handle() (SUCCESS)";
+#endif
       Json::Value tags;
       message.ParseJsonBody(tags);
         
@@ -48,6 +75,18 @@
       dicom.FromDicomAsJson(tags);
 
       DicomStructureSetLoader& loader = GetLoader<DicomStructureSetLoader>();
+
+#if 0
+      {
+        std::stringstream ss;
+        //DumpDicomMap(ss, dicom);
+        std::string dicomMapStr = ss.str();
+        if (logbgo115)
+          LOG(TRACE) << "  DicomStructureSetLoader::AddReferencedInstance::Handle() about to call AddReferencedSlice on dicom = " << dicomMapStr;
+      }
+#endif
+
+
       loader.content_->AddReferencedSlice(dicom);
 
       loader.countProcessedInstances_ ++;
@@ -80,6 +119,9 @@
 
     virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
     {
+#if 0
+      LOG(TRACE) << "DicomStructureSetLoader::LookupInstance::Handle() (SUCCESS)";
+#endif
       DicomStructureSetLoader& loader = GetLoader<DicomStructureSetLoader>();
 
       Json::Value lookup;
@@ -108,11 +150,24 @@
       const std::string instanceId = lookup[0]["ID"].asString();
 
       {
+#if 0
+        if(logbgo115)
+          LOG(TRACE) << "DicomStructureSetLoader::LookupInstance::Handle() (SUCCESS)";
+#endif
         std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
         command->SetHttpHeader("Accept-Encoding", "gzip");
-        command->SetUri("/instances/" + instanceId + "/tags");
+        std::string uri = "/instances/" + instanceId + "/tags";
+        command->SetUri(uri);
         command->SetPayload(new AddReferencedInstance(loader, instanceId));
+#if 0
+        if (logbgo115)
+          LOG(TRACE) << "  DicomStructureSetLoader::LookupInstance::Handle() about to schedule request with AddReferencedInstance subsequent command on uri \"" << uri << "\"";
+#endif
         Schedule(command.release());
+#if 0
+        if (logbgo115)
+          LOG(TRACE) << "  DicomStructureSetLoader::LookupInstance::Handle() request+command scheduled";
+#endif
       }
     }
   };
@@ -126,8 +181,13 @@
     {
     }
 
+
     virtual void Handle(const OrthancRestApiCommand::SuccessMessage& message)
     {
+#if 0
+      if (logbgo115)
+        LOG(TRACE) << "DicomStructureSetLoader::LoadStructure::Handle() (SUCCESS)";
+#endif
       DicomStructureSetLoader& loader = GetLoader<DicomStructureSetLoader>();
         
       {
@@ -144,12 +204,30 @@
              it = instances.begin(); it != instances.end(); ++it)
       {
         std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
+#if 0
+        if (logbgo115)
+          LOG(TRACE) << "  DicomStructureSetLoader::LoadStructure::Handle() about to schedule /tools/lookup command with LookupInstance on result";
+#endif
         command->SetUri("/tools/lookup");
         command->SetMethod(Orthanc::HttpMethod_Post);
         command->SetBody(*it);
+        command->SetHttpHeader("pragma", "no-cache");
+        command->SetHttpHeader("cache-control", "no-cache");
+#if 0
+        std::string itStr(*it);
+        if(itStr == "1.3.12.2.1107.5.1.4.66930.30000018062412550879500002198") {
+          if (logbgo115)
+            LOG(ERROR) << "******** BOGUS LOOKUPS FROM NOW ON ***********";
+          logbgo233 = true;
+        }
+#endif
         command->SetPayload(new LookupInstance(loader, *it));
         //LOG(TRACE) << "About to schedule a /tools/lookup POST request. URI = " << command->GetUri() << " Body size = " << (*it).size() << " Body = " << (*it) << "\n";
         Schedule(command.release());
+#if 0
+        if (logbgo115)
+          LOG(TRACE) << "  DicomStructureSetLoader::LoadStructure::Handle() request scheduled. Command will be LookupInstance and post body is " << *it;
+#endif
       }
     }
   };
@@ -250,9 +328,19 @@
     {
       std::auto_ptr<OrthancRestApiCommand> command(new OrthancRestApiCommand);
       command->SetHttpHeader("Accept-Encoding", "gzip");
-      command->SetUri("/instances/" + instanceId + "/tags?ignore-length=3006-0050");
+
+      std::string uri = "/instances/" + instanceId + "/tags?ignore-length=3006-0050";
+#if 0
+      if (logbgo115)
+        LOG(TRACE) << "DicomStructureSetLoader::LoadInstance() instanceId = " << instanceId << " | uri = \"" << uri << "\"";
+#endif
+      command->SetUri(uri);
       command->SetPayload(new LoadStructure(*this));
       Schedule(command.release());
+#if 0
+      if (logbgo115)
+        LOG(TRACE) << "DicomStructureSetLoader::LoadInstance() command (with LoadStructure) scheduled.";
+#endif
     }
   }
 
--- a/Framework/Loaders/LoaderStateMachine.cpp	Mon Aug 26 11:03:14 2019 +0200
+++ b/Framework/Loaders/LoaderStateMachine.cpp	Tue Aug 27 14:28:19 2019 +0200
@@ -23,6 +23,11 @@
 
 #include <Core/OrthancException.h>
 
+#if 0
+extern bool logbgo233;
+extern bool logbgo115;
+#endif
+
 namespace OrthancStone
 {
   void LoaderStateMachine::State::Handle(const OrthancRestApiCommand::SuccessMessage& message)
@@ -45,6 +50,13 @@
 
   void LoaderStateMachine::Schedule(OracleCommandWithPayload* command)
   {
+#if 0
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "  LoaderStateMachine::Schedule()";
+    }
+#endif
+
     std::auto_ptr<OracleCommandWithPayload> protection(command);
 
     if (command == NULL)
@@ -58,7 +70,14 @@
                                       "The payload must contain the next state");
     }
 
+#if 0
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "  * LoaderStateMachine::Schedule(): adding command with addr: " << std::hex << protection.get() << std::dec << " pendingCommands_.size() is now : " << pendingCommands_.size()+1;
+    }
+#endif
     pendingCommands_.push_back(protection.release());
+
     Step();
   }
 
@@ -67,7 +86,7 @@
   {
     if (active_)
     {
-      LOG(ERROR) << "LoaderStateMachine::Start() called while active_ is true";
+      LOG(TRACE) << "LoaderStateMachine::Start() called while active_ is true";
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
     }
 
@@ -82,14 +101,37 @@
 
   void LoaderStateMachine::Step()
   {
+#if 0
+    if (logbgo115)
+      LOG(TRACE) << "    LoaderStateMachine::Step(): pendingCommands_.size() =  " << pendingCommands_.size();
+#endif
     if (!pendingCommands_.empty() &&
         activeCommands_ < simultaneousDownloads_)
     {
-      oracle_.Schedule(*this, pendingCommands_.front());
+
+      IOracleCommand* nextCommand = pendingCommands_.front();
+
+#if 0
+      if (logbgo233) {
+        if (logbgo115)
+          LOG(TRACE) << "    * LoaderStateMachine::Step(): activeCommands_ (" << activeCommands_ << ") < simultaneousDownloads_ (" << simultaneousDownloads_ << ") --> will Schedule command addr " << std::hex << nextCommand << std::dec;
+      }
+#endif
+
+      oracle_.Schedule(*this, nextCommand);
       pendingCommands_.pop_front();
 
       activeCommands_++;
     }
+    else
+    {
+#if 0
+      if (logbgo233) {
+        if (logbgo115)
+          LOG(TRACE) << "    * pendingCommands_.size() == " << pendingCommands_.size() << " LoaderStateMachine::Step(): activeCommands_ (" << activeCommands_ << ") >= simultaneousDownloads_ (" << simultaneousDownloads_ << ") --> will NOT Schedule anything";
+      }
+#endif
+    }
   }
 
 
--- a/Framework/Oracle/WebAssemblyOracle.cpp	Mon Aug 26 11:03:14 2019 +0200
+++ b/Framework/Oracle/WebAssemblyOracle.cpp	Tue Aug 27 14:28:19 2019 +0200
@@ -30,6 +30,10 @@
 #include <emscripten/html5.h>
 #include <emscripten/fetch.h>
 
+#if 0
+extern bool logbgo233;
+extern bool logbgo115;
+#endif
 
 namespace OrthancStone
 {
@@ -145,6 +149,44 @@
       return dynamic_cast<T&>(*command_);
     }
 
+#if 0
+    static std::string ToString(Orthanc::HttpMethod method)
+    {
+      switch (method) {
+      case Orthanc::HttpMethod_Get:
+        return "GET";
+        break;
+      case Orthanc::HttpMethod_Post:
+        return "POST";
+        break;
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+        break;
+      }
+    }
+    static void DumpCommand(emscripten_fetch_t* fetch, std::string answer)
+    {
+      FetchContext* context = reinterpret_cast<FetchContext*>(fetch->userData);
+
+      const auto& command = context->GetTypedCommand<OrthancRestApiCommand>();
+      auto commandStr = ToString(command.GetMethod());
+      LOG(TRACE) << "SuccessCallback for REST command. Method is : " << commandStr;
+      switch (command.GetMethod()) {
+      case Orthanc::HttpMethod_Get:
+        LOG(TRACE) << "  * SuccessCallback GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout();
+        LOG(TRACE) << "  * SuccessCallback GET RESPONSE = " << answer;
+        break;
+      case Orthanc::HttpMethod_Post:
+        LOG(TRACE) << "  * SuccessCallback POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout();
+        LOG(TRACE) << "  * SuccessCallback POST RESPONSE = " << answer;
+        break;
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+        break;
+      }
+    }
+#endif
+
     static void SuccessCallback(emscripten_fetch_t *fetch)
     {
       /**
@@ -182,9 +224,14 @@
         }
       }
       
+#if 0
+      if (context->GetCommand().GetType() == IOracleCommand::Type_OrthancRestApi) {
+        //if (logbgo115)
+        DumpCommand(fetch, answer);
+      }
+#endif
       emscripten_fetch_close(fetch);
 
-
       /**
        * Secondly, use the retrieved data.
        **/
@@ -315,6 +362,13 @@
 
     void Execute()
     {
+#if 0
+      if (logbgo233) {
+        if (logbgo115)
+          LOG(TRACE) << "        WebAssemblyOracle::Execute () command addr " <<
+          std::hex << command_.get() << std::dec;
+      }
+#endif
       if (command_.get() == NULL)
       {
         // Cannot call Execute() twice
@@ -397,6 +451,9 @@
         attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType);
 
         // Must be the last call to prevent memory leak on error
+#if 0
+        LOG(TRACE) << "Performing " << method << " request on URI: \"" << uri_ << "\"";
+#endif
         emscripten_fetch(&attr, uri_.c_str());
       }        
       catch(...)
@@ -407,10 +464,39 @@
       }
     }
   };
-    
+
+#if 0
+  static void DumpCommand(OrthancRestApiCommand* pCommand)
+  {
+    OrthancRestApiCommand& command = *pCommand;
+    LOG(TRACE) << "WebAssemblyOracle::Execute for REST command.";
+    switch (command.GetMethod()) {
+    case Orthanc::HttpMethod_Get:
+      LOG(TRACE) << "  * WebAssemblyOracle::Execute GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout();
+      break;
+    case Orthanc::HttpMethod_Post:
+      LOG(TRACE) << "  * WebAssemblyOracle::Execute POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout();
+      break;
+    default:
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      break;
+    }
+  }
+#endif
+
   void WebAssemblyOracle::Execute(const IObserver& receiver,
                                   OrthancRestApiCommand* command)
   {
+#if 0
+    DumpCommand(command);
+
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "        WebAssemblyOracle::Execute (OrthancRestApiCommand) command addr " <<
+        std::hex << command << std::dec;
+    }
+#endif
+
     try
     {
       //LOG(TRACE) << "*********** WebAssemblyOracle::Execute.";
@@ -464,6 +550,14 @@
   void WebAssemblyOracle::Execute(const IObserver& receiver,
                                   GetOrthancImageCommand* command)
   {
+#if 0
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "        WebAssemblyOracle::Execute (GetOrthancImageCommand) command addr " <<
+        std::hex << command << std::dec;
+    }
+#endif
+
     FetchCommand fetch(*this, receiver, command);
 
     fetch.SetUri(command->GetUri());
@@ -477,6 +571,13 @@
   void WebAssemblyOracle::Execute(const IObserver& receiver,
                                   GetOrthancWebViewerJpegCommand* command)
   {
+#if 0
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "        WebAssemblyOracle::Execute (GetOrthancWebViewerJpegCommand) command addr " << std::hex << command << std::dec;
+    }
+#endif
+
     FetchCommand fetch(*this, receiver, command);
 
     fetch.SetUri(command->GetUri());
@@ -491,6 +592,13 @@
   void WebAssemblyOracle::Schedule(const IObserver& receiver,
                                    IOracleCommand* command)
   {
+#if 0
+    if (logbgo233) {
+      if (logbgo115)
+        LOG(TRACE) << "      WebAssemblyOracle::Schedule command addr " <<
+        std::hex << command << std::dec;
+    }
+#endif
     std::auto_ptr<IOracleCommand> protection(command);
 
     if (command == NULL)
--- a/Framework/Viewport/WebAssemblyViewport.cpp	Mon Aug 26 11:03:14 2019 +0200
+++ b/Framework/Viewport/WebAssemblyViewport.cpp	Tue Aug 27 14:28:19 2019 +0200
@@ -97,7 +97,7 @@
         //LOG(ERROR) << "Before calling it, isContextLost == " << context_.IsContextLost();
 
         if (!context_.IsContextLost()) {
-          LOG(ERROR) << "Context restored!";
+          LOG(TRACE) << "Context restored!";
           //LOG(ERROR) << "After calling it, isContextLost == " << context_.IsContextLost();
           RestoreCompositor();
           UpdateSize();