changeset 301:547e1cf7aa7b am-callable-and-promise

cleanup
author am@osimis.io
date Tue, 18 Sep 2018 15:34:28 +0200
parents b4abaeb783b1
children 4a79193ffb58
files Framework/Toolbox/IWebService.h Framework/Toolbox/OrthancApiClient.cpp Platforms/Generic/OracleWebService.h Platforms/Generic/WebServiceCommandBase.cpp Platforms/Generic/WebServiceCommandBase.h Platforms/Generic/WebServiceGetCommand.cpp Platforms/Generic/WebServiceGetCommand.h Platforms/Generic/WebServicePostCommand.cpp Platforms/Generic/WebServicePostCommand.h
diffstat 9 files changed, 57 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Toolbox/IWebService.h	Tue Sep 18 15:23:21 2018 +0200
+++ b/Framework/Toolbox/IWebService.h	Tue Sep 18 15:34:28 2018 +0200
@@ -42,110 +42,37 @@
   public:
     typedef std::map<std::string, std::string> Headers;
 
-    struct NewHttpRequestSuccessMessage: public BaseMessage<MessageType_HttpRequestSuccess>
+    struct HttpRequestSuccessMessage: public BaseMessage<MessageType_HttpRequestSuccess>
     {
-      const std::string& Uri;
-      const void* Answer;
-      size_t AnswerSize;
-      Orthanc::IDynamicObject* Payload;
-      NewHttpRequestSuccessMessage(const std::string& uri,
+      const std::string& uri_;
+      const void* answer_;
+      size_t answerSize_;
+      Orthanc::IDynamicObject* payload_;
+      HttpRequestSuccessMessage(const std::string& uri,
                                    const void* answer,
                                    size_t answerSize,
                                    Orthanc::IDynamicObject* payload)
         : BaseMessage(),
-          Uri(uri),
-          Answer(answer),
-          AnswerSize(answerSize),
-          Payload(payload)
+          uri_(uri),
+          answer_(answer),
+          answerSize_(answerSize),
+          payload_(payload)
       {}
     };
 
-    struct NewHttpRequestErrorMessage: public BaseMessage<MessageType_HttpRequestError>
+    struct HttpRequestErrorMessage: public BaseMessage<MessageType_HttpRequestError>
     {
-      const std::string& Uri;
-      Orthanc::IDynamicObject* Payload;
-      NewHttpRequestErrorMessage(const std::string& uri,
+      const std::string& uri_;
+      Orthanc::IDynamicObject* payload_;
+      HttpRequestErrorMessage(const std::string& uri,
                                  Orthanc::IDynamicObject* payload)
         : BaseMessage(),
-          Uri(uri),
-          Payload(payload)
+          uri_(uri),
+          payload_(payload)
       {}
     };
 
 
-    class ICallback : public IObserver
-    {
-    public:
-      struct HttpRequestSuccessMessage: public IMessage
-      {
-        const std::string& Uri;
-        const void* Answer;
-        size_t AnswerSize;
-        Orthanc::IDynamicObject* Payload;
-        HttpRequestSuccessMessage(const std::string& uri,
-                                  const void* answer,
-                                  size_t answerSize,
-                                  Orthanc::IDynamicObject* payload)
-          : IMessage(MessageType_HttpRequestSuccess),
-            Uri(uri),
-            Answer(answer),
-            AnswerSize(answerSize),
-            Payload(payload)
-        {}
-      };
-
-      struct HttpRequestErrorMessage: public IMessage
-      {
-        const std::string& Uri;
-        Orthanc::IDynamicObject* Payload;
-        HttpRequestErrorMessage(const std::string& uri,
-                                Orthanc::IDynamicObject* payload)
-          : IMessage(MessageType_HttpRequestError),
-            Uri(uri),
-            Payload(payload)
-        {}
-      };
-
-      ICallback(MessageBroker& broker)
-        : IObserver(broker)
-      {
-      }
-      virtual ~ICallback()
-      {
-      }
-
-      virtual void HandleMessage(IObservable& from, const IMessage& message)
-      {
-        switch(message.GetType())
-        {
-        case MessageType_HttpRequestError:
-        {
-          const HttpRequestErrorMessage& msg = dynamic_cast<const HttpRequestErrorMessage&>(message);
-          OnHttpRequestError(msg.Uri,
-                             msg.Payload);
-        }; break;
-
-        case MessageType_HttpRequestSuccess:
-        {
-          const HttpRequestSuccessMessage& msg = dynamic_cast<const HttpRequestSuccessMessage&>(message);
-          OnHttpRequestSuccess(msg.Uri,
-                               msg.Answer,
-                               msg.AnswerSize,
-                               msg.Payload);
-        }; break;
-        default:
-          VLOG("unhandled message type" << message.GetType());
-        }
-      }
-
-      virtual void OnHttpRequestError(const std::string& uri,
-                                      Orthanc::IDynamicObject* payload) = 0;
-
-      virtual void OnHttpRequestSuccess(const std::string& uri,
-                                        const void* answer,
-                                        size_t answerSize,
-                                        Orthanc::IDynamicObject* payload) = 0;
-    };
 
     IWebService(MessageBroker& broker)
       : broker_(broker)
@@ -158,15 +85,15 @@
     virtual void GetAsync(const std::string& uri,
                           const Headers& headers,
                           Orthanc::IDynamicObject* payload,
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) = 0;
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback) = 0;
 
     virtual void PostAsync(const std::string& uri,
                            const Headers& headers,
                            const std::string& body,
                            Orthanc::IDynamicObject* payload,
-                           MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,
-                           MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) = 0;
+                           MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
+                           MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback) = 0;
 
   };
 }
