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