Salome HOME
updated copyright message
[modules/gui.git] / src / PVServerService / PVServer_ServiceWrapper.cxx
1 // Copyright (C) 2015-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Adrien Bruneton (CEA)
20
21 #include "PVServer_ServiceWrapper.h"
22 #include <Utils_SALOME_Exception.hxx>
23
24 #include <PyInterp_Utils.h>
25
26 class PVServer_ServiceWrapper::Private
27 {
28 public:
29   // Borrowed reference to the Python object representing the service
30   PyObject* pvserverService;
31 };
32
33 PVServer_ServiceWrapper * PVServer_ServiceWrapper::instance = NULL;
34
35 PVServer_ServiceWrapper * PVServer_ServiceWrapper::GetInstance()
36 {
37   if (!instance)
38     instance = new PVServer_ServiceWrapper();
39   return instance;
40 }
41
42 PVServer_ServiceWrapper::PVServer_ServiceWrapper()
43 {
44   myData = new Private;
45   PyLockWrapper lock;
46 #ifdef GUI_DISABLE_CORBA
47   const char* code = "import PVSERVER_impl as pi;__servicePVSERVER=pi.PVSERVER_Impl()";
48 #else
49   const char* code = "import PVSERVER_utils as pa;__servicePVSERVER=pa.getService()";
50 #endif
51
52   int ret = PyRun_SimpleString(const_cast<char*>(code));
53   if (ret == -1)
54     throw SALOME_Exception("Unable to retrieve PVSERVER service!");
55
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;
62 }
63
64 PVServer_ServiceWrapper::~PVServer_ServiceWrapper()
65 {
66   StopPVServer();
67   delete myData;
68 }
69
70 bool PVServer_ServiceWrapper::GetGUIConnected()
71 {
72   PyLockWrapper lock;
73   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("GetGUIConnected"), NULL));
74   if (!obj)
75     {
76       PyErr_Print();
77       throw SALOME_Exception("Unable to invoke PVSERVER engine!");
78     }
79   return PyObject_IsTrue(obj);
80 }
81
82 void PVServer_ServiceWrapper::SetGUIConnected(bool isConnected)
83 {
84   PyLockWrapper lock;
85
86   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("SetGUIConnected"),
87                                        (char *)"i", (int)isConnected ) );
88   if (!obj)
89     {
90       PyErr_Print();
91       throw SALOME_Exception("Unable to invoke PVSERVER service!");
92     }
93 }
94
95 std::string PVServer_ServiceWrapper::FindOrStartPVServer(int port)
96 {
97   PyLockWrapper lock;
98   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("FindOrStartPVServer"),
99                                          (char *)"i", port ) );
100   if (!obj)
101     {
102       PyErr_Print();
103       throw SALOME_Exception("Unable to invoke PVSERVER service!");
104     }
105   return std::string(PyUnicode_AsUTF8(obj));
106 }
107
108
109 bool PVServer_ServiceWrapper::StopPVServer()
110 {
111   PyLockWrapper lock;
112   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverService, (char*)("StopPVServer"), NULL ));
113   if (!obj)
114     {
115       PyErr_Print();
116       throw SALOME_Exception("Unable to invoke PVSERVER service!");
117     }
118   return PyObject_IsTrue(obj);
119 }
120