Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/WasmWebService.cpp @ 303:ed1a4302154f am-callable-and-promise
new messages in wasm too
author | am@osimis.io |
---|---|
date | Tue, 18 Sep 2018 18:17:26 +0200 |
parents | 3897f9f28cfa |
children | 14ef1227120f |
rev | line source |
---|---|
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
1 #include "WasmWebService.h" |
257 | 2 #include "json/value.h" |
3 #include "json/writer.h" | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
4 #include <emscripten/emscripten.h> |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
5 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
6 #ifdef __cplusplus |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
7 extern "C" { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
8 #endif |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
9 |
303 | 10 extern void WasmWebService_GetAsync(void* callableSuccess, |
11 void* callableFailure, | |
12 const char* uri, | |
13 const char* headersInJsonString, | |
14 void* payload); | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
15 |
303 | 16 extern void WasmWebService_PostAsync(void* callableSuccess, |
17 void* callableFailure, | |
18 const char* uri, | |
19 const char* headersInJsonString, | |
20 const void* body, | |
21 size_t bodySize, | |
22 void* payload); | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
23 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
24 |
303 | 25 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* failureCallable, |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
26 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
27 void* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
28 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
29 if (failureCallable == NULL) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
30 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
31 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
32 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
33 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
34 { |
303 | 35 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)-> |
36 Apply(OrthancStone::IWebService::HttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload))); | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
37 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
38 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
39 |
303 | 40 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable, |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
41 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
42 const void* body, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
43 size_t bodySize, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
44 void* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
45 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
46 if (successCallable == NULL) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
47 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
48 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
49 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
50 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
51 { |
303 | 52 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> |
53 Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload))); | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
54 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
55 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
56 |
253 | 57 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri) |
240 | 58 { |
253 | 59 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri); |
240 | 60 } |
61 | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
62 #ifdef __cplusplus |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
63 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
64 #endif |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
65 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
66 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
67 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
68 namespace OrthancStone |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
69 { |
253 | 70 MessageBroker* WasmWebService::broker_ = NULL; |
71 | |
72 void WasmWebService::SetBaseUri(const std::string baseUri) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
73 { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
74 // Make sure the base url ends with "/" |
253 | 75 if (baseUri.empty() || |
76 baseUri[baseUri.size() - 1] != '/') | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
77 { |
253 | 78 baseUri_ = baseUri + "/"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
79 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
80 else |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
81 { |
253 | 82 baseUri_ = baseUri; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
83 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
84 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
85 |
257 | 86 void ToJsonString(std::string& output, const IWebService::Headers& headers) |
87 { | |
88 Json::Value jsonHeaders; | |
89 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ ) | |
90 { | |
91 jsonHeaders[it->first] = it->second; | |
92 } | |
93 | |
94 Json::StreamWriterBuilder builder; | |
95 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); | |
96 std::ostringstream outputStr; | |
97 | |
98 writer->write(jsonHeaders, &outputStr); | |
99 output = outputStr.str(); | |
100 } | |
101 | |
303 | 102 void WasmWebService::PostAsync(const std::string& relativeUri, |
103 const Headers& headers, | |
104 const std::string& body, | |
105 Orthanc::IDynamicObject* payload, | |
106 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
107 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
108 { |
253 | 109 std::string uri = baseUri_ + relativeUri; |
257 | 110 std::string headersInJsonString; |
111 ToJsonString(headersInJsonString, headers); | |
303 | 112 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
113 body.c_str(), body.size(), payload); |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
114 } |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
115 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
116 void WasmWebService::GetAsync(const std::string& relativeUri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
117 const Headers& headers, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
118 Orthanc::IDynamicObject* payload, |
303 | 119 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, |
120 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable) | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
121 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
122 std::string uri = baseUri_ + relativeUri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
123 std::string headersInJsonString; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
124 ToJsonString(headersInJsonString, headers); |
303 | 125 WasmWebService_GetAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), payload); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
126 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
127 |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
128 } |