Salome HOME
Change dot image format from jpg to png.
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_FileTransferCORBA.cxx
1 // Copyright (C) 2007-2012  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
23 //  File   : SALOME_FileTransferCORBA.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SALOME_FileTransferCORBA.hxx"
29 #include "SALOME_LifeCycleCORBA.hxx"
30 #include "utilities.h"
31 #include "Basics_Utils.hxx"
32 #include <cstdio>
33
34 /*! \class SALOME_FileTransferCORBA
35     \brief A class to manage file transfer in SALOME (CORBA context)
36
37 */
38
39 //=============================================================================
40 /*! 
41  *  Default constructor, not for use.
42  */
43 //=============================================================================
44
45 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
46 {
47   ASSERT(0);
48 }
49
50 //=============================================================================
51 /*! \brief Constructor to use when we get a fileRef CORBA object from a component
52  *
53  *  \param aFileRef file reference CORBA object
54  */
55 //=============================================================================
56
57 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
58                                                    aFileRef)
59 {
60   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
61   _theFileRef = aFileRef;
62 }
63
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.
67  *
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
71  */
72 //=============================================================================
73
74 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(std::string refMachine,
75                                                    std::string origFileName,
76                                                    std::string containerName)
77 {
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())
84     {
85       INFOS("bad parameters: machine and file name must be given");
86     } 
87 }
88
89 //=============================================================================
90 /*! 
91  *  Destructor
92  */
93 //=============================================================================
94
95 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
96 {
97   MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
98 }
99
100 //=============================================================================
101 /*! \brief Get a local copy of the reference file
102  *
103  *  CORBA method
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
107  */
108 //=============================================================================
109
110 std::string SALOME_FileTransferCORBA::getLocalFile(std::string localFile)
111 {
112   MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
113
114   Engines::Container_var container;
115
116   if (CORBA::is_nil(_theFileRef))
117     {
118       if (_refMachine.empty() || _origFileName.empty())
119         {
120           INFOS("not enough parameters: machine and file name must be given");
121           return "";
122         }
123
124       SALOME_LifeCycleCORBA LCC;
125       Engines::ContainerManager_var contManager = LCC.getContainerManager();
126       Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
127
128       Engines::MachineParameters params;
129       LCC.preSet(params);
130       params.container_name = _containerName.c_str();
131       params.hostname = _refMachine.c_str();
132
133       Engines::ContainerParameters new_params;
134       LCC.convert(params, new_params);
135       new_params.mode = CORBA::string_dup("findorstart");
136       container = contManager->GiveContainer(new_params);
137       if (CORBA::is_nil(container))
138         {
139           INFOS("machine " << _refMachine << " unreachable");
140           return "";
141         }
142
143       _theFileRef = container->createFileRef(_origFileName.c_str());
144       if (CORBA::is_nil(_theFileRef))
145         {
146           INFOS("impossible to create fileRef on " << _refMachine);
147           return "";
148         }
149     }
150
151   container = _theFileRef->getContainer();
152   ASSERT(! CORBA::is_nil(container));
153
154   std::string myMachine = Kernel_Utils::GetHostname();
155   std::string localCopy = _theFileRef->getRef(myMachine.c_str());
156
157   if (localCopy.empty()) // no existing copy available
158     {
159       if (localFile.empty()) // no name provided for local copy
160         {
161           char bufName[256];
162           localCopy = tmpnam(bufName);
163           localFile = bufName;
164           SCRUTE(localFile);
165         }
166
167       FILE* fp;
168       if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
169         {
170           INFOS("file " << localFile << " cannot be open for writing");
171           return "";
172         }
173
174       Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
175       ASSERT(! CORBA::is_nil(fileTransfer));
176
177       CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
178       if (fileId > 0)
179         {
180           Engines::fileBlock* aBlock;
181           int toFollow = 1;
182           int ctr=0;
183           while (toFollow)
184             {
185               ctr++;
186               SCRUTE(ctr);
187               aBlock = fileTransfer->getBlock(fileId);
188               toFollow = aBlock->length();
189               SCRUTE(toFollow);
190               CORBA::Octet *buf = aBlock->get_buffer();
191 #if defined(_DEBUG_) || defined(_DEBUG)
192               int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
193               ASSERT(nbWri == toFollow);
194 #else
195               fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
196 #endif
197               delete aBlock;
198             }
199           fclose(fp);
200           MESSAGE("end of transfer");
201           fileTransfer->close(fileId);
202           _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
203           localCopy = localFile;
204         }
205       else
206         {
207           INFOS("open reference file for copy impossible");
208           return "";
209         }
210       
211     }
212   SCRUTE(localCopy);
213   return localCopy;
214 }