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