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