Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[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 /* $Header$ */
27
28 #include <iostream>
29 #include "SALOME_NamingService.hxx"
30 #include "SALOME_ModuleCatalog_impl.hxx"
31 #include "utilities.h"
32 #include "SALOMETraceCollector.hxx"
33 #include "Utils_SINGLETON.hxx"
34
35 #ifdef CHECKTIME
36 #include <Utils_Timer.hxx>
37 #endif
38 using namespace std;
39
40 int main(int argc,char **argv)
41 {
42   // initialize the ORB
43   CORBA::ORB_ptr orb = CORBA::ORB_init (argc, argv);
44   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
45   try 
46     {
47       CosNaming::NamingContext_var _rootContext, catalogContext;
48
49       // initialize POA
50       //
51       long TIMESleep = 500000000;
52       int NumberOfTries = 40;
53       int a;
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             a=nanosleep(&ts_req,&ts_rem);
78           try
79             { 
80               obj = orb->resolve_initial_references("RootPOA");
81               if(!CORBA::is_nil(obj))
82                 poa = PortableServer::POA::_narrow(obj);
83               if(!CORBA::is_nil(poa))
84                 mgr = poa->the_POAManager();
85               if(!CORBA::is_nil(orb)) 
86                 theObj = orb->resolve_initial_references("NameService"); 
87             }
88           catch( CORBA::COMM_FAILURE& )
89             {
90               INFOS( "Module Catalog Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
91             }
92           if (!CORBA::is_nil(theObj))
93             {
94               inc = CosNaming::NamingContext::_narrow(theObj);
95               if(!CORBA::is_nil(inc))
96                 {
97                   INFOS( "Module Catalog Server: Naming Service was found" );
98                   if(EnvL==1)
99                     {
100                       CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
101                       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
102                       NS.init_orb( orb1 ) ;
103                       for(int j=1; j<=NumberOfTries; j++)
104                         {
105                           if (j!=1) 
106                             a=nanosleep(&ts_req, &ts_rem);
107                           try{
108                             object = inc->resolve(name);}
109                           catch(CosNaming::NamingContext::NotFound)
110                             {
111                               INFOS( "Logger Server wasn't found" );
112                             }
113                           catch(...)
114                             {
115                               INFOS( "Module Catalog Server: Unknown exception" ) ;
116                             }
117                           if (!CORBA::is_nil(object))
118                             {
119                               INFOS( "Module Catalog Server: Logger Server was found" );
120                               MODULE_CATALOG=1;
121                               break;
122                             }
123                         }
124                     }
125                 }
126             }
127           if ((MODULE_CATALOG==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
128             break;
129         }
130   
131       // Active catalog
132
133       SALOME_ModuleCatalogImpl Catalogue_i(argc, argv, orb);
134       poa->activate_object (&Catalogue_i);
135
136       mgr->activate();
137
138   
139       CORBA::Object_ptr myCata = Catalogue_i._this();
140
141       // initialise Naming Service
142       SALOME_NamingService *_NS;
143       _NS = new SALOME_NamingService(orb);
144       // register Catalog in Naming Service
145       _NS->Register(myCata ,"/Kernel/ModulCatalog");
146
147       MESSAGE("Running CatalogServer.");
148
149 #ifdef CHECKTIME
150       Utils_Timer timer;
151       timer.Start();
152       timer.Stop();
153       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
154       timer.ShowAbsolute();
155 #endif
156       orb->run();
157  
158       mgr->deactivate(true,true);
159       poa->destroy(1,1);
160
161     }
162   catch(CORBA::SystemException&) {
163     INFOS("Caught CORBA::SystemException.")
164       }
165   catch(CORBA::Exception&) {
166     INFOS("Caught CORBA::Exception.")
167       }
168
169   delete myThreadTrace;
170   return 0;
171 }