Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/testWasmIntegrated/main.cpp @ 535:79bb0a02d1cc bgo-commands-codegen
- Added ORTHANC_OVERRIDE to several methods (translates to "override" in C++ 11 compilers)
- Last fixes to new style command handling (removed useless commands and switch command handling in simple samples to use XxxxSerializedMessageXxxx instead of XxxxCommandXxxx
- Fixed hardcoded input instance in SingleFrameEditorApplication
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 19 Mar 2019 09:13:57 +0100 |
parents | 7105a0bad250 |
children | 8c0b073efda8 |
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> |
499 | 4 #include "testWasmIntegratedCpp_generated.hpp" |
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"; \ |
499 | 58 testWasmIntegratedCpp::StoneDumpValue(ss, value, 0); \ |
59 SendFreeTextFromCppJS(ss.str().c_str()); \ | |
60 return true; | |
61 | |
62 class MyHandler : public testWasmIntegratedCpp::IHandler | |
63 { | |
64 public: | |
65 virtual bool Handle(const testWasmIntegratedCpp::A& value) override | |
66 { | |
67 HANDLE_MESSAGE(testWasmIntegratedCpp::A,value) | |
68 } | |
69 virtual bool Handle(const testWasmIntegratedCpp::B& value) override | |
70 { | |
71 HANDLE_MESSAGE(testWasmIntegratedCpp::B,value) | |
72 } | |
73 | |
74 virtual bool Handle(const testWasmIntegratedCpp::Message1& value) override | |
75 { | |
76 HANDLE_MESSAGE(testWasmIntegratedCpp::Message1,value) | |
77 } | |
78 | |
79 virtual bool Handle(const testWasmIntegratedCpp::Message2& value) override | |
80 { | |
81 HANDLE_MESSAGE(testWasmIntegratedCpp::Message2,value) | |
82 } | |
83 | |
84 virtual bool Handle(const testWasmIntegratedCpp::C& value) override | |
85 { | |
86 HANDLE_MESSAGE(testWasmIntegratedCpp::C,value) | |
87 } | |
88 }; | |
89 | |
90 extern "C" void EMSCRIPTEN_KEEPALIVE SendMessageToCpp(const char* message) | |
91 { | |
92 MyHandler handler; | |
93 try | |
94 { | |
95 bool handled = testWasmIntegratedCpp::StoneDispatchToHandler(message,&handler); | |
96 if(!handled) | |
97 { | |
98 SendFreeTextFromCppJS("This message is valid JSON, but was not recognized!"); | |
99 } | |
100 } | |
101 catch(std::exception& e) | |
102 { | |
103 stringstream ss; | |
104 ss << "Error while parsing message: " << e.what() << "\n"; | |
105 SendFreeTextFromCppJS(ss.str().c_str()); | |
106 } | |
107 } | |
498 | 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 | 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 | 129 SendFreeTextFromCppJS("Hello world from C++!"); |
130 } |