1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOME_FileTransferCORBA.cxx
23 // Author : Paul RASCLE, EDF
27 #include "SALOME_FileTransferCORBA.hxx"
28 #include "SALOME_LifeCycleCORBA.hxx"
29 #include "utilities.h"
30 #include "Basics_Utils.hxx"
34 /*! \class SALOME_FileTransferCORBA
35 \brief A class to manage file transfer in SALOME (CORBA context)
39 //=============================================================================
41 * Default constructor, not for use.
43 //=============================================================================
45 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
50 //=============================================================================
51 /*! \brief Constructor to use when we get a fileRef CORBA object from a component
53 * \param aFileRef file reference CORBA object
55 //=============================================================================
57 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
60 MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
61 _theFileRef = aFileRef;
64 //=============================================================================
65 /*! \brief Constructor to use when we have the file name and machine from which to
66 * copy, plus an optional Container name on the machine.
68 * \param refMachine the machine on which is the file to transfer
69 * \param origFileName abolute file path on refMachine
70 * \param containerName default container name used (FactoryServer) if empty
72 //=============================================================================
74 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(string refMachine,
78 MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA"
79 << refMachine << " " << origFileName << " " << containerName);
80 _refMachine = refMachine;
81 _origFileName = origFileName;
82 _containerName = containerName;
83 if (_refMachine.empty() || _origFileName.empty())
85 INFOS("bad parameters: machine and file name must be given");
89 //=============================================================================
93 //=============================================================================
95 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
97 MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
100 //=============================================================================
101 /*! \brief Get a local copy of the reference file
104 * \param localFile optional absolute path to store the copy
105 * \return the file name (absolute path) of the copy, may be different from
106 * localFile parameter if the copy was already done before the call
108 //=============================================================================
110 string SALOME_FileTransferCORBA::getLocalFile(string localFile)
112 MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
114 Engines::Container_var container;
116 if (CORBA::is_nil(_theFileRef))
118 if (_refMachine.empty() || _origFileName.empty())
120 INFOS("not enough parameters: machine and file name must be given");
124 SALOME_LifeCycleCORBA LCC;
125 Engines::ContainerManager_var contManager = LCC.getContainerManager();
126 Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
128 Engines::MachineParameters params;
130 params.container_name = _containerName.c_str();
131 params.hostname = _refMachine.c_str();
133 container = contManager->FindOrStartContainer(params);
134 if (CORBA::is_nil(container))
136 INFOS("machine " << _refMachine << " unreachable");
140 _theFileRef = container->createFileRef(_origFileName.c_str());
141 if (CORBA::is_nil(_theFileRef))
143 INFOS("impossible to create fileRef on " << _refMachine);
148 container = _theFileRef->getContainer();
149 ASSERT(! CORBA::is_nil(container));
151 string myMachine = Kernel_Utils::GetHostname();
152 string localCopy = _theFileRef->getRef(myMachine.c_str());
154 if (localCopy.empty()) // no existing copy available
156 if (localFile.empty()) // no name provided for local copy
159 localCopy = tmpnam(bufName);
165 if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
167 INFOS("file " << localFile << " cannot be open for writing");
171 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
172 ASSERT(! CORBA::is_nil(fileTransfer));
174 CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
177 Engines::fileBlock* aBlock;
184 aBlock = fileTransfer->getBlock(fileId);
185 toFollow = aBlock->length();
187 CORBA::Octet *buf = aBlock->get_buffer();
188 #if defined(_DEBUG_) || defined(_DEBUG)
189 int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
190 ASSERT(nbWri == toFollow);
192 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
197 MESSAGE("end of transfer");
198 fileTransfer->close(fileId);
199 _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
200 localCopy = localFile;
204 INFOS("open reference file for copy impossible");