annotate Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 688:8c0b073efda8 am-dev

Codegen: fix
author Alain Mazy <alain@mazy.be>
date Thu, 16 May 2019 17:54:22 +0200
parents 7105a0bad250
children f185cfcb72a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
495
6405435480ae Fixed template to add dump capabilities + started work on an integrated TS/WASM test
bgo-osimis
parents:
diff changeset
1 #include <iostream>
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
2 #include <sstream>
496
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
3 #include <emscripten/emscripten.h>
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
4 #include "TestStoneCodeGen_generated.hpp"
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
5
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
6 using std::stringstream;
495
6405435480ae Fixed template to add dump capabilities + started work on an integrated TS/WASM test
bgo-osimis
parents:
diff changeset
7
6405435480ae Fixed template to add dump capabilities + started work on an integrated TS/WASM test
bgo-osimis
parents:
diff changeset
8 int main()
6405435480ae Fixed template to add dump capabilities + started work on an integrated TS/WASM test
bgo-osimis
parents:
diff changeset
9 {
498
6d62fc8a6988 Web demonstrator for codegen ongoing work
bgo-osimis
parents: 496
diff changeset
10 std::cout << "Hello world from testWasmIntegrated! (this is sent from C++)" << std::endl;
508
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
11 try
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
12 {
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
13 const char* jsonData = R"bgo({"definition":
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
14 {
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
15 "val" : [ "berk", 42 ],
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
16 "zozo" : { "23": "zloutch", "lalala": 42}
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
17 }
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
18 })bgo";
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
19 std::string strValue(jsonData);
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
20
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
21 Json::Value readValue;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
22
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
23 Json::CharReaderBuilder builder;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
24 Json::CharReader* reader = builder.newCharReader();
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
25
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
26 StoneSmartPtr<Json::CharReader> ptr(reader);
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
27
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
28 std::string errors;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
29
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
30 bool ok = reader->parse(
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
31 strValue.c_str(),
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
32 strValue.c_str() + strValue.size(),
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
33 &readValue,
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
34 &errors
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
35 );
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
36 if (!ok)
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
37 {
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
38 std::stringstream ss;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
39 ss << "Jsoncpp parsing error: " << errors;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
40 throw std::runtime_error(ss.str());
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
41 }
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
42 std::cout << "Json parsing OK" << std::endl;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
43 std::cout << readValue << std::endl;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
44 }
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
45 catch(std::exception& e)
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
46 {
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
47 std::cout << "Json parsing THROW" << std::endl;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
48 std::cout << "e.what() = " << e.what() << std::endl;
7105a0bad250 - Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents: 500
diff changeset
49 }
496
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
50 }
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
51
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
52 extern "C" void SendMessageFromCppJS(const char* message);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
53 extern "C" void SendFreeTextFromCppJS(const char* message);
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
54
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
55 #define HANDLE_MESSAGE(Type,value) \
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
56 stringstream ss; \
500
329f229c2794 Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents: 499
diff changeset
57 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
58 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
59 SendFreeTextFromCppJS(ss.str().c_str()); \
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
60 return true;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
61
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
62 class MyHandler : public TestStoneCodeGen::IHandler
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
63 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
64 public:
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
65 virtual bool Handle(const TestStoneCodeGen::A& value) override
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
66 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
67 HANDLE_MESSAGE(TestStoneCodeGen::A,value)
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
68 }
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
69 virtual bool Handle(const TestStoneCodeGen::B& value) override
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
70 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
71 HANDLE_MESSAGE(TestStoneCodeGen::B,value)
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
72 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
73
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
74 virtual bool Handle(const TestStoneCodeGen::Message1& value) override
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
75 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
76 HANDLE_MESSAGE(TestStoneCodeGen::Message1,value)
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
77 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
78
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
79 virtual bool Handle(const TestStoneCodeGen::Message2& value) override
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
80 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
81 HANDLE_MESSAGE(TestStoneCodeGen::Message2,value)
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
82 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
83
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
84 virtual bool Handle(const TestStoneCodeGen::C& value) override
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
85 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
86 HANDLE_MESSAGE(TestStoneCodeGen::C,value)
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
87 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
88 };
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
89
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
90 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
91 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
92 MyHandler handler;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
93 try
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
94 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
95 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler);
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
96 if(!handled)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
97 {
688
8c0b073efda8 Codegen: fix
Alain Mazy <alain@mazy.be>
parents: 508
diff changeset
98 SendFreeTextFromCppJS("This message is valid JSON, but was not handled!");
499
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
99 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
100 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
101 catch(std::exception& e)
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
102 {
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
103 stringstream ss;
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
104 ss << "Error while parsing message: " << e.what() << "\n";
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
105 SendFreeTextFromCppJS(ss.str().c_str());
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
106 }
baa9e1e932db wasm + ts demonstrator WORKS!
bgo-osimis
parents: 498
diff changeset
107 }
498
6d62fc8a6988 Web demonstrator for codegen ongoing work
bgo-osimis
parents: 496
diff changeset
108
496
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
109 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri)
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
110 {
498
6d62fc8a6988 Web demonstrator for codegen ongoing work
bgo-osimis
parents: 496
diff changeset
111 printf("Hello! (this is sent from C++)\n");
496
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
112
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
113 // // recreate a command line from uri arguments and parse it
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
114 // boost::program_options::variables_map parameters;
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
115 // boost::program_options::options_description options;
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
116 // application->DeclareStartupOptions(options);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
117 // startupParametersBuilder.GetStartupParameters(parameters, options);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
118
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
119 // context.reset(new OrthancStone::StoneApplicationContext(broker));
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
120 // context->SetOrthancBaseUrl(baseUri);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
121 // printf("Base URL to Orthanc API: [%s]\n", baseUri);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
122 // context->SetWebService(OrthancStone::WasmWebService::GetInstance());
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
123 // context->SetDelayedCallExecutor(OrthancStone::WasmDelayedCallExecutor::GetInstance());
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
124 // application->Initialize(context.get(), statusBar_, parameters);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
125 // application->InitializeWasm();
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
126
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
127 // // viewport->SetSize(width_, height_);
8b6ceae45ba0 Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents: 495
diff changeset
128 // printf("StartWasmApplication - completed\n");
498
6d62fc8a6988 Web demonstrator for codegen ongoing work
bgo-osimis
parents: 496
diff changeset
129 SendFreeTextFromCppJS("Hello world from C++!");
6d62fc8a6988 Web demonstrator for codegen ongoing work
bgo-osimis
parents: 496
diff changeset
130 }