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