Salome HOME
48b3ac787f6d87481e090fd2f0a8d2116819ebf8
[modules/yacs.git] / src / Container / Container_i.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   : Container_i.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA 
26 //  Module : SALOME
27 //  $Header$
28
29 //#define private public
30 #include <SALOMEconfig.h>
31 #ifndef WNT
32 #include CORBA_SERVER_HEADER(SALOME_Component)
33 #else
34 #include <SALOME_Component.hh>
35 #endif
36 #include <pthread.h>  // must be before Python.h !
37 #include <Python.h>
38 #include "SALOME_Container_i.hxx"
39 #include "SALOME_Component_i.hxx"
40 #include "SALOME_NamingService.hxx"
41 #include "OpUtil.hxx"
42 #include <string.h>
43 #include <stdio.h>
44 #ifndef WNT
45 #include <dlfcn.h>
46 #include <unistd.h>
47 #else
48 #include "../../adm/win32/SALOME_WNT.hxx"
49 #include <signal.h>
50 #include <process.h>
51 int SIGUSR1 = 1000;
52 #endif
53 #include "Container_init_python.hxx"
54
55 #include "utilities.h"
56 using namespace std;
57
58 bool _Sleeping = false ;
59
60 // // Needed by multi-threaded Python --- Supervision
61 int _ArgC ;
62 char ** _ArgV ;
63
64
65 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
66 // Other Containers are started via start_impl of FactoryServer
67
68 extern "C" {void ActSigIntHandler() ; }
69 #ifndef WNT
70 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
71 #else
72   extern "C" {void SigIntHandler( int ) ; }
73 #endif
74
75
76 map<std::string, int> Engines_Container_i::_cntInstances_map;
77 map<std::string, void *> Engines_Container_i::_library_map;
78 map<std::string, void *> Engines_Container_i::_toRemove_map;
79 omni_mutex Engines_Container_i::_numInstanceMutex ;
80
81 //=============================================================================
82 /*! 
83  *  Default constructor, not for use
84  */
85 //=============================================================================
86
87 Engines_Container_i::Engines_Container_i () :
88   _numInstance(0)
89 {
90 }
91
92 //=============================================================================
93 /*! 
94  *  Construtor to use
95  */
96 //=============================================================================
97
98 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, 
99                                           PortableServer::POA_ptr poa,
100                                           char *containerName ,
101                                           int argc , char* argv[],
102                                           bool activAndRegist,
103                                           bool isServantAloneInProcess
104                                           ) :
105   _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
106 {
107   _pid = (long)getpid();
108
109   if(activAndRegist)
110     ActSigIntHandler() ;
111
112   _argc = argc ;
113   _argv = argv ;
114
115   string hostname = GetHostname();
116   MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
117           << _argc << " Thread " << pthread_self() ) ;
118
119   int i = 0 ;
120   while ( _argv[ i ] )
121     {
122       MESSAGE("           argv" << i << " " << _argv[ i ]) ;
123       i++ ;
124     }
125
126   if ( argc < 2 )
127     {
128       INFOS("SALOME_Container usage : SALOME_Container ServerName");
129       ASSERT(0) ;
130     }
131   SCRUTE(argv[1]);
132   _isSupervContainer = false;
133   if (strcmp(argv[1],"SuperVisionContainer") == 0) _isSupervContainer = true;
134
135   if (_isSupervContainer)
136     {
137       _ArgC = argc ;
138       _ArgV = argv ;
139     }
140
141   _orb = CORBA::ORB::_duplicate(orb) ;
142   _poa = PortableServer::POA::_duplicate(poa) ;
143   
144   // Pour les containers paralleles: il ne faut pas enregistrer et activer
145   // le container generique, mais le container specialise
146
147   if(activAndRegist)
148     {
149       _id = _poa->activate_object(this);
150       _NS = new SALOME_NamingService();
151       _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
152       CORBA::Object_var obj=_poa->id_to_reference(*_id);
153       Engines::Container_var pCont 
154         = Engines::Container::_narrow(obj);
155
156       _containerName = _NS->BuildContainerNameForNS(containerName,
157                                                     hostname.c_str());
158       SCRUTE(_containerName);
159       _NS->Register(pCont, _containerName.c_str());
160       MESSAGE("Engines_Container_i::Engines_Container_i : Container name "
161               << _containerName);
162
163       // Python: 
164       // import SALOME_Container
165       // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
166     
167       CORBA::String_var sior =  _orb->object_to_string(pCont);
168       string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
169       myCommand += _containerName + "','";
170       myCommand += sior;
171       myCommand += "')\n";
172       SCRUTE(myCommand);
173
174       if (!_isSupervContainer)
175         {
176           Py_ACQUIRE_NEW_THREAD;
177 #ifdef WNT
178           // mpv: this is temporary solution: there is a unregular crash if not
179           Sleep(2000);
180           PyRun_SimpleString("import sys\n");
181           // first element is the path to Registry.dll, but it's wrong
182           PyRun_SimpleString("sys.path = sys.path[1:]\n");
183 #endif
184           PyRun_SimpleString("import SALOME_Container\n");
185           PyRun_SimpleString((char*)myCommand.c_str());
186           Py_RELEASE_NEW_THREAD;
187         }
188     }
189 }
190
191 //=============================================================================
192 /*! 
193  *  Destructor
194  */
195 //=============================================================================
196
197 Engines_Container_i::~Engines_Container_i()
198 {
199   MESSAGE("Container_i::~Container_i()");
200   delete _id;
201 }
202
203 //=============================================================================
204 /*! 
205  *  CORBA attribute: Container name (see constructor)
206  */
207 //=============================================================================
208
209 char* Engines_Container_i::name()
210 {
211    return CORBA::string_dup(_containerName.c_str()) ;
212 }
213
214 //=============================================================================
215 /*! 
216  *  CORBA method: Get the hostName of the Container (without domain extensions)
217  */
218 //=============================================================================
219
220 char* Engines_Container_i::getHostName()
221 {
222   string s = GetHostname();
223   //  MESSAGE("Engines_Container_i::getHostName " << s);
224   return CORBA::string_dup(s.c_str()) ;
225 }
226
227 //=============================================================================
228 /*! 
229  *  CORBA method: Get the PID (process identification) of the Container
230  */
231 //=============================================================================
232
233 CORBA::Long Engines_Container_i::getPID()
234 {
235   return (CORBA::Long)getpid();
236 }
237
238 //=============================================================================
239 /*! 
240  *  CORBA method: check if servant is still alive
241  */
242 //=============================================================================
243
244 void Engines_Container_i::ping()
245 {
246   MESSAGE("Engines_Container_i::ping() pid "<< getpid());
247 }
248
249 //=============================================================================
250 /*! 
251  *  CORBA method, oneway: Server shutdown. 
252  *  - Container name removed from naming service,
253  *  - servant deactivation,
254  *  - orb shutdown if no other servants in the process 
255  */
256 //=============================================================================
257
258 void Engines_Container_i::Shutdown()
259 {
260   MESSAGE("Engines_Container_i::Shutdown()");
261   _NS->Destroy_FullDirectory(_containerName.c_str());
262   //_remove_ref();
263   //_poa->deactivate_object(*_id);
264   if(_isServantAloneInProcess)
265     {
266       MESSAGE("Effective Shutdown of container Begins...");
267       LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
268       bp1->deleteInstance(bp1);
269       _orb->shutdown(0);
270     }
271 }
272
273
274 //=============================================================================
275 /*! 
276  *  CORBA method: load a new component class (Python or C++ implementation)
277  *  \param componentName like COMPONENT
278  *                          try to make a Python import of COMPONENT,
279  *                          then a lib open of libCOMPONENTEngine.so
280  *  \return true if dlopen successfull or already done, false otherwise
281  */
282 //=============================================================================
283
284 bool
285 Engines_Container_i::load_component_Library(const char* componentName)
286 {
287
288   string aCompName = componentName;
289
290   // --- try dlopen C++ component
291
292 #ifndef WNT
293   string impl_name = string ("lib") + aCompName + string("Engine.so");
294 #else
295   string impl_name = aCompName + string("Engine.dll");
296 #endif
297   SCRUTE(impl_name);
298   
299   _numInstanceMutex.lock(); // lock to be alone 
300   // (see decInstanceCnt, finalize_removal))
301   if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
302   if (_library_map[impl_name])
303     {
304       MESSAGE("Library " << impl_name << " already loaded");
305       _numInstanceMutex.unlock();
306       return true;
307     }
308   
309   void* handle;
310 #ifndef WNT
311   handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
312 #else
313   handle = dlopen( impl_name.c_str() , 0 ) ;
314 #endif
315   if ( handle )
316     {
317       _library_map[impl_name] = handle;
318       _numInstanceMutex.unlock();
319       return true;
320     }
321   else
322     {
323       INFOS("Can't load shared library : " << impl_name);
324       INFOS("error dlopen: " << dlerror());
325     }
326   _numInstanceMutex.unlock();
327
328   // --- try import Python component
329
330   INFOS("try import Python component "<<componentName);
331   if (_isSupervContainer)
332     {
333       INFOS("Supervision Container does not support Python Component Engines");
334       return false;
335     }
336   if (_library_map[aCompName])
337     {
338       return true; // Python Component, already imported
339     }
340   else
341     {
342       Py_ACQUIRE_NEW_THREAD;
343       PyObject *mainmod = PyImport_AddModule("__main__");
344       PyObject *globals = PyModule_GetDict(mainmod);
345       PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
346       PyObject *result = PyObject_CallMethod(pyCont,
347                                              "import_component",
348                                              "s",componentName);
349       int ret= PyInt_AsLong(result);
350       SCRUTE(ret);
351       Py_RELEASE_NEW_THREAD;
352   
353       if (ret) // import possible: Python component
354         {
355           _library_map[aCompName] = (void *)pyCont; // any non O value OK
356           MESSAGE("import Python: "<<aCompName<<" OK");
357           return true;
358         }
359     }
360   return false;
361 }
362
363 //=============================================================================
364 /*! 
365  *  CORBA method: Creates a new servant instance of a component.
366  *  The servant registers itself to naming service and Registry.
367  *  \param genericRegisterName  Name of the component instance to register
368  *                         in Registry & Name Service (without _inst_n suffix)
369  *  \param studyId         0 for multiStudy instance, 
370  *                         study Id (>0) otherwise
371  *  \return a loaded component
372  */
373 //=============================================================================
374
375 Engines::Component_ptr
376 Engines_Container_i::create_component_instance(const char*genericRegisterName,
377                                                CORBA::Long studyId)
378 {
379   if (studyId < 0)
380     {
381       INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
382       return Engines::Component::_nil() ;
383     }
384
385   Engines::Component_var iobject = Engines::Component::_nil() ;
386
387   string aCompName = genericRegisterName;
388   if (_library_map[aCompName]) // Python component
389     {
390       if (_isSupervContainer)
391         {
392           INFOS("Supervision Container does not support Python Component Engines");
393           return Engines::Component::_nil();
394         }
395       _numInstanceMutex.lock() ; // lock on the instance number
396       _numInstance++ ;
397       int numInstance = _numInstance ;
398       _numInstanceMutex.unlock() ;
399
400       char aNumI[12];
401       sprintf( aNumI , "%d" , numInstance ) ;
402       string instanceName = aCompName + "_inst_" + aNumI ;
403       string component_registerName =
404         _containerName + "/" + instanceName;
405
406       Py_ACQUIRE_NEW_THREAD;
407       PyObject *mainmod = PyImport_AddModule("__main__");
408       PyObject *globals = PyModule_GetDict(mainmod);
409       PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
410       PyObject *result = PyObject_CallMethod(pyCont,
411                                              "create_component_instance",
412                                              "ssl",
413                                              aCompName.c_str(),
414                                              instanceName.c_str(),
415                                              studyId);
416       string iors = PyString_AsString(result);
417       SCRUTE(iors);
418       Py_RELEASE_NEW_THREAD;
419   
420       CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
421       iobject = Engines::Component::_narrow( obj ) ;
422       return iobject._retn();
423     }
424   
425   //--- try C++
426
427 #ifndef WNT
428   string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
429 #else
430   string impl_name = genericRegisterName +string("Engine.dll");
431 #endif
432   void* handle = _library_map[impl_name];
433   if ( !handle )
434     {
435       INFOS("shared library " << impl_name <<"must be loaded before instance");
436       return Engines::Component::_nil() ;
437     }
438   else
439     {
440       iobject = createInstance(genericRegisterName,
441                                handle,
442                                studyId);
443       return iobject._retn();
444     }
445 }
446
447 //=============================================================================
448 /*! 
449  *  CORBA method: Finds a servant instance of a component
450  *  \param registeredName  Name of the component in Registry or Name Service,
451  *                         without instance suffix number
452  *  \param studyId         0 if instance is not associated to a study, 
453  *                         >0 otherwise (== study id)
454  *  \return the first instance found with same studyId
455  */
456 //=============================================================================
457
458 Engines::Component_ptr
459 Engines_Container_i::find_component_instance( const char* registeredName,
460                                               CORBA::Long studyId)
461 {
462   Engines::Component_var anEngine = Engines::Component::_nil();
463   map<string,Engines::Component_var>::iterator itm =_listInstances_map.begin();
464   while (itm != _listInstances_map.end())
465     {
466       string instance = (*itm).first;
467       SCRUTE(instance);
468       if (instance.find(registeredName) == 0)
469         {
470           anEngine = (*itm).second;
471           if (studyId == anEngine->getStudyId())
472             {
473               return anEngine._retn();
474             }
475         }
476       itm++;
477     }
478   return anEngine._retn();  
479 }
480
481 //=============================================================================
482 /*! 
483  *  CORBA method: find or create an instance of the component (servant),
484  *  load a new component class (dynamic library) if required,
485  *  ---- FOR COMPATIBILITY WITH 2.2 ---- 
486  *  ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
487  *  The servant registers itself to naming service and Registry.
488  *  \param genericRegisterName  Name of the component to register
489  *                              in Registry & Name Service
490  *  \param componentName       Name of the constructed library of the component
491  *  \return a loaded component
492  */
493 //=============================================================================
494
495 Engines::Component_ptr
496 Engines_Container_i::load_impl( const char* genericRegisterName,
497                                 const char* componentName )
498 {
499   string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
500   Engines::Component_var iobject = Engines::Component::_nil() ;
501   if (load_component_Library(genericRegisterName))
502     iobject = find_or_create_instance(genericRegisterName, impl_name);
503   return iobject._retn();
504 }
505     
506
507 //=============================================================================
508 /*! 
509  *  CORBA method: Stops the component servant, and deletes all related objects
510  *  \param component_i     Component to be removed
511  */
512 //=============================================================================
513
514 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
515 {
516   ASSERT(! CORBA::is_nil(component_i));
517   string instanceName = component_i->instanceName() ;
518   MESSAGE("unload component " << instanceName);
519   _listInstances_map.erase(instanceName);
520   component_i->destroy() ;
521   _NS->Destroy_Name(instanceName.c_str());
522 }
523
524 //=============================================================================
525 /*! 
526  *  CORBA method: Discharges unused libraries from the container.
527  */
528 //=============================================================================
529
530 void Engines_Container_i::finalize_removal()
531 {
532   MESSAGE("finalize unload : dlclose");
533   _numInstanceMutex.lock(); // lock to be alone
534                             // (see decInstanceCnt, load_component_Library)
535   map<string, void *>::iterator ith;
536   for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
537     {
538       void *handle = (*ith).second;
539       string impl_name= (*ith).first;
540       if (handle)
541         {
542           SCRUTE(handle);
543           SCRUTE(impl_name);
544 //        dlclose(handle);                // SALOME unstable after ...
545 //        _library_map.erase(impl_name);
546         }
547     }
548   _toRemove_map.clear();
549   _numInstanceMutex.unlock();
550 }
551
552 //=============================================================================
553 /*! 
554  *  CORBA method: Kill the container process with exit(0).
555  *  To remove :  never returns !
556  */
557 //=============================================================================
558
559 bool Engines_Container_i::Kill_impl()
560 {
561   MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
562           << _containerName.c_str() << " machineName "
563           << GetHostname().c_str());
564   INFOS("===============================================================");
565   INFOS("= REMOVE calls to Kill_impl in C++ container                  =");
566   INFOS("===============================================================");
567   //exit( 0 ) ;
568   ASSERT(0);
569   return false;
570 }
571
572 //=============================================================================
573 /*! 
574  *  C++ method: Finds an already existing servant instance of a component, or
575  *              create an instance.
576  *  ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
577  *  \param genericRegisterName    Name of the component instance to register
578  *                                in Registry & Name Service,
579  *                                (without _inst_n suffix, like "COMPONENT")
580  *  \param componentLibraryName   like "libCOMPONENTEngine.so"
581  *  \return a loaded component
582  * 
583  *  example with names:
584  *  aGenRegisterName = COMPONENT (= first argument)
585  *  impl_name = libCOMPONENTEngine.so (= second argument)
586  *  _containerName = /Containers/cli76ce/FactoryServer
587  *  factoryName = COMPONENTEngine_factory
588  *  component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
589  *
590  *  instanceName = COMPONENT_inst_1
591  *  component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
592  */
593 //=============================================================================
594
595 Engines::Component_ptr
596 Engines_Container_i::find_or_create_instance(string genericRegisterName,
597                                              string componentLibraryName)
598 {
599   string aGenRegisterName = genericRegisterName;
600   string impl_name = componentLibraryName;
601   void* handle = _library_map[impl_name];
602   if ( !handle )
603     {
604       INFOS("shared library " << impl_name <<"must be loaded before instance");
605       return Engines::Component::_nil() ;
606     }
607   else
608     {
609       // --- find a registered instance in naming service, or create
610
611       string component_registerBase =
612         _containerName + "/" + aGenRegisterName;
613       Engines::Component_var iobject = Engines::Component::_nil() ;
614       try
615         {
616           CORBA::Object_var obj =
617             _NS->ResolveFirst( component_registerBase.c_str());
618           if ( CORBA::is_nil( obj ) )
619             {
620               iobject = createInstance(genericRegisterName,
621                                        handle,
622                                        0); // force multiStudy instance here !
623             }
624           else
625             { 
626               iobject = Engines::Component::_narrow( obj ) ;
627               Engines_Component_i *servant =
628                 dynamic_cast<Engines_Component_i*>
629                 (_poa->reference_to_servant(iobject));
630               ASSERT(servant)
631               int studyId = servant->getStudyId();
632               ASSERT (studyId >= 0);
633               if (studyId == 0) // multiStudy instance, OK
634                 {
635                   // No ReBind !
636                   MESSAGE(component_registerBase.c_str()<<" already bound");
637                 }
638               else // monoStudy instance: NOK
639                 {
640                   iobject = Engines::Component::_nil();
641                   INFOS("load_impl & find_component_instance methods "
642                         << "NOT SUITABLE for mono study components");
643                 }
644             }
645         }
646       catch (...)
647         {
648           INFOS( "Container_i::load_impl catched" ) ;
649         }
650       return iobject._retn();
651     }
652 }
653
654 //=============================================================================
655 /*! 
656  *  C++ method: create a servant instance of a component.
657  *  \param genericRegisterName    Name of the component instance to register
658  *                                in Registry & Name Service,
659  *                                (without _inst_n suffix, like "COMPONENT")
660  *  \param handle                 loaded library handle
661  *  \param studyId                0 for multiStudy instance, 
662  *                                study Id (>0) otherwise
663  *  \return a loaded component
664  * 
665  *  example with names:
666  *  aGenRegisterName = COMPONENT (= first argument)
667  *  _containerName = /Containers/cli76ce/FactoryServer
668  *  factoryName = COMPONENTEngine_factory
669  *  component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
670  *  instanceName = COMPONENT_inst_1
671  *  component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
672  */
673 //=============================================================================
674
675 Engines::Component_ptr
676 Engines_Container_i::createInstance(string genericRegisterName,
677                                     void *handle,
678                                     int studyId)
679 {
680   // --- find the factory
681
682   string aGenRegisterName = genericRegisterName;
683   string factory_name = aGenRegisterName + string("Engine_factory");
684   SCRUTE(factory_name) ;
685
686   typedef  PortableServer::ObjectId * (*FACTORY_FUNCTION)
687     (CORBA::ORB_ptr,
688      PortableServer::POA_ptr, 
689      PortableServer::ObjectId *, 
690      const char *, 
691      const char *) ;
692
693   FACTORY_FUNCTION Component_factory
694     = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
695
696   char *error ;
697   if ( (error = dlerror() ) != NULL)
698     {
699       INFOS("Can't resolve symbol: " + factory_name);
700       SCRUTE(error);
701       return Engines::Component::_nil() ;
702     }
703
704   // --- create instance
705
706   Engines::Component_var iobject = Engines::Component::_nil() ;
707
708   try
709     {
710       _numInstanceMutex.lock() ; // lock on the instance number
711       _numInstance++ ;
712       int numInstance = _numInstance ;
713       _numInstanceMutex.unlock() ;
714
715       char aNumI[12];
716       sprintf( aNumI , "%d" , numInstance ) ;
717       string instanceName = aGenRegisterName + "_inst_" + aNumI ;
718       string component_registerName =
719         _containerName + "/" + instanceName;
720
721       // --- Instanciate required CORBA object
722
723       PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
724       id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
725                                  aGenRegisterName.c_str() ) ;
726
727       // --- get reference & servant from id
728
729       CORBA::Object_var obj = _poa->id_to_reference(*id);
730       iobject = Engines::Component::_narrow( obj ) ;
731
732       Engines_Component_i *servant =
733         dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
734       ASSERT(servant);
735       //SCRUTE(servant->pd_refCount);
736       servant->_remove_ref(); // compensate previous id_to_reference 
737       //SCRUTE(servant->pd_refCount);
738       _listInstances_map[instanceName] = iobject;
739       _cntInstances_map[aGenRegisterName] += 1;
740       SCRUTE(aGenRegisterName);
741       SCRUTE(_cntInstances_map[aGenRegisterName]);
742       //SCRUTE(servant->pd_refCount);
743       bool ret_studyId = servant->setStudyId(studyId);
744       ASSERT(ret_studyId);
745
746       // --- register the engine under the name
747       //     containerName(.dir)/instanceName(.object)
748
749       _NS->Register( iobject , component_registerName.c_str() ) ;
750       MESSAGE( component_registerName.c_str() << " bound" ) ;
751     }
752   catch (...)
753     {
754       INFOS( "Container_i::createInstance exception catched" ) ;
755     }
756   return iobject._retn();
757 }
758
759 //=============================================================================
760 /*! 
761  *
762  */
763 //=============================================================================
764
765 void Engines_Container_i::decInstanceCnt(string genericRegisterName)
766 {
767   string aGenRegisterName =genericRegisterName;
768   MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName);
769   ASSERT(_cntInstances_map[aGenRegisterName] > 0); 
770   _numInstanceMutex.lock(); // lock to be alone
771                             // (see finalize_removal, load_component_Library)
772   _cntInstances_map[aGenRegisterName] -= 1;
773   SCRUTE(_cntInstances_map[aGenRegisterName]);
774   if (_cntInstances_map[aGenRegisterName] == 0)
775     {
776       string impl_name =
777         Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
778       SCRUTE(impl_name);
779       void* handle = _library_map[impl_name];
780       ASSERT(handle);
781       _toRemove_map[impl_name] = handle;
782     }
783   _numInstanceMutex.unlock();
784 }
785
786 //=============================================================================
787 /*! 
788  *  Retrieves only with container naming convention if it is a python container
789  */
790 //=============================================================================
791
792 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
793 {
794   bool ret=false;
795   int len=strlen(ContainerName);
796   if(len>=2)
797     if(strcmp(ContainerName+len-2,"Py")==0)
798       ret=true;
799   return ret;
800 }
801
802 //=============================================================================
803 /*! 
804  *  
805  */
806 //=============================================================================
807
808 void ActSigIntHandler()
809 {
810 #ifndef WNT
811   struct sigaction SigIntAct ;
812   SigIntAct.sa_sigaction = &SigIntHandler ;
813   SigIntAct.sa_flags = SA_SIGINFO ;
814 #endif
815
816 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
817 // (SIGINT | SIGUSR1) :
818 // it must be only one signal ===> one call for SIGINT 
819 // and an other one for SIGUSR1
820
821 #ifndef WNT
822   if ( sigaction( SIGINT , &SigIntAct, NULL ) ) 
823     {
824       perror("SALOME_Container main ") ;
825       exit(0) ;
826     }
827   if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) )
828     {
829       perror("SALOME_Container main ") ;
830       exit(0) ;
831     }
832
833   //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
834   //             use of streams (and so on) should never be used because :
835   //             streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
836   //             A stream operation may be interrupted by a signal and if the Handler use stream we
837   //             may have a "Dead-Lock" ===HangUp
838   //==INFOS is commented
839   //  INFOS(pthread_self() << "SigIntHandler activated") ;
840
841 #else  
842   signal( SIGINT, SigIntHandler );
843   signal( SIGUSR1, SigIntHandler );
844 #endif
845
846 }
847
848 void SetCpuUsed() ;
849
850 #ifndef WNT
851 void SigIntHandler(int what ,
852                    siginfo_t * siginfo ,
853                    void * toto ) 
854 {
855   //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
856   //             use of streams (and so on) should never be used because :
857   //             streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
858   //             A stream operation may be interrupted by a signal and if the Handler use stream we
859   //             may have a "Dead-Lock" ===HangUp
860   //==MESSAGE is commented
861   //  MESSAGE(pthread_self() << "SigIntHandler what     " << what << endl
862   //          << "              si_signo " << siginfo->si_signo << endl
863   //          << "              si_code  " << siginfo->si_code << endl
864   //          << "              si_pid   " << siginfo->si_pid) ;
865
866   if ( _Sleeping )
867     {
868       _Sleeping = false ;
869       //     MESSAGE("SigIntHandler END sleeping.") ;
870       return ;
871     }
872   else
873     {
874       ActSigIntHandler() ;
875       if ( siginfo->si_signo == SIGUSR1 )
876         {
877           SetCpuUsed() ;
878         }
879       else 
880         {
881           _Sleeping = true ;
882           //      MESSAGE("SigIntHandler BEGIN sleeping.") ;
883           int count = 0 ;
884           while( _Sleeping )
885             {
886               sleep( 1 ) ;
887               count += 1 ;
888             }
889           //      MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
890         }
891       return ;
892     }
893 }
894 #else // Case WNT
895 void SigIntHandler( int what )
896 {
897   MESSAGE( pthread_self() << "SigIntHandler what     " << what << endl );
898   if ( _Sleeping )
899     {
900       _Sleeping = false ;
901       MESSAGE("SigIntHandler END sleeping.") ;
902       return ;
903     }
904   else
905     {
906       ActSigIntHandler() ;
907       if ( what == SIGUSR1 )
908         {
909           SetCpuUsed() ;
910         }
911       else
912         {
913           _Sleeping = true ;
914           MESSAGE("SigIntHandler BEGIN sleeping.") ;
915           int count = 0 ;
916           while( _Sleeping ) 
917             {
918               Sleep( 1000 ) ;
919               count += 1 ;
920             }
921           MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
922         }
923       return ;
924     }
925 }
926 #endif
927