annotate Platforms/Wasm/WasmWebService.cpp @ 427:3f9017db1738 am-vsol-upgrade-radiography-export

wip
author am@osimis.io
date Fri, 23 Nov 2018 16:06:23 +0100
parents c23df8b3433b
children 3a8bcc45c221
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
1 #include "WasmWebService.h"
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
2 #include "json/value.h"
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
10 extern void WasmWebService_GetAsync(void* callableSuccess,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
11 void* callableFailure,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
12 const char* uri,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
13 const char* headersInJsonString,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
14 void* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
15 unsigned int timeoutInSeconds);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
16
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
17 extern void WasmWebService_PostAsync(void* callableSuccess,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
18 void* callableFailure,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
19 const char* uri,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
20 const char* headersInJsonString,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
21 const void* body,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
22 size_t bodySize,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
23 void* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
43 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)->
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
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
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
72 #ifdef __cplusplus
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 #endif
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
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
77
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
78 namespace OrthancStone
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
79 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
80 MessageBroker* WasmWebService::broker_ = NULL;
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
81
417
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
82 void ToJsonString(std::string& output, const IWebService::HttpHeaders& headers)
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
83 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
84 Json::Value jsonHeaders;
417
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
85 for (IWebService::HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); it++ )
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
86 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
87 jsonHeaders[it->first] = it->second;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
88 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
89
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
90 Json::StreamWriterBuilder builder;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
91 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
92 std::ostringstream outputStr;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
93
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
94 writer->write(jsonHeaders, &outputStr);
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
95 output = outputStr.str();
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
96 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
97
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
98 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
99 const HttpHeaders& headers,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
100 const std::string& body,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
101 Orthanc::IDynamicObject* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
102 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
103 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
104 unsigned int timeoutInSeconds)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
105 {
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
106 std::string headersInJsonString;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
107 ToJsonString(headersInJsonString, headers);
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 417
diff changeset
108 WasmWebService_PostAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(),
417
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
109 body.c_str(), body.size(), payload, timeoutInSeconds);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
110 }
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
111
315
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
112 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
113 const HttpHeaders& headers,
315
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
114 Orthanc::IDynamicObject* payload,
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
115 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
116 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
117 unsigned int timeoutInSeconds)
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
118 {
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
119 std::string headersInJsonString;
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
120 ToJsonString(headersInJsonString, headers);
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 417
diff changeset
121 WasmWebService_DeleteAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(),
315
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
122 payload, timeoutInSeconds);
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
123 }
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
124
aad37d0b6407 Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents: 309
diff changeset
125 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
126 const HttpHeaders& headers,
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
127 Orthanc::IDynamicObject* payload,
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
128 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
129 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
aee3d7941c9b preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 315
diff changeset
130 unsigned int timeoutInSeconds)
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
131 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
132 std::string headersInJsonString;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
133 ToJsonString(headersInJsonString, headers);
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 417
diff changeset
134 WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(),
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 417
diff changeset
135 headersInJsonString.c_str(), payload, timeoutInSeconds);
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
136 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
137 }