Salome HOME
Fix compilation problems on Win32 platform
[modules/kernel.git] / src / Batch / Batch_BatchManager_eClient.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21 * BatchManager_eLSF.cxx : emulation of LSF client
22 *
23 * Auteur : Bernard SECHER - CEA DEN
24 * Mail   : mailto:bernard.secher@cea.fr
25 * Date   : Thu Apr 24 10:17:22 2008
26 * Projet : PAL Salome 
27 *
28 */
29
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include "Batch_BatchManager_eClient.hxx"
35 #include "OpUtil.hxx"
36
37 namespace Batch {
38
39   BatchManager_eClient::BatchManager_eClient(const Batch::FactBatchManager * parent, const char* host, const char* protocol, const char* mpiImpl) : BatchManager(parent, host), _protocol(protocol), _username("")
40   {
41     // instanciation of mpi implementation needed to launch executable in batch script
42     _mpiImpl = FactoryMpiImpl(mpiImpl);
43   }
44
45   // Destructeur
46   BatchManager_eClient::~BatchManager_eClient()
47   {
48     // Nothing to do
49     delete _mpiImpl;
50   }
51
52   void BatchManager_eClient::exportInputFiles(const Job& job) throw(EmulationException)
53   {
54     int status;
55     Parametre params = job.getParametre();
56     Versatile V = params[INFILE];
57     Versatile::iterator Vit;
58     string command;
59     string copy_command;
60     _username = string(params[USER]);
61
62     // Test protocol
63     if( _protocol == "rsh" )
64       copy_command = "rcp ";
65     else if( _protocol == "ssh" )
66       copy_command = "scp ";
67     else
68       throw EmulationException("Unknown protocol : only rsh and ssh are known !");
69
70     // First step : creating batch tmp files directory
71     command = _protocol;
72     command += " ";
73     if(_username != ""){
74       command += _username;
75       command += "@";
76     }
77     command += _hostname;
78     command += " \"mkdir -p ";
79     command += string(params[TMPDIR]);
80     command += "\"" ;
81     cerr << command.c_str() << endl;
82     status = system(command.c_str());
83     if(status) {
84       std::ostringstream oss;
85       oss << status;
86       std::string ex_mess("Error of connection on remote host ! status = ");
87       ex_mess += oss.str();
88       throw EmulationException(ex_mess.c_str());
89     }
90
91     // Second step : copy fileToExecute into
92     // batch tmp files directory
93     command = copy_command;
94     command += string(params[EXECUTABLE]);
95     command += " ";
96     if(_username != ""){
97       command += _username;
98       command += "@";
99     }
100     command += _hostname;
101     command += ":";
102     command += string(params[TMPDIR]);
103     cerr << command.c_str() << endl;
104     status = system(command.c_str());
105     if(status) {
106       std::ostringstream oss;
107       oss << status;
108       std::string ex_mess("Error of connection on remote host ! status = ");
109       ex_mess += oss.str();
110       throw EmulationException(ex_mess.c_str());
111     }
112
113     // Third step : copy filesToExportList into
114     // batch tmp files directory
115     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
116       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
117       Couple inputFile = cpt;
118       command = copy_command;
119       command += inputFile.getLocal();
120       command += " ";
121       if(_username != ""){
122         command += _username;
123         command += "@";
124       }
125       command += _hostname;
126       command += ":";
127       command += inputFile.getRemote();
128       cerr << command.c_str() << endl;
129       status = system(command.c_str());
130       if(status) {
131         std::ostringstream oss;
132         oss << status;
133         std::string ex_mess("Error of connection on remote host ! status = ");
134         ex_mess += oss.str();
135         throw EmulationException(ex_mess.c_str());
136       }
137     }
138
139   }
140
141   void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
142   {
143     string command;
144     int status;
145
146     Parametre params = job.getParametre();
147     Versatile V = params[OUTFILE];
148     Versatile::iterator Vit;
149
150     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
151       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
152       Couple outputFile = cpt;
153       if( _protocol == "rsh" )
154         command = "rcp ";
155       else if( _protocol == "ssh" )
156         command = "scp ";
157       else
158         throw EmulationException("Unknown protocol");
159
160       if (_username != ""){
161         command += _username;
162         command += "@";
163       }
164       command += _hostname;
165       command += ":";
166       command += outputFile.getRemote();
167       command += " ";
168       command += directory;
169       cerr << command.c_str() << endl;
170       status = system(command.c_str());
171       if(status) 
172       {
173         // Try to get what we can (logs files)
174         // throw BatchException("Error of connection on remote host");    
175         std::string mess("Copy command failed ! status is :");
176         ostringstream status_str;
177         status_str << status;
178         mess += status_str.str();
179         cerr << mess << endl;
180       }
181     }
182
183   }
184
185   MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
186   {
187     if(mpiImpl == "lam")
188       return new MpiImpl_LAM();
189     else if(mpiImpl == "mpich1")
190       return new MpiImpl_MPICH1();
191     else if(mpiImpl == "mpich2")
192       return new MpiImpl_MPICH2();
193     else if(mpiImpl == "openmpi")
194       return new MpiImpl_OPENMPI();
195     else if(mpiImpl == "slurm")
196       return new MpiImpl_SLURM();
197     else{
198       ostringstream oss;
199       oss << mpiImpl << " : not yet implemented";
200       throw EmulationException(oss.str().c_str());
201     }
202   }
203
204   string BatchManager_eClient::BuildTemporaryFileName() const
205   {
206     //build more complex file name to support multiple salome session
207     string aFileName = OpUtil_Dir::GetTmpFileName();
208 #ifndef WIN32
209     aFileName += ".sh";
210 #else
211     aFileName += ".bat";
212 #endif
213     return aFileName;
214   }
215
216   void BatchManager_eClient::RmTmpFile(std::string & TemporaryFileName)
217   {
218 #ifdef WIN32
219     string command = "del /F ";
220 #else
221     string command = "rm ";
222 #endif
223     command += TemporaryFileName;
224     char *temp = strdup(command.c_str());
225     int lgthTemp = strlen(temp);
226     temp[lgthTemp - 3] = '*';
227     temp[lgthTemp - 2] = '\0';
228     system(temp);
229     free(temp);
230   }
231 }