Salome HOME
PR: merge from branch BR_UT_V310a3 tag mergeto_trunk_05dec05
[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 #if OMNIORB_VERSION >= 4
57       const char* options[][2] = { { "giopMaxMsgSize", "104857600" }, { 0, 0 } };
58       CORBA::ORB_var orb = CORBA::ORB_init( argc , argv , "omniORB4", options) ;
59 #else
60       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB3");
61       omniORB::MaxMessageSize(100 * 1024 * 1024);
62 #endif      
63       // Obtain a reference to the root POA.
64       long TIMESleep = 500000000;
65       int NumberOfTries = 40;
66       int a;
67       timespec ts_req;
68       ts_req.tv_nsec=TIMESleep;
69       ts_req.tv_sec=0;
70       timespec ts_rem;
71       ts_rem.tv_nsec=0;
72       ts_rem.tv_sec=0;
73       CosNaming::NamingContext_var inc;
74       PortableServer::POA_var poa;
75       CORBA::Object_var theObj;
76       CORBA::Object_var obj;
77       CORBA::Object_var object;
78       int SALOMEDS=0;
79       const char * Env = getenv("USE_LOGGER");
80       int EnvL =0;
81       if ((Env!=NULL) && (strlen(Env)))
82         EnvL=1;
83       CosNaming::Name name;
84       name.length(1);
85       name[0].id=CORBA::string_dup("Logger");    
86       PortableServer::POAManager_var pman; 
87       for (int i = 1; i<=NumberOfTries; i++)
88         {
89           if (i!=1) 
90 #ifndef WNT
91             a=nanosleep(&ts_req,&ts_rem);
92 #else
93                 Sleep(TIMESleep/1000000);
94 #endif
95           try
96             { 
97               obj = orb->resolve_initial_references("RootPOA");
98               if(!CORBA::is_nil(obj))
99                 poa = PortableServer::POA::_narrow(obj);
100               if(!CORBA::is_nil(poa))
101                 pman = poa->the_POAManager();
102               if(!CORBA::is_nil(orb)) 
103                 theObj = orb->resolve_initial_references("NameService"); 
104               if (!CORBA::is_nil(theObj)){
105                 inc = CosNaming::NamingContext::_narrow(theObj);
106                 if(!CORBA::is_nil(inc))
107                   {
108                     MESSAGE( "SalomeDS Server: Naming Service was found" );
109                     if(EnvL==1)
110                       {
111                         CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
112                         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
113                         NS.init_orb( orb1 ) ;
114                         for(int j=1; j<=NumberOfTries; j++)
115                           {
116                             if (j!=1) 
117 #ifndef WNT
118                               a=nanosleep(&ts_req, &ts_rem);
119 #else
120                               Sleep(TIMESleep/1000000);
121 #endif
122                             try
123                               {
124                                 object = inc->resolve(name);
125                               }
126                             catch(CosNaming::NamingContext::NotFound)
127                               { 
128                                 MESSAGE( "SalomeDS Server: Logger Server wasn't found" ); }
129
130                             catch(...)
131                               {
132                                 MESSAGE( "SalomeDS Server: Unknown exception" );
133                               }
134                             if (!CORBA::is_nil(object))
135                               {
136                                 MESSAGE( "SalomeDS Server: Logger Server was found" );
137                                 SALOMEDS=1;
138                                 break;
139                               }
140                           }
141                       }
142                   }
143               }
144         
145             }
146           catch( const SALOME_Exception &ex )
147             {
148               MESSAGE( "Communication Error : " << ex.what() );
149               return EXIT_FAILURE ;
150             }
151           if ((SALOMEDS==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
152             break;
153         }
154     
155       // We allocate the objects on the heap.  Since these are reference
156       // counted objects, they will be deleted by the POA when they are no
157       // longer needed.    
158       SALOMEDS_StudyManager_i * myStudyManager_i = new  SALOMEDS_StudyManager_i(orb,poa);
159  
160       // Activate the objects.  This tells the POA that the objects are
161       // ready to accept requests.
162       PortableServer::ObjectId_var myStudyManager_iid = poa->activate_object(myStudyManager_i);
163       myStudyManager_i->register_name("/myStudyManager");
164        
165       // Obtain a POAManager, and tell the POA to start accepting
166       // requests on its objects.
167       pman = poa->the_POAManager();
168       pman->activate();
169
170 #ifdef CHECKTIME
171       Utils_Timer timer;
172       timer.Start();
173       timer.Stop();
174       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
175       timer.ShowAbsolute();
176 #endif
177       orb->run();
178       orb->destroy();
179     }
180   catch(CORBA::SystemException&)
181     {
182       MESSAGE( "Caught CORBA::SystemException." );
183     }
184   catch(CORBA::Exception&)
185     {
186       MESSAGE( "Caught CORBA::Exception." );
187     }
188   catch(omniORB::fatalException& fe)
189     {
190       MESSAGE( "Caught omniORB::fatalException:" );
191       MESSAGE( "  file: " << fe.file() );
192       MESSAGE( "  line: " << fe.line() );
193       MESSAGE( "  mesg: " << fe.errmsg() );
194     }
195   catch(...) 
196     {
197       MESSAGE( "Caught unknown exception." );
198     }
199   return 0;
200 }