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