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