Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_ModuleCatalog_Server.cxx
25 //  Module : SALOME
26
27 using namespace std;
28 /* $Header$ */
29 #include <iostream.h>
30 #include "SALOME_NamingService.hxx"
31 #include "SALOME_ModuleCatalog_impl.hxx"
32 #include "utilities.h"
33 # include "Utils_SINGLETON.hxx"
34
35 //#define CHECKTIME
36 #ifdef CHECKTIME
37 #include <Utils_Timer.hxx>
38 #endif
39
40 //#define CHECKTIME
41 #ifdef CHECKTIME
42 #include <Utils_Timer.hxx>
43 #endif
44
45 int main(int argc,char **argv)
46 {
47   try {
48   CosNaming::NamingContext_var _rootContext, catalogContext;
49
50   // initialize the ORB
51
52   CORBA::ORB_ptr orb = CORBA::ORB_init (argc, argv);
53
54   // initialize POA
55   //
56    long TIMESleep = 250000000;
57    int NumberOfTries = 40;
58    int a;
59    timespec ts_req;
60    ts_req.tv_nsec=TIMESleep;
61    ts_req.tv_sec=0;
62    timespec ts_rem;
63    ts_rem.tv_nsec=0;
64    ts_rem.tv_sec=0;
65    CosNaming::NamingContext_var inc;
66    PortableServer::POA_var poa;
67    CORBA::Object_var theObj;
68    CORBA::Object_var obj;
69    CORBA::Object_var object;
70    SALOME_Logger::Logger_var log;
71    int MODULE_CATALOG=0;
72    const char * Env = getenv("USE_LOGGER");
73    int EnvL =0;
74    if ((Env!=NULL) && (strlen(Env)))
75      EnvL=1;
76    CosNaming::Name name;
77    name.length(1);
78    name[0].id=CORBA::string_dup("Logger");    
79    PortableServer::POAManager_var mgr; 
80    for (int i = 1; i<=NumberOfTries; i++){
81      if (i!=1) 
82        a=nanosleep(&ts_req,&ts_rem);
83      try{ 
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::COMM_FAILURE& )
93        {
94          MESSAGE( "Module Catalog Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
95            }
96      if (!CORBA::is_nil(theObj)){
97        inc = CosNaming::NamingContext::_narrow(theObj);
98        if(!CORBA::is_nil(inc)) {
99          MESSAGE( "Module Catalog Server: Naming Service was found" )
100          if(EnvL==1){
101            CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
102            SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
103            NS.init_orb( orb1 ) ;
104            for(int j=1; j<=NumberOfTries; j++){
105              if (j!=1) 
106                a=nanosleep(&ts_req, &ts_rem);
107              try{
108                object = inc->resolve(name);}
109              catch(CosNaming::NamingContext::NotFound){ MESSAGE( "Logger Server wasn't found" ) }
110              catch(...){ MESSAGE( "Module Catalog Server: Unknown exception" ) }
111              if (!CORBA::is_nil(object))
112                log = SALOME_Logger::Logger::_narrow(object);
113              if (!CORBA::is_nil(log)){
114                MESSAGE( "Module Catalog Server: Logger Server was found" )
115                log->ping();
116                MODULE_CATALOG=1;
117                break;
118              }
119                  }}
120        }}
121      if ((MODULE_CATALOG==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
122        break;
123          }
124   
125    //
126   // Active catalog
127
128   SALOME_ModuleCatalogImpl* Catalogue_i = new SALOME_ModuleCatalogImpl(argc, argv);
129   poa->activate_object (Catalogue_i);
130
131   mgr->activate();
132
133   
134   CORBA::Object_ptr myCata = Catalogue_i->_this();
135
136   // initialise Naming Service
137   SALOME_NamingService *_NS;
138   _NS = new SALOME_NamingService(orb);
139   // register Catalog in Naming Service
140   _NS->Register(myCata ,"/Kernel/ModulCatalog");
141
142   MESSAGE("Running CatalogServer.");
143
144 #ifdef CHECKTIME
145   Utils_Timer timer;
146   timer.Start();
147   timer.Stop();
148   MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
149   timer.ShowAbsolute();
150 #endif
151   orb->run();
152  
153   poa->destroy(1,1);
154  
155   }
156     catch(CORBA::SystemException&) {
157     INFOS("Caught CORBA::SystemException.")
158   }
159     catch(CORBA::Exception&) {
160     INFOS("Caught CORBA::Exception.")
161   }
162
163   return 0;
164 }