Salome HOME
ADD a end user module (services.py) to help the manipulation of SALOME KERNEL service...
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainer_i.cxx
index 1d53af5639263a24cfbb85e35144740337c71761..888b1e358a4ffb448d7509f95cc9761f8582051b 100644 (file)
@@ -1,25 +1,24 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-//  SALOME_ParallelContainer : implementation of container and engine for ParallelKernel
 //  File   : SALOME_ParallelContainer_i.cxx
 //  Author : André RIBES, EDF
 
@@ -50,7 +49,6 @@ int SIGUSR1 = 1000;
 #include <Python.h>
 #include "Container_init_python.hxx"
 
-using namespace std;
 
 bool _Sleeping = false ;
 
@@ -243,7 +241,7 @@ void Engines_Parallel_Container_i::Shutdown()
   /* For each seq component contained in this container
   * tell it to self-destroy
   */
-  std::map<std::string, Engines::Component_var>::iterator itm;
+  std::map<std::string, Engines::EngineComponent_var>::iterator itm;
   for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
   {
     try
@@ -289,15 +287,17 @@ void Engines_Parallel_Container_i::Shutdown()
 //=============================================================================
 
 bool
-Engines_Parallel_Container_i::load_component_Library(const char* componentName)
+Engines_Parallel_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
 {
+  reason=CORBA::string_dup("");
+
   MESSAGE("Begin of load_component_Library : " << componentName)
   bool ret = false;
   std::string aCompName = componentName;
 #ifndef WIN32
-  string impl_name = string ("lib") + aCompName + string("Engine.so");
+  std::string impl_name = string ("lib") + aCompName + string("Engine.so");
 #else
-  string impl_name = aCompName + string("Engine.dll");
+  std::string impl_name = aCompName + string("Engine.dll");
 #endif
 
   _numInstanceMutex.lock(); // lock to be alone 
@@ -381,24 +381,51 @@ Engines_Parallel_Container_i::load_component_Library(const char* componentName)
  *  \return a loaded component
  */
 //=============================================================================
-
-Engines::Component_ptr
+Engines::EngineComponent_ptr
 Engines_Parallel_Container_i::create_component_instance(const char*genericRegisterName,
                                                         CORBA::Long studyId)
+{
+  Engines::FieldsDict_var env = new Engines::FieldsDict;
+  char* reason;
+  Engines::EngineComponent_ptr compo = create_component_instance_env(genericRegisterName,studyId,env, reason);
+  CORBA::string_free(reason);
+  return compo;
+}
+
+//=============================================================================
+//! Create a new component instance
+/*! 
+ *  CORBA method: Creates a new servant instance of a component.
+ *  The servant registers itself to naming service and Registry.
+ *  \param genericRegisterName  Name of the component instance to register
+ *                         in Registry & Name Service (without _inst_n suffix)
+ *  \param studyId         0 for multiStudy instance, 
+ *                         study Id (>0) otherwise
+ *  \param env             dict of environment variables
+ *  \return a loaded component
+ */
+//=============================================================================
+
+Engines::EngineComponent_ptr
+Engines_Parallel_Container_i::create_component_instance_env(const char*genericRegisterName,
+                                                            CORBA::Long studyId,
+                                                            const Engines::FieldsDict& env,
+                                                            CORBA::String_out reason)
 {
   MESSAGE("Begin of create_component_instance in node : " << getMyRank());
+  reason=CORBA::string_dup("");
 
   if (studyId < 0)
   {
     INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
-    return Engines::Component::_nil() ;
+    return Engines::EngineComponent::_nil() ;
   }
 
   std::string aCompName = genericRegisterName;
 #ifndef WIN32
-  string impl_name = string ("lib") + aCompName +string("Engine.so");
+  std::string impl_name = string ("lib") + aCompName +string("Engine.so");
 #else
-  string impl_name = aCompName +string("Engine.dll");
+  std::string impl_name = aCompName +string("Engine.dll");
 #endif
 
   _numInstanceMutex.lock();
@@ -416,10 +443,10 @@ Engines_Parallel_Container_i::create_component_instance(const char*genericRegist
   {
     std::cerr << "Component library is not loaded or imported ! lib was : " << aCompName << std::endl;
     _numInstanceMutex.unlock();
-    return Engines::Component::_nil();
+    return Engines::EngineComponent::_nil();
   }
 
-  Engines::Component_var iobject = Engines::Component::_nil();
+  Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
   if (type_of_lib == "cpp")
     iobject = createCPPInstance(aCompName, handle, studyId);
   else
@@ -441,14 +468,14 @@ Engines_Parallel_Container_i::create_component_instance(const char*genericRegist
  */
 //=============================================================================
 
-Engines::Component_ptr Engines_Parallel_Container_i::find_component_instance( const char* registeredName,
+Engines::EngineComponent_ptr Engines_Parallel_Container_i::find_component_instance( const char* registeredName,
                                                                               CORBA::Long studyId)
 {
-  Engines::Component_var anEngine = Engines::Component::_nil();
-  map<string,Engines::Component_var>::iterator itm =_listInstances_map.begin();
+  Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
+  std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
   while (itm != _listInstances_map.end())
   {
-    string instance = (*itm).first;
+    std::string instance = (*itm).first;
     SCRUTE(instance);
     if (instance.find(registeredName) == 0)
     {
@@ -478,12 +505,14 @@ Engines::Component_ptr Engines_Parallel_Container_i::find_component_instance( co
  */
 //=============================================================================
 
-Engines::Component_ptr Engines_Parallel_Container_i::load_impl( const char* genericRegisterName,
+Engines::EngineComponent_ptr Engines_Parallel_Container_i::load_impl( const char* genericRegisterName,
                                                                 const char* componentName )
 {
-  Engines::Component_var iobject = Engines::Component::_nil();
-  if (load_component_Library(genericRegisterName))
+  Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
+  char* reason;
+  if (load_component_Library(genericRegisterName,reason))
     iobject = find_or_create_instance(genericRegisterName);
+  CORBA::string_free(reason);
   return iobject._retn();
 }
 
@@ -496,13 +525,13 @@ Engines::Component_ptr Engines_Parallel_Container_i::load_impl( const char* gene
  */
 //=============================================================================
 
-void Engines_Parallel_Container_i::remove_impl(Engines::Component_ptr component_i)
+void Engines_Parallel_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
 {
   ASSERT(!CORBA::is_nil(component_i));
-  string instanceName = component_i->instanceName();
+  std::string instanceName = component_i->instanceName();
   _numInstanceMutex.lock() ; // lock to be alone (stl container write)
   // Test if the component is in this container
-  std::map<std::string, Engines::Component_var>::iterator itm;
+  std::map<std::string, Engines::EngineComponent_var>::iterator itm;
   itm = _listInstances_map.find(instanceName);
   if (itm != _listInstances_map.end())
   {
@@ -529,11 +558,11 @@ void Engines_Parallel_Container_i::finalize_removal()
   MESSAGE("WARNING FINALIZE DOES CURRENTLY NOTHING !!!");
 
   // (see decInstanceCnt, load_component_Library)
-  //map<string, void *>::iterator ith;
+  //std::map<std::string, void *>::iterator ith;
   //for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
   //{
   //  void *handle = (*ith).second;
-  //  string impl_name= (*ith).first;
+  //  std::string impl_name= (*ith).first;
   //  if (handle)
   //  {
   //    SCRUTE(handle);
@@ -585,7 +614,7 @@ bool Engines_Parallel_Container_i::Kill_impl()
 Engines::fileRef_ptr
 Engines_Parallel_Container_i::createFileRef(const char* origFileName)
 {
-  string origName(origFileName);
+  std::string origName(origFileName);
   Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
 
   if (origName[0] != '/')
@@ -680,15 +709,15 @@ Engines_Parallel_Container_i::createSalome_file(const char* origFileName)
  */
 //=============================================================================
 
-Engines::Component_ptr
-Engines_Parallel_Container_i::find_or_create_instance(string genericRegisterName)
+Engines::EngineComponent_ptr
+Engines_Parallel_Container_i::find_or_create_instance(std::string genericRegisterName)
 {
-  Engines::Component_var iobject = Engines::Component::_nil();
+  Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
   try
   {
-    string aGenRegisterName = genericRegisterName;
+    std::string aGenRegisterName = genericRegisterName;
     // --- find a registered instance in naming service, or create
-    string component_registerBase = _containerName + "/" + aGenRegisterName;
+    std::string component_registerBase = _containerName + "/" + aGenRegisterName;
     CORBA::Object_var obj = _NS->ResolveFirst(component_registerBase.c_str());
     if (CORBA::is_nil( obj ))
     {
@@ -697,14 +726,14 @@ Engines_Parallel_Container_i::find_or_create_instance(string genericRegisterName
     }
     else
     { 
-      iobject = Engines::Component::_narrow(obj) ;
+      iobject = Engines::EngineComponent::_narrow(obj) ;
       Engines_Component_i *servant = dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
       ASSERT(servant)
       int studyId = servant->getStudyId();
       ASSERT (studyId >= 0);
       if (studyId != 0)  // monoStudy instance: NOK
       {
-        iobject = Engines::Component::_nil();
+        iobject = Engines::EngineComponent::_nil();
         INFOS("load_impl & find_component_instance methods "
               << "NOT SUITABLE for mono study components");
       }
@@ -738,17 +767,17 @@ Engines_Parallel_Container_i::find_or_create_instance(string genericRegisterName
  *  component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
  */
 //=============================================================================
-Engines::Component_ptr
-Engines_Parallel_Container_i::createPythonInstance(string genericRegisterName, int studyId)
+Engines::EngineComponent_ptr
+Engines_Parallel_Container_i::createPythonInstance(std::string genericRegisterName, int studyId)
 {
 
-  Engines::Component_var iobject = Engines::Component::_nil();
+  Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
 
   int numInstance = _numInstance;
   char aNumI[12];
   sprintf( aNumI , "%d" , numInstance ) ;
-  string instanceName = genericRegisterName + "_inst_" + aNumI ;
-  string component_registerName = _containerName + "/" + instanceName;
+  std::string instanceName = genericRegisterName + "_inst_" + aNumI ;
+  std::string component_registerName = _containerName + "/" + instanceName;
 
   Py_ACQUIRE_NEW_THREAD;
   PyObject *mainmod = PyImport_AddModule("__main__");
@@ -760,14 +789,17 @@ Engines_Parallel_Container_i::createPythonInstance(string genericRegisterName, i
                                          genericRegisterName.c_str(),
                                          instanceName.c_str(),
                                          studyId);
-  std::string iors = PyString_AsString(result);
+  const char *ior;
+  const char *error;
+  PyArg_ParseTuple(result,"ss", &ior, &error);
+  string iors = ior;
   Py_DECREF(result);
   Py_RELEASE_NEW_THREAD;
 
   if( iors!="" )
   {
     CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
-    iobject = Engines::Component::_narrow(obj);
+    iobject = Engines::EngineComponent::_narrow(obj);
     _listInstances_map[instanceName] = iobject;
   }
   else
@@ -797,8 +829,8 @@ Engines_Parallel_Container_i::createPythonInstance(string genericRegisterName, i
  *  component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
  */
 //=============================================================================
-Engines::Component_ptr
-Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
+Engines::EngineComponent_ptr
+Engines_Parallel_Container_i::createCPPInstance(std::string genericRegisterName,
                                                 void *handle,
                                                 int studyId)
 {
@@ -806,8 +838,8 @@ Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
 
   // --- find the factory
 
-  string aGenRegisterName = genericRegisterName;
-  string factory_name = aGenRegisterName + string("Engine_factory");
+  std::string aGenRegisterName = genericRegisterName;
+  std::string factory_name = aGenRegisterName + string("Engine_factory");
 
   typedef  PortableServer::ObjectId * (*FACTORY_FUNCTION_2)
     (CORBA::ORB_ptr,
@@ -829,18 +861,18 @@ Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
 #ifndef WIN32
     INFOS("dlerror() result is : " << dlerror());
 #endif
-    return Engines::Component::_nil() ;
+    return Engines::EngineComponent::_nil() ;
   }
 
   // --- create instance
-  Engines::Component_var iobject = Engines::Component::_nil() ;
+  Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
   try
   {
     int numInstance = _numInstance;
     char aNumI[12];
     sprintf( aNumI , "%d" , numInstance );
-    string instanceName = aGenRegisterName + "_inst_" + aNumI;
-    string component_registerName =
+    std::string instanceName = aGenRegisterName + "_inst_" + aNumI;
+    std::string component_registerName =
       _containerName + "/" + instanceName;
 
     // --- Instanciate required CORBA object
@@ -856,7 +888,7 @@ Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
 
     // --- get reference & servant from id
     CORBA::Object_var obj = _poa->id_to_reference(*id);
-    iobject = Engines::Component::_narrow(obj);
+    iobject = Engines::EngineComponent::_narrow(obj);
 
     Engines_Component_i *servant = 
       dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
@@ -890,7 +922,7 @@ Engines_Parallel_Container_i::create_paco_component_node_instance(const char* co
 {
   // Init de la méthode
   char * proxy_ior;
-  Engines::Component_PaCO_var work_node;
+  Engines::EngineComponent_PaCO_var work_node;
   std::string aCompName = componentName;
   std::string _proxy_containerName = proxy_containerName;
 
@@ -911,7 +943,7 @@ Engines_Parallel_Container_i::create_paco_component_node_instance(const char* co
   // Step 1 : Get proxy !
   string component_registerName = _proxy_containerName + "/" + instanceName;
   CORBA::Object_var temp = _NS->Resolve(component_registerName.c_str());
-  Engines::Component_var obj_proxy = Engines::Component::_narrow(temp);
+  Engines::EngineComponent_var obj_proxy = Engines::EngineComponent::_narrow(temp);
   if (CORBA::is_nil(obj_proxy))
   {
     INFOS("Proxy reference from NamingService is nil !");
@@ -943,8 +975,8 @@ Engines_Parallel_Container_i::create_paco_component_node_instance(const char* co
   {
     char aNumI2[12];
     sprintf(aNumI2 , "%d" , getMyRank()) ;
-    string instanceName = aCompName + "_inst_" + aNumI + "_work_node_" + aNumI2;
-    string component_registerName = _containerName + "/" + instanceName;
+    std::string instanceName = aCompName + "_inst_" + aNumI + "_work_node_" + aNumI2;
+    std::string component_registerName = _containerName + "/" + instanceName;
 
     // --- Instanciate work node
     PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
@@ -953,7 +985,7 @@ Engines_Parallel_Container_i::create_paco_component_node_instance(const char* co
 
     // --- get reference & servant from id
     CORBA::Object_var obj = _poa->id_to_reference(*id);
-    work_node = Engines::Component_PaCO::_narrow(obj) ;
+    work_node = Engines::EngineComponent_PaCO::_narrow(obj) ;
     if (CORBA::is_nil(work_node))
     {
       INFOS("work_node reference from factory is nil !");
@@ -984,11 +1016,11 @@ Engines_Parallel_Container_i::create_paco_component_node_instance(const char* co
  */
 //=============================================================================
 
-void Engines_Parallel_Container_i::decInstanceCnt(string genericRegisterName)
+void Engines_Parallel_Container_i::decInstanceCnt(std::string genericRegisterName)
 {
   if(_cntInstances_map.count(genericRegisterName) !=0 )
   {
-    string aGenRegisterName =genericRegisterName;
+    std::string aGenRegisterName =genericRegisterName;
     MESSAGE("Engines_Parallel_Container_i::decInstanceCnt " << aGenRegisterName);
     ASSERT(_cntInstances_map[aGenRegisterName] > 0); 
     _numInstanceMutex.lock(); // lock to be alone
@@ -997,7 +1029,7 @@ void Engines_Parallel_Container_i::decInstanceCnt(string genericRegisterName)
     SCRUTE(_cntInstances_map[aGenRegisterName]);
     if (_cntInstances_map[aGenRegisterName] == 0)
     {
-      string impl_name =
+      std::string impl_name =
         Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
       SCRUTE(impl_name);
       void* handle = _library_map[impl_name];
@@ -1095,6 +1127,14 @@ Engines_Parallel_Container_i::createPyNode(const char* nodeName, const char* cod
   return node._retn();
 }
 
+Engines::PyScriptNode_ptr 
+Engines_Parallel_Container_i::createPyScriptNode(const char* nodeName, const char* cod)
+{
+  INFOS("Python script node not yet implemented");
+  Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
+  return node._retn();
+}
+
 //=============================================================================
 /*! 
  *  
@@ -1213,4 +1253,3 @@ void SigIntHandler( int what ) {
   }
 }
 #endif
-