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
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,
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
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
60 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
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
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
65 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri)
240
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
66 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
67 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri);
240
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
68 }
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
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
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
78 MessageBroker* WasmWebService::broker_ = NULL;
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
79
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
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
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
83 if (baseUri.empty() ||
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
84 baseUri[baseUri.size() - 1] != '/')
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
85 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
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
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
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
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
94 void ToJsonString(std::string& output, const IWebService::Headers& headers)
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
95 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
96 Json::Value jsonHeaders;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
97 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ )
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
98 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
99 jsonHeaders[it->first] = it->second;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
100 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
101
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
102 Json::StreamWriterBuilder builder;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
103 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
104 std::ostringstream outputStr;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
105
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
106 writer->write(jsonHeaders, &outputStr);
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
107 output = outputStr.str();
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
108 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
109
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
110 void WasmWebService::PostAsync(const std::string& relativeUri,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
111 const Headers& headers,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
112 const std::string& body,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
113 Orthanc::IDynamicObject* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
114 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
115 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
116 unsigned int timeoutInSeconds)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
117 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
118 std::string uri = baseUri_ + relativeUri;
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
119 std::string headersInJsonString;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
120 ToJsonString(headersInJsonString, headers);
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
121 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(),
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
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
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
140 const Headers& headers,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
141 Orthanc::IDynamicObject* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
142 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
143 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
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
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
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 }