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"
29 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
31 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
33 YACSEvalSession::YACSEvalSession():_isLaunched(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
35 if(!Py_IsInitialized())
39 YACS::ENGINE::AutoGIL gal;
40 _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
44 YACSEvalSession::~YACSEvalSession()
47 YACS::ENGINE::AutoGIL gal;
50 PyObject *terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
51 PyObject *res(PyObject_CallObject(terminateSession,0));
53 Py_XDECREF(terminateSession);
55 Py_XDECREF(_salomeInstance);
56 Py_XDECREF(_salomeInstanceModule);
59 void YACSEvalSession::launch()
63 YACS::ENGINE::AutoGIL gal;
64 PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
65 PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
66 Py_XDECREF(salomeInstance);
67 PyObject *myArgs(PyTuple_New(0));//new
68 PyObject *myKWArgs(PyDict_New());//new
69 PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
70 _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
71 PyObject *getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
72 PyObject *portPy(PyObject_CallObject(getPortMeth,0));//new
73 _port=PyInt_AsLong(portPy);
75 Py_XDECREF(getPortMeth);
79 PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
80 PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
81 PyObject *corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
82 PyObject *corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
83 _corbaConfigFileName=PyString_AsString(corbaConfigFileNamePy);
84 Py_XDECREF(corbaConfigFileNamePy);
85 Py_XDECREF(corbaConfigStr);
86 Py_XDECREF(environPy);
91 void YACSEvalSession::checkLaunched() const
94 throw YACS::Exception("YACSEvalSession::checkLaunched : not launched !");
97 int YACSEvalSession::getPort() const
103 std::string YACSEvalSession::getCorbaConfigFileName() const
106 return _corbaConfigFileName;
109 std::string YACSEvalSession::GetPathToAdd()
112 YACS::ENGINE::AutoGIL gal;
113 PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
114 PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
116 PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
117 PyObject *kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
118 kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
119 Py_XDECREF(kernelRootDirStr);
120 Py_XDECREF(environPy);
123 PyObject *pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
124 PyObject *joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
125 PyObject *myArgs(PyTuple_New(4));
126 Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
127 PyTuple_SetItem(myArgs,1,PyString_FromString(const_cast<char *>("bin")));
128 PyTuple_SetItem(myArgs,2,PyString_FromString(const_cast<char *>("salome")));
129 PyTuple_SetItem(myArgs,3,PyString_FromString(const_cast<char *>("appliskel")));
130 PyObject *res(PyObject_CallObject(joinPy,myArgs));
131 ret=PyString_AsString(res);
137 Py_XDECREF(kernelRootDir);