1 // Copyright (C) 2006 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : SALOME_FileTransferCORBA.cxx
22 // Author : Paul RASCLE, EDF
26 #include "SALOME_FileTransferCORBA.hxx"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "utilities.h"
33 //=============================================================================
35 * Default constructor, not for use.
37 //=============================================================================
39 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
44 //=============================================================================
46 * Constructor to use when we get a fileRef CORBA object from a component.
47 * \param aFileRef file reference CORBA object
49 //=============================================================================
51 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
54 MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
55 _theFileRef = aFileRef;
58 //=============================================================================
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
66 //=============================================================================
68 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(string refMachine,
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())
79 INFOS("bad parameters: machine and file name must be given");
83 //=============================================================================
87 //=============================================================================
89 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
91 MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
94 //=============================================================================
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
101 //=============================================================================
103 string SALOME_FileTransferCORBA::getLocalFile(string localFile)
105 MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
107 Engines::Container_var container;
109 if (CORBA::is_nil(_theFileRef))
111 if (_refMachine.empty() || _origFileName.empty())
113 INFOS("not enough parameters: machine and file name must be given");
117 SALOME_LifeCycleCORBA LCC;
118 Engines::ContainerManager_var contManager = LCC.getContainerManager();
120 Engines::MachineParameters params;
122 params.container_name = _containerName.c_str();
123 params.hostname = _refMachine.c_str();
125 Engines::MachineList_var listOfMachines =
126 contManager->GetFittingResources(params, "");
128 container = contManager->FindOrStartContainer(params,
130 if (CORBA::is_nil(container))
132 INFOS("machine " << _refMachine << " unreachable");
136 _theFileRef = container->createFileRef(_origFileName.c_str());
137 if (CORBA::is_nil(_theFileRef))
139 INFOS("imposssible to create fileRef on " << _refMachine);
144 container = _theFileRef->getContainer();
145 ASSERT(! CORBA::is_nil(container));
147 string myMachine = GetHostname();
148 string localCopy = _theFileRef->getRef(myMachine.c_str());
150 if (localCopy.empty()) // no existing copy available
152 if (localFile.empty()) // no name provided for local copy
155 localCopy = tmpnam(bufName);
161 if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
163 INFOS("file " << localFile << " cannot be open for writing");
167 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
168 ASSERT(! CORBA::is_nil(fileTransfer));
170 CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
173 Engines::fileBlock* aBlock;
180 aBlock = fileTransfer->getBlock(fileId);
181 toFollow = aBlock->length();
183 CORBA::Octet *buf = aBlock->get_buffer();
184 int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
185 ASSERT(nbWri == toFollow);
188 MESSAGE("end of transfer");
189 fileTransfer->close(fileId);
190 _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
191 localCopy = localFile;
195 INFOS("open reference file for copy impossible");