]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMELocalTrace/LocalTrace_WaitForServerReadiness.cxx
Salome HOME
PR: mergefrom_PRAS_br3_06Nov04
[modules/kernel.git] / src / SALOMELocalTrace / LocalTrace_WaitForServerReadiness.cxx
1 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : LocalTrace_WaitForServerReadiness.cxx
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26
27 #include "LocalTrace_WaitForServerReadiness.hxx"
28 #include <iostream>
29 #include <ctime>
30
31 using namespace std;
32
33 // ============================================================================
34 /*!
35  *  Wait until a server is registered in naming service.
36  *  \param serverName name of the server to find.
37  *  When SALOME_NamingService is available,
38  *  use NamingService_WaitForServerReadiness instead.
39  *  This function is needed when macro MESSAGE used by SALOME_NamingService
40  *  is not available (inside LocalTrace methods, for instance !).
41  *  Direct access to CORBA Name Service. Look for serverName at Name service
42  *  Root without extensions.
43  */
44 // ============================================================================
45
46 CORBA::Object_ptr LocalTrace_WaitForServerReadiness(CORBA::ORB_ptr orb,
47                                                     string serverName)
48 {
49   long TIMESleep = 250000000;
50   int NumberOfTries = 40;
51
52   timespec ts_req;
53   ts_req.tv_nsec=TIMESleep;
54   ts_req.tv_sec=0;
55   timespec ts_rem;
56   ts_rem.tv_nsec=0;
57   ts_rem.tv_sec=0;
58
59   CORBA::Object_var obj;
60
61   try
62     {
63       // NB. You can't use SALOME_NamingService class because
64       // it uses MESSAGE macro
65       // Otherwise, you will get segmentation fault.   
66
67       CosNaming::NamingContext_var inc;
68       CosNaming::Name name;
69       name.length(1);
70       name[0].id = CORBA::string_dup(serverName.c_str());
71       CORBA::Object_var theObj;
72
73       for (int itry=0; itry < NumberOfTries; itry++)
74         {
75           try
76             { 
77               if(!CORBA::is_nil(orb)) 
78                 theObj = orb->resolve_initial_references("NameService");
79               if (!CORBA::is_nil(theObj))
80                 inc = CosNaming::NamingContext::_narrow(theObj);
81             }  
82           catch( CORBA::COMM_FAILURE& )
83             {
84               cout << "LocalTrace_WaitForServerReadiness: "
85                    << "CORBA::COMM_FAILURE: "
86                    << "Unable to contact the Naming Service" << endl;
87             }
88           catch(...)
89             {
90               cout << "LocalTrace_WaitForServerReadiness: "
91                    << "Unknown exception dealing with Naming Service" << endl;
92             }
93           
94           if(!CORBA::is_nil(inc))
95             {
96               obj = inc->resolve(name);
97               if (!CORBA::is_nil(obj))
98                 {
99                   cout << "LocalTrace_WaitForServerReadiness: "
100                        << serverName << " found in CORBA Name Service" << endl;
101                   break;
102                 }
103             }
104           int a = nanosleep(&ts_req,&ts_rem);
105           cout << "LocalTrace_WaitForServerReadiness: retry look for"
106                << serverName << endl;
107         }          
108     }
109   catch (const CosNaming::NamingContext::NotFound&)
110     {
111       cout << "Caught exception: Naming Service can't found Logger";
112     }
113   catch (CORBA::COMM_FAILURE&)
114     {
115       cout << "Caught CORBA::SystemException CommFailure.";
116     }
117   catch (CORBA::SystemException&)
118     {
119       cout << "Caught CORBA::SystemException.";
120     }
121   catch (CORBA::Exception&)
122     {
123       cout << "Caught CORBA::Exception.";
124     }
125   catch (...)
126     {
127       cout << "Caught unknown exception.";
128     }
129   return obj._retn();
130 }
131