Salome HOME
PR: merge from tag mergeto_trunk_17Jan05
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Server.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
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   : SALOMEDS_Server.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28
29 #include <SALOMEconfig.h>
30 #include CORBA_SERVER_HEADER(SALOMEDS)
31 #include "SALOMEDS_StudyManager_i.hxx"
32 #include "utilities.h"
33 #include "Utils_SINGLETON.hxx"
34 #include "SALOME_NamingService.hxx"
35
36 #ifdef CHECKTIME
37 #include <Utils_Timer.hxx>
38 #endif
39 using namespace std;
40
41 // extern "C"
42 // { // for ccmalloc memory debug
43 // void ccmalloc_static_initialization(void);
44 // }
45
46 ///////////////////////////// MAIN ///////////////////////////////////////
47 //////////////////////////////////////////////////////////////////////////
48 int main(int argc, char** argv)
49 {
50   //   ccmalloc_static_initialization();
51
52   MESSAGE("SALOMEDS_Server - main");
53   try 
54     {
55       // Initialise the ORB.
56       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB3");
57       omniORB::MaxMessageSize(100 * 1024 * 1024);
58       // Obtain a reference to the root POA.
59       //
60       long TIMESleep = 250000000;
61       int NumberOfTries = 40;
62       int a;
63       timespec ts_req;
64       ts_req.tv_nsec=TIMESleep;
65       ts_req.tv_sec=0;
66       timespec ts_rem;
67       ts_rem.tv_nsec=0;
68       ts_rem.tv_sec=0;
69       CosNaming::NamingContext_var inc;
70       PortableServer::POA_var poa;
71       CORBA::Object_var theObj;
72       CORBA::Object_var obj;
73       CORBA::Object_var object;
74       int SALOMEDS=0;
75       const char * Env = getenv("USE_LOGGER");
76       int EnvL =0;
77       if ((Env!=NULL) && (strlen(Env)))
78         EnvL=1;
79       CosNaming::Name name;
80       name.length(1);
81       name[0].id=CORBA::string_dup("Logger");    
82       PortableServer::POAManager_var pman; 
83       for (int i = 1; i<=NumberOfTries; i++)
84         {
85           if (i!=1) 
86             a=nanosleep(&ts_req,&ts_rem);
87           try
88             { 
89               obj = orb->resolve_initial_references("RootPOA");
90               if(!CORBA::is_nil(obj))
91                 poa = PortableServer::POA::_narrow(obj);
92               if(!CORBA::is_nil(poa))
93                 pman = poa->the_POAManager();
94               if(!CORBA::is_nil(orb)) 
95                 theObj = orb->resolve_initial_references("NameService"); 
96               if (!CORBA::is_nil(theObj)){
97                 inc = CosNaming::NamingContext::_narrow(theObj);
98                 if(!CORBA::is_nil(inc))
99                   {
100                     MESSAGE( "SalomeDS Server: Naming Service was found" );
101                     if(EnvL==1)
102                       {
103                         CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
104                         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
105                         NS.init_orb( orb1 ) ;
106                         for(int j=1; j<=NumberOfTries; j++)
107                           {
108                             if (j!=1) 
109                               a=nanosleep(&ts_req, &ts_rem);
110                             try
111                               {
112                                 object = inc->resolve(name);
113                               }
114                             catch(CosNaming::NamingContext::NotFound)
115                               { 
116                                 MESSAGE( "SalomeDS Server: Logger Server wasn't found" ); }
117
118                             catch(...)
119                               {
120                                 MESSAGE( "SalomeDS Server: Unknown exception" );
121                               }
122                             if (!CORBA::is_nil(object))
123                               {
124                                 MESSAGE( "SalomeDS Server: Logger Server was found" );
125                                 SALOMEDS=1;
126                                 break;
127                               }
128                           }
129                       }
130                   }
131               }
132         
133             }
134           catch( const SALOME_Exception &ex )
135             {
136               MESSAGE( "Communication Error : " << ex.what() );
137               return EXIT_FAILURE ;
138             }
139           if ((SALOMEDS==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
140             break;
141         }
142     
143       // We allocate the objects on the heap.  Since these are reference
144       // counted objects, they will be deleted by the POA when they are no
145       // longer needed.    
146       SALOMEDS_StudyManager_i * myStudyManager_i = new  SALOMEDS_StudyManager_i(orb,poa);
147  
148       // Activate the objects.  This tells the POA that the objects are
149       // ready to accept requests.
150       PortableServer::ObjectId_var myStudyManager_iid = poa->activate_object(myStudyManager_i);
151       myStudyManager_i->register_name("/myStudyManager");
152        
153       // Obtain a POAManager, and tell the POA to start accepting
154       // requests on its objects.
155       pman = poa->the_POAManager();
156       pman->activate();
157
158 #ifdef CHECKTIME
159       Utils_Timer timer;
160       timer.Start();
161       timer.Stop();
162       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
163       timer.ShowAbsolute();
164 #endif
165       orb->run();
166       orb->destroy();
167     }
168   catch(CORBA::SystemException&)
169     {
170       MESSAGE( "Caught CORBA::SystemException." );
171     }
172   catch(CORBA::Exception&)
173     {
174       MESSAGE( "Caught CORBA::Exception." );
175     }
176   catch(omniORB::fatalException& fe)
177     {
178       MESSAGE( "Caught omniORB::fatalException:" );
179       MESSAGE( "  file: " << fe.file() );
180       MESSAGE( "  line: " << fe.line() );
181       MESSAGE( "  mesg: " << fe.errmsg() );
182     }
183   catch(...) 
184     {
185       MESSAGE( "Caught unknown exception." );
186     }
187   return 0;
188 }