1 // Copyright (C) 2015-2022 CEA/DEN, EDF R&D, OPEN CASCADE
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: Adrien Bruneton (CEA)
21 #include "PVServer_ServiceWrapper.h"
22 #include <Utils_SALOME_Exception.hxx>
24 #include <PyInterp_Utils.h>
26 class PVServer_ServiceWrapper::Private
29 // Borrowed reference to the Python object representing the service
30 PyObject* pvserverService;
33 PVServer_ServiceWrapper * PVServer_ServiceWrapper::instance = NULL;
35 PVServer_ServiceWrapper * PVServer_ServiceWrapper::GetInstance()
38 instance = new PVServer_ServiceWrapper();
42 PVServer_ServiceWrapper::PVServer_ServiceWrapper()
46 #ifdef GUI_DISABLE_CORBA
47 const char* code = "import PVSERVER_impl as pi;__servicePVSERVER=pi.PVSERVER_Impl()";
49 const char* code = "import PVSERVER_utils as pa;__servicePVSERVER=pa.getService()";
52 int ret = PyRun_SimpleString(const_cast<char*>(code));
54 throw SALOME_Exception("Unable to retrieve PVSERVER service!");
56 // Now get the reference to __engine and save the pointer.
57 // All the calls below returns *borrowed* references
58 PyObject* main_module = PyImport_AddModule((char*)"__main__");
59 PyObject* global_dict = PyModule_GetDict(main_module);
60 PyObject* tmp = PyDict_GetItemString(global_dict, "__servicePVSERVER");
61 myData->pvserverService = tmp;
64 PVServer_ServiceWrapper::~PVServer_ServiceWrapper()
70 bool PVServer_ServiceWrapper::GetGUIConnected()
73 PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("GetGUIConnected"), NULL));
77 throw SALOME_Exception("Unable to invoke PVSERVER engine!");
79 return PyObject_IsTrue(obj);
82 void PVServer_ServiceWrapper::SetGUIConnected(bool isConnected)
86 PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("SetGUIConnected"),
87 (char *)"i", (int)isConnected ) );
91 throw SALOME_Exception("Unable to invoke PVSERVER service!");
95 std::string PVServer_ServiceWrapper::FindOrStartPVServer(int port)
98 PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("FindOrStartPVServer"),
99 (char *)"i", port ) );
103 throw SALOME_Exception("Unable to invoke PVSERVER service!");
105 return std::string(PyUnicode_AsUTF8(obj));
109 bool PVServer_ServiceWrapper::StopPVServer()
112 PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("StopPVServer"), NULL ));
116 throw SALOME_Exception("Unable to invoke PVSERVER service!");
118 return PyObject_IsTrue(obj);