Mercurial > hg > orthanc-stone
comparison OrthancStone/Resources/Graveyard/Deprecated/Platforms/Wasm/WasmWebService.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Resources/Graveyard/Deprecated/Platforms/Wasm/WasmWebService.cpp@553084468225 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "WasmWebService.h" | |
23 #include "json/value.h" | |
24 #include "json/writer.h" | |
25 #include <emscripten/emscripten.h> | |
26 #include <boost/shared_ptr.hpp> | |
27 | |
28 struct CachedSuccessNotification | |
29 { | |
30 boost::shared_ptr<Deprecated::BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage; | |
31 std::unique_ptr<Orthanc::IDynamicObject> payload; | |
32 OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestSuccessMessage>* successCallback; | |
33 }; | |
34 | |
35 | |
36 #ifdef __cplusplus | |
37 extern "C" { | |
38 #endif | |
39 | |
40 extern void WasmWebService_GetAsync(void* callableSuccess, | |
41 void* callableFailure, | |
42 const char* uri, | |
43 const char* headersInJsonString, | |
44 void* payload, | |
45 unsigned int timeoutInSeconds); | |
46 | |
47 extern void WasmWebService_ScheduleLaterCachedSuccessNotification(void* brol); | |
48 | |
49 extern void WasmWebService_PostAsync(void* callableSuccess, | |
50 void* callableFailure, | |
51 const char* uri, | |
52 const char* headersInJsonString, | |
53 const void* body, | |
54 size_t bodySize, | |
55 void* payload, | |
56 unsigned int timeoutInSeconds); | |
57 | |
58 extern void WasmWebService_DeleteAsync(void* callableSuccess, | |
59 void* callableFailure, | |
60 const char* uri, | |
61 const char* headersInJsonString, | |
62 void* payload, | |
63 unsigned int timeoutInSeconds); | |
64 | |
65 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* failureCallable, | |
66 const char* uri, | |
67 unsigned int httpStatus, | |
68 void* payload) | |
69 { | |
70 if (failureCallable != NULL) | |
71 { | |
72 reinterpret_cast<OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestErrorMessage>*>(failureCallable)-> | |
73 Apply(Deprecated::IWebService::HttpRequestErrorMessage(uri, static_cast<Orthanc::HttpStatus>(httpStatus), reinterpret_cast<Orthanc::IDynamicObject*>(payload))); | |
74 } | |
75 } | |
76 | |
77 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyCachedSuccess(void* notification_) | |
78 { | |
79 // notification has been allocated in C++ and passed to JS. It must be deleted by this method | |
80 std::unique_ptr<CachedSuccessNotification> notification(reinterpret_cast<CachedSuccessNotification*>(notification_)); | |
81 | |
82 notification->successCallback->Apply(Deprecated::IWebService::HttpRequestSuccessMessage( | |
83 notification->cachedMessage->GetUri(), | |
84 notification->cachedMessage->GetAnswer(), | |
85 notification->cachedMessage->GetAnswerSize(), | |
86 notification->cachedMessage->GetAnswerHttpHeaders(), | |
87 notification->payload.get() | |
88 )); | |
89 } | |
90 | |
91 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable, | |
92 const char* uri, | |
93 const void* body, | |
94 size_t bodySize, | |
95 const char* answerHeaders, | |
96 void* payload) | |
97 { | |
98 if (successCallable != NULL) | |
99 { | |
100 Deprecated::IWebService::HttpHeaders headers; | |
101 | |
102 // TODO - Parse "answerHeaders" | |
103 //printf("TODO: parse headers [%s]\n", answerHeaders); | |
104 | |
105 reinterpret_cast<OrthancStone::MessageHandler<Deprecated::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> | |
106 Apply(Deprecated::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers, | |
107 reinterpret_cast<Orthanc::IDynamicObject*>(payload))); | |
108 } | |
109 } | |
110 | |
111 #ifdef __cplusplus | |
112 } | |
113 #endif | |
114 | |
115 | |
116 | |
117 namespace Deprecated | |
118 { | |
119 OrthancStone::MessageBroker* WasmWebService::broker_ = NULL; | |
120 | |
121 void ToJsonString(std::string& output, const IWebService::HttpHeaders& headers) | |
122 { | |
123 Json::Value jsonHeaders; | |
124 for (IWebService::HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); it++ ) | |
125 { | |
126 jsonHeaders[it->first] = it->second; | |
127 } | |
128 | |
129 Json::StreamWriterBuilder builder; | |
130 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); | |
131 std::ostringstream outputStr; | |
132 | |
133 writer->write(jsonHeaders, &outputStr); | |
134 output = outputStr.str(); | |
135 } | |
136 | |
137 void WasmWebService::PostAsync(const std::string& relativeUri, | |
138 const HttpHeaders& headers, | |
139 const std::string& body, | |
140 Orthanc::IDynamicObject* payload, | |
141 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
142 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
143 unsigned int timeoutInSeconds) | |
144 { | |
145 std::string headersInJsonString; | |
146 ToJsonString(headersInJsonString, headers); | |
147 WasmWebService_PostAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(), | |
148 body.c_str(), body.size(), payload, timeoutInSeconds); | |
149 } | |
150 | |
151 void WasmWebService::DeleteAsync(const std::string& relativeUri, | |
152 const HttpHeaders& headers, | |
153 Orthanc::IDynamicObject* payload, | |
154 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable, | |
155 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable, | |
156 unsigned int timeoutInSeconds) | |
157 { | |
158 std::string headersInJsonString; | |
159 ToJsonString(headersInJsonString, headers); | |
160 WasmWebService_DeleteAsync(successCallable, failureCallable, relativeUri.c_str(), headersInJsonString.c_str(), | |
161 payload, timeoutInSeconds); | |
162 } | |
163 | |
164 void WasmWebService::GetAsyncInternal(const std::string &relativeUri, | |
165 const HttpHeaders &headers, | |
166 Orthanc::IDynamicObject *payload, | |
167 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage> *successCallable, | |
168 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage> *failureCallable, | |
169 unsigned int timeoutInSeconds) | |
170 { | |
171 std::string headersInJsonString; | |
172 ToJsonString(headersInJsonString, headers); | |
173 WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(), | |
174 headersInJsonString.c_str(), payload, timeoutInSeconds); | |
175 } | |
176 | |
177 void WasmWebService::NotifyHttpSuccessLater(boost::shared_ptr<BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage, | |
178 Orthanc::IDynamicObject* payload, // takes ownership | |
179 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback) | |
180 { | |
181 CachedSuccessNotification* notification = new CachedSuccessNotification(); // allocated on the heap, it will be passed to JS and deleted when coming back to C++ | |
182 notification->cachedMessage = cachedMessage; | |
183 notification->payload.reset(payload); | |
184 notification->successCallback = successCallback; | |
185 | |
186 WasmWebService_ScheduleLaterCachedSuccessNotification(notification); | |
187 } | |
188 | |
189 } |