1 // SALOME Container : implementation of container and engine for Kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Container_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
29 #include <SALOMEconfig.h>
30 #include CORBA_SERVER_HEADER(SALOME_Component)
31 #include "SALOME_Container_i.hxx"
32 #include "SALOME_NamingService.hxx"
33 //#include "Utils_SINGLETON.hxx"
40 #include "utilities.h"
43 bool _Sleeping = false ;
45 // Needed by multi-threaded Python
50 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
51 // Other Containers are started via start_impl of FactoryServer
53 extern "C" {void ActSigIntHandler() ; }
54 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
56 const char *Engines_Container_i::_defaultContainerName="FactoryServer";
58 Engines_Container_i::Engines_Container_i () :
63 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
64 PortableServer::POA_ptr poa,
66 int argc , char* argv[],
67 bool activAndRegist ) :
70 _pid = (long)getpid();
80 int i = strlen( _argv[ 0 ] ) - 1 ;
82 if ( _argv[ 0 ][ i ] == '/' ) {
83 _argv[ 0 ][ i+1 ] = '\0' ;
88 string hostname = GetHostname();
89 MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
90 << _argc << " Thread " << pthread_self() ) ;
92 while ( _argv[ i ] ) {
93 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
97 MESSAGE("SALOME_Container usage : SALOME_Container ServerName -ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ;
103 _containerName = BuildContainerNameForNS(containerName,hostname.c_str());
105 _orb = CORBA::ORB::_duplicate(orb) ;
106 _poa = PortableServer::POA::_duplicate(poa) ;
108 // Pour les containers paralleles: il ne faut pas enregistrer et activer le container generique, mais le container specialise
110 _id = _poa->activate_object(this);
111 _NS = new SALOME_NamingService();//SINGLETON_<SALOME_NamingService>::Instance() ;
112 //ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
113 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
114 CORBA::Object_var obj=_poa->id_to_reference(*_id);
115 Engines::Container_var pCont
116 = Engines::Container::_narrow(obj);
117 SCRUTE(_containerName);
118 _NS->Register(pCont, _containerName.c_str());
122 Engines_Container_i::~Engines_Container_i()
124 MESSAGE("Container_i::~Container_i()");
128 char* Engines_Container_i::name()
130 return CORBA::string_dup(_containerName.c_str()) ;
133 char* Engines_Container_i::machineName()
135 string s = GetHostname();
136 MESSAGE("Engines_Container_i::machineName " << s);
137 return CORBA::string_dup(s.c_str()) ;
140 void Engines_Container_i::ping()
142 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
145 // shutdown corba server
146 void Engines_Container_i::Shutdown()
148 MESSAGE("Engines_Container_i::Shutdown()");
149 _NS->Destroy_Name(_containerName.c_str());
151 //_poa->deactivate_object(*_id);
155 //! Kill current container
156 bool Engines_Container_i::Kill_impl() {
157 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
158 << _containerName.c_str() << " machineName "
159 << GetHostname().c_str());
163 Engines::Component_ptr Engines_Container_i::load_impl( const char* nameToRegister,
164 const char* componentName ) {
166 _numInstanceMutex.lock() ; // lock on the instance number
167 BEGIN_OF( "Container_i::load_impl " << componentName ) ;
170 sprintf( _aNumI , "%d" , _numInstance ) ;
172 string _impl_name = componentName;
173 string _nameToRegister = nameToRegister;
174 string instanceName = _nameToRegister + "_inst_" + _aNumI ;
175 //SCRUTE(instanceName);
177 //string absolute_impl_name = _library_path + "lib" + _impl_name + ".so";
178 string absolute_impl_name( _impl_name ) ;
179 SCRUTE(absolute_impl_name);
181 handle = dlopen( absolute_impl_name.c_str() , RTLD_LAZY ) ;
183 INFOS("Can't load shared library : " << absolute_impl_name);
184 INFOS("error dlopen: " << dlerror());
185 _numInstanceMutex.unlock() ;
186 return Engines::Component::_nil() ;
189 string factory_name = _nameToRegister + string("Engine_factory");
190 // SCRUTE(factory_name) ;
192 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
194 PortableServer::POA_ptr,
195 PortableServer::ObjectId *,
198 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
201 if ( (error = dlerror() ) != NULL) {
202 INFOS("Can't resolve symbol: " + factory_name);
204 _numInstanceMutex.unlock() ;
205 return Engines::Component::_nil() ;
208 string component_registerName = _containerName + "/" + _nameToRegister;
209 Engines::Component_var iobject = Engines::Component::_nil() ;
211 CORBA::Object_var obj = _NS->Resolve( component_registerName.c_str() ) ;
212 if ( CORBA::is_nil( obj ) ) {
213 // Instanciate required CORBA object
214 PortableServer::ObjectId * id ;
215 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str() ,
216 _nameToRegister.c_str() ) ;
217 // get reference from id
218 obj = _poa->id_to_reference(*id);
219 iobject = Engines::Component::_narrow( obj ) ;
221 // _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
222 // register the engine under the name containerName.dir/nameToRegister.object
223 _NS->Register( iobject , component_registerName.c_str() ) ;
224 MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " bound" ) ;
226 else { // JR : No ReBind !!!
227 MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " already bound" ) ;
228 iobject = Engines::Component::_narrow( obj ) ;
232 INFOS( "Container_i::load_impl catched" ) ;
235 //Jr _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
236 handle_map[instanceName] = handle;
237 END_OF("Container_i::load_impl");
238 _numInstanceMutex.unlock() ;
239 return Engines::Component::_duplicate(iobject);
242 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
244 ASSERT(! CORBA::is_nil(component_i));
245 string instanceName = component_i->instanceName() ;
246 MESSAGE("unload component " << instanceName);
247 component_i->destroy() ;
248 MESSAGE("test key handle_map");
249 _numInstanceMutex.lock() ; // lock on the remove on handle_map
250 if (handle_map[instanceName]) // if key does not exist, created & initialized null
252 remove_map[instanceName] = handle_map[instanceName] ;
254 else MESSAGE("pas d'entree handle_map");
255 handle_map.erase(instanceName) ;
256 _numInstanceMutex.unlock() ;
257 MESSAGE("contenu handle_map");
258 map<string, void *>::iterator im ;
259 for (im = handle_map.begin() ; im != handle_map.end() ; im ++)
261 MESSAGE("reste " << (*im).first);
265 void Engines_Container_i::finalize_removal()
267 MESSAGE("finalize unload : dlclose");
268 map<string, void *>::iterator im ;
269 _numInstanceMutex.lock() ; // lock on the explore remove_map & dlclose
270 for (im = remove_map.begin() ; im != remove_map.end() ; im ++)
272 void * handle = (*im).second ;
274 MESSAGE("dlclose " << (*im).first);
277 _numInstanceMutex.unlock() ;
278 MESSAGE("remove_map.clear()");
281 void ActSigIntHandler() {
282 struct sigaction SigIntAct ;
283 SigIntAct.sa_sigaction = &SigIntHandler ;
284 SigIntAct.sa_flags = SA_SIGINFO ;
285 if ( sigaction( SIGINT | SIGUSR1 , &SigIntAct, NULL ) ) {
286 perror("SALOME_Container main ") ;
290 INFOS(pthread_self() << "SigIntHandler activated") ;
296 void SigIntHandler(int what , siginfo_t * siginfo ,
298 MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
299 << " si_signo " << siginfo->si_signo << endl
300 << " si_code " << siginfo->si_code << endl
301 << " si_pid " << siginfo->si_pid) ;
304 MESSAGE("SigIntHandler END sleeping.") ;
309 if ( siginfo->si_signo == SIGUSR1 ) {
314 MESSAGE("SigIntHandler BEGIN sleeping.") ;
320 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
326 // Get the PID of the Container
328 long Engines_Container_i::getPID() {
329 return (long)getpid();
332 // Get the hostName of the Container
334 char* Engines_Container_i::getHostName() {
335 return((char*)(GetHostname().c_str()));
338 // Retrieves only with container naming convention if it is a python container
339 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
342 int len=strlen(ContainerName);
344 if(strcmp(ContainerName+len-2,"Py")==0)
349 string Engines_Container_i::BuildContainerNameForNS(const char *ContainerName, const char *hostname)
351 string ret="/Containers/";
354 if (strlen(ContainerName)== 0)
355 ret+=_defaultContainerName;
357 ret += ContainerName;