changeset 253:8ff70c04c6df am-2

IObservable/IObserver now working in WASM too
author am@osimis.io
date Tue, 03 Jul 2018 10:48:47 +0200
parents 40b21c1f8b8d
children abc1c6231947
files Applications/Samples/SampleMainWasm.cpp Applications/Samples/SimpleViewerApplication.h Framework/Messages/MessageBroker.cpp Platforms/Wasm/Defaults.cpp Platforms/Wasm/Defaults.h Platforms/Wasm/WasmWebService.cpp Platforms/Wasm/WasmWebService.h Platforms/Wasm/wasm-application.ts
diffstat 8 files changed, 44 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Samples/SampleMainWasm.cpp	Tue Jul 03 10:26:56 2018 +0200
+++ b/Applications/Samples/SampleMainWasm.cpp	Tue Jul 03 10:48:47 2018 +0200
@@ -26,7 +26,7 @@
 #include "SampleList.h"
 
 
-OrthancStone::IBasicApplication* CreateUserApplication() {
+OrthancStone::IBasicApplication* CreateUserApplication(OrthancStone::MessageBroker& broker) {
   
-  return new Application();
+  return new Application(broker);
 }
\ No newline at end of file
--- a/Applications/Samples/SimpleViewerApplication.h	Tue Jul 03 10:26:56 2018 +0200
+++ b/Applications/Samples/SimpleViewerApplication.h	Tue Jul 03 10:48:47 2018 +0200
@@ -179,6 +179,8 @@
         case MessageType_GeometryReady:
           mainLayout_->SetDefaultView();
           break;
+        default:
+          VLOG("unhandled message type" << message.GetType());
         }
       }
 
--- a/Framework/Messages/MessageBroker.cpp	Tue Jul 03 10:26:56 2018 +0200
+++ b/Framework/Messages/MessageBroker.cpp	Tue Jul 03 10:48:47 2018 +0200
@@ -23,6 +23,7 @@
 
 #include <algorithm>
 #include <assert.h>
+#include <vector>
 
 #include "IObserver.h"
 #include "MessageType.h"
--- a/Platforms/Wasm/Defaults.cpp	Tue Jul 03 10:26:56 2018 +0200
+++ b/Platforms/Wasm/Defaults.cpp	Tue Jul 03 10:48:47 2018 +0200
@@ -16,11 +16,11 @@
 static std::unique_ptr<OrthancStone::IBasicApplication> application;
 static std::unique_ptr<OrthancStone::BasicApplicationContext> context;
 static OrthancStone::StartupParametersBuilder startupParametersBuilder;
+static OrthancStone::MessageBroker broker;
 
 static OrthancStone::ChangeObserver changeObserver_;
 static OrthancStone::StatusBar statusBar_;
 
-
 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_;
 
 std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) {
@@ -66,7 +66,7 @@
 
     printf("CreateWasmApplication\n");
 
-    application.reset(CreateUserApplication());
+    application.reset(CreateUserApplication(broker));
 
     startupParametersBuilder.Clear();
   }
@@ -86,6 +86,7 @@
     application->DeclareStartupOptions(options);
     startupParametersBuilder.GetStartupParameters(parameters, options);
 
+    WasmWebService::SetBroker(broker);
     context.reset(new OrthancStone::BasicApplicationContext(OrthancStone::WasmWebService::GetInstance()));
     application->Initialize(context.get(), statusBar_, parameters);
     application->InitializeWasm();
--- a/Platforms/Wasm/Defaults.h	Tue Jul 03 10:26:56 2018 +0200
+++ b/Platforms/Wasm/Defaults.h	Tue Jul 03 10:48:47 2018 +0200
@@ -24,7 +24,7 @@
 }
 #endif
 
-extern OrthancStone::IBasicApplication* CreateUserApplication();
+extern OrthancStone::IBasicApplication* CreateUserApplication(OrthancStone::MessageBroker& broker);
 
 namespace OrthancStone {
 
--- a/Platforms/Wasm/WasmWebService.cpp	Tue Jul 03 10:26:56 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.cpp	Tue Jul 03 10:48:47 2018 +0200
@@ -27,7 +27,7 @@
     else
     {
       reinterpret_cast<OrthancStone::IWebService::ICallback*>(callback)->
-        NotifyError(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload));
+        OnHttpRequestError(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload));
     }
   }
 
