Back to index

salome-kernel  6.5.0
SALOME_FileTransferCORBA.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOME_FileTransferCORBA.cxx
00024 //  Author : Paul RASCLE, EDF
00025 //  Module : SALOME
00026 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/LifeCycleCORBA/SALOME_FileTransferCORBA.cxx,v 1.5.2.6.10.2.12.1 2012-04-12 14:05:16 vsr Exp $
00027 //
00028 #include "SALOME_FileTransferCORBA.hxx"
00029 #include "SALOME_LifeCycleCORBA.hxx"
00030 #include "utilities.h"
00031 #include "Basics_Utils.hxx"
00032 #include <cstdio>
00033 
00039 //=============================================================================
00043 //=============================================================================
00044 
00045 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
00046 {
00047   ASSERT(0);
00048 }
00049 
00050 //=============================================================================
00055 //=============================================================================
00056 
00057 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
00058                                                    aFileRef)
00059 {
00060   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
00061   _theFileRef = aFileRef;
00062 }
00063 
00064 //=============================================================================
00072 //=============================================================================
00073 
00074 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(std::string refMachine,
00075                                                    std::string origFileName,
00076                                                    std::string containerName)
00077 {
00078   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA"
00079           << refMachine << " " << origFileName  << " " << containerName);
00080   _refMachine = refMachine;
00081   _origFileName = origFileName;
00082   _containerName = containerName;
00083   if (_refMachine.empty() || _origFileName.empty())
00084     {
00085       INFOS("bad parameters: machine and file name must be given");
00086     } 
00087 }
00088 
00089 //=============================================================================
00093 //=============================================================================
00094 
00095 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
00096 {
00097   MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
00098 }
00099 
00100 //=============================================================================
00108 //=============================================================================
00109 
00110 std::string SALOME_FileTransferCORBA::getLocalFile(std::string localFile)
00111 {
00112   MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
00113 
00114   Engines::Container_var container;
00115 
00116   if (CORBA::is_nil(_theFileRef))
00117     {
00118       if (_refMachine.empty() || _origFileName.empty())
00119         {
00120           INFOS("not enough parameters: machine and file name must be given");
00121           return "";
00122         }
00123 
00124       SALOME_LifeCycleCORBA LCC;
00125       Engines::ContainerManager_var contManager = LCC.getContainerManager();
00126       Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
00127 
00128       Engines::MachineParameters params;
00129       LCC.preSet(params);
00130       params.container_name = _containerName.c_str();
00131       params.hostname = _refMachine.c_str();
00132 
00133       Engines::ContainerParameters new_params;
00134       LCC.convert(params, new_params);
00135       new_params.mode = CORBA::string_dup("findorstart");
00136       container = contManager->GiveContainer(new_params);
00137       if (CORBA::is_nil(container))
00138         {
00139           INFOS("machine " << _refMachine << " unreachable");
00140           return "";
00141         }
00142 
00143       _theFileRef = container->createFileRef(_origFileName.c_str());
00144       if (CORBA::is_nil(_theFileRef))
00145         {
00146           INFOS("impossible to create fileRef on " << _refMachine);
00147           return "";
00148         }
00149     }
00150 
00151   container = _theFileRef->getContainer();
00152   ASSERT(! CORBA::is_nil(container));
00153 
00154   std::string myMachine = Kernel_Utils::GetHostname();
00155   std::string localCopy = _theFileRef->getRef(myMachine.c_str());
00156 
00157   if (localCopy.empty()) // no existing copy available
00158     {
00159       if (localFile.empty()) // no name provided for local copy
00160         {
00161           char bufName[256];
00162           localCopy = tmpnam(bufName);
00163           localFile = bufName;
00164           SCRUTE(localFile);
00165         }
00166 
00167       FILE* fp;
00168       if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
00169         {
00170           INFOS("file " << localFile << " cannot be open for writing");
00171           return "";
00172         }
00173 
00174       Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
00175       ASSERT(! CORBA::is_nil(fileTransfer));
00176 
00177       CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
00178       if (fileId > 0)
00179         {
00180           Engines::fileBlock* aBlock;
00181           int toFollow = 1;
00182           int ctr=0;
00183           while (toFollow)
00184             {
00185               ctr++;
00186               SCRUTE(ctr);
00187               aBlock = fileTransfer->getBlock(fileId);
00188               toFollow = aBlock->length();
00189               SCRUTE(toFollow);
00190               CORBA::Octet *buf = aBlock->get_buffer();
00191 #if defined(_DEBUG_) || defined(_DEBUG)
00192               int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
00193               ASSERT(nbWri == toFollow);
00194 #else
00195               fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
00196 #endif
00197               delete aBlock;
00198             }
00199           fclose(fp);
00200           MESSAGE("end of transfer");
00201           fileTransfer->close(fileId);
00202           _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
00203           localCopy = localFile;
00204         }
00205       else
00206         {
00207           INFOS("open reference file for copy impossible");
00208           return "";
00209         }
00210       
00211     }
00212   SCRUTE(localCopy);
00213   return localCopy;
00214 }