1 // Copyright (C) 2012-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "YACSEvalSession.hxx"
22 #include "YACSEvalSessionInternal.hxx"
24 #include "PythonCppUtils.hxx"
25 #include "Exception.hxx"
32 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
34 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
36 const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
38 YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_isForcedPyThreadSaved(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
41 _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
44 YACSEvalSession::~YACSEvalSession()
48 if(isLaunched() && !isAttached())
50 AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
51 AutoPyRef res(PyObject_CallObject(terminateSession,0));
53 Py_XDECREF(_salomeInstance);
54 Py_XDECREF(_salomeInstanceModule);
57 void YACSEvalSession::launch()
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);
74 _corbaConfigFileName=GetConfigAndPort(dummy);
75 _isAttached=false; _isLaunched=true;
78 void YACSEvalSession::launchUsingCurrentSession()
83 _corbaConfigFileName=GetConfigAndPort(_port);
84 _isAttached=true; _isLaunched=true;
87 bool YACSEvalSession::isAlreadyPyThreadSaved() const
89 return _isForcedPyThreadSaved;
92 void YACSEvalSession::checkLaunched() const
95 throw YACS::Exception("YACSEvalSession::checkLaunched : not launched !");
98 int YACSEvalSession::getPort() const
104 std::string YACSEvalSession::getCorbaConfigFileName() const
107 return _corbaConfigFileName;
110 std::string YACSEvalSession::GetPathToAdd()
114 AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
115 PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
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
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);
132 Py_XDECREF(kernelRootDir);
136 std::string YACSEvalSession::GetConfigAndPort(int& port)
138 std::istringstream iss(std::getenv(NSPORT_VAR_NAME));
140 return std::getenv(CORBA_CONFIG_ENV_VAR_NAME);