Salome HOME
[EDF29138] : measure CPU/Mem even in OutOfProcess mode
[modules/kernel.git] / src / Container / SALOME_FileTransfer_i.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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_FileTransfer_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SALOME_FileTransfer_i.hxx"
29 #include "utilities.h"
30
31 /*! \class fileTransfer_i
32     \brief A class to manage file transfer in SALOME
33
34 */
35
36 //=============================================================================
37 /*! 
38  *  Default constructor,
39  */
40 //=============================================================================
41
42 fileTransfer_i::fileTransfer_i()
43 {
44   _fileKey=1;
45 }
46
47 //=============================================================================
48 /*! 
49  *  Destructor
50  */
51 //=============================================================================
52
53 fileTransfer_i::~fileTransfer_i()
54 {
55 }
56
57
58 //=============================================================================
59 /*! \brief open the given file
60  *
61  *  CORBA method: try to open the file. If the file is readable, return
62  *  a positive integer else return 0;
63  *  \param  fileName path to the file to be transferred
64  *  \return fileId = positive integer > 0 if open OK.
65  */
66 //=============================================================================
67
68 CORBA::Long fileTransfer_i::open(const char* fileName)
69 {
70   int aKey = _fileKey++;
71   _ctr=0;
72   FILE* fp;
73   if ((fp = fopen(fileName,"rb")) == NULL)
74     {
75       INFOS("file " << fileName << " is not readable");
76       return 0;
77     }
78   _fileAccess[aKey] = fp;
79   return aKey;
80 }
81
82 //=============================================================================
83 /*! \brief close a file
84  *
85  *  CORBA method: close the file associated to the fileId given at open.
86  *  \param fileId got in return from open method
87  */
88 //=============================================================================
89
90 void fileTransfer_i::close(CORBA::Long fileId)
91 {
92   FILE* fp;
93   if (! (fp = _fileAccess[fileId]) )
94     {
95       INFOS(" no FILE structure associated to fileId " <<fileId);
96     }
97   else 
98     {
99       fclose(fp);
100       _fileAccess.erase(fileId);
101     }
102 }
103
104 #define FILEBLOCK_SIZE 256*1024
105
106 //=============================================================================
107 /*! \brief get a data block from a file
108  * 
109  *  CORBA method: get a block of data from the file associated to the fileId
110  *  given at open.
111  *  \param fileId got in return from open method
112  *  \return an octet sequence. Last one is empty.
113  */
114 //=============================================================================
115
116 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
117 {
118   Engines::fileBlock* aBlock = new Engines::fileBlock;
119
120   FILE* fp;
121   if (! (fp = _fileAccess[fileId]) )
122     {
123       INFOS(" no FILE structure associated to fileId " <<fileId);
124       return aBlock;
125     }
126
127   // use replace member function for sequence to avoid copy
128   // see Advanced CORBA Programming with C++ pp 187-194
129   
130   CORBA::Octet *buf;
131   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
132   size_t nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
133   //SCRUTE(nbRed);
134   aBlock->replace((CORBA::ULong)nbRed, (CORBA::ULong)nbRed, buf, 1); // 1 means give ownership
135   return aBlock;
136 }
137
138 /*! \brief open the given file in write mode (for copy)
139  *
140  *  CORBA method: try to open the file. If the file is writable, 
141  *  return a positive integer else return 0;
142  *  \param  fileName path to the file to be transferred
143  *  \return fileId = positive integer > 0 if open OK.
144  */
145 CORBA::Long fileTransfer_i::openW(const char* fileName)
146 {
147   int aKey = _fileKey++;
148   _ctr=0;
149   FILE* fp;
150   if ((fp = fopen(fileName,"wb")) == NULL)
151     {
152       INFOS("file " << fileName << " is not writable");
153       return 0;
154     }
155   _fileAccess[aKey] = fp;
156   return aKey;
157 }
158
159 /*! \brief put a data block for copy into a file
160  * 
161  *  CORBA method: put a block of data into the file associated to the fileId
162  *  given at openW.
163  *  \param fileId got in return from openW method
164  *  \param block an octet sequence to copy into opened file
165  */
166 void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block)
167 {
168   FILE* fp;
169   if (! (fp = _fileAccess[fileId]) )
170     {
171       INFOS(" no FILE structure associated to fileId " <<fileId);
172       return ;
173     }
174   int toFollow = block.length();
175   SCRUTE(toFollow);
176   const CORBA::Octet *buf = block.get_buffer();
177   fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
178 }
179
180