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