Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[modules/kernel.git] / src / RessourcesCatalog / SALOME_RessourcesCatalog_Server.cxx
1 //  SALOME RessourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
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_RessourcesCatalog_Server.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include <iostream.h>
31 #include "SALOME_NamingService.hxx"
32 #include "SALOME_RessourcesCatalog_impl.hxx"
33 #include "utilities.h"
34 # include "Utils_SINGLETON.hxx"
35 int main(int argc,char **argv)
36 {
37   try {
38   CosNaming::NamingContext_var _rootContext, catalogContext;
39
40   // initialize the ORB
41
42   CORBA::ORB_ptr orb = CORBA::ORB_init (argc, argv);
43
44   // initialize POA
45   //
46   long TIMESleep = 250000000;
47         int NumberOfTries = 40;
48         int a;
49         timespec ts_req;
50         ts_req.tv_nsec=TIMESleep;
51         ts_req.tv_sec=0;
52         timespec ts_rem;
53         ts_rem.tv_nsec=0;
54         ts_rem.tv_sec=0;
55         CosNaming::NamingContext_var inc;
56         PortableServer::POA_var poa;
57         CORBA::Object_var theObj;
58         CORBA::Object_var obj;
59         CORBA::Object_var object;
60         SALOME_Logger::Logger_var log;
61         SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
62         int RESSOURCES_CATALOG=0;
63         const char * Env = getenv("USE_LOGGER");
64         int EnvL =0;
65         if ((Env!=NULL) && (strlen(Env)))
66           EnvL=1;
67         CosNaming::Name name;
68         name.length(1);
69         name[0].id=CORBA::string_dup("Logger");    
70         PortableServer::POAManager_var mgr; 
71         for (int i = 1; i<=NumberOfTries; i++){
72           if (i!=1) 
73             a=nanosleep(&ts_req,&ts_rem);
74           try{ 
75             obj = orb->resolve_initial_references("RootPOA");
76             if(!CORBA::is_nil(obj))
77               poa = PortableServer::POA::_narrow(obj);
78             if(!CORBA::is_nil(poa))
79               mgr = poa->the_POAManager();
80             if(!CORBA::is_nil(orb)) 
81           theObj = orb->resolve_initial_references("NameService");
82             if (!CORBA::is_nil(theObj))
83               inc = CosNaming::NamingContext::_narrow(theObj);
84             }
85           catch( CORBA::COMM_FAILURE& )
86             {
87               MESSAGE( "Ressources Catalog: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
88                 }
89           if(!CORBA::is_nil(inc)) {
90             MESSAGE( "Ressources Catalog: Naming Service was found" )
91               if(EnvL==1){
92                 for(int j=1; j<=NumberOfTries; j++){
93                   if (j!=1) 
94                     a=nanosleep(&ts_req, &ts_rem);
95                   try{
96                     object = inc->resolve(name);
97                   }
98                   catch(CosNaming::NamingContext::NotFound){ MESSAGE( "Ressources Catalog: Logger Server wasn't found" ) }
99                   catch(...){ MESSAGE( "Ressources Catalog: Unknown exception" ) }
100                   if (!CORBA::is_nil(object))
101                     log = SALOME_Logger::Logger::_narrow(object);
102                   if (!CORBA::is_nil(log)){
103                     MESSAGE( "Ressources Catalog: Loger Server was found" )
104                       log->ping();
105                     RESSOURCES_CATALOG=1;
106                     break;
107                   }
108                 }}
109           }
110           if ((RESSOURCES_CATALOG==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
111             break;
112               }
113   //
114   // Active catalog
115   
116   SALOME_RessourcesCatalogImpl* Catalogue_i = new SALOME_RessourcesCatalogImpl(argc, argv);
117   poa->activate_object (Catalogue_i);
118   mgr->activate();
119   CORBA::Object_ptr myCata = Catalogue_i->_this();
120
121   // initialise Naming Service
122   SALOME_NamingService *_NS;
123   _NS = new SALOME_NamingService(orb);
124   // register Catalog in Naming Service
125   _NS->Register(myCata ,"/Kernel/RessourcesCatalog");
126
127   MESSAGE("Running Ressources Catalog Server.")
128
129   orb->run();
130  
131   poa->destroy(1,1);
132  
133   }
134     catch(CORBA::SystemException&) {
135     INFOS("Caught CORBA::SystemException.")
136   }
137     catch(CORBA::Exception&) {
138     INFOS("Caught CORBA::Exception.")
139   }
140
141   return 0;
142 }