#include "YACSEvalSession.hxx"
#include "YACSEvalSessionInternal.hxx"
+#include "AutoGIL.hxx"
#include "Exception.hxx"
#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();
//
- _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
+ {
+ YACS::ENGINE::AutoGIL gal;
+ _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
+ }
}
YACSEvalSession::~YACSEvalSession()
{
delete _internal;
- if(isLaunched())
+ YACS::ENGINE::AutoGIL gal;
+ if(isLaunched() && !isAttached())
{
- 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);
{
if(isLaunched())
return ;
+ YACS::ENGINE::AutoGIL gal;
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);
+ _isAttached=true; _isLaunched=true;
}
void YACSEvalSession::checkLaunched() const
std::string YACSEvalSession::GetPathToAdd()
{
std::string ret;
- PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
+ YACS::ENGINE::AutoGIL gal;
+ 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;
}