view Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 508:7105a0bad250 bgo-commands-codegen

- Added HandleSerializedMessage to IStoneApplication (empty impl) - Split UpdateWebApplication with "WithString" and "WithSerializedMessage" variants - Due to the modules in TS, globals are now unallowed and the callbacks from C++ to JS are stored in the "window" instance - Split UpdateStoneApplicationStatusFromCpp with "WithString" and "WithSerializedMessage" variants - Split NotifyStatusUpdateFromCppToWeb with "WithString" and "WithSerializedMessage" variants - SendMessageToStoneApplication (C++ global) has been split into SendSerializedMessageToStoneApplication and SendCommandToStoneApplication - In WasmPlatformApplicationAdapter: HandleMessageFromWeb becomes HandleCommandFromWeb - In WasmPlatformApplicationAdapter: added HandleSerializedMessageFromWeb - stonegentool now handles the "json" primitive type (used, a.o., in the VSOL "EditInstance" message) - Fixed indentation and added json serialization overloads in the stonegentool templates - Added test of the json primitive type to testWasmIntegrated (in Resources/CodeGeneration) - Adapted testWasmIntegrated (in Resources/CodeGeneration) to the changes above
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 06 Mar 2019 10:14:59 +0100
parents 329f229c2794
children 8c0b073efda8
line wrap: on
line source

#include <iostream>
#include <sstream>
#include <emscripten/emscripten.h>
#include "testWasmIntegratedCpp_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"; \
  testWasmIntegratedCpp::StoneDumpValue(ss, value, 0); \
  SendFreeTextFromCppJS(ss.str().c_str()); \
  return true;

class MyHandler : public testWasmIntegratedCpp::IHandler
{
  public:
    virtual bool Handle(const testWasmIntegratedCpp::A& value) override
    {
      HANDLE_MESSAGE(testWasmIntegratedCpp::A,value)
    }
    virtual bool Handle(const testWasmIntegratedCpp::B& value) override
    {
      HANDLE_MESSAGE(testWasmIntegratedCpp::B,value)
    }

    virtual bool Handle(const testWasmIntegratedCpp::Message1& value) override
    {
      HANDLE_MESSAGE(testWasmIntegratedCpp::Message1,value)
    }

    virtual bool Handle(const testWasmIntegratedCpp::Message2& value) override
    {
      HANDLE_MESSAGE(testWasmIntegratedCpp::Message2,value)
    }

    virtual bool Handle(const testWasmIntegratedCpp::C& value) override
    {
      HANDLE_MESSAGE(testWasmIntegratedCpp::C,value)
    }
};

extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message)
{
    MyHandler handler;
    try
    {
      bool handled = testWasmIntegratedCpp::StoneDispatchToHandler(message,&handler);
      if(!handled)
      {
        SendFreeTextFromCppJS("This message is valid JSON, but was not recognized!");  
      }
    }
    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++!");
}