Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/WasmWebService.cpp @ 313:8bdc6112bc2e am-2
initial resize of canvas
author | am@osimis.io |
---|---|
date | Wed, 03 Oct 2018 17:01:05 +0200 |
parents | 14ef1227120f |
children | aad37d0b6407 |
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 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
26 |
303 | 27 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
|
28 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
29 void* payload) |
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 if (failureCallable == NULL) |
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 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
34 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
35 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
36 { |
303 | 37 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)-> |
38 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
|
39 } |
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 |
303 | 42 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
|
43 const char* uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
44 const void* body, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
45 size_t bodySize, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
46 void* payload) |
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 if (successCallable == NULL) |
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 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
51 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
52 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
53 { |
303 | 54 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> |
55 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
|
56 } |
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 |
253 | 59 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri) |
240 | 60 { |
253 | 61 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri); |
240 | 62 } |
63 | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
64 #ifdef __cplusplus |
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 #endif |
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 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
69 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
70 namespace OrthancStone |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
71 { |
253 | 72 MessageBroker* WasmWebService::broker_ = NULL; |
73 | |
74 void WasmWebService::SetBaseUri(const std::string baseUri) | |
221
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 // Make sure the base url ends with "/" |
253 | 77 if (baseUri.empty() || |
78 baseUri[baseUri.size() - 1] != '/') | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
79 { |
253 | 80 baseUri_ = 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 else |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
83 { |
253 | 84 baseUri_ = baseUri; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
85 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
86 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
87 |
257 | 88 void ToJsonString(std::string& output, const IWebService::Headers& headers) |
89 { | |
90 Json::Value jsonHeaders; | |
91 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ ) | |
92 { | |
93 jsonHeaders[it->first] = it->second; | |
94 } | |
95 | |
96 Json::StreamWriterBuilder builder; | |
97 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); | |
98 std::ostringstream outputStr; | |
99 | |
100 writer->write(jsonHeaders, &outputStr); | |
101 output = outputStr.str(); | |
102 } | |
103 | |
303 | 104 void WasmWebService::PostAsync(const std::string& relativeUri, |
309 | 105 const Headers& headers, |
106 const std::string& body, | |
107 Orthanc::IDynamicObject* payload, | |
108 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
109 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
110 unsigned int timeoutInSeconds) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
111 { |
253 | 112 std::string uri = baseUri_ + relativeUri; |
257 | 113 std::string headersInJsonString; |
114 ToJsonString(headersInJsonString, headers); | |
303 | 115 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), |
309 | 116 body.c_str(), body.size(), payload, timeoutInSeconds); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
117 } |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
118 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
119 void WasmWebService::GetAsync(const std::string& relativeUri, |
309 | 120 const Headers& headers, |
121 Orthanc::IDynamicObject* payload, | |
122 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
123 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
124 unsigned int timeoutInSeconds) | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
125 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
126 std::string uri = baseUri_ + relativeUri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
127 std::string headersInJsonString; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
128 ToJsonString(headersInJsonString, headers); |
309 | 129 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
|
130 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
131 |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
132 } |