Salome HOME
857c07a38c8648d3142f715fc9bf2d27b5358fa7
[tools/libbatch.git] / src / Core / CommunicationProtocol.cxx
1 //  Copyright (C) 2007-2013  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  *  CommunicationProtocol.cxx :
24  *
25  *  Created on: 14 sept. 2009
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <stdlib.h>
30
31 #include <libbatch_config.h>
32
33 #include "CommunicationProtocol.hxx"
34 #ifdef HAS_RSH
35  #include "CommunicationProtocolRSH.hxx"
36 #endif
37 #ifdef HAS_SH
38  #include "CommunicationProtocolSH.hxx"
39 #endif
40 #ifdef HAS_SSH
41  #include "CommunicationProtocolSSH.hxx"
42 #endif
43 #include "APIInternalFailureException.hxx"
44 #include "RunTimeException.hxx"
45 #include "Log.hxx"
46
47 using namespace std;
48
49 namespace Batch {
50
51   CommunicationProtocol::CommunicationProtocol(CommunicationProtocolType type)
52   : _type(type)
53   {
54   }
55
56   CommunicationProtocol::~CommunicationProtocol()
57   {
58   }
59
60   const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
61   {
62     if (protocolType == SH) {
63 #ifdef HAS_SH
64       static CommunicationProtocolSH instanceSH;
65       return instanceSH;
66 #else
67       throw RunTimeException("Can't use SH protocol (SH tools were "
68                              "not found on the system at compile time).");
69 #endif
70     } else if (protocolType == RSH) {
71 #ifdef HAS_RSH
72       static CommunicationProtocolRSH instanceRSH;
73       return instanceRSH;
74 #else
75       throw RunTimeException("Can't use RSH protocol (RSH tools were "
76                              "not found on the system at compile time).");
77 #endif
78     } else if (protocolType == SSH) {
79 #ifdef HAS_SSH
80       static CommunicationProtocolSSH instanceSSH;
81       return instanceSSH;
82 #else
83       throw RunTimeException("Can't use SSH protocol (SSH tools were "
84                              "not found on the system at compile time).");
85 #endif
86     } else
87       throw APIInternalFailureException("Unknown communication protocol.");
88   }
89
90   string CommunicationProtocol::getExecCommand(const string & subCommand,
91                                                const string & host,
92                                                const string & user) const
93   {
94     return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
95   }
96
97   int CommunicationProtocol::copyFile(const std::string & sourcePath,
98                                       const std::string & sourceHost,
99                                       const std::string & sourceUser,
100                                       const std::string & destinationPath,
101                                       const std::string & destinationHost,
102                                       const std::string & destinationUser) const
103   {
104     string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
105                                                               destinationPath, destinationHost,
106                                                               destinationUser));
107     LOG(command);
108     int status = system(command.c_str());
109     return status;
110   }
111
112   string CommunicationProtocol::getRemoveSubCommand(const string & path) const
113   {
114     return string("rm ") + path;
115   }
116
117   string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
118   {
119     return string("mkdir -p ") + path;
120   }
121
122   int CommunicationProtocol::removeFile(const std::string & path,
123                                         const std::string & host,
124                                         const std::string & user) const
125   {
126     string command = getExecCommand(getRemoveSubCommand(path), host, user);
127     LOG(command);
128     int status = system(command.c_str());
129     return status;
130   }
131
132   int CommunicationProtocol::makeDirectory(const std::string & path,
133                                            const std::string & host,
134                                            const std::string & user) const
135   {
136     string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
137     LOG(command);
138     int status = system(command.c_str());
139     return status;
140   }
141
142   string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
143   {
144     string commandStr;
145
146     // On Windows we surround the whole command with quotes to avoid problems when
147     // we have several quoted arguments.
148 #ifdef WIN32
149     commandStr += "\"";
150 #endif
151
152     for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
153       if (i != 0) commandStr += " ";
154
155       // if the argument contains spaces, we surround it with simple quotes (Linux)
156       // or double quotes (Windows)
157       if (commandArgs[i].find(' ') != string::npos) {
158         commandStr += string("\"") + commandArgs[i] + "\"";
159       } else {
160         commandStr += commandArgs[i];
161       }
162     }
163
164 #ifdef WIN32
165     commandStr += "\"";
166 #endif
167
168     return commandStr;
169   }
170
171   CommunicationProtocolType CommunicationProtocol::getType() const
172   {
173     return _type;
174   }
175
176 }