Salome HOME
To avoid compilation pb on RedHat 8.0.
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_FileTransferCORBA.cxx
1 // Copyright (C) 2006  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 //  This library is free software; you can redistribute it and/or 
4 //  modify it under the terms of the GNU Lesser General Public 
5 //  License as published by the Free Software Foundation; either 
6 //  version 2.1 of the License. 
7 // 
8 //  This library is distributed in the hope that it will be useful, 
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 //  Lesser General Public License for more details. 
12 // 
13 //  You should have received a copy of the GNU Lesser General Public 
14 //  License along with this library; if not, write to the Free Software 
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
16 // 
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //
20 //
21 //  File   : SALOME_FileTransferCORBA.cxx
22 //  Author : Paul RASCLE, EDF
23 //  Module : SALOME
24 //  $Header$
25
26 #include "SALOME_FileTransferCORBA.hxx"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "utilities.h"
29 #include "OpUtil.hxx"
30
31 using namespace std;
32
33 //=============================================================================
34 /*! 
35  *  Default constructor, not for use.
36  */
37 //=============================================================================
38
39 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
40 {
41   ASSERT(0);
42 }
43
44 //=============================================================================
45 /*! 
46  *  Constructor to use when we get a fileRef CORBA object from a component.
47  *  \param aFileRef file reference CORBA object
48  */
49 //=============================================================================
50
51 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
52                                                    aFileRef)
53 {
54   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
55   _theFileRef = aFileRef;
56 }
57
58 //=============================================================================
59 /*! 
60  *  Constructor to use when we have the file name and machine from which to
61  *  copy, plus an optional Container name on the machine.
62  *  \param refMachine    the machine on which is the file to transfer
63  *  \param origFileName  abolute file path on refMachine
64  *  \param containerName default container name used (FactoryServer) if empty
65  */
66 //=============================================================================
67
68 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(string refMachine,
69                                                    string origFileName,
70                                                    string containerName)
71 {
72   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA"
73           << refMachine << " " << origFileName  << " " << containerName);
74   _refMachine = refMachine;
75   _origFileName = origFileName;
76   _containerName = containerName;
77   if (_refMachine.empty() || _origFileName.empty())
78     {
79       INFOS("bad parameters: machine and file name must be given");
80     } 
81 }
82
83 //=============================================================================
84 /*! 
85  *  Destructor
86  */
87 //=============================================================================
88
89 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
90 {
91   MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
92 }
93
94 //=============================================================================
95 /*! 
96  *  CORBA method: get a local copy of the reference file.
97  *  \param  localFile optional absolute path to store the copy
98  *  \return the file name (absolute path) of the copy, may be different from
99  *          localFile parameter if the copy was already done before the call
100  */
101 //=============================================================================
102
103 string SALOME_FileTransferCORBA::getLocalFile(string localFile)
104 {
105   MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
106
107   Engines::Container_var container;
108
109   if (CORBA::is_nil(_theFileRef))
110     {
111       if (_refMachine.empty() || _origFileName.empty())
112         {
113           INFOS("not enough parameters: machine and file name must be given");
114           return "";
115         }
116
117       SALOME_LifeCycleCORBA LCC;
118       Engines::ContainerManager_var contManager = LCC.getContainerManager();
119
120       Engines::MachineParameters params;
121       LCC.preSet(params);
122       params.container_name = _containerName.c_str();
123       params.hostname = _refMachine.c_str();
124
125       Engines::MachineList_var listOfMachines =
126         contManager->GetFittingResources(params, "");
127
128       container = contManager->FindOrStartContainer(params,
129                                                     listOfMachines);
130       if (CORBA::is_nil(container))
131         {
132           INFOS("machine " << _refMachine << " unreachable");
133           return "";
134         }
135
136       _theFileRef = container->createFileRef(_origFileName.c_str());
137       if (CORBA::is_nil(_theFileRef))
138         {
139           INFOS("imposssible to create fileRef on " << _refMachine);
140           return "";
141         }
142     }
143
144   container = _theFileRef->getContainer();
145   ASSERT(! CORBA::is_nil(container));
146
147   string myMachine = GetHostname();
148   string localCopy = _theFileRef->getRef(myMachine.c_str());
149
150   if (localCopy.empty()) // no existing copy available
151     {
152       if (localFile.empty()) // no name provided for local copy
153         {
154           char bufName[256];
155           localCopy = tmpnam(bufName);
156           localFile = bufName;
157           SCRUTE(localFile);
158         }
159
160       FILE* fp;
161       if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
162         {
163           INFOS("file " << localFile << " cannot be open for writing");
164           return "";
165         }
166
167       Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
168       ASSERT(! CORBA::is_nil(fileTransfer));
169
170       CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
171       if (fileId > 0)
172         {
173           Engines::fileBlock* aBlock;
174           int toFollow = 1;
175           int ctr=0;
176           while (toFollow)
177             {
178               ctr++;
179               SCRUTE(ctr);
180               aBlock = fileTransfer->getBlock(fileId);
181               toFollow = aBlock->length();
182               SCRUTE(toFollow);
183               CORBA::Octet *buf = aBlock->get_buffer();
184               int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
185               ASSERT(nbWri == toFollow);
186             }
187           fclose(fp);
188           MESSAGE("end of transfer");
189           fileTransfer->close(fileId);
190           _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
191           localCopy = localFile;
192         }
193       else
194         {
195           INFOS("open reference file for copy impossible");
196           return "";
197         }
198       
199     }
200   SCRUTE(localCopy);
201   return localCopy;
202 }