]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_Server.cxx
Salome HOME
Merge tag 'V8_5_0' into omu/Launcher9
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Server.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
24 //  File   : SALOMEDS_Server.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include "utilities.h"
30 #include "Utils_SINGLETON.hxx"
31
32 #include "SALOME_NamingService.hxx"
33 #include "SALOMEDS_StudyManager_i.hxx"
34
35 #include <SALOMEconfig.h>
36 #include CORBA_SERVER_HEADER(SALOMEDS)
37
38 #ifdef CHECKTIME
39 #include <Utils_Timer.hxx>
40 #endif
41
42 // extern "C"
43 // { // for ccmalloc memory debug
44 // void ccmalloc_static_initialization(void);
45 // }
46
47 ///////////////////////////// MAIN ///////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////////
49 int main(int argc, char** argv)
50 {
51   //   ccmalloc_static_initialization();
52
53   MESSAGE("SALOMEDS_Server - main");
54   try 
55     {
56       // Initialise the ORB.
57 #if OMNIORB_VERSION >= 4
58       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB4" ) ;
59 #else
60       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB3" );
61 #endif      
62       // Obtain a reference to the root POA.
63       long TIMESleep = 500000000;
64       int NumberOfTries = 40;
65       int a;
66       timespec ts_req;
67       ts_req.tv_nsec=TIMESleep;
68       ts_req.tv_sec=0;
69       timespec ts_rem;
70       ts_rem.tv_nsec=0;
71       ts_rem.tv_sec=0;
72       CosNaming::NamingContext_var inc;
73       PortableServer::POA_var defaultPoa;
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 WIN32
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                 defaultPoa = PortableServer::POA::_narrow(obj);
100               if(!CORBA::is_nil(defaultPoa))
101                 pman = defaultPoa->the_POAManager();
102
103               PortableServer::POAManager_var pman = defaultPoa->the_POAManager();
104               CORBA::PolicyList policies;
105               policies.length(2);
106               //PortableServer::ThreadPolicy_var threadPol(defaultPoa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
107               PortableServer::ThreadPolicy_var threadPol(defaultPoa->create_thread_policy(PortableServer::ORB_CTRL_MODEL)); // default for all POAs
108               PortableServer::ImplicitActivationPolicy_var implicitPol(defaultPoa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION)); // default for Root_POA, NO for others
109               policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
110               policies[1] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitPol);
111               poa = defaultPoa->create_POA("KERNELStandaloneStudySingleThreadPOA",pman,policies);
112               threadPol->destroy();
113
114               if(!CORBA::is_nil(orb)) 
115                 theObj = orb->resolve_initial_references("NameService"); 
116               if (!CORBA::is_nil(theObj)){
117                 inc = CosNaming::NamingContext::_narrow(theObj);
118                 if(!CORBA::is_nil(inc))
119                   {
120                     MESSAGE( "SalomeDS Server: Naming Service was found" );
121                     if(EnvL==1)
122                       {
123                         CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
124                         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
125                         NS.init_orb( orb1 ) ;
126                         for(int j=1; j<=NumberOfTries; j++)
127                           {
128                             if (j!=1) 
129 #ifndef WIN32
130                               a=nanosleep(&ts_req, &ts_rem);
131 #else
132                               Sleep(TIMESleep/1000000);
133 #endif
134                             try
135                               {
136                                 object = inc->resolve(name);
137                               }
138                             catch(CosNaming::NamingContext::NotFound)
139                               { 
140                                 MESSAGE( "SalomeDS Server: Logger Server wasn't found" ); }
141
142                             catch(...)
143                               {
144                                 MESSAGE( "SalomeDS Server: Unknown exception" );
145                               }
146                             if (!CORBA::is_nil(object))
147                               {
148                                 MESSAGE( "SalomeDS Server: Logger Server was found" );
149                                 SALOMEDS=1;
150                                 break;
151                               }
152                           }
153                       }
154                   }
155               }
156         
157             }
158           catch( const SALOME_Exception &ex )
159             {
160               MESSAGE( "Communication Error : " << ex.what() );
161               return EXIT_FAILURE ;
162             }
163           if ((SALOMEDS==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
164             break;
165         }
166     
167       // We allocate the objects on the heap.  Since these are reference
168       // counted objects, they will be deleted by the POA when they are no
169       // longer needed.    
170       SALOMEDS_StudyManager_i * myStudyManager_i = new  SALOMEDS_StudyManager_i(orb,poa);
171  
172       // Activate the objects.  This tells the POA that the objects are
173       // ready to accept requests.
174       PortableServer::ObjectId_var myStudyManager_iid = poa->activate_object(myStudyManager_i);
175       myStudyManager_i->register_name("/myStudyManager");
176       myStudyManager_i->_remove_ref();
177        
178       // Obtain a POAManager, and tell the POA to start accepting
179       // requests on its objects.
180       pman = poa->the_POAManager();
181       pman->activate();
182
183 #ifdef CHECKTIME
184       Utils_Timer timer;
185       timer.Start();
186       timer.Stop();
187       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
188       timer.ShowAbsolute();
189 #endif
190       orb->run();
191       MESSAGE( "end of SALOME_DS server" );
192       orb->destroy();
193     }
194   catch(CORBA::SystemException&)
195     {
196       MESSAGE( "Caught CORBA::SystemException." );
197     }
198   catch(CORBA::Exception&)
199     {
200       MESSAGE( "Caught CORBA::Exception." );
201     }
202   catch(omniORB::fatalException& fe)
203     {
204       MESSAGE( "Caught omniORB::fatalException:" );
205       MESSAGE( "  file: " << fe.file() );
206       MESSAGE( "  line: " << fe.line() );
207       MESSAGE( "  mesg: " << fe.errmsg() );
208     }
209   catch(...) 
210     {
211       MESSAGE( "Caught unknown exception." );
212     }
213   return 0;
214 }