Salome HOME
f04078ebe141325991080c73a4495cdbcd0d6d33
[modules/yacs.git] / src / evalyfx / YACSEvalSessionInternal.cxx
1 // Copyright (C) 2012-2024  CEA, EDF
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 : Anthony Geay (EDF R&D)
20
21 #include "YACSEvalSessionInternal.hxx"
22 #include "YACSEvalSession.hxx"
23
24 #include "PyStdout.hxx"
25 #include "PythonCppUtils.hxx"
26 #include "Exception.hxx"
27
28 YACSEvalSession::YACSEvalSessionInternal::YACSEvalSessionInternal():_orb(CORBA::ORB::_nil()),_sl(Engines::SalomeLauncher::_nil())
29 {
30 }
31
32 void YACSEvalSession::YACSEvalSessionInternal::checkSalomeLauncher()
33 {
34   if(CORBA::is_nil(_sl))
35     throw YACS::Exception("YACSEvalSessionInternal::checkSalomeLauncher : salome launcher is null !");
36 }
37
38 Engines::SalomeLauncher_var YACSEvalSession::YACSEvalSessionInternal::goFetchingSalomeLauncherInNS()
39 {
40   if(!CORBA::is_nil(_sl))
41     return _sl;
42   int argc(0);
43   _orb=CORBA::ORB_init(argc,0);
44   if(CORBA::is_nil(_orb))
45     throw YACS::Exception("YACSEvalSessionInternal contrctor : ORB is null !");
46   //
47   const char methName[]="goFetchingSalomeLauncherInNS";
48   const char fetchPyCmd[]="import salome,CORBA\nsalome.salome_init()\nsl=salome.naming_service.Resolve(\"/SalomeLauncher\")\nif not CORBA.is_nil(sl):\n  return salome.orb.object_to_string(sl)\nelse:\n  raise Exception(\"Impossible to locate salome launcher !\")";
49   AutoPyRef func(YACS::ENGINE::evalPy(methName,fetchPyCmd));
50   AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func));
51   std::string ior;
52   if (PyUnicode_Check(val))
53     ior = PyUnicode_AsUTF8(val);
54   else
55     throw YACS::Exception("goFetchingSalomeLauncherInNS: python call error. ");
56   CORBA::Object_var obj(string_to_object(ior));
57   if(CORBA::is_nil(obj))
58     throw YACS::Exception("goFetchingSalomeLauncherInNS : fetched ior is NIL !");
59   _sl=Engines::SalomeLauncher::_narrow(obj);
60   checkSalomeLauncher();
61   return _sl;
62 }
63
64 Engines::SalomeLauncher_var YACSEvalSession::YACSEvalSessionInternal::getNotNullSL()
65 {
66   checkSalomeLauncher();
67   return _sl;
68 }