Mercurial > hg > orthanc-stone
view Deprecated/Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 1492:4b5ec9ea268e
Building WebAssembly samples using Docker
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Jun 2020 09:13:56 +0200 |
parents | 828a9b4ee1b7 |
children |
line wrap: on
line source
/** * Stone of Orthanc * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics * Department, University Hospital of Liege, Belgium * Copyright (C) 2017-2020 Osimis S.A., Belgium * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. **/ #include <iostream> #include <sstream> #include <emscripten/emscripten.h> #include "TestStoneCodeGen_generated.hpp" using std::stringstream; int main() { std::cout << "Hello world from testWasmIntegrated! (this is sent from C++)" << std::endl; try { const char* jsonData = R"bgo({"definition": { "val" : [ "berk", 42 ], "zozo" : { "23": "zloutch", "lalala": 42} } })bgo"; std::string strValue(jsonData); Json::Value readValue; Json::CharReaderBuilder builder; Json::CharReader* reader = builder.newCharReader(); StoneSmartPtr<Json::CharReader> ptr(reader); std::string errors; bool ok = reader->parse( strValue.c_str(), strValue.c_str() + strValue.size(), &readValue, &errors ); if (!ok) { std::stringstream ss; ss << "Jsoncpp parsing error: " << errors; throw std::runtime_error(ss.str()); } std::cout << "Json parsing OK" << std::endl; std::cout << readValue << std::endl; } catch(std::exception& e) { std::cout << "Json parsing THROW" << std::endl; std::cout << "e.what() = " << e.what() << std::endl; } } extern "C" void SendMessageFromCppJS(const char* message); extern "C" void SendFreeTextFromCppJS(const char* message); #define HANDLE_MESSAGE(Type,value) \ stringstream ss; \ ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ SendFreeTextFromCppJS(ss.str().c_str()); \ return true; #define ECHO_MESSAGE(Type,value) \ stringstream ss; \ ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ SendFreeTextFromCppJS(ss.str().c_str()); \ std::string serializedInCpp = StoneSerialize(value); \ SendMessageFromCppJS(serializedInCpp.c_str()); \ return true; class MyHandler : public TestStoneCodeGen::IHandler { public: virtual bool Handle(const TestStoneCodeGen::A& value) override { HANDLE_MESSAGE(TestStoneCodeGen::A,value) } virtual bool Handle(const TestStoneCodeGen::B& value) override { HANDLE_MESSAGE(TestStoneCodeGen::B,value) } virtual bool Handle(const TestStoneCodeGen::Message1& value) override { HANDLE_MESSAGE(TestStoneCodeGen::Message1,value) } virtual bool Handle(const TestStoneCodeGen::Message2& value) override { HANDLE_MESSAGE(TestStoneCodeGen::Message2,value) } virtual bool Handle(const TestStoneCodeGen::C& value) override { HANDLE_MESSAGE(TestStoneCodeGen::C,value) } }; class MyEchoHandler : public TestStoneCodeGen::IHandler { public: virtual bool Handle(const TestStoneCodeGen::A& value) override { ECHO_MESSAGE(TestStoneCodeGen::A,value) } virtual bool Handle(const TestStoneCodeGen::B& value) override { ECHO_MESSAGE(TestStoneCodeGen::B,value) } virtual bool Handle(const TestStoneCodeGen::Message1& value) override { ECHO_MESSAGE(TestStoneCodeGen::Message1,value) } virtual bool Handle(const TestStoneCodeGen::Message2& value) override { ECHO_MESSAGE(TestStoneCodeGen::Message2,value) } virtual bool Handle(const TestStoneCodeGen::C& value) override { ECHO_MESSAGE(TestStoneCodeGen::C,value) } }; extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) { MyHandler handler; try { bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); if(!handled) { SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); } } catch(std::exception& e) { stringstream ss; ss << "Error while parsing message: " << e.what() << "\n"; SendFreeTextFromCppJS(ss.str().c_str()); } } extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) { MyEchoHandler echoHandler; try { bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); if(!handled) { SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); } } catch(std::exception& e) { stringstream ss; ss << "Error while parsing message: " << e.what() << "\n"; SendFreeTextFromCppJS(ss.str().c_str()); } } void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) { printf("Hello! (this is sent from C++)\n"); // // recreate a command line from uri arguments and parse it // boost::program_options::variables_map parameters; // boost::program_options::options_description options; // application->DeclareStartupOptions(options); // startupParametersBuilder.GetStartupParameters(parameters, options); // context.reset(new OrthancStone::StoneApplicationContext(broker)); // context->SetOrthancBaseUrl(baseUri); // printf("Base URL to Orthanc API: [%s]\n", baseUri); // context->SetWebService(OrthancStone::WasmWebService::GetInstance()); // context->SetDelayedCallExecutor(OrthancStone::WasmDelayedCallExecutor::GetInstance()); // application->Initialize(context.get(), statusBar_, parameters); // application->InitializeWasm(); // // viewport->SetSize(width_, height_); // printf("StartWasmApplication - completed\n"); SendFreeTextFromCppJS("Hello world from C++!"); }