Salome HOME
Fix compilation with gcc 4.7
[tools/libbatch.git] / src / Core / Batch_BatchManager_eClient.cxx
index 81f03a34ed54cc3a00659ef7b3b2907c880c5229..835b5956f8b27e910a4c4ef452fe1e6be60b6ca4 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -29,6 +29,7 @@
 *
 */
 
+#include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -46,6 +47,7 @@
 
 #include <Batch_config.h>
 
+#include "Batch_Constants.hxx"
 #include "Batch_BatchManager_eClient.hxx"
 #include "Batch_RunTimeException.hxx"
 
@@ -61,8 +63,10 @@ using namespace std;
 namespace Batch {
 
   BatchManager_eClient::BatchManager_eClient(const Batch::FactBatchManager * parent, const char* host,
+                                             const char * username,
                                              CommunicationProtocolType protocolType, const char* mpiImpl)
-    : BatchManager(parent, host), _protocol(CommunicationProtocol::getInstance(protocolType)), _username("")
+    : BatchManager(parent, host), _protocol(CommunicationProtocol::getInstance(protocolType)),
+      _username(username)
   {
     // instanciation of mpi implementation needed to launch executable in batch script
     _mpiImpl = FactoryMpiImpl(mpiImpl);
@@ -79,14 +83,10 @@ namespace Batch {
   {
     int status;
     Parametre params = job.getParametre();
-    Versatile V = params[INFILE];
-    Versatile::iterator Vit;
-    _username = string(params[USER]);
-
-    string subCommand = string("mkdir -p ") + string(params[TMPDIR]) + string("/logs");
-    string command = _protocol.getExecCommand(subCommand, _hostname, _username);
-    cerr << command.c_str() << endl;
-    status = system(command.c_str());
+    const Versatile & V = params[INFILE];
+    Versatile::const_iterator Vit;
+
+    status = _protocol.makeDirectory(string(params[TMPDIR]) + "/logs", _hostname, _username);
     if(status) {
       std::ostringstream oss;
       oss << status;
@@ -112,9 +112,9 @@ namespace Batch {
 #ifdef WIN32
       // On Windows, we make the remote file executable afterward because
       // pscp does not preserve access permissions on files
-      subCommand = string("chmod u+x ") + string(params[TMPDIR]) + "/" +
-                   string(params[EXECUTABLE]);
-      command = _protocol.getExecCommand(subCommand, _hostname, _username);
+      string subCommand = string("chmod u+x ") + string(params[TMPDIR]) + "/" +
+                          string(params[EXECUTABLE]);
+      string command = _protocol.getExecCommand(subCommand, _hostname, _username);
       cerr << command.c_str() << endl;
       status = system(command.c_str());
       if(status) {
@@ -145,18 +145,27 @@ namespace Batch {
 
   }
 
-  void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
+  void BatchManager_eClient::importOutputFiles( const Job & job, const string directory )
   {
     Parametre params = job.getParametre();
-    Versatile V = params[OUTFILE];
-    Versatile::iterator Vit;
-    _username = string(params[USER]);
+    const Versatile & V = params[OUTFILE];
+    Versatile::const_iterator Vit;
+
+    // Create local result directory
+    int status = CommunicationProtocol::getInstance(SH).makeDirectory(directory, "", "");
+    if (status) {
+      string mess("Directory creation failed. Status is :");
+      ostringstream status_str;
+      status_str << status;
+      mess += status_str.str();
+      cerr << mess << endl;
+    }
 
     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
       Couple outputFile = cpt;
-      int status = _protocol.copyFile(outputFile.getRemote(), _hostname, _username,
-                                      directory, "", "");
+      status = _protocol.copyFile(outputFile.getRemote(), _hostname, _username,
+                                  directory, "", "");
       if (status) {
         // Try to get what we can (logs files)
         // throw BatchException("Error of connection on remote host");
@@ -169,8 +178,8 @@ namespace Batch {
     }
 
     // Copy logs
-    int status = _protocol.copyFile(string(params[TMPDIR]) + string("/logs"), _hostname, _username,
-                                   directory, "", "");
+    status = _protocol.copyFile(string(params[TMPDIR]) + string("/logs"), _hostname, _username,
+                                directory, "", "");
     if (status) {
       std::string mess("Copy logs directory failed ! status is :");
       ostringstream status_str;
@@ -181,7 +190,37 @@ namespace Batch {
 
   }
 
-  MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
+  bool BatchManager_eClient::importDumpStateFile( const Job & job, const string directory )
+  {
+    Parametre params = job.getParametre();
+
+    // Create local result directory
+    int status = CommunicationProtocol::getInstance(SH).makeDirectory(directory, "", "");
+    if (status) {
+      string mess("Directory creation failed. Status is :");
+      ostringstream status_str;
+      status_str << status;
+      mess += status_str.str();
+      cerr << mess << endl;
+    }
+
+    bool ret = true;
+    status = _protocol.copyFile(string(params[TMPDIR]) + string("/dumpState*.xml"), _hostname, _username,
+                                directory, "", "");
+    if (status) {
+      // Try to get what we can (logs files)
+      // throw BatchException("Error of connection on remote host");
+      std::string mess("Copy command failed ! status is :");
+      ostringstream status_str;
+      status_str << status;
+      mess += status_str.str();
+      cerr << mess << endl;
+      ret = false;
+    }
+    return ret;
+  }
+
+  MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl)
   {
     if(mpiImpl == "lam")
       return new MpiImpl_LAM();
@@ -243,7 +282,8 @@ namespace Batch {
 #ifdef WIN32
 
     string fileName = generateTemporaryFileName(prefix);
-    outputStream.open(fileName.c_str());
+    // Open the file as binary to avoid problems with Windows newlines
+    outputStream.open(fileName.c_str(), ios_base::binary | ios_base::out);
 
 #else
 
@@ -285,13 +325,13 @@ namespace Batch {
   const std::string & BatchManager_eClient::getTmpDir()
   {
     if (tmpDirName.empty()) {
-      char * baseDir = getenv("TEMP");
+      const char * baseDir = getenv("TEMP");
       if (baseDir == NULL) baseDir = getenv("TMP");
       if (baseDir == NULL) baseDir = getenv("TEMPDIR");
       if (baseDir == NULL) baseDir = getenv("TMPDIR");
       if (baseDir == NULL) baseDir = "/tmp";
 
-      char * userName = getenv("USER");
+      const char * userName = getenv("USER");
       if (userName == NULL) userName = getenv("USERNAME");
       if (userName == NULL) userName = "unknown";