--- a/Framework/Toolbox/OrthancApiClient.cpp	Tue Sep 18 15:23:21 2018 +0200
+++ b/Framework/Toolbox/OrthancApiClient.cpp	Tue Sep 18 15:34:28 2018 +0200
@@ -48,29 +48,29 @@
     {
     }
 
-    void ConvertResponseToJson(const IWebService::NewHttpRequestSuccessMessage& message)
+    void ConvertResponseToJson(const IWebService::HttpRequestSuccessMessage& message)
     {
       Json::Value response;
-      if (MessagingToolbox::ParseJson(response, message.Answer, message.AnswerSize))
+      if (MessagingToolbox::ParseJson(response, message.answer_, message.answerSize_))
       {
         if (orthancApiSuccessCallback_.get() != NULL)
         {
-          orthancApiSuccessCallback_->Apply(OrthancApiClient::JsonResponseReadyMessage(message.Uri, response, message.Payload));
+          orthancApiSuccessCallback_->Apply(OrthancApiClient::JsonResponseReadyMessage(message.uri_, response, message.payload_));
         }
       }
       else if (orthancApiFailureCallback_.get() != NULL)
       {
-        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.Uri, message.Payload));
+        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_));
       }
 
       delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
     }
 
-    void ConvertError(const IWebService::NewHttpRequestErrorMessage& message)
+    void ConvertError(const IWebService::HttpRequestErrorMessage& message)
     {
       if (orthancApiFailureCallback_.get() != NULL)
       {
-        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.Uri));
+        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_));
       }
 
       delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
@@ -94,25 +94,25 @@
     {
     }
 
-    void ConvertResponseToBinary(const IWebService::NewHttpRequestSuccessMessage& message)
+    void ConvertResponseToBinary(const IWebService::HttpRequestSuccessMessage& message)
     {
       if (orthancApiSuccessCallback_.get() != NULL)
       {
-        orthancApiSuccessCallback_->Apply(OrthancApiClient::BinaryResponseReadyMessage(message.Uri, message.Answer, message.AnswerSize, message.Payload));
+        orthancApiSuccessCallback_->Apply(OrthancApiClient::BinaryResponseReadyMessage(message.uri_, message.answer_, message.answerSize_, message.payload_));
       }
       else if (orthancApiFailureCallback_.get() != NULL)
       {
-        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.Uri, message.Payload));
+        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_));
       }
 
       delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
     }
 
