changeset 303:ed1a4302154f am-callable-and-promise

new messages in wasm too
author am@osimis.io
date Tue, 18 Sep 2018 18:17:26 +0200
parents 4a79193ffb58
children 6c22e0506587
files Platforms/Wasm/WasmWebService.cpp Platforms/Wasm/WasmWebService.h Platforms/Wasm/WasmWebService.js Platforms/Wasm/wasm-application-runner.ts
diffstat 4 files changed, 44 insertions(+), 131 deletions(-) [+]
line wrap: on
line diff
--- a/Platforms/Wasm/WasmWebService.cpp	Tue Sep 18 18:04:53 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.cpp	Tue Sep 18 18:17:26 2018 +0200
@@ -7,57 +7,22 @@
 extern "C" {
 #endif
 
-  extern void WasmWebService_ScheduleGetRequest(void* callback,
-                                                const char* uri,
-                                                const char* headersInJsonString,
-                                                void* payload);
-
-  extern void WasmWebService_NewScheduleGetRequest(void* callableSuccess,
-                                                void* callableFailure,
-                                                const char* uri,
-                                                const char* headersInJsonString,
-                                                void* payload);
-
-  extern void WasmWebService_SchedulePostRequest(void* callback,
-                                                 const char* uri,
-                                                 const char* headersInJsonString,
-                                                 const void* body,
-                                                 size_t bodySize,
-                                                 void* payload);
+  extern void WasmWebService_GetAsync(void* callableSuccess,
+                                      void* callableFailure,
+                                      const char* uri,
+                                      const char* headersInJsonString,
+                                      void* payload);
 
-  void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* callback,
-                                                       const char* uri,
-                                                       void* payload)
-  {
-    if (callback == NULL)
-    {
-      throw;
-    }
-    else
-    {
-      reinterpret_cast<OrthancStone::IWebService::ICallback*>(callback)->
-        OnHttpRequestError(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload));
-    }
-  }
+  extern void WasmWebService_PostAsync(void* callableSuccess,
+                                       void* callableFailure,
+                                       const char* uri,
+                                       const char* headersInJsonString,
+                                       const void* body,
+                                       size_t bodySize,
+                                       void* payload);
 
-  void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* callback,
-                                                         const char* uri,
-                                                         const void* body,
-                                                         size_t bodySize,
-                                                         void* payload)
-  {
-    if (callback == NULL)
-    {
-      throw;
-    }
-    else
-    {
-      reinterpret_cast<OrthancStone::IWebService::ICallback*>(callback)->
-        OnHttpRequestSuccess(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)); 
-   }
-  }
 
-  void EMSCRIPTEN_KEEPALIVE WasmWebService_NewNotifyError(void* failureCallable,
+  void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* failureCallable,
                                                        const char* uri,
                                                        void* payload)
   {
@@ -67,12 +32,12 @@
     }
     else
     {
-      reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::NewHttpRequestErrorMessage>*>(failureCallable)->
-        Apply(OrthancStone::IWebService::NewHttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
+      reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)->
+        Apply(OrthancStone::IWebService::HttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
     }
   }
 
-  void EMSCRIPTEN_KEEPALIVE WasmWebService_NewNotifySuccess(void* successCallable,
+  void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable,
                                                          const char* uri,
                                                          const void* body,
                                                          size_t bodySize,
@@ -84,8 +49,8 @@
     }
     else
     {
-      reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::NewHttpRequestSuccessMessage>*>(successCallable)->
-        Apply(OrthancStone::IWebService::NewHttpRequestSuccessMessage(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
+      reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
+        Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
    }
   }
 
@@ -134,40 +99,30 @@
     output = outputStr.str();
   }
 
-  void WasmWebService::ScheduleGetRequest(ICallback& callback,
-                                          const std::string& relativeUri,
-                                          const Headers& headers,
-                                          Orthanc::IDynamicObject* payload)
+  void WasmWebService::PostAsync(const std::string& relativeUri,
+                           const Headers& headers,
+                           const std::string& body,
+                           Orthanc::IDynamicObject* payload,
+                           MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
+                           MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable)
   {
     std::string uri = baseUri_ + relativeUri;
     std::string headersInJsonString;
     ToJsonString(headersInJsonString, headers);
-    WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), headersInJsonString.c_str(), payload);
-  }
-
-  void WasmWebService::SchedulePostRequest(ICallback& callback,
-                                           const std::string& relativeUri,
-                                           const Headers& headers,
-                                           const std::string& body,
-                                           Orthanc::IDynamicObject* payload)
-  {
-    std::string uri = baseUri_ + relativeUri;
-    std::string headersInJsonString;
-    ToJsonString(headersInJsonString, headers);
-    WasmWebService_SchedulePostRequest(&callback, uri.c_str(), headersInJsonString.c_str(),
+    WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(),
                                        body.c_str(), body.size(), payload);
   }
 
    void WasmWebService::GetAsync(const std::string& relativeUri,
                           const Headers& headers,
                           Orthanc::IDynamicObject* payload,
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallable,
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallable)
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable)
   {
     std::string uri = baseUri_ + relativeUri;
     std::string headersInJsonString;
     ToJsonString(headersInJsonString, headers);
-    WasmWebService_NewScheduleGetRequest(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), payload);
+    WasmWebService_GetAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), payload);
   }
 
 }
