Salome HOME
41a526d0a5ff8cc54122b90094e9057e798bae42
[modules/kernel.git] / src / Container / SALOME_Container.cxx
1 //  SALOME Container : implementation of container and engine for Kernel
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_Container.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28
29 #include <iostream>
30 #include <string>
31 #include <stdio.h>
32
33 #include "Utils_ORB_INIT.hxx"
34 #include "Utils_SINGLETON.hxx"
35 #include "SALOME_NamingService.hxx"
36 #include "SALOME_Container_i.hxx"
37 #include "utilities.h"
38 #include "LocalTraceCollector.hxx"
39
40 #ifdef CHECKTIME
41 #include <Utils_Timer.hxx>
42 #endif
43
44 #ifdef HAVE_MPI2
45 #include <mpi.h>
46 #endif
47
48 #include <Python.h>
49
50 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
51
52 using namespace std;
53
54 static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }};
55
56 int main(int argc, char* argv[])
57 {
58 #ifdef HAVE_MPI2
59   MPI_Init(&argc,&argv);
60 #endif
61   // Initialise the ORB.
62   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
63   CORBA::ORB_var &orb = init( argc , argv ) ;
64   LocalTraceCollector *myThreadTrace = LocalTraceCollector::instance(orb);
65   INFOS_COMPILATION;
66   BEGIN_OF(argv[0]);
67     
68   Py_Initialize() ;
69   PySys_SetArgv( argc , argv ) ;
70   Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ;
71   
72   try{
73     // Obtain a reference to the root POA.
74     // obtain the root poa manager
75     //
76     long TIMESleep = 250000000;
77     int NumberOfTries = 40;
78     int a;
79     timespec ts_req;
80     ts_req.tv_nsec=TIMESleep;
81     ts_req.tv_sec=0;
82     timespec ts_rem;
83     ts_rem.tv_nsec=0;
84     ts_rem.tv_sec=0;
85     CosNaming::NamingContext_var inc;
86     PortableServer::POA_var root_poa;
87     CORBA::Object_var theObj;
88     CORBA::Object_var obj;
89     CORBA::Object_var object;
90     SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
91     int CONTAINER=0;
92     const char * Env = getenv("USE_LOGGER");
93     int EnvL =0;
94     if(Env != NULL && strlen(Env))
95       EnvL=1;
96     
97     CosNaming::Name name;
98     name.length(1);
99     name[0].id=CORBA::string_dup("Logger");    
100     PortableServer::POAManager_var pman; 
101     for(int i = 1; i <= NumberOfTries; i++){
102       if(i != 1) 
103         a=nanosleep(&ts_req,&ts_rem);
104       try{ 
105         obj = orb->resolve_initial_references("RootPOA");
106         if(!CORBA::is_nil(obj))
107           root_poa = PortableServer::POA::_narrow(obj);
108         if(!CORBA::is_nil(root_poa))
109           pman = root_poa->the_POAManager();
110         if(!CORBA::is_nil(orb)) 
111           theObj = orb->resolve_initial_references("NameService");
112         if (!CORBA::is_nil(theObj))
113           inc = CosNaming::NamingContext::_narrow(theObj);
114       }catch(CORBA::COMM_FAILURE&){
115         MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
116       }
117       if(!CORBA::is_nil(inc)){
118         MESSAGE( "Container: Naming Service was found" );
119         if(EnvL == 1){
120           for(int j = 1; j <= NumberOfTries; j++){
121             if(j != 1) 
122               a=nanosleep(&ts_req, &ts_rem);
123             try{
124               object = inc->resolve(name);
125             }catch(CosNaming::NamingContext::NotFound){
126               MESSAGE( "Container: Logger Server wasn't found" );
127             }catch(...){
128               MESSAGE( "Container: Unknown exception" );
129             }
130             if(!CORBA::is_nil(object)){
131               MESSAGE( "Container: Logger Server was found" );
132               CONTAINER = 1;
133               break;
134             }
135           }
136         }
137       }
138       if(CONTAINER == 1 || (EnvL == 0 && !CORBA::is_nil(inc)))
139         break;
140     }
141     
142     // define policy objects     
143     PortableServer::ImplicitActivationPolicy_var implicitActivation =
144       root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION) ;
145     
146     // default = NO_IMPLICIT_ACTIVATION
147     PortableServer::ThreadPolicy_var threadPolicy =
148       root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
149     // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
150     
151     // create policy list
152     CORBA::PolicyList policyList;
153     policyList.length(2);
154     policyList[0] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitActivation) ;
155     policyList[1] = PortableServer::ThreadPolicy::_duplicate(threadPolicy) ;
156     
157     // create the child POA
158     PortableServer::POAManager_var nil_mgr = PortableServer::POAManager::_nil() ;
159     PortableServer::POA_var factory_poa =
160       root_poa->create_POA("factory_poa", pman, policyList) ;
161     //with nil_mgr instead of pman, a new POA manager is created with the new POA
162     
163     // destroy policy objects
164     implicitActivation->destroy() ;
165     threadPolicy->destroy() ;
166     
167     char *containerName = "";
168     if(argc > 1){
169       containerName = argv[1] ;
170     }
171     
172     Engines_Container_i * myContainer 
173       = new Engines_Container_i(orb, factory_poa, containerName , argc , argv );
174     
175     //     Engines_Container_i * myContainer 
176     //      = new Engines_Container_i(string(argv[1]),string(argv[2]), orb, factory_poa);
177     
178     // use naming service
179     //     myContainer->_NS.init_orb(orb);
180     //     Engines::Container_ptr pCont = Engines::Container::_narrow(myContainer->_this());
181     //     myContainer->_NS.Register(pCont, argv[2]); 
182     
183     pman->activate();
184     
185 #ifdef CHECKTIME
186     Utils_Timer timer;
187     timer.Start();
188     timer.Stop();
189     MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
190     timer.ShowAbsolute();
191 #endif
192     
193     HandleServerSideSignals(orb);
194     
195     orb->destroy();
196   }catch(CORBA::SystemException&){
197     INFOS("Caught CORBA::SystemException.");
198   }catch(PortableServer::POA::WrongPolicy&){
199     INFOS("Caught CORBA::WrongPolicyException.");
200   }catch(PortableServer::POA::ServantAlreadyActive&){
201     INFOS("Caught CORBA::ServantAlreadyActiveException");
202   }catch(CORBA::Exception&){
203     INFOS("Caught CORBA::Exception.");
204   }catch(std::exception& exc){
205     INFOS("Caught std::exception - "<<exc.what()); 
206   }catch(...){
207     INFOS("Caught unknown exception.");
208   }
209 #ifdef HAVE_MPI2
210   MPI_Finalize();
211 #endif
212   END_OF(argv[0]);
213   delete myThreadTrace;
214   return 0 ;
215 }
216