Salome HOME
ee4acba69f96d03310cb54221e3f0f04972497e2
[modules/kernel.git] / src / Container / SALOME_FileRef_i.cxx
1 // Copyright (C) 2007-2014  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, 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_FileRef_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SALOME_FileRef_i.hxx"
29 #include "utilities.h"
30 #include "Basics_Utils.hxx"
31 #include <string>
32
33 //=============================================================================
34 /*! 
35  *  Default constructor, not for use
36  */
37 //=============================================================================
38
39 fileRef_i::fileRef_i()
40 {
41   ASSERT(0);
42 }
43 //=============================================================================
44 /*! 
45  *  Constructor to use
46  */
47 //=============================================================================
48
49 fileRef_i::fileRef_i(Engines::Container_ptr container,
50                      const char* origFileName)
51 {
52   MESSAGE("fileRef_i::fileRef_i "<< origFileName);
53   _container =  Engines::Container::_duplicate(container);
54   _origFileName = origFileName;
55   _machine = Kernel_Utils::GetHostname();
56 #if defined(_DEBUG_) || defined(_DEBUG)
57   int OK = addRef(_machine.c_str(), _origFileName.c_str());
58   SCRUTE(OK);
59 #else
60   addRef(_machine.c_str(), _origFileName.c_str());
61 #endif
62 }
63
64
65 //=============================================================================
66 /*! 
67  *  Destructor
68  */
69 //=============================================================================
70
71 fileRef_i::~fileRef_i()
72 {
73   MESSAGE("fileRef_i::~fileRef_i");
74 }
75
76 //=============================================================================
77 /*! 
78  *  CORBA method: 
79  *  \return the file name (absolute path) on the computer which runs the
80  *          container server
81  */
82 //=============================================================================
83
84 char* fileRef_i::origFileName()
85 {
86   MESSAGE("fileRef_i::origFileName " << _origFileName);
87   return CORBA::string_dup(_origFileName.c_str());
88 }
89
90 //=============================================================================
91 /*! 
92  *  CORBA method: 
93  *  \return the hostname of the computer which runs the container server
94  */
95 //=============================================================================
96
97 char* fileRef_i::refMachine()
98 {
99   MESSAGE("fileRef_i::refMachine " << _machine);
100   return CORBA::string_dup(_machine.c_str());
101 }
102
103 //=============================================================================
104 /*! 
105  *  CORBA method: 
106  *  \return the container reference
107  */
108 //=============================================================================
109
110 Engines::Container_ptr fileRef_i::getContainer()
111 {
112   MESSAGE("fileRef_i::getContainer");
113  Engines::Container_var theContainer = 
114    Engines::Container::_duplicate(_container);
115  return theContainer._retn();
116 }
117
118 //=============================================================================
119 /*! 
120  *  CORBA method: after a file transfer on a client computer, registers 
121  *  hostname of client and file path of the copied file.
122  *  \param machine client hostname
123  *  \param fileName asolute path of the transfered file on client computer
124  */
125 //=============================================================================
126
127 CORBA::Boolean fileRef_i::addRef(const char* machine,
128                                  const char* fileName)
129 {
130   MESSAGE("fileRef_i::addRef " << machine << " " << fileName);
131   std::string theMachine = machine;
132   std::string theFileName = fileName;
133
134   if (theFileName[0] != '/')
135     {
136       INFOS("destination file path must be absolute, begining with '/'");
137       return 0;
138     }
139
140   if (theMachine.empty())
141     {
142       INFOS("provide a hostname for the copy destination");
143       return 0;
144     }
145
146   if (! _copies[theMachine].empty())
147     {
148       INFOS("there is already a copy on " << theMachine << " under the path "
149             <<  _copies[theMachine] << " new ref not added! ");
150       return 0;
151     }
152
153   _copies[theMachine] = theFileName;
154   return 1;
155 }
156
157 //=============================================================================
158 /*! 
159  *  CORBA method: check if a copy of the file referenced by fileRef is
160  *  available on the client computer.
161  *  \param  machine hostname of the client computer
162  *  \return path of the copy on the client computer, if the copy exists,
163  *          else empty string
164  */
165 //=============================================================================
166
167 char* fileRef_i::getRef(const char* machine)
168 {
169   MESSAGE("fileRef_i::getRef "<< machine);
170   std::string theMachine = machine;
171   std::string theFileName = _copies[theMachine];
172   if (_copies[theMachine].empty())
173     {
174       MESSAGE("no copy of " << _machine << _origFileName << " available on "
175               << theMachine);
176     }
177   else
178     {
179       MESSAGE("a copy of " << _machine << _origFileName << "is available on "
180               << theMachine << _copies[theMachine]);
181     }
182   return CORBA::string_dup(_copies[theMachine].c_str());
183 }
184