Back to index

salome-kernel  6.5.0
SALOME_Registry_Server.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 //  SALOME Registry : Registry server implementation
00024 //  File   : SALOME_Registry_Server.cxx
00025 //  Author : Pascale NOYRET - Antoine YESSAYAN, EDF
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Registry/SALOME_Registry_Server.cxx,v 1.14.2.1.10.2.12.1 2012-04-12 14:05:19 vsr Exp $
00028 //
00029 #include <stdlib.h>
00030 #include <iostream>
00031 #include <fstream>
00032 
00033 extern "C"
00034 {
00035 # include <stdio.h>
00036 }
00037 
00038 #include "utilities.h"
00039 #include "Utils_ORB_INIT.hxx"
00040 #include "Utils_SINGLETON.hxx"
00041 #include "Utils_SALOME_Exception.hxx"
00042 #include "Utils_CommException.hxx"
00043 #include "ServiceUnreachable.hxx"
00044 #include "SALOME_NamingService.hxx"
00045 #include "RegistryService.hxx"
00046 
00047 #ifdef CHECKTIME
00048 #include <Utils_Timer.hxx>
00049 #endif
00050 
00051 int main( int argc , char **argv )
00052 {
00053   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
00054   CORBA::ORB_var &orb = init( argc , argv ) ;
00055   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
00056   BEGIN_OF( argv[0] )
00057     INFOS_COMPILATION 
00058     SCRUTE(argc) 
00059     if( argc<3 )
00060       {
00061         MESSAGE("you must provide the Salome session name when you call SALOME_Registry_Server") ;
00062         throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server") ;
00063       }
00064   const char *ptrSessionName=0 ;
00065 
00066   int k=0 ;
00067   for ( k=1 ; k<argc ; k++ )
00068     {
00069       if( strcmp(argv[k],"--salome_session")==0 )
00070         {
00071           ptrSessionName=argv[k+1] ;
00072           break ;
00073         }
00074     }
00075   ASSERT(ptrSessionName) ;
00076   ASSERT(strlen( ptrSessionName )>0) ;
00077   const char *registryName = "Registry" ;
00078   long TIMESleep = 250000000;
00079   int NumberOfTries = 40;
00080 #ifndef WIN32
00081   int a;
00082 #endif
00083   timespec ts_req;
00084   ts_req.tv_nsec=TIMESleep;
00085   ts_req.tv_sec=0;
00086   timespec ts_rem;
00087   ts_rem.tv_nsec=0;
00088   ts_rem.tv_sec=0;
00089   CosNaming::NamingContext_var inc;
00090   PortableServer::POA_var poa;
00091   CORBA::Object_var theObj;
00092   CORBA::Object_var obj;
00093   CORBA::Object_var object;
00094   SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
00095   Registry::Components_var varComponents;
00096   int REGISTRY=0;
00097   const char * Env = getenv("USE_LOGGER");
00098   int EnvL =0;
00099   if ((Env!=NULL) && (strlen(Env)))
00100     EnvL=1;
00101   CosNaming::Name name;
00102   name.length(1);
00103   name[0].id=CORBA::string_dup("Logger");  
00104   PortableServer::POAManager_var manager; 
00105   for (int i = 1; i<=NumberOfTries; i++)
00106     {
00107       if (i!=1) 
00108 #ifndef WIN32
00109         a=nanosleep(&ts_req,&ts_rem);
00110 #else
00111     Sleep(TIMESleep/1000000);
00112 #endif
00113       try
00114         { 
00115           obj = orb->resolve_initial_references("RootPOA");
00116           if(!CORBA::is_nil(obj))
00117             poa = PortableServer::POA::_narrow(obj);
00118           if(!CORBA::is_nil(poa))
00119             manager = poa->the_POAManager();
00120           if(!CORBA::is_nil(orb)) 
00121             theObj = orb->resolve_initial_references("NameService");
00122           if (!CORBA::is_nil(theObj))
00123             inc = CosNaming::NamingContext::_narrow(theObj);
00124         }
00125       catch( CORBA::SystemException& )
00126         {
00127           MESSAGE( "Registry Server: CORBA::SystemException: Unable to contact the Naming Service" );
00128         }
00129       if(!CORBA::is_nil(inc))
00130         {
00131           MESSAGE( "Registry Server: Naming Service was found" );
00132           if(EnvL==1)
00133             {
00134               for(int j=1; j<=NumberOfTries; j++)
00135                 {
00136                   if (j!=1) 
00137 #ifndef WIN32
00138                     a=nanosleep(&ts_req, &ts_rem);
00139 #else
00140                         Sleep(TIMESleep/1000000);
00141 #endif
00142                   try
00143                     {
00144                       object = inc->resolve(name);
00145                     }
00146                   catch(CosNaming::NamingContext::NotFound)
00147                     {
00148                       MESSAGE( "Registry Server: Logger Server wasn't found" );
00149                     }
00150                   catch(...)
00151                     {
00152                       MESSAGE( "Registry Server: Unknown exception" );
00153                     }
00154                   if (!CORBA::is_nil(object))
00155                     {
00156                       MESSAGE( "Module Catalog Server: Logger Server was found" );
00157                       REGISTRY=1;
00158                       break;
00159                     }
00160                 }
00161             }
00162         }
00163       if ((REGISTRY==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
00164         break;
00165     }
00166 
00167   try
00168     {
00169       naming.init_orb( orb ) ;
00170       RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance() ;
00171       ptrRegistry->SessionName( ptrSessionName ) ;
00172       ptrRegistry->SetOrb(orb);
00173       varComponents = ptrRegistry->_this() ;
00174       ptrRegistry->_remove_ref(); //let poa manage registryservice deletion
00175       // The RegistryService must not already exist.
00176             
00177       try
00178         {
00179           CORBA::Object_var pipo = naming.Resolve( registryName ) ;
00180           if (CORBA::is_nil(pipo) )  throw ServiceUnreachable() ;
00181           MESSAGE("RegistryService servant already existing" ) ;
00182           exit( EXIT_FAILURE ) ;
00183         }
00184       catch( const ServiceUnreachable & )
00185         {
00186         }
00187       catch( const CORBA::Exception & )
00188         {
00189         }
00190       std::string absoluteName = std::string("/") + registryName;
00191       naming.Register( varComponents , absoluteName.c_str() ) ;
00192       MESSAGE("Wait client requests") ;
00193       try
00194         {
00195           // Activation du POA
00196           MESSAGE("POA activation") ;
00197           manager->activate() ;
00198                 
00199           // Lancement de l'ORB
00200           MESSAGE("ORB launching") ;
00201 #ifdef CHECKTIME
00202           Utils_Timer timer;
00203           timer.Start();
00204           timer.Stop();
00205           MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
00206           timer.ShowAbsolute();
00207 #endif
00208           orb->run() ;
00209         }
00210       catch( const CORBA::Exception & )
00211         {
00212           MESSAGE("System error") ;
00213           return EXIT_FAILURE ;
00214         }
00215             
00216     }
00217   catch( const SALOME_Exception &ex )
00218     {
00219       MESSAGE( "Communication Error : " << ex.what() )
00220         return EXIT_FAILURE ;
00221     }
00222         
00223   END_OF( argv[0] ) ;
00224   //  delete myThreadTrace;
00225   return 0 ;
00226 }