]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
- Parallel Container can now load cpp and python components
authorribes <ribes>
Tue, 24 Mar 2009 16:09:39 +0000 (16:09 +0000)
committerribes <ribes>
Tue, 24 Mar 2009 16:09:39 +0000 (16:09 +0000)
src/ParallelContainer/SALOME_ParallelContainerProxyDummy.cxx
src/ParallelContainer/SALOME_ParallelContainerProxy_i.cxx
src/ParallelContainer/SALOME_ParallelContainerProxy_i.hxx
src/ParallelContainer/SALOME_ParallelContainer_i.cxx

index 0d4ba45206ee1559c681dfe54cc8424d1a6110ae..3b078228e8b3216f81ed2ccfa55b7596e2923244 100644 (file)
@@ -49,6 +49,8 @@
 #include "SALOMETraceCollector.hxx"
 #include "OpUtil.hxx"
 
+#include "Container_init_python.hxx"
+
 #ifdef DEBUG_PARALLEL
 #include <signal.h>
 using namespace std;
@@ -72,6 +74,7 @@ int main(int argc, char* argv[])
 #endif
   // Initialise the ORB.
   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
+  KERNEL_PYTHON::init_python(argc,argv);
 
   std::string containerName("");
   if(argc > 1) {
@@ -129,6 +132,9 @@ int main(int argc, char* argv[])
     ns->Register(pCont, _containerName.c_str());
     pman->activate();
     orb->run();
+    PyGILState_Ensure();
+    //Delete python container that destroy orb from python (pyCont._orb.destroy())
+    Py_Finalize();
     delete ns;
   }
   catch (PaCO::PACO_Exception& e)
index 8fc2d3ee817a1da7575ecb3b03c03f8b2dd8cfb7..60c35a3a4d7774f355f9d84465bbc20610f4397d 100644 (file)
@@ -48,6 +48,18 @@ Container_proxy_impl_final::Container_proxy_impl_final(CORBA::ORB_ptr orb,
   // Init SALOME Naming Service
   _NS = new SALOME_NamingService();
   _NS->init_orb(_orb);
+
+  // Init Python container part
+  CORBA::Object_var container_node = _poa->id_to_reference(*_id);
+  CORBA::String_var sior =  _orb->object_to_string(container_node);
+  std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
+  myCommand += _containerName + "','";
+  myCommand += sior;
+  myCommand += "')\n";
+  Py_ACQUIRE_NEW_THREAD;
+  PyRun_SimpleString("import SALOME_Container\n");
+  PyRun_SimpleString((char*)myCommand.c_str());
+  Py_RELEASE_NEW_THREAD;
 }
 
 Container_proxy_impl_final:: ~Container_proxy_impl_final() {
@@ -127,6 +139,7 @@ Container_proxy_impl_final::load_component_Library(const char* componentName)
   MESSAGE("Begin of load_component_Library on proxy : " << componentName)
   std::string aCompName = componentName;
 
+  CORBA::Boolean ret = true;
   if (_libtype_map.count(aCompName) == 0)
   {
     _numInstanceMutex.lock(); // lock to be alone
@@ -179,39 +192,68 @@ Container_proxy_impl_final::load_component_Library(const char* componentName)
 #endif
       }
     }
+    else
+    {
+      MESSAGE("Try to import Python component "<<componentName);
+      Py_ACQUIRE_NEW_THREAD;
+      PyObject *mainmod = PyImport_AddModule("__main__");
+      PyObject *globals = PyModule_GetDict(mainmod);
+      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+      PyObject *result = PyObject_CallMethod(pyCont,
+                                            (char*)"import_component",
+                                            (char*)"s",componentName);
+      int ret_p= PyInt_AsLong(result);
+      Py_XDECREF(result);
+      Py_RELEASE_NEW_THREAD;
+
+      if (ret_p) // import possible: Python component
+      {
+       MESSAGE("import Python: " << aCompName <<" OK");
+      }
+      else
+      {
+       MESSAGE("Error in importing Python component : " << aCompName);
+       ret = false;
+      }
+    }
     _numInstanceMutex.unlock();
   }
 
   // Call load_component_Library in each node
