Salome HOME
python_test_driver.py declination of salome_test_driver.py for SSL mode (timeout...
[modules/kernel.git] / src / Registry / SALOME_Registry_Server.cxx
1 // Copyright (C) 2007-2021  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 //  SALOME Registry : Registry server implementation
24 //  File   : SALOME_Registry_Server.cxx
25 //  Author : Pascale NOYRET - Antoine YESSAYAN, EDF
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include <stdlib.h>
30 #include <iostream>
31 #include <fstream>
32
33 extern "C"
34 {
35 # include <stdio.h>
36 }
37
38 #include "utilities.h"
39 #include "Utils_ORB_INIT.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "Utils_SALOME_Exception.hxx"
42 #include "Utils_CommException.hxx"
43 #include "ServiceUnreachable.hxx"
44 #include "SALOME_NamingService.hxx"
45 #include "RegistryService.hxx"
46
47 #ifdef CHECKTIME
48 #include <Utils_Timer.hxx>
49 #endif
50
51 int main( int argc , char **argv )
52 {
53   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
54   CORBA::ORB_var &orb = init( argc , argv ) ;
55   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
56   BEGIN_OF( argv[0] )
57     INFOS_COMPILATION 
58     SCRUTE(argc) 
59     if( argc<3 )
60       {
61         MESSAGE("you must provide the Salome session name when you call SALOME_Registry_Server") ;
62         throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server") ;
63       }
64   const char *ptrSessionName=0 ;
65
66   int k=0 ;
67   for ( k=1 ; k<argc ; k++ )
68     {
69       if( strcmp(argv[k],"--salome_session")==0 )
70         {
71           ptrSessionName=argv[k+1] ;
72           break ;
73         }
74     }
75   ASSERT(ptrSessionName) ;
76   ASSERT(strlen( ptrSessionName )>0) ;
77   const char *registryName = "Registry" ;
78   long TIMESleep = 250000000;
79   int NumberOfTries = 40;
80   timespec ts_req;
81   ts_req.tv_nsec=TIMESleep;
82   ts_req.tv_sec=0;
83   timespec ts_rem;
84   ts_rem.tv_nsec=0;
85   ts_rem.tv_sec=0;
86   CosNaming::NamingContext_var inc;
87   PortableServer::POA_var poa;
88   CORBA::Object_var theObj;
89   CORBA::Object_var obj;
90   CORBA::Object_var object;
91   SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
92   Registry::Components_var varComponents;
93   int REGISTRY=0;
94   const char * Env = getenv("USE_LOGGER");
95   int EnvL =0;
96   if ((Env!=NULL) && (strlen(Env)))
97     EnvL=1;
98   CosNaming::Name name;
99   name.length(1);
100   name[0].id=CORBA::string_dup("Logger");  
101   PortableServer::POAManager_var manager; 
102   for (int i = 1; i<=NumberOfTries; i++)
103     {
104       if (i!=1) 
105 #ifndef WIN32
106         nanosleep(&ts_req,&ts_rem);
107 #else
108     Sleep(TIMESleep/1000000);
109 #endif
110       try
111         { 
112           obj = orb->resolve_initial_references("RootPOA");
113           if(!CORBA::is_nil(obj))
114             poa = PortableServer::POA::_narrow(obj);
115           if(!CORBA::is_nil(poa))
116             manager = poa->the_POAManager();
117           if(!CORBA::is_nil(orb)) 
118             theObj = orb->resolve_initial_references("NameService");
119           if (!CORBA::is_nil(theObj))
120             inc = CosNaming::NamingContext::_narrow(theObj);
121         }
122       catch( CORBA::SystemException& )
123         {
124           MESSAGE( "Registry Server: CORBA::SystemException: Unable to contact the Naming Service" );
125         }
126       if(!CORBA::is_nil(inc))
127         {
128           MESSAGE( "Registry Server: Naming Service was found" );
129           if(EnvL==1)
130             {
131               for(int j=1; j<=NumberOfTries; j++)
132                 {
133                   if (j!=1) 
134 #ifndef WIN32
135                     nanosleep(&ts_req, &ts_rem);
136 #else
137                         Sleep(TIMESleep/1000000);
138 #endif
139                   try
140                     {
141                       object = inc->resolve(name);
142                     }
143                   catch(CosNaming::NamingContext::NotFound&)
144                     {
145                       MESSAGE( "Registry Server: Logger Server wasn't found" );
146                     }
147                   catch(...)
148                     {
149                       MESSAGE( "Registry Server: Unknown exception" );
150                     }
151                   if (!CORBA::is_nil(object))
152                     {
153                       MESSAGE( "Module Catalog Server: Logger Server was found" );
154                       REGISTRY=1;
155                       break;
156                     }
157                 }
158             }
159         }
160       if ((REGISTRY==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
161         break;
162     }
163
164   try
165     {
166       naming.init_orb( orb ) ;
167       RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance() ;
168       ptrRegistry->SessionName( ptrSessionName ) ;
169       ptrRegistry->SetOrb(orb);
170       //
171       CORBA::PolicyList policies;
172       policies.length(1);
173       PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
174       policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
175       PortableServer::POA_var poa2(poa->create_POA("SingleThreadPOA4RegistryStandalone",manager,policies));
176       threadPol->destroy();
177       //
178       PortableServer::ObjectId_var id(poa2->activate_object(ptrRegistry));
179       CORBA::Object_var pipo=poa2->id_to_reference(id);
180       varComponents = Registry::Components::_narrow(pipo) ;
181       ptrRegistry->_remove_ref(); //let poa manage registryservice deletion
182       // The RegistryService must not already exist.
183             
184       try
185         {
186           CORBA::Object_var pipo = naming.Resolve( registryName ) ;
187           if (CORBA::is_nil(pipo) )  throw ServiceUnreachable() ;
188           MESSAGE("RegistryService servant already existing" ) ;
189           exit( EXIT_FAILURE ) ;
190         }
191       catch( const ServiceUnreachable & )
192         {
193         }
194       catch( const CORBA::Exception & )
195         {
196         }
197       std::string absoluteName = std::string("/") + registryName;
198       naming.Register( varComponents , absoluteName.c_str() ) ;
199       MESSAGE("Wait client requests") ;
200       try
201         {
202           // Activation du POA
203           MESSAGE("POA activation") ;
204           manager->activate() ;
205                 
206           // Lancement de l'ORB
207           MESSAGE("ORB launching") ;
208 #ifdef CHECKTIME
209           Utils_Timer timer;
210           timer.Start();
211           timer.Stop();
212           MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
213           timer.ShowAbsolute();
214 #endif
215           orb->run() ;
216         }
217       catch( const CORBA::Exception & )
218         {
219           MESSAGE("System error") ;
220           return EXIT_FAILURE ;
221         }
222             
223     }
224   catch( const SALOME_Exception &ex )
225     {
226       MESSAGE( "Communication Error : " << ex.what() )
227         return EXIT_FAILURE ;
228     }
229         
230   END_OF( argv[0] ) ;
231   //  delete myThreadTrace;
232   return 0 ;
233 }