Salome HOME
PR: bug 7769 loader aborts when logger not found in naming service,
[modules/kernel.git] / src / SALOMETraceCollector / TraceCollector_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   : TraceCollector_WaitForServerReadiness.cxx
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26
27 #include "TraceCollector_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 TraceCollector_WaitForServerReadiness(CORBA::ORB_ptr orb,
47                                                         string serverName)
48 {
49   long TIMESleep = 500000000;
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=CORBA::Object::_nil();
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 << "TraceCollector_WaitForServerReadiness: "
85                    << "CORBA::COMM_FAILURE: "
86                    << "Unable to contact the Naming Service" << endl;
87             }
88           catch(...)
89             {
90               cout << "TraceCollector_WaitForServerReadiness: "
91                    << "Unknown exception dealing with Naming Service" << endl;
92             }
93           
94           obj=CORBA::Object::_nil();
95           if(!CORBA::is_nil(inc))
96             {
97               try
98                 {
99                   obj = inc->resolve(name);
100                   if (!CORBA::is_nil(obj))
101                     {
102                       cout << "TraceCollector_WaitForServerReadiness: "
103                            << serverName << " found in CORBA Name Service" << endl;
104                       break;
105                     }
106                 }
107               catch (const CosNaming::NamingContext::NotFound&)
108                 {
109                   cout << "Caught exception: Naming Service can't found Logger";
110                 }
111             }
112           int a = nanosleep(&ts_req,&ts_rem);
113           cout << "TraceCollector_WaitForServerReadiness: retry look for"
114                << serverName << endl;
115         }          
116     }
117   catch (const CosNaming::NamingContext::NotFound&)
118     {
119       cout << "Caught exception: Naming Service can't found Logger";
120     }
121   catch (CORBA::COMM_FAILURE&)
122     {
123       cout << "Caught CORBA::SystemException CommFailure.";
124     }
125   catch (CORBA::SystemException&)
126     {
127       cout << "Caught CORBA::SystemException.";
128     }
129   catch (CORBA::Exception&)
130     {
131       cout << "Caught CORBA::Exception.";
132     }
133   catch (...)
134     {
135       cout << "Caught unknown exception.";
136     }
137   return obj._retn();
138 }
139