Salome HOME
Changed year in copyrights
[tools/libbatch.git] / src / Local / Batch_BatchManager_Local.hxx
index 5fac3d8f877b9d58cf2bef331f6dffdcc15b2e51..53d94a225ada3e4691b5d820ac04e8160a2c486e 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
  * Date   : Thu Nov  6 10:17:22 2003
  * Projet : Salome 2
  *
+ * Refactored by Renaud Barate (EDF R&D) in September 2009 to use
+ * CommunicationProtocol classes and merge Local_SH, Local_RSH and Local_SSH batch
+ * managers.
+ *
  */
 
 #ifndef _BATCHMANAGER_LOCAL_H_
 #define _BATCHMANAGER_LOCAL_H_
 
 #include "Batch_Defines.hxx"
+#include "Batch_CommunicationProtocol.hxx"
 
 #ifdef WIN32
 #include <Windows.h>
 #include "Batch_JobInfo.hxx"
 #include "Batch_JobInfo_Local.hxx"
 #include "Batch_Job_Local.hxx"
-#include "Batch_InvalidArgumentException.hxx"
-#include "Batch_ConnexionFailureException.hxx"
-#include "Batch_APIInternalFailureException.hxx"
-#include "Batch_NotYetImplementedException.hxx"
 #include "Batch_BatchManager.hxx"
 
 namespace Batch {
 
   class FactBatchManager;
 
-  class BATCH_EXPORT BatchManager_Local : public BatchManager
+  /*!
+   *  This class defines a local pseudo batch manager that can launch jobs locally or on a remote
+   *  machine with SSH or RSH. This class is NOT thread-safe.
+   */
+  class BATCH_EXPORT BatchManager_Local : virtual public BatchManager
   {
   private:
+    typedef int Id;
 #ifdef WIN32
     typedef HANDLE Process;
 #else
@@ -68,13 +74,15 @@ namespace Batch {
     friend class ThreadAdapter;
     class ThreadAdapter{
     public:
-      ThreadAdapter(BatchManager_Local & bm, const Job_Local & job);
+      ThreadAdapter(BatchManager_Local & bm, const Job_Local & job, Id id);
       static void * run(void * arg);
       BatchManager_Local & getBatchManager() const { return _bm; };
+      Id getId() const { return _id; };
 
     protected:
       BatchManager_Local & _bm;
       const Job_Local _job;
+      Id _id;
 
     private:
       void pere(Process child);
@@ -86,8 +94,6 @@ namespace Batch {
 
     };
 
-    typedef int Id;
-
     enum Commande {
       NOP = 0,
       HOLD,
@@ -97,20 +103,12 @@ namespace Batch {
       ALTER
     };
 
-    enum Status {
-      UNKNOWN = 0,
-      RUNNING,
-      STOPPED,
-      DONE,
-      DEAD
-    };
-
     struct Child {
       pthread_t thread_id;
       std::queue<Commande, std::deque<Commande> > command_queue;
       pid_t pid;
       int exit_code;
-      Status status;
+      bool hasFailed;
       Parametre param;
       Environnement env;
     };
@@ -119,12 +117,17 @@ namespace Batch {
 
   public:
     // Constructeur et destructeur
-    BatchManager_Local(const FactBatchManager * parent, const char * host="localhost") throw(InvalidArgumentException,ConnexionFailureException); // connexion a la machine host
+    BatchManager_Local(const FactBatchManager * parent,
+                       const char * host="localhost",
+                       CommunicationProtocolType protocolType = SSH); // connexion a la machine host
     virtual ~BatchManager_Local();
 
     // Recupere le nom du serveur par defaut
     // static string BatchManager_Local::getDefaultServer();
 
+    // Get the underlying communication protocol
+    const CommunicationProtocol & getProtocol() const;
+
     // Methodes pour le controle des jobs
     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
@@ -139,44 +142,24 @@ namespace Batch {
     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
 
+    virtual const Batch::JobId addJob(const Batch::Job & job, const std::string reference); // ajoute un nouveau job sans le soumettre
 
   protected:
     int _connect; // Local connect id
     pthread_mutex_t _threads_mutex;
     std::map<Id, Child > _threads;
 
-    // Methode abstraite qui renvoie la commande de copie du fichier source en destination
-    virtual std::string copy_command( const std::string & user_source,
-                                      const std::string & host_source,
-                                      const std::string & source,
-                                      const std::string & user_destination,
-                                      const std::string & host_destination,
-                                      const std::string & destination) const = 0;
-
-    // Methode abstraite qui renvoie la commande a executer
-    virtual std::string exec_command(Parametre & param) const = 0;
+    const CommunicationProtocol & _protocol;
 
-    // Methode abstraite qui renvoie la commande d'effacement du fichier
-    virtual std::string remove_command( const std::string & user_destination,
-                                        const std::string & host_destination,
-                                        const std::string & destination) const = 0;
+    // Methode qui renvoie la commande a executer
+    std::vector<std::string> exec_command(const Parametre & param) const;
 
   private:
-    struct ThreadIdIdAssociation {
-      pthread_t threadId;
-      Id id;
-    };
-
-    virtual pthread_t submit(const Job_Local & job);
-    virtual void cancel(pthread_t thread_id);
     static  void kill_child_on_exit(void * p_pid);
     static  void delete_on_exit(void * arg);
-    Id nextId(); // Retourne un identifiant unique pour un thread (clef de la map)
-    Id getIdByThread_id(pthread_t thread_id);
-    Id registerThread_id(pthread_t thread_id);
-    pthread_mutex_t _thread_id_id_association_mutex;
-    pthread_cond_t  _thread_id_id_association_cond;
-    std::list<struct ThreadIdIdAssociation> _thread_id_id_association;
+    static void setFailedOnCancel(void * arg);
+    pthread_cond_t _threadSyncCondition;
+    Id _idCounter;
 
 #ifdef SWIG
   public: