Salome HOME
Fix pb with duplication of PVSERVER service
[modules/gui.git] / src / PVServerService / ServiceLoader / PVServer_ServiceLoader.cxx
1 // Copyright (C) 2015-2016  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_ServiceLoader.h"
22 #include "PVServer_ServiceWrapper.h"
23
24 #include <SALOME_LifeCycleCORBA.hxx>
25 #include <SALOME_NamingService.hxx>
26 #include <utilities.h> // MESSAGE() macro
27
28 PVServer_ServiceLoader::PVServer_ServiceLoader():
29      _lcc(0)
30 {
31   _lcc = new SALOME_LifeCycleCORBA();
32   _ns = _lcc->namingService();
33   _cm = _lcc->getContainerManager();
34   _orb = _lcc->orb();
35
36 }
37
38 PVServer_ServiceLoader::~PVServer_ServiceLoader()
39 {
40   if (_lcc)
41     delete _lcc;
42   _lcc = 0;
43 }
44
45 std::string PVServer_ServiceLoader::findOrLoadService(const char * containerName)
46 {
47   std::string ior = findService(containerName);
48   if (ior == "")
49     ior = loadService(containerName);
50   return ior;
51 }
52
53 std::string PVServer_ServiceLoader::findService(const char * containerName)
54 {
55   std::string path = "/Containers";
56   path += containerName;
57   path += "/PVSERVER";
58   CORBA::Object_ptr obj = _ns->Resolve(path.c_str());
59   if(!CORBA::is_nil(obj))
60     {
61       MESSAGE("PVServer_ServiceLoader::findService(): Service found! ");
62       char * ior = _orb->object_to_string(obj);
63       return std::string(ior);
64     }
65   else
66     {
67       MESSAGE("PVServer_ServiceLoader::findService(): Service NOT found! ");
68       return std::string("");
69     }
70
71 }
72
73 std::string PVServer_ServiceLoader::loadService(const char * containerName)
74 {
75   // Get the requested container
76   Engines::ContainerParameters params;
77   params.container_name = CORBA::string_dup(containerName);
78   params.isMPI = false;
79   params.mode = "getorstart";
80   Engines::Container_ptr contain = _cm->GiveContainer(params);
81
82   if (!CORBA::is_nil(contain))
83     {
84       char * reason;
85       char * ior = contain->create_python_service_instance("PVSERVER", reason);
86       std::string reas(reason);
87       CORBA::string_free(reason);
88       if (reas != "")
89         {
90           MESSAGE("PVServer_ServiceLoader::loadService(): Python service instance could not be created! ");
91           MESSAGE("PVServer_ServiceLoader::loadService(): " << reas);
92           return std::string("");
93         }
94       else
95         {
96           MESSAGE("PVServer_ServiceLoader::loadService(): Container and service loaded! ");
97           return std::string(ior);
98         }
99     }
100   else
101     {
102       MESSAGE("PVServer_ServiceLoader::loadService(): Container could not be retrieved! ");
103       return std::string("");
104     }
105 }
106