Salome HOME
Removed CASCatch
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_FileTransferCORBA.cxx
1 // Copyright (C) 2006  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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. 
7 // 
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. 
12 // 
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 
16 // 
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //
20 //
21 //  File   : SALOME_FileTransferCORBA.cxx
22 //  Author : Paul RASCLE, EDF
23 //  Module : SALOME
24 //  $Header$
25
26 #include "SALOME_FileTransferCORBA.hxx"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "utilities.h"
29 #include "OpUtil.hxx"
30
31 using namespace std;
32
33 //=============================================================================
34 /*! 
35  *  Default constructor, not for use.
36  */
37 //=============================================================================
38
39 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA()
40 {
41   ASSERT(0);
42 }
43
44 //=============================================================================
45 /*! 
46  *  Constructor to use when we get a fileRef CORBA object from a component.
47  *  \param aFileRef file reference CORBA object
48  */
49 //=============================================================================
50
51 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(Engines::fileRef_ptr
52                                                    aFileRef)
53 {
54   MESSAGE("SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(aFileRef)");
55   _theFileRef = aFileRef;
56 }
57
58 //=============================================================================
59 /*! 
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
65  */
66 //=============================================================================
67
68 SALOME_FileTransferCORBA::SALOME_FileTransferCORBA(string refMachine,
69                                                    string origFileName,
70                                                    string containerName)
71 {
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())
78     {
79       INFOS("bad parameters: machine and file name must be given");
80     } 
81 }
82
83 //=============================================================================
84 /*! 
85  *  Destructor
86  */
87 //=============================================================================
88
89 SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA()
90 {
91   MESSAGE("SALOME_FileTransferCORBA::~SALOME_FileTransferCORBA");
92 }
93
94 //=============================================================================
95 /*! 
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
100  */
101 //=============================================================================
102
103 string SALOME_FileTransferCORBA::getLocalFile(string localFile)
104 {
105   MESSAGE("SALOME_FileTransferCORBA::getLocalFile " << localFile);
106
107   Engines::Container_var container;
108
109   if (CORBA::is_nil(_theFileRef))
110     {
111       if (_refMachine.empty() || _origFileName.empty())
112         {
113           INFOS("not enough parameters: machine and file name must be given");
114           return "";
115         }
116
117       SALOME_LifeCycleCORBA LCC;
118       Engines::ContainerManager_var contManager = LCC.getContainerManager();
119       Engines::ResourcesManager_var resManager = LCC.getResourcesManager();
120
121       Engines::MachineParameters params;
122       LCC.preSet(params);
123       params.container_name = _containerName.c_str();
124       params.hostname = _refMachine.c_str();
125
126       Engines::CompoList clist;
127       Engines::MachineList_var listOfMachines =
128         resManager->GetFittingResources(params, clist);
129
130       container = contManager->FindOrStartContainer(params,
131                                                     listOfMachines);
132       if (CORBA::is_nil(container))
133         {
134           INFOS("machine " << _refMachine << " unreachable");
135           return "";
136         }
137
138       _theFileRef = container->createFileRef(_origFileName.c_str());
139       if (CORBA::is_nil(_theFileRef))
140         {
141           INFOS("imposssible to create fileRef on " << _refMachine);
142           return "";
143         }
144     }
145
146   container = _theFileRef->getContainer();
147   ASSERT(! CORBA::is_nil(container));
148
149   string myMachine = GetHostname();
150   string localCopy = _theFileRef->getRef(myMachine.c_str());
151
152   if (localCopy.empty()) // no existing copy available
153     {
154       if (localFile.empty()) // no name provided for local copy
155         {
156           char bufName[256];
157           localCopy = tmpnam(bufName);
158           localFile = bufName;
159           SCRUTE(localFile);
160         }
161
162       FILE* fp;
163       if ((fp = fopen(localFile.c_str(),"wb")) == NULL)
164         {
165           INFOS("file " << localFile << " cannot be open for writing");
166           return "";
167         }
168
169       Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
170       ASSERT(! CORBA::is_nil(fileTransfer));
171
172       CORBA::Long fileId = fileTransfer->open(_origFileName.c_str());
173       if (fileId > 0)
174         {
175           Engines::fileBlock* aBlock;
176           int toFollow = 1;
177           int ctr=0;
178           while (toFollow)
179             {
180               ctr++;
181               SCRUTE(ctr);
182               aBlock = fileTransfer->getBlock(fileId);
183               toFollow = aBlock->length();
184               SCRUTE(toFollow);
185               CORBA::Octet *buf = aBlock->get_buffer();
186               int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
187               ASSERT(nbWri == toFollow);
188             }
189           fclose(fp);
190           MESSAGE("end of transfer");
191           fileTransfer->close(fileId);
192           _theFileRef->addRef(myMachine.c_str(), localFile.c_str());
193           localCopy = localFile;
194         }
195       else
196         {
197           INFOS("open reference file for copy impossible");
198           return "";
199         }
200       
201     }
202   SCRUTE(localCopy);
203   return localCopy;
204 }