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();
119 Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
121 Engines::MachineParameters params;
123 params.container_name = _containerName.c_str();
124 params.hostname = _refMachine.c_str();
126 Engines::CompoList clist;
127 Engines::MachineList_var listOfMachines =
128 resManager->GetFittingResources(params, clist);
130 container = contManager->FindOrStartContainer(params,
132 if (CORBA::is_nil(container))
134 INFOS("machine " << _refMachine << " unreachable");
138 _theFileRef = container->createFileRef(_origFileName.c_str());
139 if (CORBA::is_nil(_theFileRef))
141 INFOS("imposssible to create fileRef on " << _refMachine);
146 container = _theFileRef->getContainer();
147 ASSERT(! CORBA::is_nil(container));
149 string myMachine = GetHostname();
150 string localCopy = _theFileRef->getRef(myMachine.c_str());
152 if (localCopy.empty()) // no existing copy available
154 if (localFile.empty()) // no name provided for local copy
157 localCopy = tmpnam(bufName);
163 if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
165 INFOS("file " << localFile << " cannot be open for writing");
169 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
170 ASSERT(! CORBA::is_nil(fileTransfer));
172 CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
175 Engines::fileBlock* aBlock;
182 aBlock = fileTransfer->getBlock(fileId);
183 toFollow = aBlock->length();
185 CORBA::Octet *buf = aBlock->get_buffer();
186 int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
187 ASSERT(nbWri == toFollow);
190 MESSAGE("end of transfer");
191 fileTransfer->close(fileId);
192 _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
193 localCopy = localFile;
197 INFOS("open reference file for copy impossible");