Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/WasmWebService.cpp @ 339:5a7915b23138 am-2
cont
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 19 Oct 2018 14:44:12 +0200 |
parents | aad37d0b6407 |
children | aee3d7941c9b |
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, | |
309 | 14 void* payload, |
15 unsigned int timeoutInSeconds); | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
16 |
303 | 17 extern void WasmWebService_PostAsync(void* callableSuccess, |
18 void* callableFailure, | |
19 const char* uri, | |
20 const char* headersInJsonString, | |
21 const void* body, | |
22 size_t bodySize, | |
309 | 23 void* payload, |
24 unsigned int timeoutInSeconds); | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
25 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
26 extern void WasmWebService_DeleteAsync(void* callableSuccess, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
27 void* callableFailure, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
28 const char* uri, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
29 const char* headersInJsonString, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
30 void* payload, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
31 unsigned int timeoutInSeconds); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
32 |
303 | 33 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
|
34 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
35 void* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
36 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
37 if (failureCallable == NULL) |
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 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
40 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
41 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
42 { |
303 | 43 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)-> |
44 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
|
45 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
46 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
47 |
303 | 48 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
|
49 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
50 const void* body, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
51 size_t bodySize, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
52 void* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
53 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
54 if (successCallable == NULL) |
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 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
57 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
58 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
59 { |
303 | 60 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> |
61 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
|
62 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
63 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
64 |
253 | 65 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri) |
240 | 66 { |
253 | 67 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri); |
240 | 68 } |
69 | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
70 #ifdef __cplusplus |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
71 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
72 #endif |
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 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
75 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
76 namespace OrthancStone |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
77 { |
253 | 78 MessageBroker* WasmWebService::broker_ = NULL; |
79 | |
80 void WasmWebService::SetBaseUri(const std::string baseUri) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
81 { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
82 // Make sure the base url ends with "/" |
253 | 83 if (baseUri.empty() || |
84 baseUri[baseUri.size() - 1] != '/') | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
85 { |
253 | 86 baseUri_ = baseUri + "/"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
87 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
88 else |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
89 { |
253 | 90 baseUri_ = baseUri; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
91 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
92 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
93 |
257 | 94 void ToJsonString(std::string& output, const IWebService::Headers& headers) |
95 { | |
96 Json::Value jsonHeaders; | |
97 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ ) | |
98 { | |
99 jsonHeaders[it->first] = it->second; | |
100 } | |
101 | |
102 Json::StreamWriterBuilder builder; | |
103 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); | |
104 std::ostringstream outputStr; | |
105 | |
106 writer->write(jsonHeaders, &outputStr); | |
107 output = outputStr.str(); | |
108 } | |
109 | |
303 | 110 void WasmWebService::PostAsync(const std::string& relativeUri, |
309 | 111 const Headers& headers, |
112 const std::string& body, | |
113 Orthanc::IDynamicObject* payload, | |
114 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
115 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
116 unsigned int timeoutInSeconds) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
117 { |
253 | 118 std::string uri = baseUri_ + relativeUri; |
257 | 119 std::string headersInJsonString; |
120 ToJsonString(headersInJsonString, headers); | |
303 | 121 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), |
309 | 122 body.c_str(), body.size(), payload, timeoutInSeconds); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
123 } |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
124 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
125 void WasmWebService::DeleteAsync(const std::string& relativeUri, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
126 const Headers& headers, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
127 Orthanc::IDynamicObject* payload, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
128 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
129 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
130 unsigned int timeoutInSeconds) |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
131 { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
132 std::string uri = baseUri_ + relativeUri; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
133 std::string headersInJsonString; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
134 ToJsonString(headersInJsonString, headers); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
135 WasmWebService_DeleteAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
136 payload, timeoutInSeconds); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
137 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
138 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
139 void WasmWebService::GetAsync(const std::string& relativeUri, |
309 | 140 const Headers& headers, |
141 Orthanc::IDynamicObject* payload, | |
142 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
143 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
144 unsigned int timeoutInSeconds) | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
145 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
146 std::string uri = baseUri_ + relativeUri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
147 std::string headersInJsonString; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
148 ToJsonString(headersInJsonString, headers); |
309 | 149 WasmWebService_GetAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), payload, timeoutInSeconds); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
150 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
151 |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
152 } |