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