General KERNEL Documentation     End User KERNEL Services  


src/Batch/Batch_BatchManager_Local.hxx

Go to the documentation of this file.
00001 // Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
00002 //           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either 
00006 // version 2.1 of the License.
00007 // 
00008 // This library is distributed in the hope that it will be useful 
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011 // Lesser General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU Lesser General Public  
00014 // License along with this library; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 // 
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 // 
00019 /*
00020  * BatchManager_Local.hxx : 
00021  *
00022  * Auteur : Ivan DUTKA-MALEN - EDF R&D
00023  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
00024  * Date   : Thu Nov  6 10:17:22 2003
00025  * Projet : Salome 2
00026  *
00027  */
00028 
00029 #ifndef _BATCHMANAGER_LOCAL_H_
00030 #define _BATCHMANAGER_LOCAL_H_
00031 
00032 
00033 #include <vector>
00034 #include <map>
00035 #include <queue>
00036 #include <deque>
00037 #include <pthread.h>
00038 #include "Batch_Job.hxx"
00039 #include "Batch_JobId.hxx"
00040 #include "Batch_JobInfo.hxx"
00041 #include "Batch_JobInfo_Local.hxx"
00042 #include "Batch_Job_Local.hxx"
00043 #include "Batch_InvalidArgumentException.hxx"
00044 #include "Batch_ConnexionFailureException.hxx"
00045 #include "Batch_APIInternalFailureException.hxx"
00046 #include "Batch_NotYetImplementedException.hxx"
00047 #include "Batch_BatchManager.hxx"
00048 
00049 namespace Batch {
00050 
00051   class FactBatchManager;
00052 
00053   class BatchManager_Local : public BatchManager
00054   {
00055   private:
00056     friend class ThreadAdapter;
00057     class ThreadAdapter{
00058     public:
00059       ThreadAdapter(BatchManager_Local & bm, const Job_Local & job);
00060       static void * run(void * arg);
00061       BatchManager_Local & getBatchManager() const { return _bm; };
00062 
00063     protected:
00064       BatchManager_Local & _bm;
00065       const Job_Local _job;
00066 
00067     private:
00068       void pere(pid_t child);
00069       void fils();
00070 
00071     };
00072 
00073     typedef int Id;
00074 
00075     enum Commande {
00076       NOP = 0,
00077       HOLD,
00078       RELEASE,
00079       TERM,
00080       KILL,
00081       ALTER,
00082     };
00083 
00084     enum Status {
00085       UNKNOWN = 0,
00086       RUNNING,
00087       STOPPED,
00088       DONE,
00089       DEAD,
00090     };
00091 
00092     struct Child {
00093       pthread_t thread_id;
00094       queue<Commande, deque<Commande> > command_queue;
00095       pid_t pid;
00096       int exit_code;
00097       Status status;
00098       Parametre param;
00099       Environnement env;
00100     };
00101 
00102 
00103 
00104   public:
00105     // Constructeur et destructeur
00106     BatchManager_Local(const FactBatchManager * parent, const char * host="localhost") throw(InvalidArgumentException,ConnexionFailureException); // connexion a la machine host
00107     virtual ~BatchManager_Local();
00108 
00109     // Recupere le nom du serveur par defaut
00110     // static string BatchManager_Local::getDefaultServer();
00111 
00112     // Methodes pour le controle des jobs
00113     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
00114     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
00115     virtual void holdJob(const JobId & jobid); // suspend un job en file d'attente
00116     virtual void releaseJob(const JobId & jobid); // relache un job suspendu
00117     virtual void alterJob(const JobId & jobid, const Parametre & param, const Environnement & env); // modifie un job en file d'attente
00118     virtual void alterJob(const JobId & jobid, const Parametre & param); // modifie un job en file d'attente
00119     virtual void alterJob(const JobId & jobid, const Environnement & env); // modifie un job en file d'attente
00120     virtual JobInfo queryJob(const JobId & jobid); // renvoie l'etat du job
00121     virtual bool isRunning(const JobId & jobid); // teste si un job est present en machine
00122 
00123     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
00124     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
00125 
00126 
00127   protected:
00128     int _connect; // Local connect id
00129     pthread_mutex_t _threads_mutex;
00130     map<Id, Child > _threads;
00131 
00132     // Methode abstraite qui renvoie la commande de copie du fichier source en destination
00133     virtual string copy_command(const string & host_source, const string & source, const string & host_destination, const string & destination) const = 0;
00134 
00135     // Methode abstraite qui renvoie la commande a executer
00136     virtual string exec_command(Parametre & param) const = 0;
00137 
00138     // Methode abstraite qui renvoie la commande d'effacement du fichier
00139     virtual string remove_command(const string & host_destination, const string & destination) const = 0;
00140 
00141   private:
00142     virtual pthread_t submit(const Job_Local & job);
00143     virtual void cancel(pthread_t thread_id);
00144     static  void kill_child_on_exit(void * p_pid);
00145     static  void delete_on_exit(void * arg);
00146     Id nextId(); // Retourne un identifiant unique pour un thread (clef de la map)
00147     Id getIdByThread_id(pthread_t thread_id);
00148     Id registerThread_id(pthread_t thread_id);
00149     pthread_mutex_t _thread_id_id_association_mutex;
00150     pthread_cond_t  _thread_id_id_association_cond;
00151     map<pthread_t, Id> _thread_id_id_association;
00152 
00153 #ifdef SWIG
00154   public:
00155     // Recupere le l'identifiant d'un job deja soumis au BatchManager
00156     //virtual const JobId getJobIdByReference(const string & ref) { return BatchManager::getJobIdByReference(ref); }
00157     virtual const JobId getJobIdByReference(const char * ref) { return BatchManager::getJobIdByReference(ref); }
00158 #endif
00159 
00160   };
00161 
00162 }
00163 
00164 #endif