changeset 1229:b9f2a111c5b9 broker

fix compilation of WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Dec 2019 14:41:37 +0100
parents c471a0aa137b
children a28861abf888
files Framework/Oracle/WebAssemblyOracle.cpp Framework/Oracle/WebAssemblyOracle.h
diffstat 2 files changed, 84 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Oracle/WebAssemblyOracle.cpp	Mon Dec 09 13:58:37 2019 +0100
+++ b/Framework/Oracle/WebAssemblyOracle.cpp	Mon Dec 09 14:41:37 2019 +0100
@@ -42,12 +42,12 @@
   {
   private:
     WebAssemblyOracle&                 oracle_;
-    const IObserver&                   receiver_;
+    boost::weak_ptr<IObserver>         receiver_;
     std::auto_ptr<SleepOracleCommand>  command_;
 
   public:
     TimeoutContext(WebAssemblyOracle& oracle,
-                   const IObserver& receiver,
+                   boost::weak_ptr<IObserver> receiver,
                    IOracleCommand* command) :
       oracle_(oracle),
       receiver_(receiver)
@@ -63,7 +63,9 @@
     }
 
     void EmitMessage()
-    {
+    {      
+      assert(command_.get() != NULL);
+
       SleepOracleCommand::TimeoutMessage message(*command_);
       oracle_.EmitMessage(receiver_, message);
     }
@@ -76,26 +78,6 @@
   };
     
 
-  class WebAssemblyOracle::Emitter : public IMessageEmitter
-  {
-  private:
-    WebAssemblyOracle&  oracle_;
-
-  public:
-    Emitter(WebAssemblyOracle&  oracle) :
-      oracle_(oracle)
-    {
-    }
-
-    virtual void EmitMessage(const IObserver& receiver,
-                             const IMessage& message)
-    {
-      LOG(TRACE) << "WebAssemblyOracle::Emitter::EmitMessage receiver = "
-        << std::hex << &receiver << std::dec;
-      oracle_.EmitMessage(receiver, message);
-    }
-  };
-
   /**
   This object is created on the heap for every http request.
   It is deleted in the success (or error) callbacks.
@@ -108,26 +90,23 @@
   class WebAssemblyOracle::FetchContext : public boost::noncopyable
   {
   private:
-    Emitter                        emitter_;
-    const IObserver&               receiver_;
+    WebAssemblyOracle&             oracle_;
+    boost::weak_ptr<IObserver>     receiver_;
     std::auto_ptr<IOracleCommand>  command_;
     std::string                    expectedContentType_;
-    std::string                    receiverFingerprint_;
 
   public:
     FetchContext(WebAssemblyOracle& oracle,
-                 const IObserver& receiver,
+                 boost::weak_ptr<IObserver> receiver,
                  IOracleCommand* command,
                  const std::string& expectedContentType) :
-      emitter_(oracle),
+      oracle_(oracle),
       receiver_(receiver),
       command_(command),
-      expectedContentType_(expectedContentType),
-      receiverFingerprint_(receiver.GetFingerprint())
+      expectedContentType_(expectedContentType)
     {
       LOG(TRACE) << "WebAssemblyOracle::FetchContext::FetchContext() | "
-        << "receiver address = " << std::hex << &receiver << std::dec 
-        << " with fingerprint = " << receiverFingerprint_;
+                 << "receiver address = " << std::hex << &receiver;
 
       if (command == NULL)
       {
@@ -140,21 +119,21 @@
       return expectedContentType_;
     }
 
+    IMessageEmitter& GetEmitter() const
+    {
+      return oracle_;
+    }
+
+    boost::weak_ptr<IObserver> GetReceiver() const
+    {
+      return receiver_;
+    }
+
     void EmitMessage(const IMessage& message)
     {
       LOG(TRACE) << "WebAssemblyOracle::FetchContext::EmitMessage receiver_ = "
         << std::hex << &receiver_ << std::dec;
-      emitter_.EmitMessage(receiver_, message);
-    }
-
-    IMessageEmitter& GetEmitter()
-    {
-      return emitter_;
-    }
-
-    const IObserver& GetReceiver() const
-    {
-      return receiver_;
+      oracle_.EmitMessage(receiver_, message);
     }
 
     IOracleCommand& GetCommand() const
@@ -213,48 +192,14 @@
        * free data associated with the fetch.
        **/
       
