Mercurial > hg > orthanc-stone
comparison Deprecated/Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 1401:f6a2d46d2b76
moved CodeGeneration into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:48:18 +0200 |
parents | Resources/CodeGeneration/testWasmIntegrated/main.cpp@f185cfcb72a0 |
children | 828a9b4ee1b7 |
comparison
equal
deleted
inserted
replaced
1400:419d0320c344 | 1401:f6a2d46d2b76 |
---|---|
1 #include <iostream> | |
2 #include <sstream> | |
3 #include <emscripten/emscripten.h> | |
4 #include "TestStoneCodeGen_generated.hpp" | |
5 | |
6 using std::stringstream; | |
7 | |
8 int main() | |
9 { | |
10 std::cout << "Hello world from testWasmIntegrated! (this is sent from C++)" << std::endl; | |
11 try | |
12 { | |
13 const char* jsonData = R"bgo({"definition": | |
14 { | |
15 "val" : [ "berk", 42 ], | |
16 "zozo" : { "23": "zloutch", "lalala": 42} | |
17 } | |
18 })bgo"; | |
19 std::string strValue(jsonData); | |
20 | |
21 Json::Value readValue; | |
22 | |
23 Json::CharReaderBuilder builder; | |
24 Json::CharReader* reader = builder.newCharReader(); | |
25 | |
26 StoneSmartPtr<Json::CharReader> ptr(reader); | |
27 | |
28 std::string errors; | |
29 | |
30 bool ok = reader->parse( | |
31 strValue.c_str(), | |
32 strValue.c_str() + strValue.size(), | |
33 &readValue, | |
34 &errors | |
35 ); | |
36 if (!ok) | |
37 { | |
38 std::stringstream ss; | |
39 ss << "Jsoncpp parsing error: " << errors; | |
40 throw std::runtime_error(ss.str()); | |
41 } | |
42 std::cout << "Json parsing OK" << std::endl; | |
43 std::cout << readValue << std::endl; | |
44 } | |
45 catch(std::exception& e) | |
46 { | |
47 std::cout << "Json parsing THROW" << std::endl; | |
48 std::cout << "e.what() = " << e.what() << std::endl; | |
49 } | |
50 } | |
51 | |
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; \ | |
57 ss << "Received an instance of:\n" #Type "\n. Here's the dump:\n"; \ | |
58 TestStoneCodeGen::StoneDumpValue(ss, value, 0); \ | |
59 SendFreeTextFromCppJS(ss.str().c_str()); \ | |
60 return true; | |
61 | |
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 | |
71 class MyHandler : public TestStoneCodeGen::IHandler | |
72 { | |
73 public: | |
74 virtual bool Handle(const TestStoneCodeGen::A& value) override | |
75 { | |
76 HANDLE_MESSAGE(TestStoneCodeGen::A,value) | |
77 } | |
78 virtual bool Handle(const TestStoneCodeGen::B& value) override | |
79 { | |
80 HANDLE_MESSAGE(TestStoneCodeGen::B,value) | |
81 } | |
82 | |
83 virtual bool Handle(const TestStoneCodeGen::Message1& value) override | |
84 { | |
85 HANDLE_MESSAGE(TestStoneCodeGen::Message1,value) | |
86 } | |
87 | |
88 virtual bool Handle(const TestStoneCodeGen::Message2& value) override | |
89 { | |
90 HANDLE_MESSAGE(TestStoneCodeGen::Message2,value) | |
91 } | |
92 | |
93 virtual bool Handle(const TestStoneCodeGen::C& value) override | |
94 { | |
95 HANDLE_MESSAGE(TestStoneCodeGen::C,value) | |
96 } | |
97 }; | |
98 | |
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 | |
127 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) | |
128 { | |
129 MyHandler handler; | |
130 try | |
131 { | |
132 bool handled = TestStoneCodeGen::StoneDispatchToHandler(message,&handler); | |
133 if(!handled) | |
134 { | |
135 SendFreeTextFromCppJS("This message is valid JSON, but was not handled!"); | |
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 } | |
145 | |
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 | |
165 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) | |
166 { | |
167 printf("Hello! (this is sent from C++)\n"); | |
168 | |
169 // // recreate a command line from uri arguments and parse it | |
170 // boost::program_options::variables_map parameters; | |
171 // boost::program_options::options_description options; | |
172 // application->DeclareStartupOptions(options); | |
173 // startupParametersBuilder.GetStartupParameters(parameters, options); | |
174 | |
175 // context.reset(new OrthancStone::StoneApplicationContext(broker)); | |
176 // context->SetOrthancBaseUrl(baseUri); | |
177 // printf("Base URL to Orthanc API: [%s]\n", baseUri); | |
178 // context->SetWebService(OrthancStone::WasmWebService::GetInstance()); | |
179 // context->SetDelayedCallExecutor(OrthancStone::WasmDelayedCallExecutor::GetInstance()); | |
180 // application->Initialize(context.get(), statusBar_, parameters); | |
181 // application->InitializeWasm(); | |
182 | |
183 // // viewport->SetSize(width_, height_); | |
184 // printf("StartWasmApplication - completed\n"); | |
185 SendFreeTextFromCppJS("Hello world from C++!"); | |
186 } |