Salome HOME
Refactored submission based on SH, RSH and SSH by grouping related commands in Commun...
[tools/libbatch.git] / src / Core / Batch_CommunicationProtocol.cxx
1 //  Copyright (C) 2007-2009  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  *  Batch_CommunicationProtocol.cxx :
24  *
25  *  Created on: 14 sept. 2009
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <iostream>
30
31 #include <Batch_config.h>
32
33 #include "Batch_CommunicationProtocol.hxx"
34 #include "Batch_CommunicationProtocolRSH.hxx"
35 #include "Batch_CommunicationProtocolSH.hxx"
36 #include "Batch_CommunicationProtocolSSH.hxx"
37 #include "Batch_APIInternalFailureException.hxx"
38 #include "Batch_RunTimeException.hxx"
39
40 using namespace std;
41
42 namespace Batch {
43
44   CommunicationProtocol::~CommunicationProtocol()
45   {
46   }
47
48   const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
49   {
50     if (protocolType == SH) {
51     #ifdef HAS_SH
52           static CommunicationProtocolSH instanceSH;
53           return instanceSH;
54     #else
55           throw RunTimeException("Can't use SH protocol (SH tools were "
56                                  "not found on the system at compile time).");
57     #endif
58     } else if (protocolType == RSH) {
59 #ifdef HAS_RSH
60       static CommunicationProtocolRSH instanceRSH;
61       return instanceRSH;
62 #else
63       throw RunTimeException("Can't use RSH protocol (RSH tools were "
64                              "not found on the system at compile time).");
65 #endif
66     } else if (protocolType == SSH) {
67 #ifdef HAS_SSH
68       static CommunicationProtocolSSH instanceSSH;
69       return instanceSSH;
70 #else
71       throw RunTimeException("Can't use SSH protocol (SSH tools were "
72                              "not found on the system at compile time).");
73 #endif
74     } else
75       throw APIInternalFailureException("Unknown communication protocol.");
76   }
77
78   string CommunicationProtocol::getExecCommand(const string & subCommand,
79                                                const string & host,
80                                                const string & user) const
81   {
82     return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
83   }
84
85   int CommunicationProtocol::copyFile(const std::string & sourcePath,
86                                       const std::string & sourceHost,
87                                       const std::string & sourceUser,
88                                       const std::string & destinationPath,
89                                       const std::string & destinationHost,
90                                       const std::string & destinationUser) const
91   {
92     string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
93                                                               destinationPath, destinationHost,
94                                                               destinationUser));
95     cout << command.c_str() << endl;
96     int status = system(command.c_str());
97     return status;
98   }
99
100   string CommunicationProtocol::getRemoveSubCommand(const string & path) const
101   {
102     return string("rm ") + path;
103   }
104
105   int CommunicationProtocol::removeFile(const std::string & path,
106                                         const std::string & host,
107                                         const std::string & user) const
108   {
109     string command = getExecCommand(getRemoveSubCommand(path), host, user);
110     cout << command.c_str() << endl;
111     int status = system(command.c_str());
112     return status;
113   }
114
115   string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
116   {
117     string commandStr;
118
119     // On Windows we surround the whole command with quotes to avoid problems when
120     // we have several quoted arguments.
121 #ifdef WIN32
122     commandStr += "\"";
123 #endif
124
125     for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
126       if (i != 0) commandStr += " ";
127
128       // if the argument contains spaces, we surround it with double quotes
129       if (commandArgs[i].find(' ') != string::npos) {
130         commandStr += string("\"") + commandArgs[i] + "\"";
131       } else {
132         commandStr += commandArgs[i];
133       }
134     }
135
136 #ifdef WIN32
137     commandStr += "\"";
138 #endif
139
140     return commandStr;
141   }
142
143 }