# HG changeset patch # User Sebastien Jodogne # Date 1557225141 -7200 # Node ID 8a3a25f2d42ce8884c33bf6dbbf62b3ebfeb28aa # Parent 8adc8cfb50c7e185bb123ccd270817902a1fb7ed uncoupling oracle from context diff -r 8adc8cfb50c7 -r 8a3a25f2d42c Samples/Sdl/Loader.cpp --- a/Samples/Sdl/Loader.cpp Tue May 07 11:59:46 2019 +0200 +++ b/Samples/Sdl/Loader.cpp Tue May 07 12:32:21 2019 +0200 @@ -62,6 +62,17 @@ }; + class IMessageEmitter : public boost::noncopyable + { + public: + virtual ~IMessageEmitter() + { + } + + virtual void EmitMessage(const OrthancStone::IMessage& message) = 0; + }; + + class IOracle : public boost::noncopyable { public: @@ -268,74 +279,6 @@ - class NativeApplicationContext : public boost::noncopyable - { - private: - boost::shared_mutex mutex_; - Orthanc::WebServiceParameters orthanc_; - OrthancStone::MessageBroker broker_; - OrthancStone::IObservable oracleObservable_; - - public: - NativeApplicationContext() : - oracleObservable_(broker_) - { - orthanc_.SetUrl("http://localhost:8042/"); - } - - - class ReaderLock : public boost::noncopyable - { - private: - NativeApplicationContext& that_; - boost::shared_lock lock_; - - public: - ReaderLock(NativeApplicationContext& that) : - that_(that), - lock_(that.mutex_) - { - } - - const Orthanc::WebServiceParameters& GetOrthancParameters() const - { - return that_.orthanc_; - } - }; - - - class WriterLock : public boost::noncopyable - { - private: - NativeApplicationContext& that_; - boost::unique_lock lock_; - - public: - WriterLock(NativeApplicationContext& that) : - that_(that), - lock_(that.mutex_) - { - } - - OrthancStone::MessageBroker& GetBroker() - { - return that_.broker_; - } - - void SetOrthancParameters(Orthanc::WebServiceParameters& orthanc) - { - that_.orthanc_ = orthanc; - } - - OrthancStone::IObservable& GetOracleObservable() - { - return that_.oracleObservable_; - } - }; - }; - - - class NativeOracle : public IOracle { private: @@ -370,31 +313,26 @@ }; - NativeApplicationContext& context_; - Orthanc::SharedMessageQueue queue_; - State state_; - boost::mutex mutex_; - std::vector workers_; + IMessageEmitter& emitter_; + Orthanc::WebServiceParameters orthanc_; + Orthanc::SharedMessageQueue queue_; + State state_; + boost::mutex mutex_; + std::vector workers_; void Execute(const OrthancApiOracleCommand& command) { - std::auto_ptr client; - - { - NativeApplicationContext::ReaderLock lock(context_); - client.reset(new Orthanc::HttpClient(lock.GetOrthancParameters(), command.GetUri())); - } - - client->SetMethod(command.GetMethod()); - client->SetBody(command.GetBody()); - client->SetTimeout(command.GetTimeout()); + Orthanc::HttpClient client(orthanc_, command.GetUri()); + client.SetMethod(command.GetMethod()); + client.SetBody(command.GetBody()); + client.SetTimeout(command.GetTimeout()); { const HttpHeaders& headers = command.GetHttpHeaders(); for (HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); it++ ) { - client->AddHeader(it->first, it->second); + client.AddHeader(it->first, it->second); } } @@ -404,26 +342,22 @@ bool success; try { - success = client->Apply(answer, answerHeaders); + success = client.Apply(answer, answerHeaders); } catch (Orthanc::OrthancException& e) { success = false; } + if (success) { - NativeApplicationContext::WriterLock lock(context_); - - if (success) - { - OrthancApiOracleCommand::SuccessMessage message(command, answerHeaders, answer); - lock.GetOracleObservable().EmitMessage(message); - } - else - { - OrthancApiOracleCommand::FailureMessage message(command, client->GetLastStatus()); - lock.GetOracleObservable().EmitMessage(message); - } + OrthancApiOracleCommand::SuccessMessage message(command, answerHeaders, answer); + emitter_.EmitMessage(message); + } + else + { + OrthancApiOracleCommand::FailureMessage message(command, client.GetLastStatus()); + emitter_.EmitMessage(message); } } @@ -501,8 +435,8 @@ public: - NativeOracle(NativeApplicationContext& context) : - context_(context), + NativeOracle(IMessageEmitter& emitter) : + emitter_(emitter), state_(State_Setup), workers_(4) { @@ -513,6 +447,20 @@ StopInternal(); } + void SetOrthancParameters(const Orthanc::WebServiceParameters& orthanc) + { + boost::mutex::scoped_lock lock(mutex_); + + if (state_ != State_Setup) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + else + { + orthanc_ = orthanc; + } + } + void SetWorkersCount(unsigned int count) { boost::mutex::scoped_lock lock(mutex_); @@ -560,6 +508,69 @@ queue_.Enqueue(new Item(command)); } }; + + + + class NativeApplicationContext : public IMessageEmitter + { + private: + boost::shared_mutex mutex_; + OrthancStone::MessageBroker broker_; + OrthancStone::IObservable oracleObservable_; + + public: + NativeApplicationContext() : + oracleObservable_(broker_) + { + } + + + virtual void EmitMessage(const OrthancStone::IMessage& message) + { + boost::unique_lock lock(mutex_); + oracleObservable_.EmitMessage(message); + } + + + class ReaderLock : public boost::noncopyable + { + private: + NativeApplicationContext& that_; + boost::shared_lock lock_; + + public: + ReaderLock(NativeApplicationContext& that) : + that_(that), + lock_(that.mutex_) + { + } + }; + + + class WriterLock : public boost::noncopyable + { + private: + NativeApplicationContext& that_; + boost::unique_lock lock_; + + public: + WriterLock(NativeApplicationContext& that) : + that_(that), + lock_(that.mutex_) + { + } + + OrthancStone::MessageBroker& GetBroker() + { + return that_.broker_; + } + + OrthancStone::IObservable& GetOracleObservable() + { + return that_.oracleObservable_; + } + }; + }; } @@ -600,6 +611,14 @@ } Refactoring::NativeOracle oracle(context); + + { + Orthanc::WebServiceParameters p; + //p.SetUrl("http://localhost:8043/"); + p.SetCredentials("orthanc", "orthanc"); + oracle.SetOrthancParameters(p); + } + oracle.Start(); {