Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/WasmWebService.cpp @ 417:aee3d7941c9b
preparing to load images using DICOMweb
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 15 Nov 2018 17:28:15 +0100 |
parents | aad37d0b6407 |
children | c23df8b3433b |
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, |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
52 const char* answerHeaders, |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
53 void* payload) |
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 if (successCallable == NULL) |
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 throw; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
58 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
59 else |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
60 { |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
61 OrthancStone::IWebService::HttpHeaders headers; |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
62 |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
63 // TODO - Parse "answerHeaders" |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
64 printf("[%s]\n", answerHeaders); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
65 |
303 | 66 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
67 Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
68 reinterpret_cast<Orthanc::IDynamicObject*>(payload))); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
69 } |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
70 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
71 |
253 | 72 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri) |
240 | 73 { |
253 | 74 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri); |
240 | 75 } |
76 | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
77 #ifdef __cplusplus |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
78 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
79 #endif |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
80 |
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 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
83 namespace OrthancStone |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
84 { |
253 | 85 MessageBroker* WasmWebService::broker_ = NULL; |
86 | |
87 void WasmWebService::SetBaseUri(const std::string baseUri) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
88 { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
89 // Make sure the base url ends with "/" |
253 | 90 if (baseUri.empty() || |
91 baseUri[baseUri.size() - 1] != '/') | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
92 { |
253 | 93 baseUri_ = baseUri + "/"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
94 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
95 else |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
96 { |
253 | 97 baseUri_ = baseUri; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
98 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
99 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
100 |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
101 void ToJsonString(std::string& output, const IWebService::HttpHeaders& headers) |
257 | 102 { |
103 Json::Value jsonHeaders; | |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
104 for (IWebService::HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); it++ ) |
257 | 105 { |
106 jsonHeaders[it->first] = it->second; | |
107 } | |
108 | |
109 Json::StreamWriterBuilder builder; | |
110 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); | |
111 std::ostringstream outputStr; | |
112 | |
113 writer->write(jsonHeaders, &outputStr); | |
114 output = outputStr.str(); | |
115 } | |
116 | |
303 | 117 void WasmWebService::PostAsync(const std::string& relativeUri, |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
118 const HttpHeaders& headers, |
309 | 119 const std::string& body, |
120 Orthanc::IDynamicObject* payload, | |
121 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
122 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
123 unsigned int timeoutInSeconds) | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
124 { |
253 | 125 std::string uri = baseUri_ + relativeUri; |
257 | 126 std::string headersInJsonString; |
127 ToJsonString(headersInJsonString, headers); | |
303 | 128 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
129 body.c_str(), body.size(), payload, timeoutInSeconds); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
130 } |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
131 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
132 void WasmWebService::DeleteAsync(const std::string& relativeUri, |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
133 const HttpHeaders& headers, |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
134 Orthanc::IDynamicObject* payload, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
135 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
136 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
137 unsigned int timeoutInSeconds) |
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 std::string uri = baseUri_ + relativeUri; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
140 std::string headersInJsonString; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
141 ToJsonString(headersInJsonString, headers); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
142 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
|
143 payload, timeoutInSeconds); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
144 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
145 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
146 void WasmWebService::GetAsync(const std::string& relativeUri, |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
147 const HttpHeaders& headers, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
148 Orthanc::IDynamicObject* payload, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
149 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
150 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
151 unsigned int timeoutInSeconds) |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
152 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
153 std::string uri = baseUri_ + relativeUri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
154 std::string headersInJsonString; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
155 ToJsonString(headersInJsonString, headers); |
309 | 156 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
|
157 } |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
158 } |