Salome HOME
DCQ : Merge with Ecole_ete_a6.
[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 #include <iostream.h>
30 #include "SALOME_NamingService.hxx"
31 #include "SALOME_RessourcesCatalog_impl.hxx"
32 #include "utilities.h"
33 #include "Utils_SINGLETON.hxx"
34 using namespace std;
35
36 int main(int argc,char **argv)
37 {
38   try
39     {
40       CosNaming::NamingContext_var _rootContext, catalogContext;
41
42       // initialize the ORB
43
44       CORBA::ORB_ptr orb = CORBA::ORB_init (argc, argv);
45
46       // initialize POA
47       //
48       long TIMESleep = 250000000;
49       int NumberOfTries = 40;
50       int a;
51       timespec ts_req;
52       ts_req.tv_nsec=TIMESleep;
53       ts_req.tv_sec=0;
54       timespec ts_rem;
55       ts_rem.tv_nsec=0;
56       ts_rem.tv_sec=0;
57       CosNaming::NamingContext_var inc;
58       PortableServer::POA_var poa;
59       CORBA::Object_var theObj;
60       CORBA::Object_var obj;
61       CORBA::Object_var object;
62       SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
63       int RESSOURCES_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             a=nanosleep(&ts_req,&ts_rem);
76           try
77             { 
78               obj = orb->resolve_initial_references("RootPOA");
79               if(!CORBA::is_nil(obj))
80                 poa = PortableServer::POA::_narrow(obj);
81               if(!CORBA::is_nil(poa))
82                 mgr = poa->the_POAManager();
83               if(!CORBA::is_nil(orb)) 
84                 theObj = orb->resolve_initial_references("NameService");
85               if (!CORBA::is_nil(theObj))
86                 inc = CosNaming::NamingContext::_narrow(theObj);
87             }
88           catch( CORBA::COMM_FAILURE& )
89             {
90               INFOS( "Ressources Catalog: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
91             }
92           if(!CORBA::is_nil(inc)) 
93             {
94               MESSAGE( "Ressources Catalog: Naming Service was found" );
95               if(EnvL==1)
96                 {
97                   for(int j=1; j<=NumberOfTries; j++)
98                     {
99                       if (j!=1) 
100                         a=nanosleep(&ts_req, &ts_rem);
101                       try{
102                         object = inc->resolve(name);
103                       }
104                       catch(CosNaming::NamingContext::NotFound)
105                         { 
106                           INFOS( "Ressources Catalog: Logger Server wasn't found" );
107                         }
108                       catch(...)
109                         { 
110                           INFOS( "Ressources Catalog: Unknown exception" );
111                         }
112                       if (!CORBA::is_nil(object))
113                         {
114                           MESSAGE( "Ressources Catalog: Loger Server was found" );
115                           RESSOURCES_CATALOG=1;
116                           break;
117                         }
118                     }
119                 }
120             }
121           if ((RESSOURCES_CATALOG==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
122             break;
123         }
124
125       // Active catalog
126   
127       SALOME_RessourcesCatalogImpl* Catalogue_i = new SALOME_RessourcesCatalogImpl(argc, argv);
128       poa->activate_object (Catalogue_i);
129       mgr->activate();
130       CORBA::Object_ptr myCata = Catalogue_i->_this();
131
132       // initialise Naming Service
133       SALOME_NamingService *_NS;
134       _NS = new SALOME_NamingService(orb);
135       // register Catalog in Naming Service
136       _NS->Register(myCata ,"/Kernel/RessourcesCatalog");
137
138       MESSAGE("Running Ressources Catalog Server.")
139
140         orb->run();
141  
142       poa->destroy(1,1);
143  
144     }
145   catch(CORBA::SystemException&) {
146     INFOS("Caught CORBA::SystemException.")
147       }
148   catch(CORBA::Exception&) {
149     INFOS("Caught CORBA::Exception.")
150       }
151
152   return 0;
153 }