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