comparison Deprecated/Platforms/Wasm/WasmWebService.cpp @ 1400:419d0320c344

moved Platforms into Deprecated
author Alain Mazy <alain@mazy.be>
date Wed, 29 Apr 2020 20:45:14 +0200
parents Platforms/Wasm/WasmWebService.cpp@8a0a62189f46
children 828a9b4ee1b7
comparison
equal deleted inserted replaced
1399:ff8d2e46ac63 1400:419d0320c344
1 #include "WasmWebService.h"
2 #include "json/value.h"
3 #include "json/writer.h"
4 #include <emscripten/emscripten.h>
5 #include <boost/shared_ptr.hpp>
6
7 struct CachedSuccessNotification
8 {
9 boost::shared_ptr<Deprecated::BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage;
10 std::unique_ptr<Orthanc::IDynamicObject> payload;
11 OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestSuccessMessage>* successCallback;
12 };
13
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 extern void WasmWebService_GetAsync(void* callableSuccess,
20 void* callableFailure,
21 const char* uri,
22 const char* headersInJsonString,
23 void* payload,
24 unsigned int timeoutInSeconds);
25
26 extern void WasmWebService_ScheduleLaterCachedSuccessNotification(void* brol);
27
28 extern void WasmWebService_PostAsync(void* callableSuccess,
29 void* callableFailure,
30 const char* uri,
31 const char* headersInJsonString,
32 const void* body,
33 size_t bodySize,
34 void* payload,
35 unsigned int timeoutInSeconds);
36
37 extern void WasmWebService_DeleteAsync(void* callableSuccess,
38 void* callableFailure,
39 const char* uri,
40 const char* headersInJsonString,
41 void* payload,
42 unsigned int timeoutInSeconds);
43
44 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* failureCallable,
45 const char* uri,
46 unsigned int httpStatus,
47 void* payload)
48 {
49 if (failureCallable != NULL)
50 {
51 reinterpret_cast<OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestErrorMessage>*>(failureCallable)->
52 Apply(Deprecated::IWebService::HttpRequestErrorMessage(uri, static_cast<Orthanc::HttpStatus>(httpStatus), reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
53 }
54 }
55
56 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyCachedSuccess(void* notification_)
57 {
58 // notification has been allocated in C++ and passed to JS. It must be deleted by this method
59 std::unique_ptr<CachedSuccessNotification> notification(reinterpret_cast<CachedSuccessNotification*>(notification_));
60
61 notification->successCallback->Apply(Deprecated::IWebService::HttpRequestSuccessMessage(
62 notification->cachedMessage->GetUri(),
63 notification->cachedMessage->GetAnswer(),
64 notification->cachedMessage->GetAnswerSize(),
65 notification->cachedMessage->GetAnswerHttpHeaders(),
66 notification->payload.get()
67 ));
68 }
69
70 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable,
71 const char* uri,
72 const void* body,
73 size_t bodySize,
74 const char* answerHeaders,
75 void* payload)
76 {
77 if (successCallable != NULL)
78 {
79 Deprecated::IWebService::HttpHeaders headers;
80
81 // TODO - Parse "answerHeaders"
82 //printf("TODO: parse headers [%s]\n", answerHeaders);
83
84 reinterpret_cast<OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
85 Apply(Deprecated::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers,
86 reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
87 }
88 }
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94
95
96 namespace Deprecated
97 {
98 OrthancStone::MessageBroker* WasmWebService::broker_ = NULL;
99
100 void ToJsonString(std::string& output, const IWebService::HttpHeaders& headers)
101 {
102 Json::Value jsonHeaders;
103 for (IWebService::HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); it++ )
104 {
105 jsonHeaders[it->first] = it->second;
106 }
107
108 Json::StreamWriterBuilder builder;
109 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
110 std::ostringstream outputStr;
111
112 writer->write(jsonHeaders, &outputStr);
113 output = outputStr.str();
114 }
115
116 void WasmWebService::PostAsync(const std::string& relativeUri,
117 const HttpHeaders& headers,
118 const std::string& body,
119 Orthanc::IDynamicObject* payload,
120 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
121 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
122 unsigned int timeoutInSeconds)
123 {
124 std::string headersInJsonString;
125 ToJsonString(headersInJsonString, headers);
126 WasmWebService_PostAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(),
127 body.c_str(), body.size(), payload, timeoutInSeconds);
128 }
129
130 void WasmWebService::DeleteAsync(const std::string& relativeUri,
131 const HttpHeaders& headers,
132 Orthanc::IDynamicObject* payload,
133 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
134 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
135 unsigned int timeoutInSeconds)
136 {
137 std::string headersInJsonString;
138 ToJsonString(headersInJsonString, headers);
139 WasmWebService_DeleteAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(),
140 payload, timeoutInSeconds);
141 }
142
143 void WasmWebService::GetAsyncInternal(const std::string &relativeUri,
144 const HttpHeaders &headers,
145 Orthanc::IDynamicObject *payload,
146 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage> *successCallable,
147 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> *failureCallable,
148 unsigned int timeoutInSeconds)
149 {
150 std::string headersInJsonString;
151 ToJsonString(headersInJsonString, headers);
152 WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(),
153 headersInJsonString.c_str(), payload, timeoutInSeconds);
154 }
155
156 void WasmWebService::NotifyHttpSuccessLater(boost::shared_ptr<BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage,
157 Orthanc::IDynamicObject* payload, // takes ownership
158 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback)
159 {
160 CachedSuccessNotification* notification = new CachedSuccessNotification(); // allocated on the heap, it will be passed to JS and deleted when coming back to C++
161 notification->cachedMessage = cachedMessage;
162 notification->payload.reset(payload);
163 notification->successCallback = successCallback;
164
165 WasmWebService_ScheduleLaterCachedSuccessNotification(notification);
166 }
167
168 }