Salome HOME
Added support for Microsoft Visual C++ (also experimental)
[tools/libbatch.git] / src / Local / 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  */
31
32 #ifndef _BATCHMANAGER_LOCAL_H_
33 #define _BATCHMANAGER_LOCAL_H_
34
35 #include "Batch_Defines.hxx"
36
37 #ifdef WIN32
38 #include <Windows.h>
39 #endif
40
41 #include <list>
42 #include <map>
43 #include <queue>
44 #include <pthread.h>
45 #include "Batch_Job.hxx"
46 #include "Batch_JobId.hxx"
47 #include "Batch_JobInfo.hxx"
48 #include "Batch_JobInfo_Local.hxx"
49 #include "Batch_Job_Local.hxx"
50 #include "Batch_InvalidArgumentException.hxx"
51 #include "Batch_ConnexionFailureException.hxx"
52 #include "Batch_APIInternalFailureException.hxx"
53 #include "Batch_NotYetImplementedException.hxx"
54 #include "Batch_BatchManager.hxx"
55
56 namespace Batch {
57
58   class FactBatchManager;
59
60   class BATCH_EXPORT BatchManager_Local : public BatchManager
61   {
62   private:
63 #ifdef WIN32
64     typedef HANDLE Process;
65 #else
66     typedef pid_t Process;
67 #endif
68     friend class ThreadAdapter;
69     class ThreadAdapter{
70     public:
71       ThreadAdapter(BatchManager_Local & bm, const Job_Local & job);
72       static void * run(void * arg);
73       BatchManager_Local & getBatchManager() const { return _bm; };
74
75     protected:
76       BatchManager_Local & _bm;
77       const Job_Local _job;
78
79     private:
80       void pere(Process child);
81 #ifndef WIN32
82       void fils();
83 #else
84       Process launchWin32ChildProcess();
85 #endif
86
87     };
88
89     typedef int Id;
90
91     enum Commande {
92       NOP = 0,
93       HOLD,
94       RELEASE,
95       TERM,
96       KILL,
97       ALTER
98     };
99
100     enum Status {
101       UNKNOWN = 0,
102       RUNNING,
103       STOPPED,
104       DONE,
105       DEAD
106     };
107
108     struct Child {
109       pthread_t thread_id;
110       std::queue<Commande, std::deque<Commande> > command_queue;
111       pid_t pid;
112       int exit_code;
113       Status status;
114       Parametre param;
115       Environnement env;
116     };
117
118
119
120   public:
121     // Constructeur et destructeur
122     BatchManager_Local(const FactBatchManager * parent, const char * host="localhost") throw(InvalidArgumentException,ConnexionFailureException); // connexion a la machine host
123     virtual ~BatchManager_Local();
124
125     // Recupere le nom du serveur par defaut
126     // static string BatchManager_Local::getDefaultServer();
127
128     // Methodes pour le controle des jobs
129     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
130     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
131     virtual void holdJob(const JobId & jobid); // suspend un job en file d'attente
132     virtual void releaseJob(const JobId & jobid); // relache un job suspendu
133     virtual void alterJob(const JobId & jobid, const Parametre & param, const Environnement & env); // modifie un job en file d'attente
134     virtual void alterJob(const JobId & jobid, const Parametre & param); // modifie un job en file d'attente
135     virtual void alterJob(const JobId & jobid, const Environnement & env); // modifie un job en file d'attente
136     virtual JobInfo queryJob(const JobId & jobid); // renvoie l'etat du job
137     virtual bool isRunning(const JobId & jobid); // teste si un job est present en machine
138
139     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
140     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
141
142
143   protected:
144     int _connect; // Local connect id
145     pthread_mutex_t _threads_mutex;
146     std::map<Id, Child > _threads;
147
148     // Methode abstraite qui renvoie la commande de copie du fichier source en destination
149     virtual std::string copy_command( const std::string & user_source,
150                                       const std::string & host_source,
151                                       const std::string & source,
152                                       const std::string & user_destination,
153                                       const std::string & host_destination,
154                                       const std::string & destination) const = 0;
155
156     // Methode abstraite qui renvoie la commande a executer
157     virtual std::string exec_command(Parametre & param) const = 0;
158
159     // Methode abstraite qui renvoie la commande d'effacement du fichier
160     virtual std::string remove_command( const std::string & user_destination,
161                                         const std::string & host_destination,
162                                         const std::string & destination) const = 0;
163
164   private:
165     struct ThreadIdIdAssociation {
166       pthread_t threadId;
167       Id id;
168     };
169
170     virtual pthread_t submit(const Job_Local & job);
171     virtual void cancel(pthread_t thread_id);
172     static  void kill_child_on_exit(void * p_pid);
173     static  void delete_on_exit(void * arg);
174     Id nextId(); // Retourne un identifiant unique pour un thread (clef de la map)
175     Id getIdByThread_id(pthread_t thread_id);
176     Id registerThread_id(pthread_t thread_id);
177     pthread_mutex_t _thread_id_id_association_mutex;
178     pthread_cond_t  _thread_id_id_association_cond;
179     std::list<struct ThreadIdIdAssociation> _thread_id_id_association;
180
181 #ifdef SWIG
182   public:
183     // Recupere le l'identifiant d'un job deja soumis au BatchManager
184     //virtual const JobId getJobIdByReference(const string & ref) { return BatchManager::getJobIdByReference(ref); }
185     virtual const JobId getJobIdByReference(const char * ref) { return BatchManager::getJobIdByReference(ref); }
186 #endif
187
188   };
189
190 }
191
192 #endif