changeset 1009:26642cecd36d lua-scripting

clearer job interface
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Jul 2014 16:11:44 +0200
parents 187ed107a59f
children 160dfe770618
files OrthancServer/Scheduler/IServerCommand.h OrthancServer/Scheduler/ModifyInstanceCommand.h OrthancServer/Scheduler/ServerCommandInstance.cpp OrthancServer/Scheduler/ServerCommandInstance.h OrthancServer/Scheduler/ServerJob.cpp OrthancServer/Scheduler/ServerJob.h OrthancServer/Scheduler/ServerScheduler.cpp OrthancServer/Scheduler/StorePeerCommand.h OrthancServer/Scheduler/StoreScuCommand.h OrthancServer/ServerContext.cpp UnitTestsSources/MultiThreadingTests.cpp
diffstat 11 files changed, 73 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- 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;
   };
 }
--- 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);
   };
 }
--- 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_;
     }
   }
 }
--- 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<ServerCommandInstance*> 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<ServerCommandInstance*>& GetNextCommands() const
     {
       return next_;
     }
-
-    IServerCommand& GetCommand() const
-    {
-      return *filter_;
-    }
   };
 }
--- 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<IDynamicObject*>::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();
+  }
+
 }
--- 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<ServerCommandInstance*> filters_;
+    std::list<IDynamicObject*> 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);
   };
 }
--- 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);
       }
     }
 
--- 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);
   };
 }
--- 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);
   };
 }
--- 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
         {
--- 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<std::string>(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<std::string>(42));
   //f3.AddInput(boost::lexical_cast<std::string>(42));
   //f4.AddInput(boost::lexical_cast<std::string>(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<int>(l.front()));
+  ASSERT_EQ(42 * 2 * 3 * 4 * 5, boost::lexical_cast<int>(l.back()));
+
   for (IServerCommand::ListOfStrings::iterator i = l.begin(); i != l.end(); i++)
   {
     printf("** %s\n", i->c_str());