1 // Copyright (C) 2012-2016 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 "AutoGIL.hxx"
25 #include "Exception.hxx"
31 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
33 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
35 const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
37 YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_isForcedPyThreadSaved(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
39 if(!Py_IsInitialized())
43 YACS::ENGINE::AutoGIL gal;
44 _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
48 YACSEvalSession::~YACSEvalSession()
51 YACS::ENGINE::AutoGIL gal;
52 if(isLaunched() && !isAttached())
54 YACS::ENGINE::AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
55 YACS::ENGINE::AutoPyRef res(PyObject_CallObject(terminateSession,0));
57 Py_XDECREF(_salomeInstance);
58 Py_XDECREF(_salomeInstanceModule);
61 void YACSEvalSession::launch()
65 YACS::ENGINE::AutoGIL gal;
66 PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
67 PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
68 Py_XDECREF(salomeInstance);
69 YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(0));//new
70 YACS::ENGINE::AutoPyRef myKWArgs(PyDict_New());//new
71 PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
72 _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
73 YACS::ENGINE::AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
74 YACS::ENGINE::AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new
75 _port=PyLong_AsLong(portPy);
78 _corbaConfigFileName=GetConfigAndPort(dummy);
79 _isAttached=false; _isLaunched=true;
82 void YACSEvalSession::launchUsingCurrentSession()
86 YACS::ENGINE::AutoGIL gal;
87 _corbaConfigFileName=GetConfigAndPort(_port);
88 _isAttached=true; _isLaunched=true;
91 bool YACSEvalSession::isAlreadyPyThreadSaved() const
93 return _isForcedPyThreadSaved;
96 void YACSEvalSession::checkLaunched() const
99 throw YACS::Exception("YACSEvalSession::checkLaunched : not launched !");
102 int YACSEvalSession::getPort() const
108 std::string YACSEvalSession::getCorbaConfigFileName() const
111 return _corbaConfigFileName;
114 std::string YACSEvalSession::GetPathToAdd()
117 YACS::ENGINE::AutoGIL gal;
118 YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
119 PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
121 YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
122 YACS::ENGINE::AutoPyRef kernelRootDirStr(PyBytes_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
123 kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
126 YACS::ENGINE::AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
127 YACS::ENGINE::AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
128 YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(4));
129 Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
130 PyTuple_SetItem(myArgs,1,PyBytes_FromString(const_cast<char *>("bin")));
131 PyTuple_SetItem(myArgs,2,PyBytes_FromString(const_cast<char *>("salome")));
132 PyTuple_SetItem(myArgs,3,PyBytes_FromString(const_cast<char *>("appliskel")));
133 YACS::ENGINE::AutoPyRef res(PyObject_CallObject(joinPy,myArgs));
134 ret=PyBytes_AsString(res);
136 Py_XDECREF(kernelRootDir);
140 std::string YACSEvalSession::GetConfigAndPort(int& port)
142 YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
143 YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
145 YACS::ENGINE::AutoPyRef corbaConfigStr(PyBytes_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
146 YACS::ENGINE::AutoPyRef corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
147 std::string ret(PyBytes_AsString(corbaConfigFileNamePy));
149 YACS::ENGINE::AutoPyRef nsPortStr(PyBytes_FromString(const_cast<char *>(NSPORT_VAR_NAME)));//new
150 YACS::ENGINE::AutoPyRef nsPortValuePy(PyObject_GetItem(environPy,nsPortStr));//new
151 std::string portStr(PyBytes_AsString(nsPortValuePy));
152 std::istringstream iss(portStr);