]> SALOME platform Git repositories - modules/kernel.git/blob - src/LifeCycleCORBA/SALOME_FileTransferCORBA.cxx
Salome HOME
Merge from V5_1_3_BR 07/12/2009
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_FileTransferCORBA.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_FileTransferCORBA.cxx
23 //  Author : Paul RASCLE, EDF
24 //  Module : SALOME
25 //  $Header$
26 //
27 #include "SALOME_FileTransferCORBA.hxx"
28 #include "SALOME_LifeCycleCORBA.hxx"
29 #include "utilities.h"
30 #include "Basics_Utils.hxx"
31 #include <cstdio>
32
33 using namespace std;
34
35 /*! \class SALOME_FileTransferCORBA
36     \brief A class to manage file transfer in SALOME (CORBA context)
37
38 */
39
40 //=============================================================================
41 /*! 
42  *  Default constructor, not for use.
43  */
44 //=============================================================================
45
46 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
47 {
48   ASSERT(0);
49 }
50
51 //=============================================================================
52 /*! \brief Constructor to use when we get a fileRef CORBA object from a component
53  *
54  *  \param aFileRef file reference CORBA object
55  */
56 //=============================================================================
57
58 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
59                                                    aFileRef)
60 {
61   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
62   _theFileRef = aFileRef;
63 }
64
65 //=============================================================================
66 /*! \brief Constructor to use when we have the file name and machine from which to
67  *         copy, plus an optional Container name on the machine.
68  *
69  *  \param refMachine    the machine on which is the file to transfer
70  *  \param origFileName  abolute file path on refMachine
71  *  \param containerName default container name used (FactoryServer) if empty
72  */
73 //=============================================================================
74
75 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(string refMachine,
76                                                    string origFileName,
77                                                    string containerName)
78 {
79   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA"
80           << refMachine << " " << origFileName  << " " << containerName);
81   _refMachine = refMachine;
82   _origFileName = origFileName;
83   _containerName = containerName;
84   if (_refMachine.empty() || _origFileName.empty())
85     {
86       INFOS("bad parameters: machine and file name must be given");
87     } 
88 }
89
90 //=============================================================================
91 /*! 
92  *  Destructor
93  */
94 //=============================================================================
95
96 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
97 {
98   MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
99 }
100
101 //=============================================================================
102 /*! \brief Get a local copy of the reference file
103  *
104  *  CORBA method
105  *  \param  localFile optional absolute path to store the copy
106  *  \return the file name (absolute path) of the copy, may be different from
107  *          localFile parameter if the copy was already done before the call
108  */
109 //=============================================================================
110
111 string SALOME_FileTransferCORBA::getLocalFile(string localFile)
112 {
113   MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
114
115   Engines::Container_var container;
116
117   if (CORBA::is_nil(_theFileRef))
118     {
119       if (_refMachine.empty() || _origFileName.empty())
120         {
121           INFOS("not enough parameters: machine and file name must be given");
122           return "";
123         }
124
125       SALOME_LifeCycleCORBA LCC;
126       Engines::ContainerManager_var contManager = LCC.getContainerManager();
127       Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
128
129       Engines::MachineParameters params;
130       LCC.preSet(params);
131       params.container_name = _containerName.c_str();
132       params.hostname = _refMachine.c_str();
133
134       container = contManager->FindOrStartContainer(params);
135       if (CORBA::is_nil(container))
136         {
137           INFOS("machine " << _refMachine << " unreachable");
138           return "";
139         }
140
141       _theFileRef = container->createFileRef(_origFileName.c_str());
142       if (CORBA::is_nil(_theFileRef))
143         {
144           INFOS("impossible to create fileRef on " << _refMachine);
145           return "";
146         }
147     }
148
149   container = _theFileRef->getContainer();
150   ASSERT(! CORBA::is_nil(container));
151
152   string myMachine = Kernel_Utils::GetHostname();
153   string localCopy = _theFileRef->getRef(myMachine.c_str());
154
155   if (localCopy.empty()) // no existing copy available
156     {
157       if (localFile.empty()) // no name provided for local copy
158         {
159           char bufName[256];
160           localCopy = tmpnam(bufName);
161           localFile = bufName;
162           SCRUTE(localFile);
163         }
164
165       FILE* fp;
166       if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
167         {
168           INFOS("file " << localFile << " cannot be open for writing");
169           return "";
170         }
171
172       Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
173       ASSERT(! CORBA::is_nil(fileTransfer));
174
175       CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
176       if (fileId > 0)
177         {
178           Engines::fileBlock* aBlock;
179           int toFollow = 1;
180           int ctr=0;
181           while (toFollow)
182             {
183               ctr++;
184               SCRUTE(ctr);
185               aBlock = fileTransfer->getBlock(fileId);
186               toFollow = aBlock->length();
187               SCRUTE(toFollow);
188               CORBA::Octet *buf = aBlock->get_buffer();
189 #if defined(_DEBUG_) || defined(_DEBUG)
190               int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
191               ASSERT(nbWri == toFollow);
192 #else
193               fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
194 #endif
195               delete aBlock;
196             }
197           fclose(fp);
198           MESSAGE("end of transfer");
199           fileTransfer->close(fileId);
200           _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
201           localCopy = localFile;
202         }
203       else
204         {
205           INFOS("open reference file for copy impossible");
206           return "";
207         }
208       
209     }
210   SCRUTE(localCopy);
211   return localCopy;
212 }