Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 1029:0f100d0b9caf
merge
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Oct 2019 21:58:30 +0200 |
parents | f185cfcb72a0 |
children |
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 | 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 | 4 #include "TestStoneCodeGen_generated.hpp" |
499 | 5 |
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 | 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 | 52 extern "C" void SendMessageFromCppJS(const char* message); |
53 extern "C" void SendFreeTextFromCppJS(const char* message); | |
54 | |
55 #define HANDLE_MESSAGE(Type,value) \ | |
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 | 58 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ |
499 | 59 SendFreeTextFromCppJS(ss.str().c_str()); \ |
60 return true; | |
61 | |
690 | 62 #define ECHO_MESSAGE(Type,value) \ |
63 stringstream ss; \ | |
64 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ | |
65 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ | |
66 SendFreeTextFromCppJS(ss.str().c_str()); \ | |
67 std::string serializedInCpp = StoneSerialize(value); \ | |
68 SendMessageFromCppJS(serializedInCpp.c_str()); \ | |
69 return true; | |
70 | |
688 | 71 class MyHandler : public TestStoneCodeGen::IHandler |
499 | 72 { |
73 public: | |
688 | 74 virtual bool Handle(const TestStoneCodeGen::A& value) override |
499 | 75 { |
688 | 76 HANDLE_MESSAGE(TestStoneCodeGen::A,value) |
499 | 77 } |
688 | 78 virtual bool Handle(const TestStoneCodeGen::B& value) override |
499 | 79 { |
688 | 80 HANDLE_MESSAGE(TestStoneCodeGen::B,value) |
499 | 81 } |
82 | |
688 | 83 virtual bool Handle(const TestStoneCodeGen::Message1& value) override |
499 | 84 { |
688 | 85 HANDLE_MESSAGE(TestStoneCodeGen::Message1,value) |
499 | 86 } |
87 | |
688 | 88 virtual bool Handle(const TestStoneCodeGen::Message2& value) override |
499 | 89 { |
688 | 90 HANDLE_MESSAGE(TestStoneCodeGen::Message2,value) |
499 | 91 } |
92 | |
688 | 93 virtual bool Handle(const TestStoneCodeGen::C& value) override |
499 | 94 { |
688 | 95 HANDLE_MESSAGE(TestStoneCodeGen::C,value) |
499 | 96 } |
97 }; | |
98 | |
690 | 99 class MyEchoHandler : public TestStoneCodeGen::IHandler |
100 { | |
101 public: | |
102 virtual bool Handle(const TestStoneCodeGen::A& value) override | |
103 { | |
104 ECHO_MESSAGE(TestStoneCodeGen::A,value) | |
105 } | |
106 virtual bool Handle(const TestStoneCodeGen::B& value) override | |
107 { | |
108 ECHO_MESSAGE(TestStoneCodeGen::B,value) | |
109 } | |
110 | |
111 virtual bool Handle(const TestStoneCodeGen::Message1& value) override | |
112 { | |
113 ECHO_MESSAGE(TestStoneCodeGen::Message1,value) | |
114 } | |
115 | |
116 virtual bool Handle(const TestStoneCodeGen::Message2& value) override | |
117 { | |
118 ECHO_MESSAGE(TestStoneCodeGen::Message2,value) | |
119 } | |
120 | |
121 virtual bool Handle(const TestStoneCodeGen::C& value) override | |
122 { | |
123 ECHO_MESSAGE(TestStoneCodeGen::C,value) | |
124 } | |
125 }; | |
126 | |
499 | 127 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) |
128 { | |
129 MyHandler handler; | |
130 try | |
131 { | |
688 | 132 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); |
499 | 133 if(!handled) |
134 { | |
688 | 135 SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); |
499 | 136 } |
137 } | |
138 catch(std::exception& e) | |
139 { | |
140 stringstream ss; | |
141 ss << "Error while parsing message: " << e.what() << "\n"; | |
142 SendFreeTextFromCppJS(ss.str().c_str()); | |
143 } | |
144 } | |
498 | 145 |
690 | 146 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCppForEcho(const char* message) |
147 { | |
148 MyEchoHandler echoHandler; | |
149 try | |
150 { | |
151 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&echoHandler); | |
152 if(!handled) | |
153 { | |
154 SendFreeTextFromCppJS("This message is valid JSON, but was not handled by the echo handler!"); | |
155 } | |
156 } | |
157 catch(std::exception& e) | |
158 { | |
159 stringstream ss; | |
160 ss << "Error while parsing message: " << e.what() << "\n"; | |
161 SendFreeTextFromCppJS(ss.str().c_str()); | |
162 } | |
163 } | |
164 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
165 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
166 { |
498 | 167 printf("Hello! (this is sent from C++)\n"); |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
168 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
169 // // 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
|
170 // boost::program_options::variables_map parameters; |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
171 // boost::program_options::options_description options; |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
172 // application->DeclareStartupOptions(options); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
173 // startupParametersBuilder.GetStartupParameters(parameters, options); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
174 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
175 // context.reset(new OrthancStone::StoneApplicationContext(broker)); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
176 // context->SetOrthancBaseUrl(baseUri); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
177 // printf("Base URL to Orthanc API: [%s]\n", baseUri); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
178 // context->SetWebService(OrthancStone::WasmWebService::GetInstance()); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
179 // context->SetDelayedCallExecutor(OrthancStone::WasmDelayedCallExecutor::GetInstance()); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
180 // application->Initialize(context.get(), statusBar_, parameters); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
181 // application->InitializeWasm(); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
182 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
183 // // viewport->SetSize(width_, height_); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
495
diff
changeset
|
184 // printf("StartWasmApplication - completed\n"); |
498 | 185 SendFreeTextFromCppJS("Hello world from C++!"); |
186 } |