Salome HOME
PR: merge from tag mergeto_trunk_20Jan05
[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   _ContManager=Engines::ContainerManager::_narrow(obj);
52 }
53
54 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
55 {
56 }
57
58 string SALOME_LifeCycleCORBA::ContainerName(
59                                          const char * aComputerContainer ,
60                                          string * theComputer ,
61                                          string * theContainer ) {
62   char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
63   strcpy( ContainerName , aComputerContainer ) ;
64   string theComputerContainer("/Containers/");
65   char * slash = strchr( ContainerName , '/' ) ;
66   if ( !slash ) {
67     *theComputer = GetHostname() ;
68     theComputerContainer += *theComputer ;
69     theComputerContainer += "/" ;
70     *theContainer = ContainerName ;
71     theComputerContainer += *theContainer ;
72   }
73   else {
74     slash[ 0 ] = '\0' ;
75     slash += 1 ;
76     *theContainer = slash ;
77     if ( !strcmp( ContainerName , "localhost" ) ) {
78       *theComputer = GetHostname() ;
79     }
80     else {
81       *theComputer = ContainerName ;
82     }
83     theComputerContainer += *theComputer ;
84     theComputerContainer += "/" ;
85     theComputerContainer += *theContainer ;
86   }
87   delete [] ContainerName;
88   return theComputerContainer ;
89 }
90
91 string SALOME_LifeCycleCORBA::ComputerPath(
92                                          const char * theComputer ) {
93   CORBA::String_var path;
94   CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
95   SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
96                      SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
97   try {
98     path = Catalog->GetPathPrefix( theComputer );
99   }
100   catch (SALOME_ModuleCatalog::NotFound&) {
101     INFOS("GetPathPrefix(" << theComputer << ") not found!");
102     path = "" ;
103   }
104   SCRUTE( path ) ;
105   return CORBA::string_dup( path ) ;
106 }
107
108 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component
109                                   (const char *containerName,
110                                    const char *componentName)
111 {
112   char *stContainer=strdup(containerName);
113   string st2Container(stContainer);
114   int rg=st2Container.find("/");
115   if(rg<0) {
116     //containerName doesn't contain "/" => Local container
117     free(stContainer);
118     Engines::MachineList_var listOfMachine=new Engines::MachineList;
119     listOfMachine->length(1);
120     listOfMachine[0]=CORBA::string_dup(GetHostname().c_str());
121     Engines::Component_ptr ret=FindComponent(containerName,componentName,listOfMachine.in());
122     if(CORBA::is_nil(ret))
123       return LoadComponent(containerName,componentName,listOfMachine);
124     else
125       return ret;
126   }
127   else {
128     //containerName contains "/" => Remote container
129     stContainer[rg]='\0';
130     Engines::MachineParameters_var params=new Engines::MachineParameters;
131     params->container_name=CORBA::string_dup(stContainer+rg+1);
132     params->hostname=CORBA::string_dup(stContainer);
133     params->OS=CORBA::string_dup("LINUX");
134     free(stContainer);
135     return FindOrLoad_Component(params,componentName);
136   }
137 }
138
139 Engines::Component_ptr SALOME_LifeCycleCORBA::FindOrLoad_Component(const Engines::MachineParameters& params,
140                                                                    const char *componentName)
141 {
142   Engines::MachineList_var listOfMachine=_ContManager->GetFittingResources(params,componentName);
143   Engines::Component_ptr ret=FindComponent(params.container_name,componentName,listOfMachine);
144   if(CORBA::is_nil(ret))
145     return LoadComponent(params.container_name,componentName,listOfMachine);
146   else
147     return ret;
148 }
149
150 Engines::Component_ptr SALOME_LifeCycleCORBA::FindComponent(const char *containerName,
151                                                                  const char *componentName,
152                                                                  const Engines::MachineList& listOfMachines)
153 {
154   if(containerName[0]!='\0')
155     {
156       Engines::MachineList_var machinesOK=new Engines::MachineList;
157       unsigned int lghtOfmachinesOK=0;
158       machinesOK->length(listOfMachines.length());
159       for(unsigned int i=0;i<listOfMachines.length();i++)
160         {
161           const char *currentMachine=listOfMachines[i];
162           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,currentMachine);
163           CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
164           if(!CORBA::is_nil(obj))
165             {
166               machinesOK[lghtOfmachinesOK++]=CORBA::string_dup(currentMachine);
167             }
168         }
169       if(lghtOfmachinesOK!=0)
170         {
171           machinesOK->length(lghtOfmachinesOK);
172           CORBA::String_var bestMachine=_ContManager->FindBest(machinesOK);
173           string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
174           CORBA::Object_var obj=_NS->Resolve(componentNameForNS.c_str());
175           return Engines::Component::_narrow(obj);
176         }
177       else
178         return Engines::Component::_nil();
179     }
180   else
181     {
182       //user specified no container name so trying to find a component in the best machine among listOfMachines
183       CORBA::String_var bestMachine=_ContManager->FindBest(listOfMachines);
184       //Normally look at all containers launched on bestMachine to see if componentName is already launched on one of them. To do..
185       string componentNameForNS=Engines_Component_i::BuildComponentNameForNS(componentName,containerName,bestMachine);
186       CORBA::Object_var obj = _NS->Resolve(componentNameForNS.c_str());
187       return Engines::Component::_narrow(obj);
188     }
189 }
190
191 Engines::Component_ptr SALOME_LifeCycleCORBA::LoadComponent(const char *containerName, const char *componentName, const Engines::MachineList& listOfMachines)
192 {
193   Engines::Container_var cont=_ContManager->FindOrStartContainer(containerName,listOfMachines);
194   string implementation=Engines_Component_i::GetDynLibraryName(componentName);
195   return cont->load_impl(componentName, implementation.c_str());
196 }