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