# HG changeset patch # User Sebastien Jodogne # Date 1404915104 -7200 # Node ID 26642cecd36dd61043e3ac729f1222913dab2a4a # Parent 187ed107a59f6de2518b5586b3a91b98e8769725 clearer job interface diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/IServerCommand.h --- a/OrthancServer/Scheduler/IServerCommand.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/IServerCommand.h Wed Jul 09 16:11:44 2014 +0200 @@ -49,7 +49,5 @@ virtual bool Apply(ListOfStrings& outputs, const ListOfStrings& inputs) = 0; - - virtual bool SendOutputsToSink() const = 0; }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ModifyInstanceCommand.h --- a/OrthancServer/Scheduler/ModifyInstanceCommand.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ModifyInstanceCommand.h Wed Jul 09 16:11:44 2014 +0200 @@ -60,12 +60,7 @@ return modification_; } - bool Apply(ListOfStrings& outputs, - const ListOfStrings& inputs); - - bool SendOutputsToSink() const - { - return false; - } + virtual bool Apply(ListOfStrings& outputs, + const ListOfStrings& inputs); }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ServerCommandInstance.cpp --- a/OrthancServer/Scheduler/ServerCommandInstance.cpp Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ServerCommandInstance.cpp Wed Jul 09 16:11:44 2014 +0200 @@ -44,7 +44,7 @@ try { - if (filter_->Apply(outputs, inputs_)) + if (command_->Apply(outputs, inputs_)) { success = true; } @@ -74,12 +74,13 @@ } - ServerCommandInstance::ServerCommandInstance(IServerCommand *filter, - const std::string& jobId) : - filter_(filter), - jobId_(jobId) + ServerCommandInstance::ServerCommandInstance(IServerCommand *command, + const std::string& jobId) : + command_(command), + jobId_(jobId), + connectedToSink_(false) { - if (filter_ == NULL) + if (command_ == NULL) { throw OrthancException(ErrorCode_ParameterOutOfRange); } @@ -88,9 +89,9 @@ ServerCommandInstance::~ServerCommandInstance() { - if (filter_ != NULL) + if (command_ != NULL) { - delete filter_; + delete command_; } } } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ServerCommandInstance.h --- a/OrthancServer/Scheduler/ServerCommandInstance.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ServerCommandInstance.h Wed Jul 09 16:11:44 2014 +0200 @@ -57,16 +57,17 @@ private: typedef IServerCommand::ListOfStrings ListOfStrings; - IServerCommand *filter_; + IServerCommand *command_; std::string jobId_; ListOfStrings inputs_; std::list next_; + bool connectedToSink_; bool Execute(IListener& listener); public: - ServerCommandInstance(IServerCommand *filter, - const std::string& jobId); + ServerCommandInstance(IServerCommand *command, + const std::string& jobId); virtual ~ServerCommandInstance(); @@ -80,19 +81,24 @@ inputs_.push_back(input); } - void ConnectNext(ServerCommandInstance& filter) + void ConnectOutput(ServerCommandInstance& next) + { + next_.push_back(&next); + } + + void SetConnectedToSink(bool connected = true) { - next_.push_back(&filter); + connectedToSink_ = connected; + } + + bool IsConnectedToSink() const + { + return connectedToSink_; } const std::list& GetNextCommands() const { return next_; } - - IServerCommand& GetCommand() const - { - return *filter_; - } }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ServerJob.cpp --- a/OrthancServer/Scheduler/ServerJob.cpp Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ServerJob.cpp Wed Jul 09 16:11:44 2014 +0200 @@ -109,6 +109,12 @@ { delete *it; } + + for (std::list::iterator + it = payloads_.begin(); it != payloads_.end(); it++) + { + delete *it; + } } @@ -123,4 +129,18 @@ return *filters_.back(); } + + + IDynamicObject& ServerJob::AddPayload(IDynamicObject* payload) + { + if (submitted_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + + payloads_.push_back(payload); + + return *filters_.back(); + } + } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ServerJob.h --- a/OrthancServer/Scheduler/ServerJob.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ServerJob.h Wed Jul 09 16:11:44 2014 +0200 @@ -43,6 +43,7 @@ private: std::list filters_; + std::list payloads_; std::string jobId_; bool submitted_; std::string description_; @@ -73,5 +74,9 @@ } ServerCommandInstance& AddCommand(IServerCommand* filter); + + // Take the ownership of a payload to a job. This payload will be + // automatically freed when the job succeeds or fails. + IDynamicObject& AddPayload(IDynamicObject* payload); }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/ServerScheduler.cpp --- a/OrthancServer/Scheduler/ServerScheduler.cpp Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/ServerScheduler.cpp Wed Jul 09 16:11:44 2014 +0200 @@ -51,11 +51,6 @@ { } - virtual bool SendOutputsToSink() const - { - return false; - } - virtual bool Apply(ListOfStrings& outputs, const ListOfStrings& inputs) { @@ -240,10 +235,9 @@ it = job.filters_.begin(); it != job.filters_.end(); it++) { if ((*it) != &sink && - (*it)->GetNextCommands().size() == 0 && - (*it)->GetCommand().SendOutputsToSink()) + (*it)->IsConnectedToSink()) { - (*it)->ConnectNext(sink); + (*it)->ConnectOutput(sink); } } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/StorePeerCommand.h --- a/OrthancServer/Scheduler/StorePeerCommand.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/StorePeerCommand.h Wed Jul 09 16:11:44 2014 +0200 @@ -47,13 +47,8 @@ public: StorePeerCommand(ServerContext& context, const OrthancPeerParameters& peer); - - bool Apply(ListOfStrings& outputs, - const ListOfStrings& inputs); - - bool SendOutputsToSink() const - { - return false; - } + + virtual bool Apply(ListOfStrings& outputs, + const ListOfStrings& inputs); }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/Scheduler/StoreScuCommand.h --- a/OrthancServer/Scheduler/StoreScuCommand.h Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/Scheduler/StoreScuCommand.h Wed Jul 09 16:11:44 2014 +0200 @@ -47,12 +47,7 @@ StoreScuCommand(ServerContext& context, const RemoteModalityParameters& modality); - bool Apply(ListOfStrings& outputs, - const ListOfStrings& inputs); - - bool SendOutputsToSink() const - { - return false; - } + virtual bool Apply(ListOfStrings& outputs, + const ListOfStrings& inputs); }; } diff -r 187ed107a59f -r 26642cecd36d OrthancServer/ServerContext.cpp --- a/OrthancServer/ServerContext.cpp Tue Jul 08 18:14:24 2014 +0200 +++ b/OrthancServer/ServerContext.cpp Wed Jul 09 16:11:44 2014 +0200 @@ -230,7 +230,7 @@ } else if (previousCommand != NULL) { - previousCommand->ConnectNext(command); + previousCommand->ConnectOutput(command); } else { diff -r 187ed107a59f -r 26642cecd36d UnitTestsSources/MultiThreadingTests.cpp --- a/UnitTestsSources/MultiThreadingTests.cpp Tue Jul 08 18:14:24 2014 +0200 +++ b/UnitTestsSources/MultiThreadingTests.cpp Wed Jul 09 16:11:44 2014 +0200 @@ -303,15 +303,10 @@ outputs.push_back(boost::lexical_cast(b)); } - Toolbox::USleep(1000000); + Toolbox::USleep(100000); return true; } - - virtual bool SendOutputsToSink() const - { - return true; - } }; @@ -319,31 +314,18 @@ { typedef IServerCommand::ListOfStrings ListOfStrings; -#if 1 while (!(*done)) { ListOfStrings l; s->GetListOfJobs(l); for (ListOfStrings::iterator i = l.begin(); i != l.end(); i++) printf(">> %s: %0.1f\n", i->c_str(), 100.0f * s->GetProgress(*i)); - Toolbox::USleep(100000); + Toolbox::USleep(10000); } -#else - ListOfStrings l; - s->GetListOfJobs(l); - for (ListOfStrings::iterator i = l.begin(); i != l.end(); i++) - printf(">> %s\n", i->c_str()); - Toolbox::USleep(1500000); - s->Cancel(*j); - Toolbox::USleep(1000000); - s->GetListOfJobs(l); - for (ListOfStrings::iterator i = l.begin(); i != l.end(); i++) - printf(">> %s\n", i->c_str()); -#endif } -TEST(MultiThreading, DISABLED_ServerScheduler) +TEST(MultiThreading, ServerScheduler) { ServerScheduler scheduler(10); @@ -355,9 +337,12 @@ f2.AddInput(boost::lexical_cast(42)); //f3.AddInput(boost::lexical_cast(42)); //f4.AddInput(boost::lexical_cast(42)); - f2.ConnectNext(f3); - f3.ConnectNext(f4); - f4.ConnectNext(f5); + f2.ConnectOutput(f3); + f3.ConnectOutput(f4); + f4.ConnectOutput(f5); + + f3.SetConnectedToSink(true); + f5.SetConnectedToSink(true); job.SetDescription("tutu"); @@ -370,6 +355,10 @@ IServerCommand::ListOfStrings l; scheduler.SubmitAndWait(l, job); + ASSERT_EQ(2, l.size()); + ASSERT_EQ(42 * 2 * 3, boost::lexical_cast(l.front())); + ASSERT_EQ(42 * 2 * 3 * 4 * 5, boost::lexical_cast(l.back())); + for (IServerCommand::ListOfStrings::iterator i = l.begin(); i != l.end(); i++) { printf("** %s\n", i->c_str());