Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 int main( int argc , char **argv )
53 {
54   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
55   CORBA::ORB_var &orb = init( argc , argv ) ;
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   int a;
82   timespec ts_req;
83   ts_req.tv_nsec=TIMESleep;
84   ts_req.tv_sec=0;
85   timespec ts_rem;
86   ts_rem.tv_nsec=0;
87   ts_rem.tv_sec=0;
88   CosNaming::NamingContext_var inc;
89   PortableServer::POA_var poa;
90   CORBA::Object_var theObj;
91   CORBA::Object_var obj;
92   CORBA::Object_var object;
93   SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
94   Registry::Components_var varComponents;
95   int REGISTRY=0;
96   const char * Env = getenv("USE_LOGGER");
97   int EnvL =0;
98   if ((Env!=NULL) && (strlen(Env)))
99     EnvL=1;
100   CosNaming::Name name;
101   name.length(1);
102   name[0].id=CORBA::string_dup("Logger");  
103   PortableServer::POAManager_var manager; 
104   for (int i = 1; i<=NumberOfTries; i++)
105     {
106       if (i!=1) 
107 #ifndef WNT
108         a=nanosleep(&ts_req,&ts_rem);
109 #else
110     Sleep(TIMESleep/1000000);
111 #endif
112       try
113         { 
114           obj = orb->resolve_initial_references("RootPOA");
115           if(!CORBA::is_nil(obj))
116             poa = PortableServer::POA::_narrow(obj);
117           if(!CORBA::is_nil(poa))
118             manager = poa->the_POAManager();
119           if(!CORBA::is_nil(orb)) 
120             theObj = orb->resolve_initial_references("NameService");
121           if (!CORBA::is_nil(theObj))
122             inc = CosNaming::NamingContext::_narrow(theObj);
123         }
124       catch( CORBA::SystemException& )
125         {
126           MESSAGE( "Registry Server: CORBA::SystemException: Unable to contact the Naming Service" );
127         }
128       if(!CORBA::is_nil(inc))
129         {
130           MESSAGE( "Registry Server: Naming Service was found" );
131           if(EnvL==1)
132             {
133               for(int j=1; j<=NumberOfTries; j++)
134                 {
135                   if (j!=1) 
136 #ifndef WNT
137                     a=nanosleep(&ts_req, &ts_rem);
138 #else
139                         Sleep(TIMESleep/1000000);
140 #endif
141                   try
142                     {
143                       object = inc->resolve(name);
144                     }
145                   catch(CosNaming::NamingContext::NotFound)
146                     {
147                       MESSAGE( "Registry Server: Logger Server wasn't found" );
148                     }
149                   catch(...)
150                     {
151                       MESSAGE( "Registry Server: Unknown exception" );
152                     }
153                   if (!CORBA::is_nil(object))
154                     {
155                       MESSAGE( "Module Catalog Server: Logger Server was found" );
156                       REGISTRY=1;
157                       break;
158                     }
159                 }
160             }
161         }
162       if ((REGISTRY==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
163         break;
164     }
165
166   try
167     {
168       naming.init_orb( orb ) ;
169       RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance() ;
170       ptrRegistry->SessionName( ptrSessionName ) ;
171       varComponents = ptrRegistry->_this() ;
172       // The RegistryService must not already exist.
173             
174       try
175         {
176           CORBA::Object_var pipo = naming.Resolve( registryName ) ;
177           if (CORBA::is_nil(pipo) )  throw ServiceUnreachable() ;
178           MESSAGE("RegistryService servant already existing" ) ;
179           exit( EXIT_FAILURE ) ;
180         }
181       catch( const ServiceUnreachable &ex )
182         {
183         }
184       catch( const CORBA::Exception &exx )
185         {
186         }
187       string absoluteName = string("/") + registryName;
188       naming.Register( varComponents , absoluteName.c_str() ) ;
189       MESSAGE("On attend les requetes des clients") ;
190       try
191         {
192           // Activation du POA
193           MESSAGE("Activation du POA") ;
194           manager->activate() ;
195                 
196           // Lancement de l'ORB
197           MESSAGE("Lancement de l'ORB") ;
198 #ifdef CHECKTIME
199           Utils_Timer timer;
200           timer.Start();
201           timer.Stop();
202           MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
203           timer.ShowAbsolute();
204 #endif
205           orb->run() ;
206         }
207       catch( const CORBA::Exception &ex )
208         {
209           MESSAGE("Erreur systeme") ;
210           return EXIT_FAILURE ;
211         }
212             
213     }
214   catch( const SALOME_Exception &ex )
215     {
216       MESSAGE( "Communication Error : " << ex.what() )
217         return EXIT_FAILURE ;
218     }
219         
220   END_OF( argv[0] ) ;
221   //  delete myThreadTrace;
222   return 0 ;
223 }