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