-  CORBA::Boolean ret = true;
-  for (CORBA::ULong i = 0; i < _infos.nodes.length(); i++)
+  if (ret)
   {
-    MESSAGE("Call load_component_Library work node : " << i);
-    CORBA::Object_var object = _orb->string_to_object(_infos.nodes[i]);
-    Engines::Container_var node = Engines::Container::_narrow(object);
-    if (!CORBA::is_nil(node))
+    for (CORBA::ULong i = 0; i < _infos.nodes.length(); i++)
     {
-      try 
+      MESSAGE("Call load_component_Library work node : " << i);
+      CORBA::Object_var object = _orb->string_to_object(_infos.nodes[i]);
+      Engines::Container_var node = Engines::Container::_narrow(object);
+      if (!CORBA::is_nil(node))
       {
-       node->load_component_Library(componentName);
-       MESSAGE("Call load_component_Library done node : " << i);
+       try 
+       {
+         node->load_component_Library(componentName);
+         MESSAGE("Call load_component_Library done node : " << i);
+       }
+       catch (...)
+       {
+         INFOS("Exception catch during load_component_Library of node : " << i);
+         ret = false;
+       }
       }
-      catch (...)
+      else
       {
-       INFOS("Exception catch during load_component_Library of node : " << i);
+       INFOS("Cannot call load_component_Library node " << i << " ref is nil !");
        ret = false;
       }
     }
-    else
-    {
-      INFOS("Cannot call load_component_Library node " << i << " ref is nil !");
-      ret = false;
-    }
   }
 
   // If ret is false -> lib is not loaded !
   if (!ret)
+  {
+    INFOS("Cannot call load_component_Library " << aCompName);
     _libtype_map.erase(aCompName);
+  }
   return ret;
 }
 
@@ -232,7 +274,12 @@ Container_proxy_impl_final::create_component_instance(const char* componentName,
 
   // If it is a sequential component
   if (_libtype_map[aCompName] == "seq")
+  {
+    _numInstanceMutex.lock() ; // lock on the instance number
+    _numInstance++ ;
+    _numInstanceMutex.unlock() ;
     return Engines::Container_proxy_impl::create_component_instance(componentName, studyId);
+  }
 
   // Parallel Component !
   Engines::Component_var component_proxy = Engines::Component::_nil();
index abc6b54b010cdd789e7a29c699f20b3f39d0bac9..6e4c56db47274168ef1ebe4890fb7febacae3a33 100644 (file)
@@ -36,6 +36,9 @@
 #include <dlfcn.h>
 #include <paco_omni.h>
 
+#include <Python.h>
+#include "Container_init_python.hxx"
+
 class Container_proxy_impl_final :
   virtual public Engines::PACO_Container_proxy_impl,
   virtual public ParallelGlobalProcessVar_i
index ef4310a8c764b8221ca0d96ca94fd5a6e708e4c3..00b219a3d25ef0156f92aa3c077619096c4fb7ea 100644 (file)
@@ -396,6 +396,9 @@ Engines_Parallel_Container_i::create_component_instance(const char*genericRegist
   string impl_name = aCompName +string("Engine.dll");
 #endif
 
+  _numInstanceMutex.lock();
+  _numInstance++;
+
   // Test if the component lib is loaded
   std::string type_of_lib("Not Loaded");
   void* handle = _library_map[impl_name];
@@ -407,6 +410,7 @@ Engines_Parallel_Container_i::create_component_instance(const char*genericRegist
   if (type_of_lib == "Not Loaded")
   {
     std::cerr << "Component library is not loaded or imported ! lib was : " << aCompName << std::endl;
+    _numInstanceMutex.unlock();
     return Engines::Component::_nil();
   }
 
@@ -416,6 +420,7 @@ Engines_Parallel_Container_i::create_component_instance(const char*genericRegist
   else
     iobject = createPythonInstance(aCompName, studyId);
 
+  _numInstanceMutex.unlock();
   return iobject._retn();
 }
 
@@ -734,10 +739,7 @@ Engines_Parallel_Container_i::createPythonInstance(string genericRegisterName, i
 
   Engines::Component_var iobject = Engines::Component::_nil();
 
-  _numInstanceMutex.lock();
-  _numInstance++;
   int numInstance = _numInstance;
-  _numInstanceMutex.unlock();
   char aNumI[12];
   sprintf( aNumI , "%d" , numInstance ) ;
   string instanceName = genericRegisterName + "_inst_" + aNumI ;
@@ -829,11 +831,7 @@ Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
   Engines::Component_var iobject = Engines::Component::_nil() ;
   try
   {
-    _numInstanceMutex.lock(); 
-    _numInstance++;
     int numInstance = _numInstance;
-    _numInstanceMutex.unlock();
-
     char aNumI[12];
     sprintf( aNumI , "%d" , numInstance );
     string instanceName = aGenRegisterName + "_inst_" + aNumI;