comparison UnitTestsSources/UnitTestsMain.cpp @ 72:c1cc3bdba18c wasm

cleaning up OrthancSlicesLoader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 24 May 2017 10:36:41 +0200
parents 30c768873d47
children ffa6dded91bd
comparison
equal deleted inserted replaced
71:30c768873d47 72:c1cc3bdba18c
392 } 392 }
393 }; 393 };
394 394
395 395
396 396
397 class OrthancSliceLoader : 397 class OrthancSlicesLoader : public boost::noncopyable
398 public IWebService::ICallback // TODO move to PImpl
399 { 398 {
400 public: 399 public:
401 class ICallback : public boost::noncopyable 400 class ICallback : public boost::noncopyable
402 { 401 {
403 public: 402 public:
404 virtual ~ICallback() 403 virtual ~ICallback()
405 { 404 {
406 } 405 }
407 406
408 virtual void NotifyGeometryReady(OrthancSliceLoader& loader) = 0; 407 virtual void NotifyGeometryReady(const OrthancSlicesLoader& loader) = 0;
409 408
410 virtual void NotifyGeometryError(OrthancSliceLoader& loader) = 0; 409 virtual void NotifyGeometryError(const OrthancSlicesLoader& loader) = 0;
411 410
412 virtual void NotifySliceImageReady(OrthancSliceLoader& loader, 411 virtual void NotifySliceImageReady(const OrthancSlicesLoader& loader,
413 unsigned int sliceIndex, 412 unsigned int sliceIndex,
414 const Slice& slice, 413 const Slice& slice,
415 Orthanc::ImageAccessor* image) = 0; 414 Orthanc::ImageAccessor* image) = 0;
416 415
417 virtual void NotifySliceImageError(OrthancSliceLoader& loader, 416 virtual void NotifySliceImageError(const OrthancSlicesLoader& loader,
418 unsigned int sliceIndex, 417 unsigned int sliceIndex,
419 const Slice& slice) = 0; 418 const Slice& slice) = 0;
420 }; 419 };
421 420
422 private: 421 private:
501 tmp->slice_ = &slice; 500 tmp->slice_ = &slice;
502 return tmp.release(); 501 return tmp.release();
503 } 502 }
504 }; 503 };
505 504
506 ICallback& callback_; 505
506 class WebCallback : public IWebService::ICallback
507 {
508 private:
509 OrthancSlicesLoader& that_;
510
511 public:
512 WebCallback(OrthancSlicesLoader& that) :
513 that_(that)
514 {
515 }
516
517 virtual void NotifySuccess(const std::string& uri,
518 const void* answer,
519 size_t answerSize,
520 Orthanc::IDynamicObject* payload)
521 {
522 std::auto_ptr<Operation> operation(dynamic_cast<Operation*>(payload));
523
524 switch (operation->GetMode())
525 {
526 case Mode_SeriesGeometry:
527 that_.ParseSeriesGeometry(answer, answerSize);
528 break;
529
530 case Mode_InstanceGeometry:
531 that_.ParseInstanceGeometry(operation->GetInstanceId(),
532 operation->GetFrame(), answer, answerSize);
533 break;
534
535 case Mode_LoadImage:
536 that_.ParseSliceImage(*operation, answer, answerSize);
537 break;
538
539 default:
540 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
541 }
542 }
543
544 virtual void NotifyError(const std::string& uri,
545 Orthanc::IDynamicObject* payload)
546 {
547 std::auto_ptr<Operation> operation(dynamic_cast<Operation*>(payload));
548 LOG(ERROR) << "Cannot download " << uri;
549
550 switch (operation->GetMode())
551 {
552 case Mode_SeriesGeometry:
553 that_.userCallback_.NotifyGeometryError(that_);
554 that_.state_ = State_Error;
555 break;
556
557 case Mode_LoadImage:
558 that_.userCallback_.NotifySliceImageError(that_, operation->GetSliceIndex(),
559 operation->GetSlice());
560 break;
561
562 default:
563 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
564 }
565 }
566 };
567
568
569 WebCallback webCallback_;
570 ICallback& userCallback_;
507 IWebService& orthanc_; 571 IWebService& orthanc_;
508 State state_; 572 State state_;
509 SliceSorter slices_; 573 SliceSorter slices_;
510 574
511 575
514 { 578 {
515 Json::Value series; 579 Json::Value series;
516 if (!MessagingToolbox::ParseJson(series, answer, size) || 580 if (!MessagingToolbox::ParseJson(series, answer, size) ||
517 series.type() != Json::objectValue) 581 series.type() != Json::objectValue)
518 { 582 {
519 callback_.NotifyGeometryError(*this); 583 userCallback_.NotifyGeometryError(*this);
520 return; 584 return;
521 } 585 }
522 586
523 Json::Value::Members instances = series.getMemberNames(); 587 Json::Value::Members instances = series.getMemberNames();
524 588
556 state_ = State_GeometryReady; 620 state_ = State_GeometryReady;
557 621
558 if (ok) 622 if (ok)
559 { 623 {
560 LOG(INFO) << "Loaded a series with " << slices_.GetSliceCount() << " slice(s)"; 624 LOG(INFO) << "Loaded a series with " << slices_.GetSliceCount() << " slice(s)";
561 callback_.NotifyGeometryReady(*this); 625 userCallback_.NotifyGeometryReady(*this);
562 } 626 }
563 else 627 else
564 { 628 {
565 LOG(ERROR) << "This series is empty"; 629 LOG(ERROR) << "This series is empty";
566 callback_.NotifyGeometryError(*this); 630 userCallback_.NotifyGeometryError(*this);
567 } 631 }
568 } 632 }
569 633
570 634
571 void ParseInstanceGeometry(const std::string& instanceId, 635 void ParseInstanceGeometry(const std::string& instanceId,
575 { 639 {
576 Json::Value tags; 640 Json::Value tags;
577 if (!MessagingToolbox::ParseJson(tags, answer, size) || 641 if (!MessagingToolbox::ParseJson(tags, answer, size) ||
578 tags.type() != Json::objectValue) 642 tags.type() != Json::objectValue)
579 { 643 {
580 callback_.NotifyGeometryError(*this); 644 userCallback_.NotifyGeometryError(*this);
581 return; 645 return;
582 } 646 }
583 647
584 OrthancPlugins::FullOrthancDataset dataset(tags); 648 OrthancPlugins::FullOrthancDataset dataset(tags);
585 649
588 Slice slice; 652 Slice slice;
589 if (slice.ParseOrthancFrame(dataset, instanceId, frame)) 653 if (slice.ParseOrthancFrame(dataset, instanceId, frame))
590 { 654 {
591 LOG(INFO) << "Loaded instance " << instanceId; 655 LOG(INFO) << "Loaded instance " << instanceId;
592 slices_.AddSlice(slice); 656 slices_.AddSlice(slice);
593 callback_.NotifyGeometryReady(*this); 657 userCallback_.NotifyGeometryReady(*this);
594 } 658 }
595 else 659 else
596 { 660 {
597 LOG(WARNING) << "Skipping invalid instance " << instanceId; 661 LOG(WARNING) << "Skipping invalid instance " << instanceId;
598 callback_.NotifyGeometryError(*this); 662 userCallback_.NotifyGeometryError(*this);
599 } 663 }
600 } 664 }
601 665
602 666
603 void ParseSliceImage(const Operation& operation, 667 void ParseSliceImage(const Operation& operation,
624 } 688 }
625 } 689 }
626 690
627 if (ok) 691 if (ok)
628 { 692 {
629 callback_.NotifySliceImageReady(*this, operation.GetSliceIndex(), 693 userCallback_.NotifySliceImageReady(*this, operation.GetSliceIndex(),
630 operation.GetSlice(), image.release()); 694 operation.GetSlice(), image.release());
631 } 695 }
632 else 696 else
633 { 697 {
634 callback_.NotifySliceImageError(*this, operation.GetSliceIndex(), 698 userCallback_.NotifySliceImageError(*this, operation.GetSliceIndex(),
635 operation.GetSlice()); 699 operation.GetSlice());
636 } 700 }
637 } 701 }
638 702
639 703
640 public: 704 public:
641 OrthancSliceLoader(ICallback& callback, 705 OrthancSlicesLoader(ICallback& callback,
642 IWebService& orthanc) : 706 IWebService& orthanc) :
643 callback_(callback), 707 webCallback_(*this),
708 userCallback_(callback),
644 orthanc_(orthanc), 709 orthanc_(orthanc),
645 state_(State_Initialization) 710 state_(State_Initialization)
646 { 711 {
647 } 712 }
648 713
654 } 719 }
655 else 720 else
656 { 721 {
657 state_ = State_LoadingGeometry; 722 state_ = State_LoadingGeometry;
658 std::string uri = "/series/" + seriesId + "/instances-tags"; 723 std::string uri = "/series/" + seriesId + "/instances-tags";
659 orthanc_.ScheduleGetRequest(*this, uri, Operation::DownloadSeriesGeometry()); 724 orthanc_.ScheduleGetRequest(webCallback_, uri, Operation::DownloadSeriesGeometry());
660 } 725 }
661 } 726 }
662 727
663 void ScheduleLoadInstance(const std::string& instanceId, 728 void ScheduleLoadInstance(const std::string& instanceId,
664 unsigned int frame) 729 unsigned int frame)
670 else 735 else
671 { 736 {
672 state_ = State_LoadingGeometry; 737 state_ = State_LoadingGeometry;
673 std::string uri = "/instances/" + instanceId + "/tags"; 738 std::string uri = "/instances/" + instanceId + "/tags";
674 orthanc_.ScheduleGetRequest 739 orthanc_.ScheduleGetRequest
675 (*this, uri, Operation::DownloadInstanceGeometry(instanceId, frame)); 740 (webCallback_, uri, Operation::DownloadInstanceGeometry(instanceId, frame));
676 } 741 }
677 } 742 }
678 743
679 size_t GetSliceCount() const 744 size_t GetSliceCount() const
680 { 745 {
725 790
726 default: 791 default:
727 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); 792 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
728 } 793 }
729 794
730 orthanc_.ScheduleGetRequest(*this, uri, Operation::DownloadSliceImage(index, slice)); 795 orthanc_.ScheduleGetRequest(webCallback_, uri, Operation::DownloadSliceImage(index, slice));
731 }
732 }
733
734 virtual void NotifySuccess(const std::string& uri,
735 const void* answer,
736 size_t answerSize,
737 Orthanc::IDynamicObject* payload)
738 {
739 std::auto_ptr<Operation> operation(dynamic_cast<Operation*>(payload));
740
741 switch (operation->GetMode())
742 {
743 case Mode_SeriesGeometry:
744 ParseSeriesGeometry(answer, answerSize);
745 break;
746
747 case Mode_InstanceGeometry:
748 ParseInstanceGeometry(operation->GetInstanceId(), operation->GetFrame(), answer, answerSize);
749 break;
750
751 case Mode_LoadImage:
752 ParseSliceImage(*operation, answer, answerSize);
753 break;
754
755 default:
756 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
757 }
758 }
759
760 virtual void NotifyError(const std::string& uri,
761 Orthanc::IDynamicObject* payload)
762 {
763 std::auto_ptr<Operation> operation(dynamic_cast<Operation*>(payload));
764 LOG(ERROR) << "Cannot download " << uri;
765
766 switch (operation->GetMode())
767 {
768 case Mode_SeriesGeometry:
769 callback_.NotifyGeometryError(*this);
770 state_ = State_Error;
771 break;
772
773 case Mode_LoadImage:
774 callback_.NotifySliceImageError(*this, operation->GetSliceIndex(), operation->GetSlice());
775 break;
776
777 default:
778 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
779 } 796 }
780 } 797 }
781 }; 798 };
782 799
783 800
784 class Tata : public OrthancSliceLoader::ICallback 801 class Tata : public OrthancSlicesLoader::ICallback
785 { 802 {
786 public: 803 public:
787 virtual void NotifyGeometryReady(OrthancSliceLoader& loader) 804 virtual void NotifyGeometryReady(const OrthancSlicesLoader& loader)
788 { 805 {
789 printf(">> %d\n", loader.GetSliceCount()); 806 printf(">> %d\n", loader.GetSliceCount());
790 807
791 for (size_t i = 0; i < loader.GetSliceCount(); i++) 808 for (size_t i = 0; i < loader.GetSliceCount(); i++)
792 { 809 {
793 loader.ScheduleLoadSliceImage(i); 810 const_cast<OrthancSlicesLoader&>(loader).ScheduleLoadSliceImage(i);
794 } 811 }
795 } 812 }
796 813
797 virtual void NotifyGeometryError(OrthancSliceLoader& loader) 814 virtual void NotifyGeometryError(const OrthancSlicesLoader& loader)
798 { 815 {
799 printf("Error\n"); 816 printf("Error\n");
800 } 817 }
801 818
802 virtual void NotifySliceImageReady(OrthancSliceLoader& loader, 819 virtual void NotifySliceImageReady(const OrthancSlicesLoader& loader,
803 unsigned int sliceIndex, 820 unsigned int sliceIndex,
804 const Slice& slice, 821 const Slice& slice,
805 Orthanc::ImageAccessor* image) 822 Orthanc::ImageAccessor* image)
806 { 823 {
807 std::auto_ptr<Orthanc::ImageAccessor> tmp(image); 824 std::auto_ptr<Orthanc::ImageAccessor> tmp(image);
808 printf("Slice OK %dx%d\n", tmp->GetWidth(), tmp->GetHeight()); 825 printf("Slice OK %dx%d\n", tmp->GetWidth(), tmp->GetHeight());
809 } 826 }
810 827
811 virtual void NotifySliceImageError(OrthancSliceLoader& loader, 828 virtual void NotifySliceImageError(const OrthancSlicesLoader& loader,
812 unsigned int sliceIndex, 829 unsigned int sliceIndex,
813 const Slice& slice) 830 const Slice& slice)
814 { 831 {
815 printf("ERROR 2\n"); 832 printf("ERROR 2\n");
816 } 833 }
823 Orthanc::WebServiceParameters web; 840 Orthanc::WebServiceParameters web;
824 OrthancStone::OrthancAsynchronousWebService orthanc(web, 4); 841 OrthancStone::OrthancAsynchronousWebService orthanc(web, 4);
825 orthanc.Start(); 842 orthanc.Start();
826 843
827 OrthancStone::Tata tata; 844 OrthancStone::Tata tata;
828 OrthancStone::OrthancSliceLoader loader(tata, orthanc); 845 OrthancStone::OrthancSlicesLoader loader(tata, orthanc);
829 //loader.ScheduleLoadSeries("c1c4cb95-05e3bd11-8da9f5bb-87278f71-0b2b43f5"); 846 //loader.ScheduleLoadSeries("c1c4cb95-05e3bd11-8da9f5bb-87278f71-0b2b43f5");
830 //loader.ScheduleLoadSeries("67f1b334-02c16752-45026e40-a5b60b6b-030ecab5"); 847 loader.ScheduleLoadSeries("67f1b334-02c16752-45026e40-a5b60b6b-030ecab5");
831 848
832 loader.ScheduleLoadInstance("19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5", 0); 849 //loader.ScheduleLoadInstance("19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5", 0);
833 850
834 /*printf(">> %d\n", loader.GetSliceCount()); 851 /*printf(">> %d\n", loader.GetSliceCount());
835 loader.ScheduleLoadSliceImage(31);*/ 852 loader.ScheduleLoadSliceImage(31);*/
836 853
837 boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); 854 boost::this_thread::sleep(boost::posix_time::milliseconds(1000));