-      std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData));
-
-      // an UUID is 36 chars : 32 hex chars + 4 hyphens: char #0 --> char #35
-      // char #36 is \0.
-      bool callHandler = true;
-      
-      // TODO: remove this line because we are NOT allowed to call methods on GetReceiver that is maybe a dangling ref
-      if (context->GetReceiver().DoesFingerprintLookGood())
-      {
-        callHandler = true;
-        std::string currentFingerprint(context->GetReceiver().GetFingerprint());
-       
-        LOG(TRACE) << "SuccessCallback for object at address (" << std::hex 
-          << &(context->GetReceiver()) << std::dec 
-          << " with current fingerprint = " << currentFingerprint 
-          << ". Fingerprint looks OK";
-        
-        if (currentFingerprint != context->receiverFingerprint_)
-        {
-          LOG(TRACE) << "  ** SuccessCallback: BUT currentFingerprint != "
-            << "receiverFingerprint_(" << context->receiverFingerprint_ << ")";
-          callHandler = false;
-        }
-        else
-        {
-          LOG(TRACE) << "  ** SuccessCallback: FetchContext-level "
-            << "fingerprints are the same: "
-            << context->receiverFingerprint_
-            << " ---> oracle will dispatch the response to observer: "
-            << std::hex << &(context->GetReceiver()) << std::dec;
-        }
-      }
-      else {
-        LOG(TRACE) << "SuccessCallback for object at address (" << std::hex << &(context->GetReceiver()) << std::dec << " with current fingerprint is XXXXX -- NOT A VALID FINGERPRINT! OBJECT IS READ ! CALLBACK WILL NOT BE CALLED!";
-        callHandler = false;
-      }
-
       if (fetch->userData == NULL)
       {
         LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback fetch->userData is NULL!!!!!!!";
+        return;
       }
 
+      std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData));
+
       std::string answer;
       if (fetch->numBytes > 0)
       {
@@ -303,44 +248,41 @@
         }
         else
         {
-          if (callHandler)
+          switch (context->GetCommand().GetType())
           {
-            switch (context->GetCommand().GetType())
+            case IOracleCommand::Type_Http:
             {
-              case IOracleCommand::Type_Http:
-              {
-                HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer);
-                context->EmitMessage(message);
-                break;
-              }
+              HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer);
+              context->EmitMessage(message);
+              break;
+            }
+
+            case IOracleCommand::Type_OrthancRestApi:
+            {
+              LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
+              OrthancRestApiCommand::SuccessMessage message
+                (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
+              context->EmitMessage(message);
+              break;
+            }
 
-              case IOracleCommand::Type_OrthancRestApi:
-              {
-                LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
-                OrthancRestApiCommand::SuccessMessage message
-                  (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
-                context->EmitMessage(message);
-                break;
-              }
+            case IOracleCommand::Type_GetOrthancImage:
+            {
+              context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
+                (context->GetReceiver(), context->GetEmitter(), answer, headers);
+              break;
+            }
 
-              case IOracleCommand::Type_GetOrthancImage:
-              {
-                context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
-                  (context->GetEmitter(), context->GetReceiver(), answer, headers);
-                break;
-              }
+            case IOracleCommand::Type_GetOrthancWebViewerJpeg:
+            {
+              context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
+                (context->GetReceiver(), context->GetEmitter(), answer);
+              break;
+            }
 
-              case IOracleCommand::Type_GetOrthancWebViewerJpeg:
-              {
-                context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
-                  (context->GetEmitter(), context->GetReceiver(), answer);
-                break;
-              }
-
-              default:
-                LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
-                           << context->GetCommand().GetType();
-            }
+            default:
+              LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
+                         << context->GetCommand().GetType();
           }
         }
       }
