Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMETraceCollector / TraceCollector_WaitForServerReadiness.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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   : TraceCollector_WaitForServerReadiness.cxx
24 //  Author : Paul RASCLE (EDF)
25 //  Module : KERNEL
26 //  $Header$
27 //
28 #include "TraceCollector_WaitForServerReadiness.hxx"
29 #include "KernelBasis.hxx"
30 #include "SALOME_Fake_NamingService.hxx"
31 #include "OpUtil.hxx"
32 #include <iostream>
33 #include <ctime>
34 #include <memory>
35
36
37 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1900
38 #include <omnithread/pthread_nt.h>
39 #endif
40
41 // ============================================================================
42 /*!
43  *  Wait until a server is registered in naming service.
44  *  \param serverName name of the server to find.
45  *  When SALOME_NamingService is available,
46  *  use NamingService_WaitForServerReadiness instead.
47  *  This function is needed when macro MESSAGE used by SALOME_NamingService
48  *  is not available (inside LocalTrace methods, for instance !).
49  *  Direct access to CORBA Name Service. Look for serverName at Name service
50  *  Root without extensions.
51  */
52 // ============================================================================
53
54 CORBA::Object_ptr TraceCollector_WaitForServerReadiness(const std::string& serverName)
55 {
56   long TIMESleep = 500000000;
57   int NumberOfTries = 40;
58
59   timespec ts_req;
60   ts_req.tv_nsec=TIMESleep;
61   ts_req.tv_sec=0;
62   timespec ts_rem;
63   ts_rem.tv_nsec=0;
64   ts_rem.tv_sec=0;
65
66   CORBA::ORB_var orb = KERNEL::GetRefToORB();
67   CORBA::Object_var obj;
68
69   try
70     {
71       // NB. You can't use SALOME_NamingService class because
72       // it uses MESSAGE macro
73       // Otherwise, you will get segmentation fault.   
74       if(getSSLMode())
75       {
76         std::string regName(std::string("/")+serverName);
77         std::unique_ptr<SALOME_Fake_NamingService> ns(new SALOME_Fake_NamingService);
78         for (int itry=0; itry < NumberOfTries; itry++)
79         {
80           obj = ns->Resolve(regName.c_str());
81           if(!CORBA::is_nil(obj))
82             break;
83   #ifndef WIN32
84             nanosleep(&ts_req,&ts_rem);
85   #else
86             Sleep(TIMESleep / 1000000);
87   #endif
88         }
89       }
90       else
91       {
92         CosNaming::NamingContext_var inc;
93         CosNaming::Name name;
94         name.length(1);
95         name[0].id = CORBA::string_dup(serverName.c_str());
96         CORBA::Object_var theObj=CORBA::Object::_nil();
97
98         for (int itry=0; itry < NumberOfTries; itry++)
99           {
100             try
101               { 
102                 if(!CORBA::is_nil(orb)) 
103                   theObj = orb->resolve_initial_references("NameService");
104                 if (!CORBA::is_nil(theObj))
105                   inc = CosNaming::NamingContext::_narrow(theObj);
106               }  
107             catch( CORBA::SystemException& )
108               {
109                 std::cout << "TraceCollector_WaitForServerReadiness: "
110                     << "CORBA::SystemException: "
111                     << "Unable to contact the Naming Service" << std::endl;
112               }
113             catch(...)
114               {
115                 std::cout << "TraceCollector_WaitForServerReadiness: "
116                     << "Unknown exception dealing with Naming Service" << std::endl;
117               }
118             
119             obj=CORBA::Object::_nil();
120             if(!CORBA::is_nil(inc))
121               {
122                 try
123                   {
124                     obj = inc->resolve(name);
125                     if (!CORBA::is_nil(obj))
126                       {
127                         //cout << "TraceCollector_WaitForServerReadiness: "
128                         //           << serverName << " found in CORBA Name Service" << endl;
129                         break;
130                       }
131                   }
132                 catch (const CosNaming::NamingContext::NotFound&)
133                   {
134                     std::cout << "Caught exception: Naming Service can't found Logger";
135                   }
136               }
137   #ifndef WIN32
138             nanosleep(&ts_req,&ts_rem);
139   #else
140             Sleep(TIMESleep / 1000000);
141   #endif
142         std::cout << "TraceCollector_WaitForServerReadiness: retry look for"
143                 << serverName << std::endl;
144           }
145       }       
146     }
147   catch (const CosNaming::NamingContext::NotFound&)
148     {
149       std::cout << "Caught exception: Naming Service can't found Logger";
150     }
151   catch (CORBA::COMM_FAILURE&)
152     {
153       std::cout << "Caught CORBA::SystemException CommFailure.";
154     }
155   catch (CORBA::SystemException&)
156     {
157       std::cout << "Caught CORBA::SystemException.";
158     }
159   catch (CORBA::Exception&)
160     {
161       std::cout << "Caught CORBA::Exception.";
162     }
163   catch (...)
164     {
165       std::cout << "Caught unknown exception.";
166     }
167   return obj._retn();
168 }
169