Back to index

salome-kernel  6.5.0
TraceCollector_WaitForServerReadiness.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : TraceCollector_WaitForServerReadiness.cxx
00024 //  Author : Paul RASCLE (EDF)
00025 //  Module : KERNEL
00026 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/SALOMETraceCollector/TraceCollector_WaitForServerReadiness.cxx,v 1.5.2.1.10.2.12.1 2012-04-12 14:05:31 vsr Exp $
00027 //
00028 #include "TraceCollector_WaitForServerReadiness.hxx"
00029 #include <iostream>
00030 #include <ctime>
00031 
00032 #ifdef WIN32
00033 #include <omnithread/pthread_nt.h>
00034 #endif
00035 
00036 // ============================================================================
00047 // ============================================================================
00048 
00049 CORBA::Object_ptr TraceCollector_WaitForServerReadiness(CORBA::ORB_ptr orb,
00050                                                         std::string serverName)
00051 {
00052   long TIMESleep = 500000000;
00053   int NumberOfTries = 40;
00054 
00055   timespec ts_req;
00056   ts_req.tv_nsec=TIMESleep;
00057   ts_req.tv_sec=0;
00058   timespec ts_rem;
00059   ts_rem.tv_nsec=0;
00060   ts_rem.tv_sec=0;
00061 
00062   CORBA::Object_var obj;
00063 
00064   try
00065     {
00066       // NB. You can't use SALOME_NamingService class because
00067       // it uses MESSAGE macro
00068       // Otherwise, you will get segmentation fault.   
00069 
00070       CosNaming::NamingContext_var inc;
00071       CosNaming::Name name;
00072       name.length(1);
00073       name[0].id = CORBA::string_dup(serverName.c_str());
00074       CORBA::Object_var theObj=CORBA::Object::_nil();
00075 
00076       for (int itry=0; itry < NumberOfTries; itry++)
00077         {
00078           try
00079             { 
00080               if(!CORBA::is_nil(orb)) 
00081                 theObj = orb->resolve_initial_references("NameService");
00082               if (!CORBA::is_nil(theObj))
00083                 inc = CosNaming::NamingContext::_narrow(theObj);
00084             }  
00085           catch( CORBA::SystemException& )
00086             {
00087               std::cout << "TraceCollector_WaitForServerReadiness: "
00088                    << "CORBA::SystemException: "
00089                    << "Unable to contact the Naming Service" << std::endl;
00090             }
00091           catch(...)
00092             {
00093               std::cout << "TraceCollector_WaitForServerReadiness: "
00094                    << "Unknown exception dealing with Naming Service" << std::endl;
00095             }
00096           
00097           obj=CORBA::Object::_nil();
00098           if(!CORBA::is_nil(inc))
00099             {
00100               try
00101                 {
00102                   obj = inc->resolve(name);
00103                   if (!CORBA::is_nil(obj))
00104                     {
00105                       //cout << "TraceCollector_WaitForServerReadiness: "
00106                       //           << serverName << " found in CORBA Name Service" << endl;
00107                       break;
00108                     }
00109                 }
00110               catch (const CosNaming::NamingContext::NotFound&)
00111                 {
00112                   std::cout << "Caught exception: Naming Service can't found Logger";
00113                 }
00114             }
00115 #ifndef WIN32
00116           nanosleep(&ts_req,&ts_rem);
00117 #else
00118           Sleep(TIMESleep / 1000000);
00119 #endif
00120                 std::cout << "TraceCollector_WaitForServerReadiness: retry look for"
00121                << serverName << std::endl;
00122         }          
00123     }
00124   catch (const CosNaming::NamingContext::NotFound&)
00125     {
00126       std::cout << "Caught exception: Naming Service can't found Logger";
00127     }
00128   catch (CORBA::COMM_FAILURE&)
00129     {
00130       std::cout << "Caught CORBA::SystemException CommFailure.";
00131     }
00132   catch (CORBA::SystemException&)
00133     {
00134       std::cout << "Caught CORBA::SystemException.";
00135     }
00136   catch (CORBA::Exception&)
00137     {
00138       std::cout << "Caught CORBA::Exception.";
00139     }
00140   catch (...)
00141     {
00142       std::cout << "Caught unknown exception.";
00143     }
00144   return obj._retn();
00145 }
00146