@@ -44,13 +44,13 @@
     else
     {
       reinterpret_cast<OrthancStone::IWebService::ICallback*>(callback)->
-        NotifySuccess(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)); 
+        OnHttpRequestSuccess(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)); 
    }
   }
 
-  void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUrl(const char* baseUrl)
+  void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri)
   {
-    OrthancStone::WasmWebService::GetInstance().SetBaseUrl(baseUrl);
+    OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri);
   }
 
 #ifdef __cplusplus
@@ -61,35 +61,37 @@
 
 namespace OrthancStone
 {
-  void WasmWebService::SetBaseUrl(const std::string base)
+  MessageBroker* WasmWebService::broker_ = NULL;
+
+  void WasmWebService::SetBaseUri(const std::string baseUri)
   {
     // Make sure the base url ends with "/"
-    if (base.empty() ||
-        base[base.size() - 1] != '/')
+    if (baseUri.empty() ||
+        baseUri[baseUri.size() - 1] != '/')
     {
-      base_ = base + "/";
+      baseUri_ = baseUri + "/";
     }
     else
     {
-      base_ = base;
+      baseUri_ = baseUri;
     }
   }
 
   void WasmWebService::ScheduleGetRequest(ICallback& callback,
-                                          const std::string& uri,
+                                          const std::string& relativeUri,
                                           Orthanc::IDynamicObject* payload)
   {
-    std::string url = base_ + uri;
-    WasmWebService_ScheduleGetRequest(&callback, url.c_str(), payload);
+    std::string uri = baseUri_ + relativeUri;
+    WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), payload);
   }
 
   void WasmWebService::SchedulePostRequest(ICallback& callback,
-                                           const std::string& uri,
+                                           const std::string& relativeUri,
                                            const std::string& body,
                                            Orthanc::IDynamicObject* payload)
   {
-    std::string url = base_ + uri;
-    WasmWebService_SchedulePostRequest(&callback, url.c_str(),
+    std::string uri = baseUri_ + relativeUri;
+    WasmWebService_SchedulePostRequest(&callback, uri.c_str(),
                                        body.c_str(), body.size(), payload);
   }
 }
--- a/Platforms/Wasm/WasmWebService.h	Tue Jul 03 10:26:56 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.h	Tue Jul 03 10:48:47 2018 +0200
@@ -1,28 +1,40 @@
 #pragma once
 
 #include <Framework/Toolbox/IWebService.h>
+#include <Core/OrthancException.h>
 
 namespace OrthancStone
 {
   class WasmWebService : public IWebService
   {
   private:
-    std::string  base_;
+    std::string  baseUri_;
+    static MessageBroker* broker_;
 
     // Private constructor => Singleton design pattern
-    WasmWebService() :
-      base_("../../")   // note: this is configurable from the JS code by calling WasmWebService_SetBaseUrl
+    WasmWebService(MessageBroker& broker) :
+      IWebService(broker),
+      baseUri_("../../")   // note: this is configurable from the JS code by calling WasmWebService_SetBaseUri
     {
     }
 
   public:
     static WasmWebService& GetInstance()
     {
-      static WasmWebService instance;
+      if (broker_ == NULL)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+      }
+      static WasmWebService instance(*broker_);
       return instance;
     }
 
-    void SetBaseUrl(const std::string base);
+    static void SetBroker(MessageBroker& broker)
+    {
+      broker_ = &broker;
+    }
+
+    void SetBaseUri(const std::string baseUri);
 
     virtual void ScheduleGetRequest(ICallback& callback,
                                     const std::string& uri,
--- a/Platforms/Wasm/wasm-application.ts	Tue Jul 03 10:26:56 2018 +0200
+++ b/Platforms/Wasm/wasm-application.ts	Tue Jul 03 10:48:47 2018 +0200
@@ -106,7 +106,7 @@
     WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
     NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []);
 
-    StoneFrameworkModule.ccall('WasmWebService_SetBaseUrl', null, ['string'], [orthancBaseUrl]);
+    StoneFrameworkModule.ccall('WasmWebService_SetBaseUri', null, ['string'], [orthancBaseUrl]);
 
     // Prevent scrolling
     document.body.addEventListener('touchmove', function (event) {