Salome HOME
76afb9b8d2adebdacd268793bff9e81878afc72f
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Server.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 "ArgvKeeper.hxx"
30 #include "OpUtil.hxx"
31 #include "utilities.h"
32 #include "Utils_SINGLETON.hxx"
33
34 #include "SALOME_NamingService.hxx"
35 #include "SALOMEDS_Study_i.hxx"
36
37 #include <SALOMEconfig.h>
38 #include CORBA_SERVER_HEADER(SALOMEDS)
39
40 #ifdef CHECKTIME
41 #include <Utils_Timer.hxx>
42 #endif
43
44 // extern "C"
45 // { // for ccmalloc memory debug
46 // void ccmalloc_static_initialization(void);
47 // }
48
49 ///////////////////////////// MAIN ///////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////////
51 int main(int argc, char** argv)
52 {
53   //   ccmalloc_static_initialization();
54
55   MESSAGE("SALOMEDS_Server - main");
56   try 
57     {
58       // Initialise the ORB.
59       SetArgcArgv(argc, argv);
60       CORBA::ORB_var orb = KERNEL::GetRefToORB() ;
61       SALOME_NamingService* NS = 0;
62       // Obtain a reference to the root POA.
63       long TIMESleep = 500000000;
64       int NumberOfTries = 40;
65       timespec ts_req;
66       ts_req.tv_nsec=TIMESleep;
67       ts_req.tv_sec=0;
68       timespec ts_rem;
69       ts_rem.tv_nsec=0;
70       ts_rem.tv_sec=0;
71       CosNaming::NamingContext_var inc;
72       PortableServer::POA_var defaultPoa;
73       PortableServer::POA_var poa;
74       CORBA::Object_var theObj;
75       CORBA::Object_var obj;
76       CORBA::Object_var object;
77       int SALOMEDS=0;
78       const char * Env = getenv("USE_LOGGER");
79       int EnvL =0;
80       if ((Env!=NULL) && (strlen(Env)))
81         EnvL=1;
82       CosNaming::Name name;
83       name.length(1);
84       name[0].id=CORBA::string_dup("Logger");    
85       PortableServer::POAManager_var pman; 
86       for (int i = 1; i<=NumberOfTries; i++)
87         {
88           if (i!=1) 
89 #ifndef WIN32
90             nanosleep(&ts_req,&ts_rem);
91 #else
92             Sleep(TIMESleep/1000000);
93 #endif
94           try
95             { 
96               obj = orb->resolve_initial_references("RootPOA");
97               if(!CORBA::is_nil(obj))
98                 defaultPoa = PortableServer::POA::_narrow(obj);
99               if(!CORBA::is_nil(defaultPoa))
100                 pman = defaultPoa->the_POAManager();
101
102               PortableServer::POAManager_var pman = defaultPoa->the_POAManager();
103               CORBA::PolicyList policies;
104               policies.length(2);
105               //PortableServer::ThreadPolicy_var threadPol(defaultPoa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
106               PortableServer::ThreadPolicy_var threadPol(defaultPoa->create_thread_policy(PortableServer::ORB_CTRL_MODEL)); // default for all POAs
107               PortableServer::ImplicitActivationPolicy_var implicitPol(defaultPoa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION)); // default for Root_POA, NO for others
108               policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
109               policies[1] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitPol);
110               poa = defaultPoa->create_POA("KERNELStandaloneStudySingleThreadPOA",pman,policies);
111               threadPol->destroy();
112               SALOMEDS_Study_i::SetThePOA(poa);
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 = KERNEL::GetRefToORB() ;
124                         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                               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_Study_i * myStudy_i = new  SALOMEDS_Study_i(orb);
171  
172       // Activate the objects.  This tells the POA that the objects are
173       // ready to accept requests.
174       PortableServer::ObjectId_var myStudy_iid = poa->activate_object(myStudy_i);
175       SALOMEDS::Study_var Study = myStudy_i->_this();
176       if (!NS)
177       {
178         NS = SINGLETON_<SALOME_NamingService>::Instance();
179         NS->init_orb( orb );
180       }
181       NS->Register(Study, "/Study");
182       myStudy_i->_remove_ref();
183        
184       // Obtain a POAManager, and tell the POA to start accepting
185       // requests on its objects.
186       pman = poa->the_POAManager();
187       pman->activate();
188
189 #ifdef CHECKTIME
190       Utils_Timer timer;
191       timer.Start();
192       timer.Stop();
193       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
194       timer.ShowAbsolute();
195 #endif
196       orb->run();
197       MESSAGE( "end of SALOME_DS server" );
198       NS->Destroy_Name("/Study");
199       orb->destroy();
200     }
201   catch(CORBA::SystemException&)
202     {
203       MESSAGE( "Caught CORBA::SystemException." );
204     }
205   catch(CORBA::Exception&)
206     {
207       MESSAGE( "Caught CORBA::Exception." );
208     }
209   catch(omniORB::fatalException& fe)
210     {
211       MESSAGE( "Caught omniORB::fatalException:" );
212       MESSAGE( "  file: " << fe.file() );
213       MESSAGE( "  line: " << fe.line() );
214       MESSAGE( "  mesg: " << fe.errmsg() );
215     }
216   catch(...) 
217     {
218       MESSAGE( "Caught unknown exception." );
219     }
220   return 0;
221 }