changeset 824:15d493101c1e

support of SleepOracleCommand in WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 29 May 2019 15:26:47 +0200
parents 467d58a59718
children 9a6c7a5dcb76
files Framework/Oracle/SleepOracleCommand.h Framework/Scene2D/Scene2D.cpp Samples/WebAssembly/BasicMPR.cpp
diffstat 3 files changed, 107 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Oracle/SleepOracleCommand.h	Wed May 29 14:44:37 2019 +0200
+++ b/Framework/Oracle/SleepOracleCommand.h	Wed May 29 15:26:47 2019 +0200
@@ -35,7 +35,7 @@
     ORTHANC_STONE_DEFINE_ORIGIN_MESSAGE(__FILE__, __LINE__, TimeoutMessage, SleepOracleCommand);
 
     SleepOracleCommand(unsigned int milliseconds) : 
-    milliseconds_(milliseconds)
+      milliseconds_(milliseconds)
     {
     }
 
--- a/Framework/Scene2D/Scene2D.cpp	Wed May 29 14:44:37 2019 +0200
+++ b/Framework/Scene2D/Scene2D.cpp	Wed May 29 15:26:47 2019 +0200
@@ -102,8 +102,7 @@
   void Scene2D::SetLayer(int depth,
                          ISceneLayer* layer)  // Takes ownership
   {
-    LOG(INFO) << "SetLayer(" << depth << ", " <<
-      reinterpret_cast<intptr_t>(layer) << ")";
+    LOG(TRACE) << "SetLayer(" << depth << ", " << reinterpret_cast<intptr_t>(layer) << ")";
     std::auto_ptr<Item> item(new Item(layer, layerCounter_++));
 
     if (layer == NULL)
--- a/Samples/WebAssembly/BasicMPR.cpp	Wed May 29 14:44:37 2019 +0200
+++ b/Samples/WebAssembly/BasicMPR.cpp	Wed May 29 15:26:47 2019 +0200
@@ -26,6 +26,7 @@
 
 #include "../../Framework/Loaders/OrthancSeriesVolumeProgressiveLoader.h"
 #include "../../Framework/OpenGL/WebAssemblyOpenGLContext.h"
+#include "../../Framework/Oracle/SleepOracleCommand.h"
 #include "../../Framework/Scene2D/GrayscaleStyleConfigurator.h"
 #include "../../Framework/Scene2D/OpenGLCompositor.h"
 #include "../../Framework/Scene2D/PanSceneTracker.h"
@@ -402,27 +403,28 @@
   private:
     typedef std::map<std::string, std::string>  HttpHeaders;
     
+    class Emitter : public IMessageEmitter
+    {
+    private:
+      WebAssemblyOracle&  oracle_;
+
+    public:
+      Emitter(WebAssemblyOracle&  oracle) :
+        oracle_(oracle)
+      {
+      }
+
+      virtual void EmitMessage(const IObserver& receiver,
+                               const IMessage& message)
+      {
+        oracle_.EmitMessage(receiver, message);
+      }
+    };
+
+
     class FetchContext : public boost::noncopyable
     {
     private:
-      class Emitter : public IMessageEmitter
-      {
-      private:
-        WebAssemblyOracle&  oracle_;
-
-      public:
-        Emitter(WebAssemblyOracle&  oracle) :
-          oracle_(oracle)
-        {
-        }
-
-        virtual void EmitMessage(const IObserver& receiver,
-                                 const IMessage& message)
-        {
-          oracle_.EmitMessage(receiver, message);
-        }
-      };
-
       Emitter                        emitter_;
       const IObserver&               receiver_;
       std::auto_ptr<IOracleCommand>  command_;
@@ -754,6 +756,47 @@
       fetch.Execute();
     }
 
+
+
+    class TimeoutContext
+    {
+    private:
+      WebAssemblyOracle&                 oracle_;
+      const IObserver&                   receiver_;
+      std::auto_ptr<SleepOracleCommand>  command_;
+
+    public:
+      TimeoutContext(WebAssemblyOracle& oracle,
+                     const IObserver& receiver,
+                     IOracleCommand* command) :
+        oracle_(oracle),
+        receiver_(receiver)
+      {
+        if (command == NULL)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+        }
+        else
+        {
+          command_.reset(dynamic_cast<SleepOracleCommand*>(command));
+        }
+      }
+
+      void EmitMessage()
+      {
+        SleepOracleCommand::TimeoutMessage message(*command_);
+        oracle_.EmitMessage(receiver_, message);
+      }
+    };
+    
+
+    static void TimeoutCallback(void *userData)
+    {
+      std::auto_ptr<TimeoutContext> context(reinterpret_cast<TimeoutContext*>(userData));
+      context->EmitMessage();
+    }
+
+
     
   public:
     WebAssemblyOracle(MessageBroker& broker) :
@@ -785,6 +828,13 @@
           Execute(receiver, dynamic_cast<GetOrthancWebViewerJpegCommand*>(protection.release()));
           break;          
             
+        case IOracleCommand::Type_Sleep:
+        {
+          unsigned int timeoutMS = dynamic_cast<SleepOracleCommand*>(command)->GetDelay();
+          emscripten_set_timeout(TimeoutCallback, timeoutMS, new TimeoutContext(*this, receiver, protection.release()));
+          break;
+        }
+            
         default:
           LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " << command->GetType();
       }
@@ -925,6 +975,43 @@
 
 
 
+namespace OrthancStone
+{
+  class TestSleep : public IObserver
+  {
+  private:
+    WebAssemblyOracle&  oracle_;
+
+    void Schedule()
+    {
+      oracle_.Schedule(*this, new OrthancStone::SleepOracleCommand(2000));
+    }
+    
+    void Handle(const SleepOracleCommand::TimeoutMessage& message)
+    {
+      LOG(INFO) << "TIMEOUT";
+      Schedule();
+    }
+    
+  public:
+    TestSleep(MessageBroker& broker,
+              WebAssemblyOracle& oracle) :
+      IObserver(broker),
+      oracle_(oracle)
+    {
+      oracle.RegisterObserverCallback(
+        new Callable<TestSleep, SleepOracleCommand::TimeoutMessage>
+        (*this, &TestSleep::Handle));
+
+      LOG(INFO) << "STARTING";
+      Schedule();
+    }
+  };
+
+  //static TestSleep testSleep(broker_, oracle_);
+}
+
+
 extern "C"
 {
   int main(int argc, char const *argv[])