Back to index

salome-kernel  6.5.0
NamingService_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   : NamingService_WaitForServerReadiness.cxx
00024 //  Author : Paul RASCLE (EDF)
00025 //  Module : KERNEL
00026 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/NamingService/NamingService_WaitForServerReadiness.cxx,v 1.7.2.1.10.2.12.1 2012-04-12 14:05:18 vsr Exp $
00027 //
00028 #include "NamingService_WaitForServerReadiness.hxx"
00029 #include "utilities.h"
00030 #include <iostream>
00031 #include <ctime>
00032 
00033 // ============================================================================
00044 // ============================================================================
00045 
00046 
00047 void NamingService_WaitForServerReadiness(SALOME_NamingService* NS,
00048                                           std::string serverName)
00049 {
00050   long TIMESleep = 500000000; // 500 ms.
00051   int NumberOfTries = 40;     // total wait = 20 s.
00052   int found = 0;
00053 
00054   timespec ts_req;
00055   ts_req.tv_nsec=TIMESleep;
00056   ts_req.tv_sec=0;
00057   timespec ts_rem;
00058   ts_rem.tv_nsec=0;
00059   ts_rem.tv_sec=0;
00060 
00061   ASSERT(NS);
00062   for (int itry=0; itry < NumberOfTries; itry++)
00063     {
00064       try
00065         {
00066           if (serverName.length() == 0)
00067             {
00068               CORBA::String_var dummyadr = NS->getIORaddr(); // to wait for naming service
00069               found = 1;
00070               break; // naming service found
00071             }
00072           else
00073             {
00074               CORBA::Object_var obj = NS->Resolve(serverName.c_str());
00075               if (! CORBA::is_nil(obj))
00076                 {
00077                   found =1;
00078                   break; // server found, no more try to do
00079                 }
00080               MESSAGE("Server "<< serverName <<" not yet ready, waiting...");
00081 #ifndef WIN32
00082               nanosleep(&ts_req,&ts_rem); // wait before retry
00083 #else
00084               Sleep(TIMESleep/1000000);
00085 #endif
00086             }
00087         }
00088       catch( ServiceUnreachable& )
00089         {
00090           MESSAGE("CORBA::COMM_FAILURE: Naming Service not yet ready, waiting...");
00091 #ifndef WIN32
00092           nanosleep(&ts_req,&ts_rem); // wait before retry
00093 #else
00094           Sleep(TIMESleep/1000000);
00095 #endif
00096         }
00097     }
00098   if (!found)
00099     {
00100     INFOS("Server "<< serverName <<" not found, abort...");
00101     exit(EXIT_FAILURE);
00102     }
00103 }