diff Platforms/Wasm/WasmWebService.cpp @ 435:e641d3978856 am-vsol-upgrade

WasmWebService now using BaseWebService and supporting cache
author am@osimis.io
date Tue, 04 Dec 2018 11:52:43 +0100
parents 3a8bcc45c221
children 56ddca73396c
line wrap: on
line diff
--- a/Platforms/Wasm/WasmWebService.cpp	Mon Dec 03 18:03:30 2018 +0100
+++ b/Platforms/Wasm/WasmWebService.cpp	Tue Dec 04 11:52:43 2018 +0100
@@ -2,6 +2,15 @@
 #include "json/value.h"
 #include "json/writer.h"
 #include <emscripten/emscripten.h>
+#include <boost/shared_ptr.hpp>
+
+struct CachedSuccessNotification
+{
+  boost::shared_ptr<OrthancStone::BaseWebService::CachedHttpRequestSuccessMessage>    cachedMessage;
+  std::auto_ptr<Orthanc::IDynamicObject>                                              payload;
+  OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>* successCallback;
+};
+
 
 #ifdef __cplusplus
 extern "C" {
@@ -14,6 +23,8 @@
                                       void* payload,
                                       unsigned int timeoutInSeconds);
 
+  extern void WasmWebService_ScheduleLaterCachedSuccessNotification(void* brol);
+
   extern void WasmWebService_PostAsync(void* callableSuccess,
                                        void* callableFailure,
                                        const char* uri,
@@ -45,6 +56,20 @@
     }
   }
 
+  void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyCachedSuccess(void* notification_)
+  {
+    // notification has been allocated in C++ and passed to JS.  It must be deleted by this method
+    std::auto_ptr<CachedSuccessNotification> notification(reinterpret_cast<CachedSuccessNotification*>(notification_));
+
+    notification->successCallback->Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(
+      notification->cachedMessage->GetUri(), 
+      notification->cachedMessage->GetAnswer(),
+      notification->cachedMessage->GetAnswerSize(),
+      notification->cachedMessage->GetAnswerHttpHeaders(),
+      notification->payload.get()
+      ));
+  }
+
   void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable,
                                                          const char* uri,
                                                          const void* body,
@@ -61,7 +86,7 @@
       OrthancStone::IWebService::HttpHeaders headers;
 
       // TODO - Parse "answerHeaders"
-      printf("[%s]\n", answerHeaders);
+      printf("TODO: parse headers [%s]\n", answerHeaders);
       
       reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
         Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers,
@@ -134,4 +159,17 @@
     WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(),
                             headersInJsonString.c_str(), payload, timeoutInSeconds);
   }
+
+  void WasmWebService::NotifyHttpSuccessLater(boost::shared_ptr<BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage,
+                                                Orthanc::IDynamicObject* payload, // takes ownership
+                                                MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback)
+  {
+    CachedSuccessNotification* notification = new CachedSuccessNotification();  // allocated on the heap, it will be passed to JS and deleted when coming back to C++
+    notification->cachedMessage = cachedMessage;
+    notification->payload.reset(payload);
+    notification->successCallback = successCallback;
+
+    WasmWebService_ScheduleLaterCachedSuccessNotification(notification);
+  }
+
 }