comparison Framework/Oracle/WebAssemblyOracle.cpp @ 1229:b9f2a111c5b9 broker

fix compilation of WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Dec 2019 14:41:37 +0100
parents 008dbc4ceb62
children b9b5d4378874
comparison
equal deleted inserted replaced
1228:c471a0aa137b 1229:b9f2a111c5b9
40 { 40 {
41 class WebAssemblyOracle::TimeoutContext 41 class WebAssemblyOracle::TimeoutContext
42 { 42 {
43 private: 43 private:
44 WebAssemblyOracle& oracle_; 44 WebAssemblyOracle& oracle_;
45 const IObserver& receiver_; 45 boost::weak_ptr<IObserver> receiver_;
46 std::auto_ptr<SleepOracleCommand> command_; 46 std::auto_ptr<SleepOracleCommand> command_;
47 47
48 public: 48 public:
49 TimeoutContext(WebAssemblyOracle& oracle, 49 TimeoutContext(WebAssemblyOracle& oracle,
50 const IObserver& receiver, 50 boost::weak_ptr<IObserver> receiver,
51 IOracleCommand* command) : 51 IOracleCommand* command) :
52 oracle_(oracle), 52 oracle_(oracle),
53 receiver_(receiver) 53 receiver_(receiver)
54 { 54 {
55 if (command == NULL) 55 if (command == NULL)
61 command_.reset(dynamic_cast<SleepOracleCommand*>(command)); 61 command_.reset(dynamic_cast<SleepOracleCommand*>(command));
62 } 62 }
63 } 63 }
64 64
65 void EmitMessage() 65 void EmitMessage()
66 { 66 {
67 assert(command_.get() != NULL);
68
67 SleepOracleCommand::TimeoutMessage message(*command_); 69 SleepOracleCommand::TimeoutMessage message(*command_);
68 oracle_.EmitMessage(receiver_, message); 70 oracle_.EmitMessage(receiver_, message);
69 } 71 }
70 72
71 static void Callback(void *userData) 73 static void Callback(void *userData)
73 std::auto_ptr<TimeoutContext> context(reinterpret_cast<TimeoutContext*>(userData)); 75 std::auto_ptr<TimeoutContext> context(reinterpret_cast<TimeoutContext*>(userData));
74 context->EmitMessage(); 76 context->EmitMessage();
75 } 77 }
76 }; 78 };
77 79
78
79 class WebAssemblyOracle::Emitter : public IMessageEmitter
80 {
81 private:
82 WebAssemblyOracle& oracle_;
83
84 public:
85 Emitter(WebAssemblyOracle& oracle) :
86 oracle_(oracle)
87 {
88 }
89
90 virtual void EmitMessage(const IObserver& receiver,
91 const IMessage& message)
92 {
93 LOG(TRACE) << "WebAssemblyOracle::Emitter::EmitMessage receiver = "
94 << std::hex << &receiver << std::dec;
95 oracle_.EmitMessage(receiver, message);
96 }
97 };
98 80
99 /** 81 /**
100 This object is created on the heap for every http request. 82 This object is created on the heap for every http request.
101 It is deleted in the success (or error) callbacks. 83 It is deleted in the success (or error) callbacks.
102 84
106 same address (it often happens in the [single-threaded] browser context). 88 same address (it often happens in the [single-threaded] browser context).
107 */ 89 */
108 class WebAssemblyOracle::FetchContext : public boost::noncopyable 90 class WebAssemblyOracle::FetchContext : public boost::noncopyable
109 { 91 {
110 private: 92 private:
111 Emitter emitter_; 93 WebAssemblyOracle& oracle_;
112 const IObserver& receiver_; 94 boost::weak_ptr<IObserver> receiver_;
113 std::auto_ptr<IOracleCommand> command_; 95 std::auto_ptr<IOracleCommand> command_;
114 std::string expectedContentType_; 96 std::string expectedContentType_;
115 std::string receiverFingerprint_;
116 97
117 public: 98 public:
118 FetchContext(WebAssemblyOracle& oracle, 99 FetchContext(WebAssemblyOracle& oracle,
119 const IObserver& receiver, 100 boost::weak_ptr<IObserver> receiver,
120 IOracleCommand* command, 101 IOracleCommand* command,
121 const std::string& expectedContentType) : 102 const std::string& expectedContentType) :
122 emitter_(oracle), 103 oracle_(oracle),
123 receiver_(receiver), 104 receiver_(receiver),
124 command_(command), 105 command_(command),
125 expectedContentType_(expectedContentType), 106 expectedContentType_(expectedContentType)
126 receiverFingerprint_(receiver.GetFingerprint())
127 { 107 {
128 LOG(TRACE) << "WebAssemblyOracle::FetchContext::FetchContext() | " 108 LOG(TRACE) << "WebAssemblyOracle::FetchContext::FetchContext() | "
129 << "receiver address = " << std::hex << &receiver << std::dec 109 << "receiver address = " << std::hex << &receiver;
130 << " with fingerprint = " << receiverFingerprint_;
131 110
132 if (command == NULL) 111 if (command == NULL)
133 { 112 {
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 113 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
135 } 114 }
136 } 115 }
137 116
138 const std::string& GetExpectedContentType() const 117 const std::string& GetExpectedContentType() const
139 { 118 {
140 return expectedContentType_; 119 return expectedContentType_;
120 }
121
122 IMessageEmitter& GetEmitter() const
123 {
124 return oracle_;
125 }
126
127 boost::weak_ptr<IObserver> GetReceiver() const
128 {
129 return receiver_;
141 } 130 }
142 131
143 void EmitMessage(const IMessage& message) 132 void EmitMessage(const IMessage& message)
144 { 133 {
145 LOG(TRACE) << "WebAssemblyOracle::FetchContext::EmitMessage receiver_ = " 134 LOG(TRACE) << "WebAssemblyOracle::FetchContext::EmitMessage receiver_ = "
146 << std::hex << &receiver_ << std::dec; 135 << std::hex << &receiver_ << std::dec;
147 emitter_.EmitMessage(receiver_, message); 136 oracle_.EmitMessage(receiver_, message);
148 }
149
150 IMessageEmitter& GetEmitter()
151 {
152 return emitter_;
153 }
154
155 const IObserver& GetReceiver() const
156 {
157 return receiver_;
158 } 137 }
159 138
160 IOracleCommand& GetCommand() const 139 IOracleCommand& GetCommand() const
161 { 140 {
162 return *command_; 141 return *command_;
211 /** 190 /**
212 * Firstly, make a local copy of the fetched information, and 191 * Firstly, make a local copy of the fetched information, and
213 * free data associated with the fetch. 192 * free data associated with the fetch.
214 **/ 193 **/
215 194
195 if (fetch->userData == NULL)
196 {
197 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback fetch->userData is NULL!!!!!!!";
198 return;
199 }
200
216 std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData)); 201 std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData));
217
218 // an UUID is 36 chars : 32 hex chars + 4 hyphens: char #0 --> char #35
219 // char #36 is \0.
220 bool callHandler = true;
221
222 // TODO: remove this line because we are NOT allowed to call methods on GetReceiver that is maybe a dangling ref
223 if (context->GetReceiver().DoesFingerprintLookGood())
224 {
225 callHandler = true;
226 std::string currentFingerprint(context->GetReceiver().GetFingerprint());
227
228 LOG(TRACE) << "SuccessCallback for object at address (" << std::hex
229 << &(context->GetReceiver()) << std::dec
230 << " with current fingerprint = " << currentFingerprint
231 << ". Fingerprint looks OK";
232
233 if (currentFingerprint != context->receiverFingerprint_)
234 {
235 LOG(TRACE) << " ** SuccessCallback: BUT currentFingerprint != "
236 << "receiverFingerprint_(" << context->receiverFingerprint_ << ")";
237 callHandler = false;
238 }
239 else
240 {
241 LOG(TRACE) << " ** SuccessCallback: FetchContext-level "
242 << "fingerprints are the same: "
243 << context->receiverFingerprint_
244 << " ---> oracle will dispatch the response to observer: "
245 << std::hex << &(context->GetReceiver()) << std::dec;
246 }
247 }
248 else {
249 LOG(TRACE) << "SuccessCallback for object at address (" << std::hex << &(context->GetReceiver()) << std::dec << " with current fingerprint is XXXXX -- NOT A VALID FINGERPRINT! OBJECT IS READ ! CALLBACK WILL NOT BE CALLED!";
250 callHandler = false;
251 }
252
253 if (fetch->userData == NULL)
254 {
255 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback fetch->userData is NULL!!!!!!!";
256 }
257 202
258 std::string answer; 203 std::string answer;
259 if (fetch->numBytes > 0) 204 if (fetch->numBytes > 0)
260 { 205 {
261 answer.assign(fetch->data, fetch->numBytes); 206 answer.assign(fetch->data, fetch->numBytes);
301 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback: (context.get() == NULL)"; 246 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback: (context.get() == NULL)";
302 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 247 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
303 } 248 }
304 else 249 else
305 { 250 {
306 if (callHandler) 251 switch (context->GetCommand().GetType())
307 { 252 {
308 switch (context->GetCommand().GetType()) 253 case IOracleCommand::Type_Http:
309 { 254 {
310 case IOracleCommand::Type_Http: 255 HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer);
311 { 256 context->EmitMessage(message);
312 HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer); 257 break;
313 context->EmitMessage(message);
314 break;
315 }
316
317 case IOracleCommand::Type_OrthancRestApi:
318 {
319 LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
320 OrthancRestApiCommand::SuccessMessage message
321 (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
322 context->EmitMessage(message);
323 break;
324 }
325
326 case IOracleCommand::Type_GetOrthancImage:
327 {
328 context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
329 (context->GetEmitter(), context->GetReceiver(), answer, headers);
330 break;
331 }
332
333 case IOracleCommand::Type_GetOrthancWebViewerJpeg:
334 {
335 context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
336 (context->GetEmitter(), context->GetReceiver(), answer);
337 break;
338 }
339
340 default:
341 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
342 << context->GetCommand().GetType();
343 } 258 }
259
260 case IOracleCommand::Type_OrthancRestApi:
261 {
262 LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
263 OrthancRestApiCommand::SuccessMessage message
264 (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
265 context->EmitMessage(message);
266 break;
267 }
268
269 case IOracleCommand::Type_GetOrthancImage:
270 {
271 context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
272 (context->GetReceiver(), context->GetEmitter(), answer, headers);
273 break;
274 }
275
276 case IOracleCommand::Type_GetOrthancWebViewerJpeg:
277 {
278 context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
279 (context->GetReceiver(), context->GetEmitter(), answer);
280 break;
281 }
282
283 default:
284 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
285 << context->GetCommand().GetType();
344 } 286 }
345 } 287 }
346 } 288 }
347 catch (Orthanc::OrthancException& e) 289 catch (Orthanc::OrthancException& e)
348 { 290 {
386 328
387 class WebAssemblyOracle::FetchCommand : public boost::noncopyable 329 class WebAssemblyOracle::FetchCommand : public boost::noncopyable
388 { 330 {
389 private: 331 private:
390 WebAssemblyOracle& oracle_; 332 WebAssemblyOracle& oracle_;
391 const IObserver& receiver_; 333 boost::weak_ptr<IObserver> receiver_;
392 std::auto_ptr<IOracleCommand> command_; 334 std::auto_ptr<IOracleCommand> command_;
393 Orthanc::HttpMethod method_; 335 Orthanc::HttpMethod method_;
394 std::string url_; 336 std::string url_;
395 std::string body_; 337 std::string body_;
396 HttpHeaders headers_; 338 HttpHeaders headers_;
397 unsigned int timeout_; 339 unsigned int timeout_;
398 std::string expectedContentType_; 340 std::string expectedContentType_;
399 341
400 public: 342 public:
401 FetchCommand(WebAssemblyOracle& oracle, 343 FetchCommand(WebAssemblyOracle& oracle,
402 const IObserver& receiver, 344 boost::weak_ptr<IObserver> receiver,
403 IOracleCommand* command) : 345 IOracleCommand* command) :
404 oracle_(oracle), 346 oracle_(oracle),
405 receiver_(receiver), 347 receiver_(receiver),
406 command_(command), 348 command_(command),
407 method_(Orthanc::HttpMethod_Get), 349 method_(Orthanc::HttpMethod_Get),
566 } 508 }
567 } 509 }
568 #endif 510 #endif
569 511
570 512
571 void WebAssemblyOracle::Execute(const IObserver& receiver, 513 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
572 HttpCommand* command) 514 HttpCommand* command)
573 { 515 {
574 FetchCommand fetch(*this, receiver, command); 516 FetchCommand fetch(*this, receiver, command);
575 517
576 fetch.SetMethod(command->GetMethod()); 518 fetch.SetMethod(command->GetMethod());
588 530
589 fetch.Execute(); 531 fetch.Execute();
590 } 532 }
591 533
592 534
593 void WebAssemblyOracle::Execute(const IObserver& receiver, 535 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
594 OrthancRestApiCommand* command) 536 OrthancRestApiCommand* command)
595 { 537 {
596 #if 0 538 #if 0
597 DumpCommand(command); 539 DumpCommand(command);
598 540
651 throw; 593 throw;
652 } 594 }
653 } 595 }
654 596
655 597
656 void WebAssemblyOracle::Execute(const IObserver& receiver, 598 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
657 GetOrthancImageCommand* command) 599 GetOrthancImageCommand* command)
658 { 600 {
659 #if 0 601 #if 0
660 if (logbgo233) { 602 if (logbgo233) {
661 if (logbgo115) 603 if (logbgo115)
672 614
673 fetch.Execute(); 615 fetch.Execute();
674 } 616 }
675 617
676 618
677 void WebAssemblyOracle::Execute(const IObserver& receiver, 619 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
678 GetOrthancWebViewerJpegCommand* command) 620 GetOrthancWebViewerJpegCommand* command)
679 { 621 {
680 #if 0 622 #if 0
681 if (logbgo233) { 623 if (logbgo233) {
682 if (logbgo115) 624 if (logbgo115)
693 fetch.Execute(); 635 fetch.Execute();
694 } 636 }
695 637
696 638
697 639
698 void WebAssemblyOracle::Schedule(boost::shared_ptr<IObserver>& receiver, 640 bool WebAssemblyOracle::Schedule(boost::shared_ptr<IObserver> receiver,
699 IOracleCommand* command) 641 IOracleCommand* command)
700 { 642 {
701 LOG(TRACE) << "WebAssemblyOracle::Schedule : receiver = " 643 LOG(TRACE) << "WebAssemblyOracle::Schedule : receiver = "
702 << std::hex << &receiver << std::dec 644 << std::hex << &receiver;
703 << " | Current fingerprint is " << receiver.GetFingerprint();
704 645
705 std::auto_ptr<IOracleCommand> protection(command); 646 std::auto_ptr<IOracleCommand> protection(command);
706 647
707 if (command == NULL) 648 if (command == NULL)
708 { 649 {
757 break; 698 break;
758 } 699 }
759 700
760 default: 701 default:
761 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " << command->GetType(); 702 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " << command->GetType();
762 } 703 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
704 }
705
706 return true;
763 } 707 }
764 } 708 }