Salome HOME
updated copyright message
[modules/yacs.git] / src / evalyfx / YACSEvalSession.cxx
1 // Copyright (C) 2012-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "YACSEvalSession.hxx"
22 #include "YACSEvalSessionInternal.hxx"
23
24 #include "PythonCppUtils.hxx"
25 #include "Exception.hxx"
26
27 #include <Python.h>
28
29 #include <sstream>
30 #include <cstdlib>
31
32 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
33
34 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
35
36 const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
37
38 YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_isForcedPyThreadSaved(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
39 {
40   AutoGIL gal;
41   _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
42 }
43
44 YACSEvalSession::~YACSEvalSession()
45 {
46   delete _internal;
47   AutoGIL gal;
48   if(isLaunched() && !isAttached())
49     {
50       AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
51       AutoPyRef res(PyObject_CallObject(terminateSession,0));
52     }
53   Py_XDECREF(_salomeInstance);
54   Py_XDECREF(_salomeInstanceModule);
55 }
56
57 void YACSEvalSession::launch()
58 {
59   if(isLaunched())
60     return ;
61   AutoGIL gal;
62   PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
63   PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
64   Py_XDECREF(salomeInstance);
65   AutoPyRef myArgs(PyTuple_New(0));//new
66   AutoPyRef myKWArgs(PyDict_New());//new
67   PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
68   _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
69   AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
70   AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new
71   _port=PyLong_AsLong(portPy);
72   //
73   int dummy;
74   _corbaConfigFileName=GetConfigAndPort(dummy);
75   _isAttached=false; _isLaunched=true;
76 }
77
78 void YACSEvalSession::launchUsingCurrentSession()
79 {
80   if(isLaunched())
81     return ;
82   AutoGIL gal;
83   _corbaConfigFileName=GetConfigAndPort(_port);
84   _isAttached=true; _isLaunched=true;
85 }
86
87 bool YACSEvalSession::isAlreadyPyThreadSaved() const
88 {
89   return _isForcedPyThreadSaved;
90 }
91
92 void YACSEvalSession::checkLaunched() const
93 {
94   if(!isLaunched())
95     throw YACS::Exception("YACSEvalSession::checkLaunched : not launched !");
96 }
97
98 int YACSEvalSession::getPort() const
99 {
100   checkLaunched();
101   return _port;
102 }
103
104 std::string YACSEvalSession::getCorbaConfigFileName() const
105 {
106   checkLaunched();
107   return _corbaConfigFileName;
108 }
109
110 std::string YACSEvalSession::GetPathToAdd()
111 {
112   std::string ret;
113   AutoGIL gal;
114   AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
115   PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
116   {
117     AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
118     AutoPyRef kernelRootDirStr(PyBytes_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
119     kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
120   }
121   {
122     AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
123     AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
124     AutoPyRef myArgs(PyTuple_New(4));
125     Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
126     PyTuple_SetItem(myArgs,1,PyBytes_FromString(const_cast<char *>("bin")));
127     PyTuple_SetItem(myArgs,2,PyBytes_FromString(const_cast<char *>("salome")));
128     PyTuple_SetItem(myArgs,3,PyBytes_FromString(const_cast<char *>("appliskel")));
129     AutoPyRef res(PyObject_CallObject(joinPy,myArgs));
130     ret=PyBytes_AsString(res);
131   }
132   Py_XDECREF(kernelRootDir);
133   return ret;
134 }
135
136 std::string YACSEvalSession::GetConfigAndPort(int& port)
137 {
138   std::istringstream iss(std::getenv(NSPORT_VAR_NAME));
139   iss >> port;
140   return std::getenv(CORBA_CONFIG_ENV_VAR_NAME);
141 }