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