Salome HOME
329ea11817f18ac6a09504267966bf6656ed4133
[modules/gui.git] / src / PVServerService / ServiceLoader / PVServer_ServiceLoader.cxx
1 // Copyright (C) 2015-2022  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
23 #include <SALOME_LifeCycleCORBA.hxx>
24 #include <SALOME_NamingService.hxx>
25 #include <utilities.h> // MESSAGE() macro
26 #include <SALOMEconfig.h>
27 #include "KernelBasis.hxx"
28
29 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
30
31 PVServer_ServiceLoader::PVServer_ServiceLoader():
32   myLcc( nullptr )
33 {
34   try
35   {
36       SALOME_NamingService_Abstract *ns(nullptr);
37       if( getSSLMode() )
38       {
39         mySSLNS.reset( new SALOME_Fake_NamingService );
40         ns = mySSLNS.get();
41       }
42       myLcc = new SALOME_LifeCycleCORBA(ns);
43   }
44   catch(...)
45   {
46       throw PVServer_ServiceLoader_Exception("Unable to instanciate SALOME_LifeCycleCORBA!");
47   }
48 }
49
50 PVServer_ServiceLoader::~PVServer_ServiceLoader()
51 {
52   delete myLcc;
53 }
54
55 SALOME_LifeCycleCORBA* PVServer_ServiceLoader::lcc()
56 {
57   return myLcc;
58 }
59
60 std::string PVServer_ServiceLoader::findOrLoadService( const std::string& containerName )
61 {
62   std::string ior = findService( containerName );
63   if ( ior == "" )
64     ior = loadService( containerName );
65   return ior;
66 }
67
68 std::string PVServer_ServiceLoader::findService( const std::string& containerName )
69 {
70   std::string ior = "";
71
72   Engines::Container_var container = getContainer( containerName, "get" );
73
74   if ( !CORBA::is_nil( container ) )
75   {
76     CORBA::String_var hName = container->getHostName();
77     CORBA::Object_var obj = lcc()->namingService()->ResolveComponent( hName.in(),
78                                                                       containerName.c_str(),
79                                                                       "PVSERVER",
80                                                                       0 );
81     if ( !CORBA::is_nil( obj ) )
82     {
83       CORBA::ORB_var orb = lcc()->orb();
84       CORBA::String_var cIor = orb->object_to_string( obj );
85       ior = cIor.in();
86     }
87   }
88
89   if ( ior == "" )
90   {
91     MESSAGE( "PVServer_ServiceLoader::findService(): Service NOT found!" );
92   }
93   else
94   {
95     MESSAGE( "PVServer_ServiceLoader::findService(): Service found!" );
96   }
97
98   return ior;
99 }
100
101 std::string PVServer_ServiceLoader::loadService( const std::string& containerName )
102 {
103   std::string ior = "";
104
105   Engines::Container_var container = getContainer( containerName, "getorstart" );
106
107   if ( !CORBA::is_nil( container ) )
108   {
109     CORBA::String_var cReason;
110     CORBA::String_var cIor = container->create_python_service_instance( "PVSERVER", cReason.out() );
111     std::string reason = cReason.in();
112
113     if ( reason != "" )
114     {
115       MESSAGE( "PVServer_ServiceLoader::loadService(): Python service instance could not be created!" );
116       MESSAGE( "PVServer_ServiceLoader::loadService(): " << reason );
117     }
118     else
119     {
120       MESSAGE( "PVServer_ServiceLoader::loadService(): Container and service loaded!" );
121       ior = cIor.in();
122     }
123   }
124   else
125   {
126     MESSAGE( "PVServer_ServiceLoader::loadService(): Container could not be retrieved!" );
127   }
128   return ior;
129 }
130
131 Engines::Container_ptr PVServer_ServiceLoader::getContainer( const std::string& containerName,
132                                                              const std::string& mode )
133 {
134   Engines::ContainerParameters params;
135   params.isMPI = false;
136   params.mode = CORBA::string_dup( mode.c_str() );
137
138   int rg = containerName.find( "/" );
139   if ( rg < 0 )
140   {
141     params.container_name = CORBA::string_dup( containerName.c_str() );
142   }
143   else
144   {
145     params.resource_params.hostname = CORBA::string_dup( containerName.substr(0, rg).c_str() );
146     params.container_name           = CORBA::string_dup( containerName.substr(rg+1).c_str() );
147   }
148
149   Engines::ContainerManager_var cm = lcc()->getContainerManager();
150   Engines::Container_var container = cm->GiveContainer( params );
151   return container._retn();
152 }