Salome HOME
33f9dfa3a681715e42a56be89069aad6cd0b1ca6
[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 <vector>
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     friend class ThreadAdapter;
60     class ThreadAdapter{
61     public:
62       ThreadAdapter(BatchManager_Local & bm, const Job_Local & job);
63       static void * run(void * arg);
64       BatchManager_Local & getBatchManager() const { return _bm; };
65
66     protected:
67       BatchManager_Local & _bm;
68       const Job_Local _job;
69
70     private:
71       void pere(pid_t child);
72       void fils();
73
74     };
75
76     typedef int Id;
77
78     enum Commande {
79       NOP = 0,
80       HOLD,
81       RELEASE,
82       TERM,
83       KILL,
84       ALTER
85     };
86
87     enum Status {
88       UNKNOWN = 0,
89       RUNNING,
90       STOPPED,
91       DONE,
92       DEAD
93     };
94
95     struct Child {
96       pthread_t thread_id;
97       std::queue<Commande, std::deque<Commande> > command_queue;
98       pid_t pid;
99       int exit_code;
100       Status status;
101       Parametre param;
102       Environnement env;
103     };
104
105
106
107   public:
108     // Constructeur et destructeur
109     BatchManager_Local(const FactBatchManager * parent, const char * host="localhost") throw(InvalidArgumentException,ConnexionFailureException); // connexion a la machine host
110     virtual ~BatchManager_Local();
111
112     // Recupere le nom du serveur par defaut
113     // static string BatchManager_Local::getDefaultServer();
114
115     // Methodes pour le controle des jobs
116     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
117     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
118     virtual void holdJob(const JobId & jobid); // suspend un job en file d'attente
119     virtual void releaseJob(const JobId & jobid); // relache un job suspendu
120     virtual void alterJob(const JobId & jobid, const Parametre & param, const Environnement & env); // modifie un job en file d'attente
121     virtual void alterJob(const JobId & jobid, const Parametre & param); // modifie un job en file d'attente
122     virtual void alterJob(const JobId & jobid, const Environnement & env); // modifie un job en file d'attente
123     virtual JobInfo queryJob(const JobId & jobid); // renvoie l'etat du job
124     virtual bool isRunning(const JobId & jobid); // teste si un job est present en machine
125
126     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
127     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
128
129
130   protected:
131     int _connect; // Local connect id
132     pthread_mutex_t _threads_mutex;
133     std::map<Id, Child > _threads;
134
135     // Methode abstraite qui renvoie la commande de copie du fichier source en destination
136     virtual std::string copy_command( const std::string & host_source,
137                                       const std::string & source,
138                                       const std::string & host_destination,
139                                       const std::string & destination) const = 0;
140
141     // Methode abstraite qui renvoie la commande a executer
142     virtual std::string exec_command(Parametre & param) const = 0;
143
144     // Methode abstraite qui renvoie la commande d'effacement du fichier
145     virtual std::string remove_command( const std::string & host_destination,
146                                         const std::string & destination) const = 0;
147
148   private:
149     virtual pthread_t submit(const Job_Local & job);
150     virtual void cancel(pthread_t thread_id);
151     static  void kill_child_on_exit(void * p_pid);
152     static  void delete_on_exit(void * arg);
153     Id nextId(); // Retourne un identifiant unique pour un thread (clef de la map)
154     Id getIdByThread_id(pthread_t thread_id);
155     Id registerThread_id(pthread_t thread_id);
156     pthread_mutex_t _thread_id_id_association_mutex;
157     pthread_cond_t  _thread_id_id_association_cond;
158 #ifndef WIN32 //TODO: porting of following functionality
159     //reason: pthread_t on win32 is a struct of pointer and int members
160     std::map<pthread_t, Id> _thread_id_id_association;
161 #endif
162
163 #ifdef SWIG
164   public:
165     // Recupere le l'identifiant d'un job deja soumis au BatchManager
166     //virtual const JobId getJobIdByReference(const string & ref) { return BatchManager::getJobIdByReference(ref); }
167     virtual const JobId getJobIdByReference(const char * ref) { return BatchManager::getJobIdByReference(ref); }
168 #endif
169
170   };
171
172 }
173
174 #endif