@@ -388,7 +330,7 @@
   {
   private:
     WebAssemblyOracle&             oracle_;
-    const IObserver&               receiver_;
+    boost::weak_ptr<IObserver>     receiver_;
     std::auto_ptr<IOracleCommand>  command_;
     Orthanc::HttpMethod            method_;
     std::string                    url_;
@@ -399,7 +341,7 @@
 
   public:
     FetchCommand(WebAssemblyOracle& oracle,
-                 const IObserver& receiver,
+                 boost::weak_ptr<IObserver> receiver,
                  IOracleCommand* command) :
       oracle_(oracle),
       receiver_(receiver),
@@ -568,7 +510,7 @@
 #endif
 
   
-  void WebAssemblyOracle::Execute(const IObserver& receiver,
+  void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
                                   HttpCommand* command)
   {
     FetchCommand fetch(*this, receiver, command);
@@ -590,7 +532,7 @@
   }
   
 
-  void WebAssemblyOracle::Execute(const IObserver& receiver,
+  void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
                                   OrthancRestApiCommand* command)
   {
 #if 0
@@ -653,7 +595,7 @@
   }
     
     
-  void WebAssemblyOracle::Execute(const IObserver& receiver,
+  void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
                                   GetOrthancImageCommand* command)
   {
 #if 0
@@ -674,7 +616,7 @@
   }
     
     
-  void WebAssemblyOracle::Execute(const IObserver& receiver,
+  void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
                                   GetOrthancWebViewerJpegCommand* command)
   {
 #if 0
@@ -695,12 +637,11 @@
 
 
 
-  void WebAssemblyOracle::Schedule(boost::shared_ptr<IObserver>& receiver,
+  bool WebAssemblyOracle::Schedule(boost::shared_ptr<IObserver> receiver,
                                    IOracleCommand* command)
   {
     LOG(TRACE) << "WebAssemblyOracle::Schedule : receiver = "
-      << std::hex << &receiver << std::dec
-      << " | Current fingerprint is " << receiver.GetFingerprint();
+               << std::hex << &receiver;
     
     std::auto_ptr<IOracleCommand> protection(command);
 
@@ -759,6 +700,9 @@
             
       default:
         LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " << command->GetType();
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
     }
+
+    return true;
   }
 }
--- a/Framework/Oracle/WebAssemblyOracle.h	Mon Dec 09 13:58:37 2019 +0100
+++ b/Framework/Oracle/WebAssemblyOracle.h	Mon Dec 09 14:41:37 2019 +0100
@@ -41,42 +41,49 @@
 {
   class WebAssemblyOracle :
     public IOracle,
-    public IObservable
+    public IObservable,
+    public IMessageEmitter
   {
   private:
     typedef std::map<std::string, std::string>  HttpHeaders;
     
     class TimeoutContext;
-    class Emitter;
     class FetchContext;
     class FetchCommand;    
     
-    void Execute(const IObserver& receiver,
+    void Execute(boost::weak_ptr<IObserver> receiver,
                  HttpCommand* command);    
     
-    void Execute(const IObserver& receiver,
+    void Execute(boost::weak_ptr<IObserver> receiver,
                  OrthancRestApiCommand* command);    
     
-    void Execute(const IObserver& receiver,
+    void Execute(boost::weak_ptr<IObserver> receiver,
                  GetOrthancImageCommand* command);    
     
-    void Execute(const IObserver& receiver,
+    void Execute(boost::weak_ptr<IObserver> receiver,
                  GetOrthancWebViewerJpegCommand* command);
 
+    IObservable oracleObservable_;
     std::string orthancRoot_;
 
   public:
-    WebAssemblyOracle(MessageBroker& broker) :
-      IObservable(broker)
+    virtual void EmitMessage(boost::weak_ptr<IObserver> observer,
+                             const IMessage& message) ORTHANC_OVERRIDE
     {
+      oracleObservable_.EmitMessage(observer, message);
+    }
+    
+    virtual bool Schedule(boost::shared_ptr<IObserver> receiver,
+                          IOracleCommand* command) ORTHANC_OVERRIDE;
+
+    IObservable& GetOracleObservable()
+    {
+      return oracleObservable_;
     }
 
     void SetOrthancRoot(const std::string& root)
     {
       orthancRoot_ = root;
     }
-    
-    virtual void Schedule(boost::shared_ptr<IObserver>& receiver,
-                          IOracleCommand* command) ORTHANC_OVERRIDE;
   };
 }