]> SALOME platform Git repositories - modules/kernel.git/blob - src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx
Salome HOME
MPV
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_LifeCycleCORBA.cxx
1 //  SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_LifeCycleCORBA.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32 #include <iomanip>
33
34 #include "OpUtil.hxx"
35 #include "utilities.h"
36
37 #include <ServiceUnreachable.hxx>
38
39 #include "SALOME_LifeCycleCORBA.hxx"
40 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
41 #include "SALOME_ContainerManager.hxx"
42 #include "SALOME_Component_i.hxx"
43 #include "SALOME_NamingService.hxx"
44 using namespace std;
45
46 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
47 {
48   _NS = ns;
49   //add try catch
50   _NS->Change_Directory("/"); // mpv 250105: current directory may be not root (in SALOMEDS for an example)
51   CORBA::Object_var obj=_NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
52   ASSERT( !CORBA::is_nil(obj));
53   _ContManager=Engines::ContainerManager::_narrow(obj);
54 }
55
56 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
57 {
58 }
59
60 string SALOME_LifeCycleCORBA::ContainerName(
61                                          const char * aComputerContainer ,
62                                          string * theComputer ,
63                                          string * theContainer ) {
64   char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
65   strcpy( ContainerName , aComputerContainer ) ;
66   string theComputerContainer("/Containers/");
67   char * slash = strchr( ContainerName , '/' ) ;
68   if ( !slash ) {
69     *theComputer = GetHostname() ;
70     theComputerContainer += *theComputer ;
71     theComputerContainer += "/" ;
72     *theContainer = ContainerName ;
73     theComputerContainer += *theContainer ;
74   }
75   else {
76     slash[ 0 ] = '\0' ;
77     slash += 1 ;
78     *theContainer = slash ;
79     if ( !strcmp( ContainerName , "localhost" ) ) {
80       *theComputer = GetHostname() ;
81     }
82     else {
83       *theComputer = ContainerName ;
84     }
85     theComputerContainer += *theComputer ;
86     theComputerContainer += "/" ;
87     theComputerContainer += *theContainer ;
88   }
89   delete [] ContainerName;
90   return theComputerContainer ;
91 }
92
93 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
94 {
95
96   try
97     {
98       CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
99       SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
100         SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
101       SALOME_ModuleCatalog::Acomponent_ptr compoInfo = 
102         Catalog->GetComponent(componentName);
103       if (CORBA::is_nil (compoInfo)) 
104         {
105           INFOS("Catalog Error : Component not found in the catalog");
106           return false;
107         }
108       else return true;
109     }
110   catch (ServiceUnreachable&)
111     {
112       INFOS("Caught exception: Naming Service Unreachable");
113     }
114   catch (...)
115     {
116       INFOS("Caught unknown exception.");
117     }
118   return false;
119 }
120
121 string SALOME_LifeCycleCORBA::ComputerPath(
122                                          const char * theComputer ) {
123   CORBA::String_var path;
124   CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
125   SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
126                      SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
127   try {
128     path = Catalog->GetPathPrefix( theComputer );
129   }
130   catch (SALOME_ModuleCatalog::NotFound&) {
131     INFOS("GetPathPrefix(" << theComputer << ") not found!");
132     path = "" ;
133   }
134   SCRUTE( path ) ;
135   return CORBA::string_dup( path ) ;
136 }
137
138 Engines::Container_ptr SALOME_LifeCycleCORBA::FindContainer(const char *containerName)
139 {
140   ASSERT(_NS != NULL);
141   string cont ;
142   if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
143     string theComputer ;
144     string theContainer ;
145     cont = ContainerName( containerName , &theComputer , &theContainer ) ;
146   }
147   else {
148     cont = containerName ;
149   }
150   try {
151
152     SCRUTE( cont );
153
154     CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
155     if( !CORBA::is_nil( obj ) ) {
156       return Engines::Container::_narrow( obj ) ;
157     }
158   }
159   catch (ServiceUnreachable&) {
160     INFOS("Caught exception: Naming Service Unreachable");
161   }
162   catch (...) {
163     INFOS("Caught unknown exception.");
164   }
165   return Engines::Container::_nil();
166 }
167
168 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
169                                   (const char *containerName,
170                                    const char *componentName)
171 {
172   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
173   char *stContainer=strdup(containerName);
174   string st2Container(stContainer);
175   int rg=st2Container.find("/");
176   if(rg>=0)
177     {
178       stContainer[rg]='\0';
179       if(strcmp(stContainer,"localhost")==0)
180         {
181           Engines::Component_ptr ret=FindOrLoad_Component(stContainer+rg+1,componentName);
182           free(stContainer);
183           return ret;
184         }
185     }
186   if(rg<0) {
187     //containerName doesn't contain "/" => Local container
188     free(stContainer);
189     Engines::MachineList_var listOfMachine=new Engines::MachineList;
190     listOfMachine->length(1);
191     listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
192     Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
193     if(CORBA::is_nil(ret))
194       return LoadComponent(containerName,componentName,listOfMachine);
195     else
196       return ret;
197   }
198   else {
199     //containerName contains "/" => Remote container
200     stContainer[rg]='\0';
201     Engines::MachineParameters_var params=new Engines::MachineParameters;
202     params->container_name=CORBA::string_dup(stContainer+rg+1);
203     params->hostname=CORBA::string_dup(stContainer);
204     params->OS=CORBA::string_dup("LINUX");
205     free(stContainer);
206     return FindOrLoad_Component(params,componentName);
207   }
208 }
209
210 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
211                                                                    const char *componentName)
212 {
213   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
214   Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
215   Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
216   if(CORBA::is_nil(ret))
217     return LoadComponent(params.container_name,componentName,listOfMachine);
218   else
219     return ret;
220 }
221
222 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
223                                                                  const char *componentName,
224                                                                  const Engines::MachineList& listOfMachines)
225 {
226   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
227   if(containerName[0]!='\0')
228     {
229       Engines::MachineList_var machinesOK=new Engines::MachineList;
230       unsigned int lghtOfmachinesOK=0;
231       machinesOK->length(listOfMachines.length());
232       for(unsigned int i=0;i<listOfMachines.length();i++)
233         {
234           const char *currentMachine=listOfMachines[i];
235           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
236           CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
237           if(!CORBA::is_nil(obj))
238             {
239               machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
240             }
241         }
242       if(lghtOfmachinesOK!=0)
243         {
244           machinesOK->length(lghtOfmachinesOK);
245           CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
246           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
247           CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
248           return Engines::Component::_narrow(obj);
249         }
250       else
251         return Engines::Component::_nil();
252     }
253   else
254     {
255       //user specified no container name so trying to find a component in the best machine among listOfMachines
256       CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
257       //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
258       string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
259       CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
260       return Engines::Component::_narrow(obj);
261     }
262 }
263
264 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
265 {
266   Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
267   string implementation=Engines_Component_i::GetDynLibraryName(componentName);
268   return cont->load_impl(componentName, implementation.c_str());
269 }