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