# HG changeset patch # User am@osimis.io # Date 1530607727 -7200 # Node ID 8ff70c04c6df8d1f80e5266f2f3d7948be88f749 # Parent 40b21c1f8b8dcc0dba0347fa509b1b5d85df4b01 IObservable/IObserver now working in WASM too diff -r 40b21c1f8b8d -r 8ff70c04c6df Applications/Samples/SampleMainWasm.cpp --- 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 diff -r 40b21c1f8b8d -r 8ff70c04c6df Applications/Samples/SimpleViewerApplication.h --- 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()); } } diff -r 40b21c1f8b8d -r 8ff70c04c6df Framework/Messages/MessageBroker.cpp --- 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 #include +#include #include "IObserver.h" #include "MessageType.h" diff -r 40b21c1f8b8d -r 8ff70c04c6df Platforms/Wasm/Defaults.cpp --- 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 application; static std::unique_ptr context; static OrthancStone::StartupParametersBuilder startupParametersBuilder; +static OrthancStone::MessageBroker broker; static OrthancStone::ChangeObserver changeObserver_; static OrthancStone::StatusBar statusBar_; - static std::list> viewports_; std::shared_ptr 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(); diff -r 40b21c1f8b8d -r 8ff70c04c6df Platforms/Wasm/Defaults.h --- 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 { diff -r 40b21c1f8b8d -r 8ff70c04c6df Platforms/Wasm/WasmWebService.cpp --- 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(callback)-> - NotifyError(uri, reinterpret_cast(payload)); + OnHttpRequestError(uri, reinterpret_cast(payload)); } } @@ -44,13 +44,13 @@ else { reinterpret_cast(callback)-> - NotifySuccess(uri, body, bodySize, reinterpret_cast(payload)); + OnHttpRequestSuccess(uri, body, bodySize, reinterpret_cast(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); } } diff -r 40b21c1f8b8d -r 8ff70c04c6df Platforms/Wasm/WasmWebService.h --- 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 +#include 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, diff -r 40b21c1f8b8d -r 8ff70c04c6df Platforms/Wasm/wasm-application.ts --- 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) {