comparison Samples/Sdl/Loader.cpp @ 623:42dadae61fa9

renamed IObservable::EmitMessage() as BroadcastMessage()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 May 2019 14:16:08 +0200
parents 8a3a25f2d42c
children 2eeb5857eb43
comparison
equal deleted inserted replaced
622:8a3a25f2d42c 623:42dadae61fa9
22 #include "../../Framework/StoneInitialization.h" 22 #include "../../Framework/StoneInitialization.h"
23 #include "../../Framework/Messages/IMessage.h" 23 #include "../../Framework/Messages/IMessage.h"
24 #include "../../Framework/Messages/MessageBroker.h" 24 #include "../../Framework/Messages/MessageBroker.h"
25 #include "../../Framework/Messages/ICallable.h" 25 #include "../../Framework/Messages/ICallable.h"
26 #include "../../Framework/Messages/IObservable.h" 26 #include "../../Framework/Messages/IObservable.h"
27 #include "../../Framework/Volumes/ImageBuffer3D.h"
27 28
28 // From Orthanc framework 29 // From Orthanc framework
29 #include <Core/IDynamicObject.h> 30 #include <Core/IDynamicObject.h>
30 #include <Core/Images/Image.h> 31 #include <Core/Images/Image.h>
31 #include <Core/Images/ImageProcessing.h> 32 #include <Core/Images/ImageProcessing.h>
67 public: 68 public:
68 virtual ~IMessageEmitter() 69 virtual ~IMessageEmitter()
69 { 70 {
70 } 71 }
71 72
72 virtual void EmitMessage(const OrthancStone::IMessage& message) = 0; 73 virtual void EmitMessage(const OrthancStone::IObserver& observer,
74 const OrthancStone::IMessage& message) = 0;
73 }; 75 };
74 76
75 77
76 class IOracle : public boost::noncopyable 78 class IOracle : public boost::noncopyable
77 { 79 {
78 public: 80 public:
79 virtual ~IOracle() 81 virtual ~IOracle()
80 { 82 {
81 } 83 }
82 84
83 virtual void Schedule(IOracleCommand* command) = 0; // Takes ownership 85 virtual void Schedule(const OrthancStone::IObserver& receiver,
86 IOracleCommand* command) = 0; // Takes ownership
84 }; 87 };
85 88
86 89
87 90
88 91
283 { 286 {
284 private: 287 private:
285 class Item : public Orthanc::IDynamicObject 288 class Item : public Orthanc::IDynamicObject
286 { 289 {
287 private: 290 private:
288 std::auto_ptr<IOracleCommand> command_; 291 const OrthancStone::IObserver& receiver_;
292 std::auto_ptr<IOracleCommand> command_;
289 293
290 public: 294 public:
291 Item(IOracleCommand* command) : 295 Item(const OrthancStone::IObserver& receiver,
292 command_(command) 296 IOracleCommand* command) :
297 receiver_(receiver),
298 command_(command)
293 { 299 {
294 if (command == NULL) 300 if (command == NULL)
295 { 301 {
296 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 302 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
297 } 303 }
298 } 304 }
299 305
300 const IOracleCommand& GetCommand() 306 const OrthancStone::IObserver& GetReceiver() const
307 {
308 return receiver_;
309 }
310
311 const IOracleCommand& GetCommand() const
301 { 312 {
302 assert(command_.get() != NULL); 313 assert(command_.get() != NULL);
303 return *command_; 314 return *command_;
304 } 315 }
305 }; 316 };
319 State state_; 330 State state_;
320 boost::mutex mutex_; 331 boost::mutex mutex_;
321 std::vector<boost::thread*> workers_; 332 std::vector<boost::thread*> workers_;
322 333
323 334
324 void Execute(const OrthancApiOracleCommand& command) 335 void Execute(const OrthancStone::IObserver& receiver,
336 const OrthancApiOracleCommand& command)
325 { 337 {
326 Orthanc::HttpClient client(orthanc_, command.GetUri()); 338 Orthanc::HttpClient client(orthanc_, command.GetUri());
327 client.SetMethod(command.GetMethod()); 339 client.SetMethod(command.GetMethod());
328 client.SetBody(command.GetBody()); 340 client.SetBody(command.GetBody());
329 client.SetTimeout(command.GetTimeout()); 341 client.SetTimeout(command.GetTimeout());
350 } 362 }
351 363
352 if (success) 364 if (success)
353 { 365 {
354 OrthancApiOracleCommand::SuccessMessage message(command, answerHeaders, answer); 366 OrthancApiOracleCommand::SuccessMessage message(command, answerHeaders, answer);
355 emitter_.EmitMessage(message); 367 emitter_.EmitMessage(receiver, message);
356 } 368 }
357 else 369 else
358 { 370 {
359 OrthancApiOracleCommand::FailureMessage message(command, client.GetLastStatus()); 371 OrthancApiOracleCommand::FailureMessage message(command, client.GetLastStatus());
360 emitter_.EmitMessage(message); 372 emitter_.EmitMessage(receiver, message);
361 } 373 }
362 } 374 }
363 375
364 376
365 377
366 void Step() 378 void Step()
367 { 379 {
368 std::auto_ptr<Orthanc::IDynamicObject> item(queue_.Dequeue(100)); 380 std::auto_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100));
369 381
370 if (item.get() != NULL) 382 if (object.get() != NULL)
371 { 383 {
372 const IOracleCommand& command = dynamic_cast<Item*>(item.get())->GetCommand(); 384 const Item& item = dynamic_cast<Item&>(*object);
373 385
374 switch (command.GetType()) 386 switch (item.GetCommand().GetType())
375 { 387 {
376 case IOracleCommand::Type_OrthancApi: 388 case IOracleCommand::Type_OrthancApi:
377 Execute(dynamic_cast<const OrthancApiOracleCommand&>(command)); 389 Execute(item.GetReceiver(),
390 dynamic_cast<const OrthancApiOracleCommand&>(item.GetCommand()));
378 break; 391 break;
379 392
380 default: 393 default:
381 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); 394 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
382 } 395 }
501 void Stop() 514 void Stop()
502 { 515 {
503 StopInternal(); 516 StopInternal();
504 } 517 }
505 518
506 virtual void Schedule(IOracleCommand* command) 519 virtual void Schedule(const OrthancStone::IObserver& receiver,
507 { 520 IOracleCommand* command)
508 queue_.Enqueue(new Item(command)); 521 {
522 queue_.Enqueue(new Item(receiver, command));
509 } 523 }
510 }; 524 };
511 525
512 526
513 527
523 oracleObservable_(broker_) 537 oracleObservable_(broker_)
524 { 538 {
525 } 539 }
526 540
527 541
528 virtual void EmitMessage(const OrthancStone::IMessage& message) 542 virtual void EmitMessage(const OrthancStone::IObserver& observer,
543 const OrthancStone::IMessage& message)
529 { 544 {
530 boost::unique_lock<boost::shared_mutex> lock(mutex_); 545 boost::unique_lock<boost::shared_mutex> lock(mutex_);
531 oracleObservable_.EmitMessage(message); 546 oracleObservable_.EmitMessage(observer, message);
532 } 547 }
533 548
534 549
535 class ReaderLock : public boost::noncopyable 550 class ReaderLock : public boost::noncopyable
536 { 551 {
569 { 584 {
570 return that_.oracleObservable_; 585 return that_.oracleObservable_;
571 } 586 }
572 }; 587 };
573 }; 588 };
589
590
591
592 class AxialVolumeOrthancLoader : public OrthancStone::IObserver
593 {
594 private:
595 void Handle(const Refactoring::OrthancApiOracleCommand::SuccessMessage& message)
596 {
597 Json::Value v;
598 message.GetJsonBody(v);
599
600 printf("ICI [%s]\n", v.toStyledString().c_str());
601 }
602
603
604 std::auto_ptr<OrthancStone::ImageBuffer3D> image_;
605
606
607 public:
608 AxialVolumeOrthancLoader(OrthancStone::IObservable& oracle) :
609 IObserver(oracle.GetBroker())
610 {
611 oracle.RegisterObserverCallback(
612 new OrthancStone::Callable<AxialVolumeOrthancLoader, OrthancApiOracleCommand::SuccessMessage>
613 (*this, &AxialVolumeOrthancLoader::Handle));
614 }
615 };
616
574 } 617 }
575 618
576 619
577 620
578 class Toto : public OrthancStone::IObserver 621 class Toto : public OrthancStone::IObserver
593 636
594 public: 637 public:
595 Toto(OrthancStone::IObservable& oracle) : 638 Toto(OrthancStone::IObservable& oracle) :
596 IObserver(oracle.GetBroker()) 639 IObserver(oracle.GetBroker())
597 { 640 {
598 oracle.RegisterObserverCallback(new OrthancStone::Callable<Toto, Refactoring::OrthancApiOracleCommand::SuccessMessage>(*this, &Toto::Handle)); 641 oracle.RegisterObserverCallback
599 oracle.RegisterObserverCallback(new OrthancStone::Callable<Toto, Refactoring::OrthancApiOracleCommand::FailureMessage>(*this, &Toto::Handle)); 642 (new OrthancStone::Callable
643 <Toto, Refactoring::OrthancApiOracleCommand::SuccessMessage>(*this, &Toto::Handle));
600 } 644 }
601 }; 645 };
602 646
603 647
604 void Run(Refactoring::NativeApplicationContext& context) 648 void Run(Refactoring::NativeApplicationContext& context)
606 std::auto_ptr<Toto> toto; 650 std::auto_ptr<Toto> toto;
607 651
608 { 652 {
609 Refactoring::NativeApplicationContext::WriterLock lock(context); 653 Refactoring::NativeApplicationContext::WriterLock lock(context);
610 toto.reset(new Toto(lock.GetOracleObservable())); 654 toto.reset(new Toto(lock.GetOracleObservable()));
655 }
656
657 std::auto_ptr<Refactoring::AxialVolumeOrthancLoader> loader;
658
659 {
660 Refactoring::NativeApplicationContext::WriterLock lock(context);
661 loader.reset(new Refactoring::AxialVolumeOrthancLoader(lock.GetOracleObservable()));
611 } 662 }
612 663
613 Refactoring::NativeOracle oracle(context); 664 Refactoring::NativeOracle oracle(context);
614 665
615 { 666 {
629 std::auto_ptr<Refactoring::OrthancApiOracleCommand> command(new Refactoring::OrthancApiOracleCommand); 680 std::auto_ptr<Refactoring::OrthancApiOracleCommand> command(new Refactoring::OrthancApiOracleCommand);
630 command->SetMethod(Orthanc::HttpMethod_Post); 681 command->SetMethod(Orthanc::HttpMethod_Post);
631 command->SetUri("/tools/find"); 682 command->SetUri("/tools/find");
632 command->SetBody(v); 683 command->SetBody(v);
633 684
634 oracle.Schedule(command.release()); 685 oracle.Schedule(*toto, command.release());
635 } 686 }
636 687
637 boost::this_thread::sleep(boost::posix_time::seconds(1)); 688 boost::this_thread::sleep(boost::posix_time::seconds(1));
638 689
639 oracle.Stop(); 690 oracle.Stop();