Salome HOME
evalyfx Session can be attached to existing SALOME session.
authorAnthony Geay <anthony.geay@edf.fr>
Wed, 6 Apr 2016 15:53:45 +0000 (17:53 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Wed, 6 Apr 2016 15:53:45 +0000 (17:53 +0200)
src/evalyfx/YACSEvalSession.cxx
src/evalyfx/YACSEvalSession.hxx
src/evalyfx_swig/evalyfx.i

index d28f8eb0b289b1e1a1c746021f4173fc732843d9..250202fa4f8635c558ceca03c2e31e73e83ed5e6 100644 (file)
 
 #include <Python.h>
 
+#include <sstream>
+
 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
 
 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
 
-YACSEvalSession::YACSEvalSession():_isLaunched(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
+const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
+
+YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
 {
   if(!Py_IsInitialized())
     Py_Initialize();
@@ -47,10 +51,8 @@ YACSEvalSession::~YACSEvalSession()
   YACS::ENGINE::AutoGIL gal;
   if(isLaunched())
     {
-      PyObject *terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
-      PyObject *res(PyObject_CallObject(terminateSession,0));
-      Py_XDECREF(res);
-      Py_XDECREF(terminateSession);
+      YACS::ENGINE::AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
+      YACS::ENGINE::AutoPyRef res(PyObject_CallObject(terminateSession,0));
     }
   Py_XDECREF(_salomeInstance);
   Py_XDECREF(_salomeInstanceModule);
@@ -64,28 +66,25 @@ void YACSEvalSession::launch()
   PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
   PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
   Py_XDECREF(salomeInstance);
-  PyObject *myArgs(PyTuple_New(0));//new
-  PyObject *myKWArgs(PyDict_New());//new
+  YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(0));//new
+  YACS::ENGINE::AutoPyRef myKWArgs(PyDict_New());//new
   PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
   _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
-  PyObject *getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
-  PyObject *portPy(PyObject_CallObject(getPortMeth,0));//new
+  YACS::ENGINE::AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
+  YACS::ENGINE::AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new
   _port=PyInt_AsLong(portPy);
-  Py_XDECREF(portPy);
-  Py_XDECREF(getPortMeth);
-  Py_XDECREF(myKWArgs);
-  Py_XDECREF(myArgs);
   //
-  PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
-  PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
-  PyObject *corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
-  PyObject *corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
-  _corbaConfigFileName=PyString_AsString(corbaConfigFileNamePy);
-  Py_XDECREF(corbaConfigFileNamePy);
-  Py_XDECREF(corbaConfigStr);
-  Py_XDECREF(environPy);
-  Py_XDECREF(osPy);
-  _isLaunched=true;
+  int dummy;
+  _corbaConfigFileName=GetConfigAndPort(dummy);
+  _isAttached=false; _isLaunched=true;
+}
+
+void YACSEvalSession::launchUsingCurrentSession()
+{
+  if(isLaunched())
+    return ;
+  YACS::ENGINE::AutoGIL gal;
+  _corbaConfigFileName=GetConfigAndPort(_port);
 }
 
 void YACSEvalSession::checkLaunched() const
@@ -110,31 +109,41 @@ std::string YACSEvalSession::GetPathToAdd()
 {
   std::string ret;
   YACS::ENGINE::AutoGIL gal;
-  PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
+  YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
   PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
   {
-    PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
-    PyObject *kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
+    YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
+    YACS::ENGINE::AutoPyRef kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
     kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
-    Py_XDECREF(kernelRootDirStr);
-    Py_XDECREF(environPy);
   }
   {
-    PyObject *pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
-    PyObject *joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
-    PyObject *myArgs(PyTuple_New(4));
+    YACS::ENGINE::AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
+    YACS::ENGINE::AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
+    YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(4));
     Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
     PyTuple_SetItem(myArgs,1,PyString_FromString(const_cast<char *>("bin")));
     PyTuple_SetItem(myArgs,2,PyString_FromString(const_cast<char *>("salome")));
     PyTuple_SetItem(myArgs,3,PyString_FromString(const_cast<char *>("appliskel")));
-    PyObject *res(PyObject_CallObject(joinPy,myArgs));
+    YACS::ENGINE::AutoPyRef res(PyObject_CallObject(joinPy,myArgs));
     ret=PyString_AsString(res);
-    Py_XDECREF(res);
-    Py_XDECREF(myArgs);
-    Py_XDECREF(joinPy);
-    Py_XDECREF(pathPy);
   }
   Py_XDECREF(kernelRootDir);
-  Py_XDECREF(osPy);
+  return ret;
+}
+
+std::string YACSEvalSession::GetConfigAndPort(int& port)
+{
+  YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
+  YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
+  //
+  YACS::ENGINE::AutoPyRef corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
+  YACS::ENGINE::AutoPyRef corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
+  std::string ret(PyString_AsString(corbaConfigFileNamePy));
+  //
+  YACS::ENGINE::AutoPyRef nsPortStr(PyString_FromString(const_cast<char *>(NSPORT_VAR_NAME)));//new
+  YACS::ENGINE::AutoPyRef nsPortValuePy(PyObject_GetItem(environPy,nsPortStr));//new
+  std::string portStr(PyString_AsString(nsPortValuePy));
+  std::istringstream iss(portStr);
+  iss >> port;
   return ret;
 }
index a58c340114943962d6eb66e35bfc8fdd273caa22..1b0f60414d485556093b1a7d317fd731ab18706d 100644 (file)
@@ -36,7 +36,9 @@ public:
   YACSEVALYFX_EXPORT YACSEvalSession();
   YACSEVALYFX_EXPORT ~YACSEvalSession();
   YACSEVALYFX_EXPORT void launch();
+  YACSEVALYFX_EXPORT void launchUsingCurrentSession();
   YACSEVALYFX_EXPORT bool isLaunched() const { return _isLaunched; }
+  YACSEVALYFX_EXPORT bool isAttached() const { return _isAttached; }
   YACSEVALYFX_EXPORT void checkLaunched() const;
   YACSEVALYFX_EXPORT int getPort() const;
   YACSEVALYFX_EXPORT std::string getCorbaConfigFileName() const;
@@ -44,10 +46,13 @@ public:
   YACSEvalSessionInternal *getInternal() { checkLaunched(); return _internal; }
 private:
   static std::string GetPathToAdd();
+  static std::string GetConfigAndPort(int& port);
 public:
   static const char KERNEL_ROOT_DIR[];
   static const char CORBA_CONFIG_ENV_VAR_NAME[];
+  static const char NSPORT_VAR_NAME[];
 private:
+  bool _isAttached;
   bool _isLaunched;
   int _port;
   std::string _corbaConfigFileName;
index 3c2c12e4aa25ce5902e3384cc7f07fdf1d1665be..3297d0bc524b7bc1580246ab1b55ec2a5b36c85d 100644 (file)
@@ -391,6 +391,7 @@ public:
   ~YACSEvalSession();
   void launch();
   bool isLaunched() const;
+  bool isAttached() const;
   void checkLaunched() const;
   int getPort() const;
   std::string getCorbaConfigFileName() const;