comparison Platforms/Wasm/WasmWebService.cpp @ 435:e641d3978856 am-vsol-upgrade

WasmWebService now using BaseWebService and supporting cache
author am@osimis.io
date Tue, 04 Dec 2018 11:52:43 +0100
parents 3a8bcc45c221
children 56ddca73396c
comparison
equal deleted inserted replaced
434:3a8bcc45c221 435:e641d3978856
1 #include "WasmWebService.h" 1 #include "WasmWebService.h"
2 #include "json/value.h" 2 #include "json/value.h"
3 #include "json/writer.h" 3 #include "json/writer.h"
4 #include <emscripten/emscripten.h> 4 #include <emscripten/emscripten.h>
5 #include <boost/shared_ptr.hpp>
6
7 struct CachedSuccessNotification
8 {
9 boost::shared_ptr<OrthancStone::BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage;
10 std::auto_ptr<Orthanc::IDynamicObject> payload;
11 OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>* successCallback;
12 };
13
5 14
6 #ifdef __cplusplus 15 #ifdef __cplusplus
7 extern "C" { 16 extern "C" {
8 #endif 17 #endif
9 18
11 void* callableFailure, 20 void* callableFailure,
12 const char* uri, 21 const char* uri,
13 const char* headersInJsonString, 22 const char* headersInJsonString,
14 void* payload, 23 void* payload,
15 unsigned int timeoutInSeconds); 24 unsigned int timeoutInSeconds);
25
26 extern void WasmWebService_ScheduleLaterCachedSuccessNotification(void* brol);
16 27
17 extern void WasmWebService_PostAsync(void* callableSuccess, 28 extern void WasmWebService_PostAsync(void* callableSuccess,
18 void* callableFailure, 29 void* callableFailure,
19 const char* uri, 30 const char* uri,
20 const char* headersInJsonString, 31 const char* headersInJsonString,
43 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)-> 54 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)->
44 Apply(OrthancStone::IWebService::HttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload))); 55 Apply(OrthancStone::IWebService::HttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
45 } 56 }
46 } 57 }
47 58
59 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyCachedSuccess(void* notification_)
60 {
61 // notification has been allocated in C++ and passed to JS. It must be deleted by this method
62 std::auto_ptr<CachedSuccessNotification> notification(reinterpret_cast<CachedSuccessNotification*>(notification_));
63
64 notification->successCallback->Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(
65 notification->cachedMessage->GetUri(),
66 notification->cachedMessage->GetAnswer(),
67 notification->cachedMessage->GetAnswerSize(),
68 notification->cachedMessage->GetAnswerHttpHeaders(),
69 notification->payload.get()
70 ));
71 }
72
48 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable, 73 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable,
49 const char* uri, 74 const char* uri,
50 const void* body, 75 const void* body,
51 size_t bodySize, 76 size_t bodySize,
52 const char* answerHeaders, 77 const char* answerHeaders,
59 else 84 else
60 { 85 {
61 OrthancStone::IWebService::HttpHeaders headers; 86 OrthancStone::IWebService::HttpHeaders headers;
62 87
63 // TODO - Parse "answerHeaders" 88 // TODO - Parse "answerHeaders"
64 printf("[%s]\n", answerHeaders); 89 printf("TODO: parse headers [%s]\n", answerHeaders);
65 90
66 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)-> 91 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
67 Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers, 92 Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, headers,
68 reinterpret_cast<Orthanc::IDynamicObject*>(payload))); 93 reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
69 } 94 }
132 std::string headersInJsonString; 157 std::string headersInJsonString;
133 ToJsonString(headersInJsonString, headers); 158 ToJsonString(headersInJsonString, headers);
134 WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(), 159 WasmWebService_GetAsync(successCallable, failureCallable, relativeUri.c_str(),
135 headersInJsonString.c_str(), payload, timeoutInSeconds); 160 headersInJsonString.c_str(), payload, timeoutInSeconds);
136 } 161 }
162
163 void WasmWebService::NotifyHttpSuccessLater(boost::shared_ptr<BaseWebService::CachedHttpRequestSuccessMessage> cachedMessage,
164 Orthanc::IDynamicObject* payload, // takes ownership
165 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback)
166 {
167 CachedSuccessNotification* notification = new CachedSuccessNotification(); // allocated on the heap, it will be passed to JS and deleted when coming back to C++
168 notification->cachedMessage = cachedMessage;
169 notification->payload.reset(payload);
170 notification->successCallback = successCallback;
171
172 WasmWebService_ScheduleLaterCachedSuccessNotification(notification);
173 }
174
137 } 175 }