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