Salome HOME
PR: add FindContainer method for supervisor (non regression)
[modules/yacs.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   CORBA::Object_var obj=_NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
51   ASSERT( !CORBA::is_nil(obj));
52   _ContManager=Engines::ContainerManager::_narrow(obj);
53 }
54
55 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
56 {
57 }
58
59 string SALOME_LifeCycleCORBA::ContainerName(
60                                          const char * aComputerContainer ,
61                                          string * theComputer ,
62                                          string * theContainer ) {
63   char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
64   strcpy( ContainerName , aComputerContainer ) ;
65   string theComputerContainer("/Containers/");
66   char * slash = strchr( ContainerName , '/' ) ;
67   if ( !slash ) {
68     *theComputer = GetHostname() ;
69     theComputerContainer += *theComputer ;
70     theComputerContainer += "/" ;
71     *theContainer = ContainerName ;
72     theComputerContainer += *theContainer ;
73   }
74   else {
75     slash[ 0 ] = '\0' ;
76     slash += 1 ;
77     *theContainer = slash ;
78     if ( !strcmp( ContainerName , "localhost" ) ) {
79       *theComputer = GetHostname() ;
80     }
81     else {
82       *theComputer = ContainerName ;
83     }
84     theComputerContainer += *theComputer ;
85     theComputerContainer += "/" ;
86     theComputerContainer += *theContainer ;
87   }
88   delete [] ContainerName;
89   return theComputerContainer ;
90 }
91
92 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
93 {
94
95   try
96     {
97       CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
98       SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
99         SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
100       SALOME_ModuleCatalog::Acomponent_ptr compoInfo = 
101         Catalog->GetComponent(componentName);
102       if (CORBA::is_nil (compoInfo)) 
103         {
104           INFOS("Catalog Error : Component not found in the catalog");
105           return false;
106         }
107       else return true;
108     }
109   catch (ServiceUnreachable&)
110     {
111       INFOS("Caught exception: Naming Service Unreachable");
112     }
113   catch (...)
114     {
115       INFOS("Caught unknown exception.");
116     }
117   return false;
118 }
119
120 string SALOME_LifeCycleCORBA::ComputerPath(
121                                          const char * theComputer ) {
122   CORBA::String_var path;
123   CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
124   SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
125                      SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
126   try {
127     path = Catalog->GetPathPrefix( theComputer );
128   }
129   catch (SALOME_ModuleCatalog::NotFound&) {
130     INFOS("GetPathPrefix(" << theComputer << ") not found!");
131     path = "" ;
132   }
133   SCRUTE( path ) ;
134   return CORBA::string_dup( path ) ;
135 }
136
137 Engines::Container_ptr SALOME_LifeCycleCORBA::FindContainer(const char *containerName)
138 {
139   ASSERT(_NS != NULL);
140   string cont ;
141   if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
142     string theComputer ;
143     string theContainer ;
144     cont = ContainerName( containerName , &theComputer , &theContainer ) ;
145   }
146   else {
147     cont = containerName ;
148   }
149   try {
150
151     SCRUTE( cont );
152
153     CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
154     if( !CORBA::is_nil( obj ) ) {
155       return Engines::Container::_narrow( obj ) ;
156     }
157   }
158   catch (ServiceUnreachable&) {
159     INFOS("Caught exception: Naming Service Unreachable");
160   }
161   catch (...) {
162     INFOS("Caught unknown exception.");
163   }
164   return Engines::Container::_nil();
165 }
166
167 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
168                                   (const char *containerName,
169                                    const char *componentName)
170 {
171   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
172   char *stContainer=strdup(containerName);
173   string st2Container(stContainer);
174   int rg=st2Container.find("/");
175   if(rg<0) {
176     //containerName doesn't contain "/" => Local container
177     free(stContainer);
178     Engines::MachineList_var listOfMachine=new Engines::MachineList;
179     listOfMachine->length(1);
180     listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
181     Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
182     if(CORBA::is_nil(ret))
183       return LoadComponent(containerName,componentName,listOfMachine);
184     else
185       return ret;
186   }
187   else {
188     //containerName contains "/" => Remote container
189     stContainer[rg]='\0';
190     Engines::MachineParameters_var params=new Engines::MachineParameters;
191     params->container_name=CORBA::string_dup(stContainer+rg+1);
192     params->hostname=CORBA::string_dup(stContainer);
193     params->OS=CORBA::string_dup("LINUX");
194     free(stContainer);
195     return FindOrLoad_Component(params,componentName);
196   }
197 }
198
199 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
200                                                                    const char *componentName)
201 {
202   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
203   Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
204   Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
205   if(CORBA::is_nil(ret))
206     return LoadComponent(params.container_name,componentName,listOfMachine);
207   else
208     return ret;
209 }
210
211 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
212                                                                  const char *componentName,
213                                                                  const Engines::MachineList& listOfMachines)
214 {
215   if (! isKnownComponentClass(componentName)) return Engines::Component::_nil();
216   if(containerName[0]!='\0')
217     {
218       Engines::MachineList_var machinesOK=new Engines::MachineList;
219       unsigned int lghtOfmachinesOK=0;
220       machinesOK->length(listOfMachines.length());
221       for(unsigned int i=0;i<listOfMachines.length();i++)
222         {
223           const char *currentMachine=listOfMachines[i];
224           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
225           CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
226           if(!CORBA::is_nil(obj))
227             {
228               machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
229             }
230         }
231       if(lghtOfmachinesOK!=0)
232         {
233           machinesOK->length(lghtOfmachinesOK);
234           CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
235           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
236           CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
237           return Engines::Component::_narrow(obj);
238         }
239       else
240         return Engines::Component::_nil();
241     }
242   else
243     {
244       //user specified no container name so trying to find a component in the best machine among listOfMachines
245       CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
246       //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
247       string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
248       CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
249       return Engines::Component::_narrow(obj);
250     }
251 }
252
253 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
254 {
255   Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
256   string implementation=Engines_Component_i::GetDynLibraryName(componentName);
257   return cont->load_impl(componentName, implementation.c_str());
258 }