-    void ConvertError(const IWebService::NewHttpRequestErrorMessage& message)
+    void ConvertError(const IWebService::HttpRequestErrorMessage& message)
     {
       if (orthancApiFailureCallback_.get() != NULL)
       {
-        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.Uri));
+        orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_));
       }
 
       delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
@@ -126,8 +126,8 @@
   {
     HttpResponseToJsonConverter* converter = new HttpResponseToJsonConverter(broker_, successCallback, failureCallback);  // it is currently deleting itself after being used
     orthanc_.GetAsync(uri, IWebService::Headers(), payload,
-                      new Callable<HttpResponseToJsonConverter, IWebService::NewHttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson),
-                      new Callable<HttpResponseToJsonConverter, IWebService::NewHttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError));
+                      new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson),
+                      new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError));
 
   }
 
@@ -139,8 +139,8 @@
   {
     HttpResponseToBinaryConverter* converter = new HttpResponseToBinaryConverter(broker_, successCallback, failureCallback);  // it is currently deleting itself after being used
     orthanc_.GetAsync(uri, headers, payload,
-                      new Callable<HttpResponseToBinaryConverter, IWebService::NewHttpRequestSuccessMessage>(*converter, &HttpResponseToBinaryConverter::ConvertResponseToBinary),
-                      new Callable<HttpResponseToBinaryConverter, IWebService::NewHttpRequestErrorMessage>(*converter, &HttpResponseToBinaryConverter::ConvertError));
+                      new Callable<HttpResponseToBinaryConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToBinaryConverter::ConvertResponseToBinary),
+                      new Callable<HttpResponseToBinaryConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToBinaryConverter::ConvertError));
   }
 
   void OrthancApiClient::PostBinaryAsyncExpectJson(const std::string& uri,
@@ -151,8 +151,8 @@
   {
     HttpResponseToJsonConverter* converter = new HttpResponseToJsonConverter(broker_, successCallback, failureCallback);  // it is currently deleting itself after being used
     orthanc_.PostAsync(uri, IWebService::Headers(), body, payload,
-                       new Callable<HttpResponseToJsonConverter, IWebService::NewHttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson),
-                       new Callable<HttpResponseToJsonConverter, IWebService::NewHttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError));
+                       new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestSuccessMessage>(*converter, &HttpResponseToJsonConverter::ConvertResponseToJson),
+                       new Callable<HttpResponseToJsonConverter, IWebService::HttpRequestErrorMessage>(*converter, &HttpResponseToJsonConverter::ConvertError));
 
   }
 
--- a/Platforms/Generic/OracleWebService.h	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/OracleWebService.h	Tue Sep 18 15:34:28 2018 +0200
@@ -54,8 +54,8 @@
     virtual void GetAsync(const std::string& uri,
                           const Headers& headers,
                           Orthanc::IDynamicObject* payload, // takes ownership
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,   // takes ownership
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback = NULL)// takes ownership
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,   // takes ownership
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL)// takes ownership
     {
       oracle_.Submit(new WebServiceGetCommand(broker_, successCallback, failureCallback, parameters_, uri, headers, payload, context_));
     }
@@ -64,8 +64,8 @@
                            const Headers& headers,
                            const std::string& body,
                            Orthanc::IDynamicObject* payload, // takes ownership
-                           MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback, // takes ownership
-                           MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) // takes ownership
+                           MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback, // takes ownership
+                           MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback) // takes ownership
     {
       oracle_.Submit(new WebServicePostCommand(broker_, successCallback, failureCallback, parameters_, uri, headers, body, payload, context_));
     }
