Mercurial > hg > orthanc-stone
comparison Framework/Oracle/WebAssemblyOracle.cpp @ 992:a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 11 Sep 2019 12:31:49 +0200 |
parents | 262a0244e9b2 |
children | 9893fa8cd7a6 |
comparison
equal
deleted
inserted
replaced
990:3ea70a62ca00 | 992:a9f5d0742e22 |
---|---|
304 { | 304 { |
305 if (callHandler) | 305 if (callHandler) |
306 { | 306 { |
307 switch (context->GetCommand().GetType()) | 307 switch (context->GetCommand().GetType()) |
308 { | 308 { |
309 case IOracleCommand::Type_OrthancRestApi: | 309 case IOracleCommand::Type_Http: |
310 { | 310 { |
311 LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);"; | 311 HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer); |
312 OrthancRestApiCommand::SuccessMessage message | 312 context->EmitMessage(message); |
313 (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer); | 313 break; |
314 context->EmitMessage(message); | 314 } |
315 break; | 315 |
316 } | 316 case IOracleCommand::Type_OrthancRestApi: |
317 | 317 { |
318 case IOracleCommand::Type_GetOrthancImage: | 318 LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);"; |
319 { | 319 OrthancRestApiCommand::SuccessMessage message |
320 context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer | 320 (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer); |
321 (context->GetEmitter(), context->GetReceiver(), answer, headers); | 321 context->EmitMessage(message); |
322 break; | 322 break; |
323 } | 323 } |
324 | 324 |
325 case IOracleCommand::Type_GetOrthancWebViewerJpeg: | 325 case IOracleCommand::Type_GetOrthancImage: |
326 { | 326 { |
327 context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer | 327 context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer |
328 (context->GetEmitter(), context->GetReceiver(), answer); | 328 (context->GetEmitter(), context->GetReceiver(), answer, headers); |
329 break; | 329 break; |
330 } | 330 } |
331 | 331 |
332 default: | 332 case IOracleCommand::Type_GetOrthancWebViewerJpeg: |
333 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " | 333 { |
334 << context->GetCommand().GetType(); | 334 context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer |
335 (context->GetEmitter(), context->GetReceiver(), answer); | |
336 break; | |
337 } | |
338 | |
339 default: | |
340 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " | |
341 << context->GetCommand().GetType(); | |
335 } | 342 } |
336 } | 343 } |
337 } | 344 } |
338 } | 345 } |
339 catch (Orthanc::OrthancException& e) | 346 catch (Orthanc::OrthancException& e) |
373 private: | 380 private: |
374 WebAssemblyOracle& oracle_; | 381 WebAssemblyOracle& oracle_; |
375 const IObserver& receiver_; | 382 const IObserver& receiver_; |
376 std::auto_ptr<IOracleCommand> command_; | 383 std::auto_ptr<IOracleCommand> command_; |
377 Orthanc::HttpMethod method_; | 384 Orthanc::HttpMethod method_; |
378 std::string uri_; | 385 std::string url_; |
379 std::string body_; | 386 std::string body_; |
380 HttpHeaders headers_; | 387 HttpHeaders headers_; |
381 unsigned int timeout_; | 388 unsigned int timeout_; |
382 std::string expectedContentType_; | 389 std::string expectedContentType_; |
383 | 390 |
400 void SetMethod(Orthanc::HttpMethod method) | 407 void SetMethod(Orthanc::HttpMethod method) |
401 { | 408 { |
402 method_ = method; | 409 method_ = method; |
403 } | 410 } |
404 | 411 |
405 void SetUri(const std::string& uri) | 412 void SetOrthancUri(const std::string& uri) |
406 { | 413 { |
407 uri_ = oracle_.orthancRoot_ + uri; | 414 url_ = oracle_.orthancRoot_ + uri; |
415 } | |
416 | |
417 void SetUrl(const std::string& url) | |
418 { | |
419 url_ = url; | |
408 } | 420 } |
409 | 421 |
410 void SetBody(std::string& body /* will be swapped */) | 422 void SetBody(std::string& body /* will be swapped */) |
411 { | 423 { |
412 body_.swap(body); | 424 body_.swap(body); |
512 } | 524 } |
513 attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType); | 525 attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType); |
514 | 526 |
515 // Must be the last call to prevent memory leak on error | 527 // Must be the last call to prevent memory leak on error |
516 #if 0 | 528 #if 0 |
517 LOG(TRACE) << "Performing " << method << " request on URI: \"" << uri_ << "\""; | 529 LOG(TRACE) << "Performing " << method << " request on URI: \"" << url_ << "\""; |
518 #endif | 530 #endif |
519 emscripten_fetch(&attr, uri_.c_str()); | 531 emscripten_fetch(&attr, url_.c_str()); |
520 } | 532 } |
521 catch(...) | 533 catch(...) |
522 { | 534 { |
523 if(requestData != NULL) | 535 if(requestData != NULL) |
524 free(requestData); | 536 free(requestData); |
544 break; | 556 break; |
545 } | 557 } |
546 } | 558 } |
547 #endif | 559 #endif |
548 | 560 |
561 | |
562 void WebAssemblyOracle::Execute(const IObserver& receiver, | |
563 HttpCommand* command) | |
564 { | |
565 FetchCommand fetch(*this, receiver, command); | |
566 | |
567 fetch.SetMethod(command->GetMethod()); | |
568 fetch.SetUrl(command->GetUrl()); | |
569 fetch.SetHttpHeaders(command->GetHttpHeaders()); | |
570 fetch.SetTimeout(command->GetTimeout()); | |
571 | |
572 if (command->GetMethod() == Orthanc::HttpMethod_Post || | |
573 command->GetMethod() == Orthanc::HttpMethod_Put) | |
574 { | |
575 std::string body; | |
576 command->SwapBody(body); | |
577 fetch.SetBody(body); | |
578 } | |
579 | |
580 fetch.Execute(); | |
581 } | |
582 | |
583 | |
549 void WebAssemblyOracle::Execute(const IObserver& receiver, | 584 void WebAssemblyOracle::Execute(const IObserver& receiver, |
550 OrthancRestApiCommand* command) | 585 OrthancRestApiCommand* command) |
551 { | 586 { |
552 #if 0 | 587 #if 0 |
553 DumpCommand(command); | 588 DumpCommand(command); |
564 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; | 599 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; |
565 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; | 600 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; |
566 FetchCommand fetch(*this, receiver, command); | 601 FetchCommand fetch(*this, receiver, command); |
567 | 602 |
568 fetch.SetMethod(command->GetMethod()); | 603 fetch.SetMethod(command->GetMethod()); |
569 fetch.SetUri(command->GetUri()); | 604 fetch.SetOrthancUri(command->GetUri()); |
570 fetch.SetHttpHeaders(command->GetHttpHeaders()); | 605 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
571 fetch.SetTimeout(command->GetTimeout()); | 606 fetch.SetTimeout(command->GetTimeout()); |
572 | 607 |
573 if (command->GetMethod() == Orthanc::HttpMethod_Post || | 608 if (command->GetMethod() == Orthanc::HttpMethod_Post || |
574 command->GetMethod() == Orthanc::HttpMethod_Put) | 609 command->GetMethod() == Orthanc::HttpMethod_Put) |
620 } | 655 } |
621 #endif | 656 #endif |
622 | 657 |
623 FetchCommand fetch(*this, receiver, command); | 658 FetchCommand fetch(*this, receiver, command); |
624 | 659 |
625 fetch.SetUri(command->GetUri()); | 660 fetch.SetOrthancUri(command->GetUri()); |
626 fetch.SetHttpHeaders(command->GetHttpHeaders()); | 661 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
627 fetch.SetTimeout(command->GetTimeout()); | 662 fetch.SetTimeout(command->GetTimeout()); |
628 | 663 |
629 fetch.Execute(); | 664 fetch.Execute(); |
630 } | 665 } |
640 } | 675 } |
641 #endif | 676 #endif |
642 | 677 |
643 FetchCommand fetch(*this, receiver, command); | 678 FetchCommand fetch(*this, receiver, command); |
644 | 679 |
645 fetch.SetUri(command->GetUri()); | 680 fetch.SetOrthancUri(command->GetUri()); |
646 fetch.SetHttpHeaders(command->GetHttpHeaders()); | 681 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
647 fetch.SetTimeout(command->GetTimeout()); | 682 fetch.SetTimeout(command->GetTimeout()); |
648 | 683 |
649 fetch.Execute(); | 684 fetch.Execute(); |
650 } | 685 } |
665 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | 700 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
666 } | 701 } |
667 | 702 |
668 switch (command->GetType()) | 703 switch (command->GetType()) |
669 { | 704 { |
705 case IOracleCommand::Type_Http: | |
706 Execute(receiver, dynamic_cast<HttpCommand*>(protection.release())); | |
707 break; | |
708 | |
670 case IOracleCommand::Type_OrthancRestApi: | 709 case IOracleCommand::Type_OrthancRestApi: |
671 //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE | 710 //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE |
672 //{ | 711 //{ |
673 // const IObserver* pReceiver = &receiver; | 712 // const IObserver* pReceiver = &receiver; |
674 // LOG(TRACE) << "WebAssemblyOracle::Schedule | pReceiver is " << pReceiver; | 713 // LOG(TRACE) << "WebAssemblyOracle::Schedule | pReceiver is " << pReceiver; |