Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / Registry / SALOME_Registry_Server.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SALOME Registry : Registry server implementation
23 //  File   : SALOME_Registry_Server.cxx
24 //  Author : Pascale NOYRET - Antoine YESSAYAN, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include <stdlib.h>
29 #include <iostream>
30 #include <fstream>
31
32 extern "C"
33 {
34 # include <stdio.h>
35 }
36
37 #include "utilities.h"
38 #include "Utils_ORB_INIT.hxx"
39 #include "Utils_SINGLETON.hxx"
40 #include "Utils_SALOME_Exception.hxx"
41 #include "Utils_CommException.hxx"
42 #include "ServiceUnreachable.hxx"
43 #include "SALOME_NamingService.hxx"
44 #include "RegistryService.hxx"
45
46 #ifdef CHECKTIME
47 #include <Utils_Timer.hxx>
48 #endif
49 using namespace std;
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   int a;
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         a=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                     a=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       varComponents = ptrRegistry->_this() ;
172       ptrRegistry->_remove_ref(); //let poa manage registryservice deletion
173       // The RegistryService must not already exist.
174             
175       try
176         {
177           CORBA::Object_var pipo = naming.Resolve( registryName ) ;
178           if (CORBA::is_nil(pipo) )  throw ServiceUnreachable() ;
179           MESSAGE("RegistryService servant already existing" ) ;
180           exit( EXIT_FAILURE ) ;
181         }
182       catch( const ServiceUnreachable &ex )
183         {
184         }
185       catch( const CORBA::Exception &exx )
186         {
187         }
188       string absoluteName = string("/") + registryName;
189       naming.Register( varComponents , absoluteName.c_str() ) ;
190       MESSAGE("Wait client requests") ;
191       try
192         {
193           // Activation du POA
194           MESSAGE("POA activation") ;
195           manager->activate() ;
196                 
197           // Lancement de l'ORB
198           MESSAGE("ORB launching") ;
199 #ifdef CHECKTIME
200           Utils_Timer timer;
201           timer.Start();
202           timer.Stop();
203           MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
204           timer.ShowAbsolute();
205 #endif
206           orb->run() ;
207         }
208       catch( const CORBA::Exception &ex )
209         {
210           MESSAGE("System error") ;
211           return EXIT_FAILURE ;
212         }
213             
214     }
215   catch( const SALOME_Exception &ex )
216     {
217       MESSAGE( "Communication Error : " << ex.what() )
218         return EXIT_FAILURE ;
219     }
220         
221   END_OF( argv[0] ) ;
222   //  delete myThreadTrace;
223   return 0 ;
224 }