Salome HOME
import SalomePro v1.2c
[modules/kernel.git] / src / Registry / SALOME_Registry_Server.cxx
1 //  SALOME Registry : Registry server implementation
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Registry_Server.cxx
25 //  Author : Pascale NOYRET - Antoine YESSAYAN, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 # include <stdlib.h>
31 # include <iostream.h>
32 # include <fstream.h>
33
34 extern "C"
35 {
36 # include <stdio.h>
37 }
38
39 # include "utilities.h"
40 # include "Utils_ORB_INIT.hxx"
41 # include "Utils_SINGLETON.hxx"
42 # include "Utils_SALOME_Exception.hxx"
43 # include "Utils_CommException.hxx"
44 # include "ServiceUnreachable.hxx"
45 # include "SALOME_NamingService.hxx"
46 # include "RegistryService.hxx"
47
48 //#define CHECKTIME
49 #ifdef CHECKTIME
50 #include <Utils_Timer.hxx>
51 #endif
52
53 int main( int argc , char **argv )
54 {
55         BEGIN_OF( argv[0] )
56         INFOS_COMPILATION 
57         SCRUTE(argc) 
58         if( argc<3 )
59         {
60                 MESSAGE("you must provide the Salome session name when you call SALOME_Registry_Server") ;
61                 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server") ;
62         }
63         const char *ptrSessionName=0 ;
64
65         int k=0 ;
66         for ( k=1 ; k<argc ; k++ )
67         {
68                 if( strcmp(argv[k],"--salome_session")==0 )
69                 {
70                         ptrSessionName=argv[k+1] ;
71                         break ;
72                 }
73         }
74         ASSERT(ptrSessionName) ;
75         ASSERT(strlen( ptrSessionName )>0) ;
76         const char *registryName = "Registry" ;
77         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
78         CORBA::ORB_var &orb = init( argc , argv ) ;
79         //
80         long TIMESleep = 250000000;
81         int NumberOfTries = 40;
82         int a;
83         timespec ts_req;
84         ts_req.tv_nsec=TIMESleep;
85         ts_req.tv_sec=0;
86         timespec ts_rem;
87         ts_rem.tv_nsec=0;
88         ts_rem.tv_sec=0;
89         CosNaming::NamingContext_var inc;
90         PortableServer::POA_var poa;
91         CORBA::Object_var theObj;
92         CORBA::Object_var obj;
93         CORBA::Object_var object;
94         SALOME_Logger::Logger_var log;
95         SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
96         Registry::Components_var varComponents;
97         int REGISTRY=0;
98         const char * Env = getenv("USE_LOGGER");
99         int EnvL =0;
100         if ((Env!=NULL) && (strlen(Env)))
101           EnvL=1;
102         CosNaming::Name name;
103         name.length(1);
104         name[0].id=CORBA::string_dup("Logger");  
105         PortableServer::POAManager_var manager; 
106         for (int i = 1; i<=NumberOfTries; i++){
107           if (i!=1) 
108             a=nanosleep(&ts_req,&ts_rem);
109           try{ 
110             obj = orb->resolve_initial_references("RootPOA");
111             if(!CORBA::is_nil(obj))
112               poa = PortableServer::POA::_narrow(obj);
113             if(!CORBA::is_nil(poa))
114               manager = poa->the_POAManager();
115             if(!CORBA::is_nil(orb)) 
116               theObj = orb->resolve_initial_references("NameService");
117             if (!CORBA::is_nil(theObj))
118               inc = CosNaming::NamingContext::_narrow(theObj);
119             }
120           catch( CORBA::COMM_FAILURE& )
121             {
122               MESSAGE( "Registry Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
123                 }
124           if(!CORBA::is_nil(inc)) {
125             MESSAGE( "Registry Server: Naming Service was found" )
126               if(EnvL==1){
127                 for(int j=1; j<=NumberOfTries; j++){
128                   if (j!=1) 
129                     a=nanosleep(&ts_req, &ts_rem);
130                   try{
131                     object = inc->resolve(name);
132                   }
133                   catch(CosNaming::NamingContext::NotFound){ MESSAGE( "Registry Server: Logger Server wasn't found" ) }
134                   catch(...){ MESSAGE( "Registry Server: Unknown exception" ) }
135                  
136                   if (!CORBA::is_nil(object))
137                     log = SALOME_Logger::Logger::_narrow(object);
138                   if (!CORBA::is_nil(log)){
139                     MESSAGE( "Registry Server: Logger Server was found" )
140                       log->ping();
141                     REGISTRY=1;
142                     break;
143                   }
144                 }}
145           }
146           if ((REGISTRY==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
147             break;
148               }
149         //
150         try
151           {
152             naming.init_orb( orb ) ;
153             RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance() ;
154             ptrRegistry->SessionName( ptrSessionName ) ;
155             varComponents = ptrRegistry->_this() ;
156             // The RegistryService must not already exist.
157             
158             try
159               {
160                 CORBA::Object_var pipo = naming.Resolve( registryName ) ;
161                 if (CORBA::is_nil(pipo) )  throw ServiceUnreachable() ;
162                 MESSAGE("RegistryService servant already existing" ) ;
163                 exit( EXIT_FAILURE ) ;
164               }
165             catch( const ServiceUnreachable &ex )
166               {
167               }
168             catch( const CORBA::Exception &exx )
169               {
170               }
171             string absoluteName = string("/") + registryName;
172             naming.Register( varComponents , absoluteName.c_str() ) ;
173             MESSAGE("On attend les requetes des clients") ;
174             try
175               {
176                 // Activation du POA
177                 MESSAGE("Activation du POA") ;
178                 manager->activate() ;
179                 
180                 // Lancement de l'ORB
181                 MESSAGE("Lancement de l'ORB") ;
182 #ifdef CHECKTIME
183                 Utils_Timer timer;
184                 timer.Start();
185                 timer.Stop();
186                 MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
187                 timer.ShowAbsolute();
188 #endif
189                 orb->run() ;
190               }
191             catch( const CORBA::Exception &ex )
192               {
193                 MESSAGE("Erreur systeme") ;
194                 return EXIT_FAILURE ;
195               }
196             
197           }
198         catch( const SALOME_Exception &ex )
199           {
200             MESSAGE( "Communication Error : " << ex.what() )
201               return EXIT_FAILURE ;
202           }
203         
204         END_OF( argv[0] ) ;
205         return 0 ;
206 }