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