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