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