Salome HOME
Merge from V5_1_3_BR 07/12/2009
[modules/kernel.git] / src / ModuleCatalog / SALOME_ModuleCatalog_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 ModuleCatalog : implementation of ModuleCatalog server which parsers xml description of modules
23 //  File   : SALOME_ModuleCatalog_Server.cxx
24 //  Module : SALOME
25 //
26 /* $Header$ */
27
28 #include <iostream>
29 #include "SALOME_NamingService.hxx"
30 #include "SALOME_ModuleCatalog_impl.hxx"
31 #include "utilities.h"
32 #include "Utils_SINGLETON.hxx"
33
34 #ifdef CHECKTIME
35 #include <Utils_Timer.hxx>
36 #endif
37 using namespace std;
38
39 int main(int argc,char **argv)
40 {
41   // initialize the ORB
42   CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
43   try 
44     {
45       CosNaming::NamingContext_var _rootContext, catalogContext;
46
47       // initialize POA
48       //
49       long TIMESleep = 500000000;
50       int NumberOfTries = 40;
51       int a;
52       timespec ts_req;
53       ts_req.tv_nsec=TIMESleep;
54       ts_req.tv_sec=0;
55       timespec ts_rem;
56       ts_rem.tv_nsec=0;
57       ts_rem.tv_sec=0;
58       CosNaming::NamingContext_var inc;
59       PortableServer::POA_var poa;
60       CORBA::Object_var theObj;
61       CORBA::Object_var obj;
62       CORBA::Object_var object;
63       int MODULE_CATALOG=0;
64       const char * Env = getenv("USE_LOGGER");
65       int EnvL =0;
66       if ((Env!=NULL) && (strlen(Env)))
67         EnvL=1;
68       CosNaming::Name name;
69       name.length(1);
70       name[0].id=CORBA::string_dup("Logger");    
71       PortableServer::POAManager_var mgr; 
72       for (int i = 1; i<=NumberOfTries; i++)
73         {
74           if (i!=1) 
75 #ifndef WIN32
76             a=nanosleep(&ts_req,&ts_rem);
77 #else
78             Sleep(TIMESleep/1000000);
79 #endif
80           try
81             { 
82               obj = orb->resolve_initial_references("RootPOA");
83               if(!CORBA::is_nil(obj))
84                 poa = PortableServer::POA::_narrow(obj);
85               if(!CORBA::is_nil(poa))
86                 mgr = poa->the_POAManager();
87               if(!CORBA::is_nil(orb)) 
88                 theObj = orb->resolve_initial_references("NameService"); 
89             }
90           catch( CORBA::SystemException& )
91             {
92               INFOS( "Module Catalog Server: CORBA::SystemException: Unable to contact the Naming Service" );
93             }
94           if (!CORBA::is_nil(theObj))
95             {
96               inc = CosNaming::NamingContext::_narrow(theObj);
97               if(!CORBA::is_nil(inc))
98                 {
99                   INFOS( "Module Catalog Server: Naming Service was found" );
100                   if(EnvL==1)
101                     {
102                       CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
103                       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
104                       NS.init_orb( orb1 ) ;
105                       for(int j=1; j<=NumberOfTries; j++)
106                         {
107                           if (j!=1) 
108 #ifndef WIN32
109                             a=nanosleep(&ts_req, &ts_rem);
110 #else
111                             Sleep(TIMESleep/1000000);
112 #endif
113                           try{
114                             object = inc->resolve(name);}
115                           catch(CosNaming::NamingContext::NotFound)
116                             {
117                               INFOS( "Logger Server wasn't found" );
118                             }
119                           catch(...)
120                             {
121                               INFOS( "Module Catalog Server: Unknown exception" ) ;
122                             }
123                           if (!CORBA::is_nil(object))
124                             {
125                               INFOS( "Module Catalog Server: Logger Server was found" );
126                               MODULE_CATALOG=1;
127                               break;
128                             }
129                         }
130                     }
131                 }
132             }
133           if ((MODULE_CATALOG==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
134             break;
135         }
136   
137       // Active catalog
138
139       SALOME_ModuleCatalogImpl* Catalogue_i=new SALOME_ModuleCatalogImpl(argc, argv, orb);
140       PortableServer::ObjectId_var cataid = poa->activate_object (Catalogue_i);
141
142       //activate POA manager
143       mgr->activate();
144   
145       CORBA::Object_var myCata = Catalogue_i->_this();
146       Catalogue_i->_remove_ref();
147
148       // initialise Naming Service
149       SALOME_NamingService _NS(orb);
150       // register Catalog in Naming Service
151       _NS.Register(myCata ,"/Kernel/ModulCatalog");
152
153       MESSAGE("Running CatalogServer.");
154
155 #ifdef CHECKTIME
156       Utils_Timer timer;
157       timer.Start();
158       timer.Stop();
159       timer.ShowAbsolute();
160 #endif
161       orb->run();
162       orb->destroy();
163  
164 //       mgr->deactivate(true,true);
165 //       poa->destroy(1,1);
166
167     }
168   catch(CORBA::SystemException&) {
169     INFOS("Caught CORBA::SystemException.")
170       }
171   catch(CORBA::Exception&) {
172     INFOS("Caught CORBA::Exception.")
173       }
174
175   END_OF( argv[0] );
176   return 0;
177 }