--- a/Platforms/Generic/WebServiceCommandBase.cpp	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServiceCommandBase.cpp	Tue Sep 18 15:34:28 2018 +0200
@@ -26,8 +26,8 @@
 namespace OrthancStone
 {
   WebServiceCommandBase::WebServiceCommandBase(MessageBroker& broker,
-                                               MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,
-                                               MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,
+                                               MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
+                                               MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
                                                const Orthanc::WebServiceParameters& parameters,
                                                const std::string& uri,
                                                const IWebService::Headers& headers,
@@ -51,11 +51,11 @@
 
     if (success_ && successCallback_.get() != NULL)
     {
-      successCallback_->Apply(IWebService::NewHttpRequestSuccessMessage(uri_, answer_.c_str(), answer_.size(), payload_.release()));
+      successCallback_->Apply(IWebService::HttpRequestSuccessMessage(uri_, answer_.c_str(), answer_.size(), payload_.release()));
     }
     else if (!success_ && failureCallback_.get() != NULL)
     {
-      successCallback_->Apply(IWebService::NewHttpRequestErrorMessage(uri_, payload_.release()));
+      successCallback_->Apply(IWebService::HttpRequestErrorMessage(uri_, payload_.release()));
     }
 
   }
--- a/Platforms/Generic/WebServiceCommandBase.h	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServiceCommandBase.h	Tue Sep 18 15:34:28 2018 +0200
@@ -37,8 +37,8 @@
   class WebServiceCommandBase : public IOracleCommand, IObservable
   {
   protected:
-    std::auto_ptr<MessageHandler<IWebService::NewHttpRequestSuccessMessage>>                              successCallback_;
-    std::auto_ptr<MessageHandler<IWebService::NewHttpRequestErrorMessage>>                                failureCallback_;
+    std::auto_ptr<MessageHandler<IWebService::HttpRequestSuccessMessage>>                              successCallback_;
+    std::auto_ptr<MessageHandler<IWebService::HttpRequestErrorMessage>>                                failureCallback_;
     Orthanc::WebServiceParameters           parameters_;
     std::string                             uri_;
     std::map<std::string, std::string>      headers_;
@@ -49,8 +49,8 @@
 
   public:
     WebServiceCommandBase(MessageBroker& broker,
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,  // takes ownership
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,  // takes ownership
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,  // takes ownership
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,  // takes ownership
                           const Orthanc::WebServiceParameters& parameters,
                           const std::string& uri,
                           const std::map<std::string, std::string>& headers,
--- a/Platforms/Generic/WebServiceGetCommand.cpp	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServiceGetCommand.cpp	Tue Sep 18 15:34:28 2018 +0200
@@ -27,8 +27,8 @@
 {
 
   WebServiceGetCommand::WebServiceGetCommand(MessageBroker& broker,
-                                             MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,  // takes ownership
-                                             MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,  // takes ownership
+                                             MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,  // takes ownership
+                                             MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,  // takes ownership
                                              const Orthanc::WebServiceParameters& parameters,
                                              const std::string& uri,
                                              const IWebService::Headers& headers,
--- a/Platforms/Generic/WebServiceGetCommand.h	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServiceGetCommand.h	Tue Sep 18 15:34:28 2018 +0200
@@ -29,8 +29,8 @@
   {
   public:
     WebServiceGetCommand(MessageBroker& broker,
-                         MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,  // takes ownership
-                         MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,  // takes ownership
+                         MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,  // takes ownership
+                         MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,  // takes ownership
                          const Orthanc::WebServiceParameters& parameters,
                          const std::string& uri,
                          const IWebService::Headers& headers,
--- a/Platforms/Generic/WebServicePostCommand.cpp	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServicePostCommand.cpp	Tue Sep 18 15:34:28 2018 +0200
@@ -26,8 +26,8 @@
 namespace OrthancStone
 {
   WebServicePostCommand::WebServicePostCommand(MessageBroker& broker,
-                                               MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,  // takes ownership
-                                               MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,  // takes ownership
+                                               MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,  // takes ownership
+                                               MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,  // takes ownership
                                                const Orthanc::WebServiceParameters& parameters,
                                                const std::string& uri,
                                                const IWebService::Headers& headers,
--- a/Platforms/Generic/WebServicePostCommand.h	Tue Sep 18 15:23:21 2018 +0200
+++ b/Platforms/Generic/WebServicePostCommand.h	Tue Sep 18 15:34:28 2018 +0200
@@ -32,8 +32,8 @@
 
   public:
     WebServicePostCommand(MessageBroker& broker,
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,  // takes ownership
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback,  // takes ownership
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,  // takes ownership
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,  // takes ownership
                           const Orthanc::WebServiceParameters& parameters,
                           const std::string& uri,
                           const IWebService::Headers& headers,