Salome HOME
Added PBS test based on Torque.
[tools/libbatch.git] / src / Local / Batch_BatchManager_Local_RSH.cxx
1 //  Copyright (C) 2007-2008  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  * BatchManager_Local_RSH.cxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Thu Nov  6 10:17:22 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #include <iostream>
33 #include <fstream>
34 #include <sstream>
35 #include <cstdlib>
36 #include <sys/types.h>
37 #ifndef WIN32
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #endif
41 #include <ctime>
42 #include <pthread.h>
43 #include <signal.h>
44 #include <errno.h>
45 #include <string.h>
46
47 #include "Batch_IOMutex.hxx"
48 #include "Batch_BatchManager_Local_RSH.hxx"
49
50 #include "Batch_config.h"
51
52 #ifndef RCP
53 #error "RCP undefined. You must set RCP to a valid path to a rcp-like command."
54 #endif
55
56 #ifndef RSH
57 #error "RSH undefined. You must set RSH to a valid path to a rsh-like command."
58 #endif
59
60 using namespace std;
61
62 namespace Batch {
63
64
65   // Constructeur
66   BatchManager_Local_RSH::BatchManager_Local_RSH(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_Local(parent, host)
67   {
68   }
69
70   // Destructeur
71   BatchManager_Local_RSH::~BatchManager_Local_RSH()
72   {
73   }
74
75
76   // Methode abstraite qui renvoie la commande de copie du fichier source en destination
77   string BatchManager_Local_RSH::copy_command(const std::string & user_source,
78                                               const std::string & host_source,
79                                               const std::string & source,
80                                               const std::string & user_destination,
81                                               const std::string & host_destination,
82                                               const std::string & destination) const
83   {
84     ostringstream fullsource;
85     if (host_source.size() != 0) {
86       if (user_source.size() != 0) {
87 #ifdef WIN32
88         fullsource << host_source << "." << user_source << ":";
89 #else
90         fullsource << user_source << "@" << host_source << ":";
91 #endif
92       } else {
93         fullsource << host_source << ":";
94       }
95     }
96     fullsource << source;
97
98     ostringstream fulldestination;
99     if (host_destination.size() != 0) {
100       if (user_destination.size() != 0) {
101 #ifdef WIN32
102         fulldestination << host_destination << "." << user_destination << ":";
103 #else
104         fulldestination << user_destination << "@" << host_destination << ":";
105 #endif
106       } else {
107         fulldestination << host_destination << ":";
108       }
109     }
110     fulldestination << destination;
111
112     ostringstream copy_cmd;
113     copy_cmd << "\"" << RCP << "\" " << fullsource.str() << " " << fulldestination.str();
114     return copy_cmd.str();
115   }
116
117   // Methode abstraite qui renvoie la commande a executer
118   string BatchManager_Local_RSH::exec_command(Parametre & param) const
119   {
120     ostringstream exec_sub_cmd;
121     exec_sub_cmd << "cd " << param[WORKDIR] << " && " << param[EXECUTABLE];
122
123     if (param.find(ARGUMENTS) != param.end()) {
124       Versatile V = param[ARGUMENTS];
125       for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
126         StringType argt = * static_cast<StringType *>(*it);
127         string     arg  = argt;
128         exec_sub_cmd << " " << arg;
129       }
130     }
131
132     Versatile new_arguments;
133     new_arguments.setMaxSize(0);
134     new_arguments = string(param[EXECUTIONHOST]);
135
136
137     if (param.find(USER) != param.end()) {
138       new_arguments += "-l";
139       new_arguments += string(param[USER]);
140     }
141
142 #ifdef WIN32
143     new_arguments += "-n";
144 #endif
145
146     new_arguments += exec_sub_cmd.str();
147
148     param[ARGUMENTS] = new_arguments;
149
150     // Sous Linux on est oblige de modifier ces deux parametres pour faire fonctionner la commande rsh
151     param[EXECUTABLE] = RSH;
152     param.erase(NAME);
153
154     return RSH;
155   }
156
157   // Methode qui renvoie la commande d'effacement du fichier
158   string BatchManager_Local_RSH::remove_command(const std::string & user_destination,
159                                                 const std::string & host_destination,
160                                                 const std::string & destination) const
161   {
162     string fulldestination = (host_destination.size()) ? host_destination : "localhost";
163     if (user_destination.size() != 0) {
164       fulldestination += " -l " + user_destination;
165     }
166
167     // We consider here that the remote system is UNIX-like and has a "rm" command. Using the
168     // RM macro would be pointless here since the remote system is different from the local one.
169     ostringstream remove_cmd;
170     remove_cmd << "\"" << RSH << "\" " << fulldestination;
171 #ifdef WIN32
172     remove_cmd << " -n";
173 #endif
174     remove_cmd << " rm " << destination;
175     return remove_cmd.str();
176   }
177 }