diff Framework/Toolbox/OrthancApiClient.h @ 422:4fe42ae3653a am-vsol-upgrade

introduced a basic cache at OrthancApiClient level so it can be used with the RadiographyScene
author am@osimis.io
date Tue, 20 Nov 2018 21:44:48 +0100
parents aee3d7941c9b
children bb573a52fc63
line wrap: on
line diff
--- a/Framework/Toolbox/OrthancApiClient.h	Mon Nov 19 12:45:37 2018 +0100
+++ b/Framework/Toolbox/OrthancApiClient.h	Tue Nov 20 21:44:48 2018 +0100
@@ -31,12 +31,12 @@
 namespace OrthancStone
 {
   class OrthancApiClient :
-    public IObservable,
-    public IObserver
+      public IObservable,
+      public IObserver
   {
   public:
     class JsonResponseReadyMessage :
-      public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
+        public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
     {
     private:
       const std::string&              uri_;
@@ -73,7 +73,7 @@
     
 
     class BinaryResponseReadyMessage :
-      public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
+        public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
     {
     private:
       const std::string&              uri_;
@@ -118,7 +118,7 @@
 
 
     class EmptyResponseReadyMessage :
-      public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready>
+        public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready>
     {
     private:
       const std::string&              uri_;
@@ -149,11 +149,14 @@
 
   private:
     class WebServicePayload;
-    
+    class CachedHttpRequestSuccessMessage;
+
   protected:
     IWebService&  web_;
     std::string   baseUrl_;
 
+    std::map<std::string, boost::shared_ptr<CachedHttpRequestSuccessMessage>> cache_;  // TODO: this is currently an infinite cache !
+    bool          cacheEnabled_;
   public:
     OrthancApiClient(MessageBroker& broker,
                      IWebService& web,
@@ -163,6 +166,11 @@
     {
     }
 
+    void EnableCache(bool enable)
+    {
+      cacheEnabled_ = enable;
+    }
+
     // schedule a GET request expecting a JSON response.
     void GetJsonAsync(const std::string& uri,
                       MessageHandler<JsonResponseReadyMessage>* successCallback,
@@ -206,5 +214,12 @@
     void NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
 
     void NotifyHttpError(const IWebService::HttpRequestErrorMessage& message);
+
+    void CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
+
+  private:
+    void HandleFromCache(const std::string& uri,
+                         const IWebService::HttpHeaders& headers,
+                         Orthanc::IDynamicObject* payload /* takes ownership */);
   };
 }