1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * Batch_CommunicationProtocol.cxx :
25 * Created on: 14 sept. 2009
26 * Author : Renaud BARATE - EDF R&D
33 #include <Batch_config.h>
35 #include "Batch_CommunicationProtocol.hxx"
37 #include "Batch_CommunicationProtocolRSH.hxx"
40 #include "Batch_CommunicationProtocolSH.hxx"
43 #include "Batch_CommunicationProtocolSSH.hxx"
45 #include "Batch_APIInternalFailureException.hxx"
46 #include "Batch_RunTimeException.hxx"
52 CommunicationProtocol::~CommunicationProtocol()
56 const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
58 if (protocolType == SH) {
60 static CommunicationProtocolSH instanceSH;
63 throw RunTimeException("Can't use SH protocol (SH tools were "
64 "not found on the system at compile time).");
66 } else if (protocolType == RSH) {
68 static CommunicationProtocolRSH instanceRSH;
71 throw RunTimeException("Can't use RSH protocol (RSH tools were "
72 "not found on the system at compile time).");
74 } else if (protocolType == SSH) {
76 static CommunicationProtocolSSH instanceSSH;
79 throw RunTimeException("Can't use SSH protocol (SSH tools were "
80 "not found on the system at compile time).");
83 throw APIInternalFailureException("Unknown communication protocol.");
86 string CommunicationProtocol::getExecCommand(const string & subCommand,
88 const string & user) const
90 return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
93 int CommunicationProtocol::copyFile(const std::string & sourcePath,
94 const std::string & sourceHost,
95 const std::string & sourceUser,
96 const std::string & destinationPath,
97 const std::string & destinationHost,
98 const std::string & destinationUser) const
100 string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
101 destinationPath, destinationHost,
103 cout << command.c_str() << endl;
104 int status = system(command.c_str());
108 string CommunicationProtocol::getRemoveSubCommand(const string & path) const
110 return string("rm ") + path;
113 string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
115 return string("mkdir -p ") + path;
118 int CommunicationProtocol::removeFile(const std::string & path,
119 const std::string & host,
120 const std::string & user) const
122 string command = getExecCommand(getRemoveSubCommand(path), host, user);
123 cout << command.c_str() << endl;
124 int status = system(command.c_str());
128 int CommunicationProtocol::makeDirectory(const std::string & path,
129 const std::string & host,
130 const std::string & user) const
132 string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
133 cout << command.c_str() << endl;
134 int status = system(command.c_str());
138 string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
142 // On Windows we surround the whole command with quotes to avoid problems when
143 // we have several quoted arguments.
148 for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
149 if (i != 0) commandStr += " ";
151 // if the argument contains spaces, we surround it with simple quotes (Linux)
152 // or double quotes (Windows)
153 if (commandArgs[i].find(' ') != string::npos) {
154 commandStr += string("\"") + commandArgs[i] + "\"";
156 commandStr += commandArgs[i];