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