Salome HOME
To avoid compilation pb on RedHat 8.0.
[modules/kernel.git] / src / Batch / Batch_BatchManager_Local.hxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
2 //           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 // 
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 // 
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 // 
19 /*
20  * BatchManager_Local.hxx : 
21  *
22  * Auteur : Ivan DUTKA-MALEN - EDF R&D
23  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
24  * Date   : Thu Nov  6 10:17:22 2003
25  * Projet : Salome 2
26  *
27  */
28
29 #ifndef _BATCHMANAGER_LOCAL_H_
30 #define _BATCHMANAGER_LOCAL_H_
31
32
33 #include <vector>
34 #include <map>
35 #include <queue>
36 #include <deque>
37 #include <pthread.h>
38 #include "Batch_Job.hxx"
39 #include "Batch_JobId.hxx"
40 #include "Batch_JobInfo.hxx"
41 #include "Batch_JobInfo_Local.hxx"
42 #include "Batch_Job_Local.hxx"
43 #include "Batch_InvalidArgumentException.hxx"
44 #include "Batch_ConnexionFailureException.hxx"
45 #include "Batch_APIInternalFailureException.hxx"
46 #include "Batch_NotYetImplementedException.hxx"
47 #include "Batch_BatchManager.hxx"
48
49 namespace Batch {
50
51   class FactBatchManager;
52
53   class BatchManager_Local : public BatchManager
54   {
55   private:
56     friend class ThreadAdapter;
57     class ThreadAdapter{
58     public:
59       ThreadAdapter(BatchManager_Local & bm, const Job_Local & job);
60       static void * run(void * arg);
61       BatchManager_Local & getBatchManager() const { return _bm; };
62
63     protected:
64       BatchManager_Local & _bm;
65       const Job_Local _job;
66
67     private:
68       void pere(pid_t child);
69       void fils();
70
71     };
72
73     typedef int Id;
74
75     enum Commande {
76       NOP = 0,
77       HOLD,
78       RELEASE,
79       TERM,
80       KILL,
81       ALTER,
82     };
83
84     enum Status {
85       UNKNOWN = 0,
86       RUNNING,
87       STOPPED,
88       DONE,
89       DEAD,
90     };
91
92     struct Child {
93       pthread_t thread_id;
94       queue<Commande, deque<Commande> > command_queue;
95       pid_t pid;
96       int exit_code;
97       Status status;
98       Parametre param;
99       Environnement env;
100     };
101
102
103
104   public:
105     // Constructeur et destructeur
106     BatchManager_Local(const FactBatchManager * parent, const char * host="localhost") throw(InvalidArgumentException,ConnexionFailureException); // connexion a la machine host
107     virtual ~BatchManager_Local();
108
109     // Recupere le nom du serveur par defaut
110     // static string BatchManager_Local::getDefaultServer();
111
112     // Methodes pour le controle des jobs
113     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
114     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
115     virtual void holdJob(const JobId & jobid); // suspend un job en file d'attente
116     virtual void releaseJob(const JobId & jobid); // relache un job suspendu
117     virtual void alterJob(const JobId & jobid, const Parametre & param, const Environnement & env); // modifie un job en file d'attente
118     virtual void alterJob(const JobId & jobid, const Parametre & param); // modifie un job en file d'attente
119     virtual void alterJob(const JobId & jobid, const Environnement & env); // modifie un job en file d'attente
120     virtual JobInfo queryJob(const JobId & jobid); // renvoie l'etat du job
121     virtual bool isRunning(const JobId & jobid); // teste si un job est present en machine
122
123     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
124     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
125
126
127   protected:
128     int _connect; // Local connect id
129     pthread_mutex_t _threads_mutex;
130     map<Id, Child > _threads;
131
132     // Methode abstraite qui renvoie la commande de copie du fichier source en destination
133     virtual string copy_command(const string & host_source, const string & source, const string & host_destination, const string & destination) const = 0;
134
135     // Methode abstraite qui renvoie la commande a executer
136     virtual string exec_command(Parametre & param) const = 0;
137
138     // Methode abstraite qui renvoie la commande d'effacement du fichier
139     virtual string remove_command(const string & host_destination, const string & destination) const = 0;
140
141   private:
142     virtual pthread_t submit(const Job_Local & job);
143     virtual void cancel(pthread_t thread_id);
144     static  void kill_child_on_exit(void * p_pid);
145     static  void delete_on_exit(void * arg);
146     Id nextId(); // Retourne un identifiant unique pour un thread (clef de la map)
147     Id getIdByThread_id(pthread_t thread_id);
148     Id registerThread_id(pthread_t thread_id);
149     pthread_mutex_t _thread_id_id_association_mutex;
150     pthread_cond_t  _thread_id_id_association_cond;
151     map<pthread_t, Id> _thread_id_id_association;
152
153 #ifdef SWIG
154   public:
155     // Recupere le l'identifiant d'un job deja soumis au BatchManager
156     //virtual const JobId getJobIdByReference(const string & ref) { return BatchManager::getJobIdByReference(ref); }
157     virtual const JobId getJobIdByReference(const char * ref) { return BatchManager::getJobIdByReference(ref); }
158 #endif
159
160   };
161
162 }
163
164 #endif