Salome HOME
Changed year in copyrights
[tools/libbatch.git] / src / PBS / Batch_BatchManager_PBS.cxx
1 //  Copyright (C) 2007-2011  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_PBS.cxx : 
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 extern "C" {
33 #include <pbs_error.h>
34 #include <pbs_ifl.h>
35 }
36 #include <cstdlib>
37 #include <iostream>
38 #include <fstream>
39 #include <sstream>
40 //#include "MEDMEM_STRING.hxx"
41 #include "Batch_BatchManager_PBS.hxx"
42 using namespace std;
43
44 namespace Batch {
45
46   // Recupere le nom du serveur par defaut
47 //   string BatchManager_PBS::getDefaultServer() {
48 //     string server_name = "localhost";
49
50 //     const char * server_name_path = "@openpbsspooldir@/server_name";
51 //     ifstream server_name_file(server_name_path);
52 //     if (server_name_file) {
53 //       server_name_file >> server_name;
54 //       server_name_file.close();
55 //     }
56
57 //     return server_name;
58 //   }
59
60   // Constructeur
61 //   BatchManager_PBS::BatchManager_PBS() throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(BatchManager_PBS::getDefaultServer())
62 //   {
63 //     // On se connecte au serveur PBS
64 //     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
65 //     if (_connect < 0) { // si erreur
66 //       char * errmsg = pbs_geterrmsg(_connect);
67 //       string msg = "PBS Server on host \"";
68 //       msg += _hostname;
69 //       msg += "\" : ";
70 //       msg += errmsg ? errmsg : "Reason unknown";
71 //       throw ConnexionFailureException(msg.c_str());
72 //     }
73 //   }
74
75   // Constructeur
76 //   BatchManager_PBS::BatchManager_PBS(string host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(host)
77 //   {
78 //     // On se connecte au serveur PBS
79 //     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
80 //     if (_connect < 0) { // si erreur
81 //       char * errmsg = pbs_geterrmsg(_connect);
82 //       string msg = "PBS Server on host \"";
83 //       msg += _hostname;
84 //       msg += "\" : ";
85 //       msg += errmsg ? errmsg : "Reason unknown";
86 //       throw ConnexionFailureException(msg.c_str());
87 //     }
88 //   }
89   BatchManager_PBS::BatchManager_PBS(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(parent, host)
90   {
91     // On se connecte au serveur PBS
92     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
93     if (_connect < 0) { // si erreur
94       throw ConnexionFailureException(getErrorMessage("connect").c_str());
95     }
96   }
97
98   // Destructeur
99   BatchManager_PBS::~BatchManager_PBS()
100   {
101     // On se deconnecte du serveur PBS
102     int rc = pbs_disconnect(_connect);
103     if (rc < 0) { // si erreur
104       throw ConnexionFailureException(getErrorMessage("disconnect").c_str());
105     }
106   }
107
108   string BatchManager_PBS::getErrorMessage(const char * operation) const
109   {
110     char * msg = pbs_geterrmsg(_connect);
111     stringstream sstr;
112     sstr << "PBS " << operation << " error (host \"" << _hostname << "\"): ";
113     if (msg != NULL) {
114       sstr << msg;
115     } else {
116       sstr << "code = " << pbs_errno << " (" << pbse_to_txt(pbs_errno) << ")";
117     }
118     return sstr.str();
119   }
120
121   // Methode pour le controle des jobs : soumet un job au gestionnaire
122   const JobId BatchManager_PBS::submitJob(const Job & job)
123   {
124     Job_PBS jobpbs = job;
125     char * ref = pbs_submit(_connect,
126                             jobpbs.getAttributesOP(),
127                             jobpbs.getScript(),
128                             jobpbs.getDestination(),
129                             NULL);
130     if (!ref) { // si erreur
131       throw APIInternalFailureException(getErrorMessage("submit").c_str());
132     }
133
134     JobId id(this, string(ref));
135     free(ref);
136     return id;
137   }
138
139   // Ce manager permet de faire de la reprise
140   const Batch::JobId
141   BatchManager_PBS::addJob(const Batch::Job & job, const std::string reference)
142   {
143     return JobId(this, reference);
144   }
145
146   // Methode pour le controle des jobs : retire un job du gestionnaire
147   void BatchManager_PBS::deleteJob(const JobId & jobid)
148   {
149     char * ref = const_cast< char * >(jobid.getReference().c_str());
150     int rc = pbs_deljob(_connect, ref, 0);
151     if (rc) { // si erreur
152       throw APIInternalFailureException(getErrorMessage("deljob").c_str());
153     }
154   }
155    
156   // Methode pour le controle des jobs : suspend un job en file d'attente
157   void BatchManager_PBS::holdJob(const JobId & jobid)
158   {
159     char * ref = const_cast< char * >(jobid.getReference().c_str());
160     int rc = pbs_holdjob(_connect, ref, const_cast< char * >(USER_HOLD), 0);
161     if (rc) { // si erreur
162       throw APIInternalFailureException(getErrorMessage("holdjob").c_str());
163     }
164   }
165
166   // Methode pour le controle des jobs : relache un job suspendu
167   void BatchManager_PBS::releaseJob(const JobId & jobid)
168   {
169     char * ref = const_cast< char * >(jobid.getReference().c_str());
170     int rc = pbs_rlsjob(_connect, ref, const_cast< char * >(USER_HOLD), 0);
171     if (rc) { // si erreur
172       throw APIInternalFailureException(getErrorMessage("rlsjob").c_str());
173     }
174   }
175
176
177   // Methode pour le controle des jobs : modifie un job en file d'attente
178   void BatchManager_PBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
179   {
180     Job job(param, env);
181     Job_PBS jobpbs(job);
182
183     char * ref = const_cast< char * >(jobid.getReference().c_str());
184     int rc = pbs_alterjob(_connect,
185                           ref,
186                           jobpbs.getAttributes(),
187                           NULL);
188     if (rc) { // si erreur
189       throw APIInternalFailureException(getErrorMessage("alterjob").c_str());
190     }
191                 
192   }
193
194   // Methode pour le controle des jobs : modifie un job en file d'attente
195   void BatchManager_PBS::alterJob(const JobId & jobid, const Parametre & param)
196   {
197     alterJob(jobid, param, Environnement());
198   }
199
200   // Methode pour le controle des jobs : modifie un job en file d'attente
201   void BatchManager_PBS::alterJob(const JobId & jobid, const Environnement & env)
202   {
203     alterJob(jobid, Parametre(), env);
204   }
205
206
207
208   // Methode pour le controle des jobs : renvoie l'etat du job
209   JobInfo BatchManager_PBS::queryJob(const JobId & jobid)
210   {
211     char * id = const_cast< char * >(jobid.getReference().c_str());
212     JobInfo_PBS ji = JobInfo_PBS(pbs_statjob(_connect, id, 0, 0), true);
213     return ji;
214   }
215
216
217
218 }