--- a/Platforms/Wasm/WasmWebService.h	Tue Sep 18 18:04:53 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.h	Tue Sep 18 18:17:26 2018 +0200
@@ -37,22 +37,18 @@
 
     void SetBaseUri(const std::string baseUri);
 
-    virtual void ScheduleGetRequest(ICallback& callback,
-                                    const std::string& uri,
-                                    const Headers& headers,
-                                    Orthanc::IDynamicObject* payload);
-
-    virtual void SchedulePostRequest(ICallback& callback,
-                                     const std::string& uri,
-                                     const Headers& headers,
-                                     const std::string& body,
-                                     Orthanc::IDynamicObject* payload);
-
-    virtual void GetAsync(const std::string& relativeUri,
+    virtual void GetAsync(const std::string& uri,
                           const Headers& headers,
                           Orthanc::IDynamicObject* payload,
-                          MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback,
-                          MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback);
+                          MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
+                          MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable);
+
+    virtual void PostAsync(const std::string& uri,
+                           const Headers& headers,
+                           const std::string& body,
+                           Orthanc::IDynamicObject* payload,
+                           MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
+                           MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable);
 
     virtual void Start()
     {
--- a/Platforms/Wasm/WasmWebService.js	Tue Sep 18 18:04:53 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.js	Tue Sep 18 18:17:26 2018 +0200
@@ -1,36 +1,5 @@
 mergeInto(LibraryManager.library, {
-  WasmWebService_ScheduleGetRequest: function(callback, url, headersInJsonString, payload) {
-    // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
-    // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
-    var xhr = new XMLHttpRequest();
-    var url_ = UTF8ToString(url);
-    var headersInJsonString_ = UTF8ToString(headersInJsonString);
-
-    xhr.open('GET', url_, true);
-    xhr.responseType = 'arraybuffer';
-    var headers = JSON.parse(headersInJsonString_);
-    for (var key in headers) {
-      xhr.setRequestHeader(key, headers[key]);
-    }
-    // console.log(xhr); 
-    xhr.onreadystatechange = function() {
-      if (this.readyState == XMLHttpRequest.DONE) {
-        if (xhr.status === 200) {
-          // TODO - Is "new Uint8Array()" necessary? This copies the
-          // answer to the WebAssembly stack, hence necessitating
-          // increasing the TOTAL_STACK parameter of Emscripten
-          WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
-                                       this.response.byteLength, payload);
-        } else {
-          WasmWebService_NotifyError(callback, url_, payload);
-        }
-      }
-    }
-    
-    xhr.send();
-  },
-
-  WasmWebService_NewScheduleGetRequest: function(callableSuccess, callableFailure, url, headersInJsonString, payload) {
+  WasmWebService_GetAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload) {
     // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
     // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
     var xhr = new XMLHttpRequest();
@@ -50,13 +19,10 @@
           // TODO - Is "new Uint8Array()" necessary? This copies the
           // answer to the WebAssembly stack, hence necessitating
           // increasing the TOTAL_STACK parameter of Emscripten
-          console.log("WasmWebService success", this.response); 
-          WasmWebService_NewNotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
+          WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
                                        this.response.byteLength, payload);
-          console.log("WasmWebService success 2", this.response); 
         } else {
-          console.log("WasmWebService failed"); 
-          WasmWebService_NewNotifyError(callableFailure, url_, payload);
+          WasmWebService_NotifyError(callableFailure, url_, payload);
         }
       }
     }
@@ -64,7 +30,7 @@
     xhr.send();
   },
 
-  WasmWebService_SchedulePostRequest: function(callback, url, headersInJsonString, body, bodySize, payload) {
+  WasmWebService_PostAsync: function(callableSuccess, callableFailure, url, headersInJsonString, body, bodySize, payload) {
     var xhr = new XMLHttpRequest();
     var url_ = UTF8ToString(url);
     var headersInJsonString_ = UTF8ToString(headersInJsonString);
@@ -80,10 +46,10 @@
     xhr.onreadystatechange = function() {
       if (this.readyState == XMLHttpRequest.DONE) {
         if (xhr.status === 200) {
-          WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
+          WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
                                        this.response.byteLength, payload);
         } else {
-          WasmWebService_NotifyError(callback, url_, payload);
+          WasmWebService_NotifyError(callableFailure, url_, payload);
         }
       }
     }
--- a/Platforms/Wasm/wasm-application-runner.ts	Tue Sep 18 18:04:53 2018 +0200
+++ b/Platforms/Wasm/wasm-application-runner.ts	Tue Sep 18 18:17:26 2018 +0200
@@ -10,8 +10,6 @@
 // global functions
 var WasmWebService_NotifyError: Function = null;
 var WasmWebService_NotifySuccess: Function = null;
-var WasmWebService_NewNotifyError: Function = null;
-var WasmWebService_NewNotifySuccess: Function = null;
 var WasmWebService_SetBaseUri: Function = null;
 var NotifyUpdateContent: Function = null;
 var SetStartupParameter: Function = null;
@@ -97,8 +95,6 @@
 
     WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
     WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
-    WasmWebService_NewNotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NewNotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
-    WasmWebService_NewNotifyError = StoneFrameworkModule.cwrap('WasmWebService_NewNotifyError', null, ['number', 'string', 'number']);
     WasmWebService_SetBaseUri = StoneFrameworkModule.cwrap('WasmWebService_SetBaseUri', null, ['string']);
     NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []);