Salome HOME
9bcf6437187e941bbe8b1f2a0495cae2df6715ca
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 #include "SALOME_ContainerManager.hxx"
21 #include "SALOME_NamingService.hxx"
22 #include "OpUtil.hxx"
23 #include <sys/types.h>
24 #ifndef WNT
25 #include <unistd.h>
26 #endif
27 #include <vector>
28 #include "Utils_CorbaException.hxx"
29
30 #define TIME_OUT_TO_LAUNCH_CONT 21
31
32 using namespace std;
33
34 const char *SALOME_ContainerManager::_ContainerManagerNameInNS = 
35   "/ContainerManager";
36
37 //=============================================================================
38 /*! 
39  *  Constructor
40  *  \param orb
41  *  Define a CORBA single thread policy for the server, which avoid to deal
42  *  with non thread-safe usage like Change_Directory in SALOME naming service
43  */
44 //=============================================================================
45
46 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb)
47 {
48   MESSAGE("constructor");
49   _NS = new SALOME_NamingService(orb);
50   _ResManager = new SALOME_ResourcesManager(orb);
51   _id=0;
52   PortableServer::POA_var root_poa = PortableServer::POA::_the_root_poa();
53   PortableServer::POAManager_var pman = root_poa->the_POAManager();
54   PortableServer::POA_var my_poa;
55
56   CORBA::PolicyList policies;
57   policies.length(1);
58   PortableServer::ThreadPolicy_var threadPol = 
59     root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
60   policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
61
62   my_poa = 
63     root_poa->create_POA("SThreadPOA",pman,policies);
64   threadPol->destroy();
65   PortableServer::ObjectId_var id = my_poa->activate_object(this);
66   CORBA::Object_var obj = my_poa->id_to_reference(id);
67   Engines::ContainerManager_var refContMan =
68     Engines::ContainerManager::_narrow(obj);
69
70   _NS->Register(refContMan,_ContainerManagerNameInNS);
71   MESSAGE("constructor end");
72 }
73
74 //=============================================================================
75 /*! 
76  * destructor
77  */
78 //=============================================================================
79
80 SALOME_ContainerManager::~SALOME_ContainerManager()
81 {
82   MESSAGE("destructor");
83   delete _NS;
84   delete _ResManager;
85 }
86
87 //=============================================================================
88 /*! CORBA method:
89  *  shutdown all the containers, then the ContainerManager servant
90  */
91 //=============================================================================
92
93 void SALOME_ContainerManager::Shutdown()
94 {
95   MESSAGE("Shutdown");
96   ShutdownContainers();
97   PortableServer::ObjectId_var oid = _default_POA()->servant_to_id(this);
98   _default_POA()->deactivate_object(oid);
99   _remove_ref();
100   
101 }
102
103 //=============================================================================
104 /*! CORBA Method:
105  *  Loop on all the containers listed in naming service, ask shutdown on each
106  */
107 //=============================================================================
108
109 void SALOME_ContainerManager::ShutdownContainers()
110 {
111   MESSAGE("ShutdownContainers");
112   _NS->Change_Directory("/Containers");
113   vector<string> vec = _NS->list_directory_recurs();
114   for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++)
115     {
116       SCRUTE((*iter));
117       CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
118       Engines::Container_var cont=Engines::Container::_narrow(obj);
119       if(!CORBA::is_nil(cont))
120         {
121           MESSAGE("ShutdownContainers: " << (*iter));
122           cont->Shutdown();
123         }
124       else MESSAGE("ShutdownContainers: no container ref for " << (*iter));
125     }
126 }
127
128 //=============================================================================
129 /*! CORBA Method:
130  *  Find a suitable Container in a list of machines, or start one
131  *  \param params            Machine Parameters required for the container
132  *  \param possibleComputers list of machines usable for find or start
133  */
134 //=============================================================================
135
136 Engines::Container_ptr
137 SALOME_ContainerManager::
138 FindOrStartContainer(const Engines::MachineParameters& params,
139                      const Engines::MachineList& possibleComputers)
140 {
141   long id;
142   string containerNameInNS;
143   char idc[3*sizeof(long)];
144
145   Engines::Container_ptr ret = FindContainer(params,possibleComputers);
146   if(!CORBA::is_nil(ret))
147     return ret;
148   MESSAGE("Container doesn't exist try to launch it ...");
149   MESSAGE("SALOME_ContainerManager::FindOrStartContainer " <<
150           possibleComputers.length());
151   //vector<string> vector;
152   string theMachine=_ResManager->FindBest(possibleComputers);
153   MESSAGE("try to launch it on " << theMachine);
154
155   // Get Id for container: a parallel container registers in Naming Service
156   // on the machine where is process 0. ContainerManager does'nt know the name
157   // of this machine before the launch of the parallel container. So to get
158   // the IOR of the parallel container in Naming Service, ContainerManager
159   // gives a unique Id. The parallel container registers his name under
160   // /ContainerManager/Id directory in NamingService
161
162   id = GetIdForContainer();
163
164   string command;
165   if(theMachine=="")
166     {
167       MESSAGE("SALOME_ContainerManager::FindOrStartContainer : " <<
168               "no possible computer");
169       return Engines::Container::_nil();
170     }
171   else if(theMachine==GetHostname())
172     {
173       command=_ResManager->BuildCommandToLaunchLocalContainer(params,id);
174     }
175   else
176     command =
177       _ResManager->BuildCommandToLaunchRemoteContainer(theMachine,params,id);
178
179   _ResManager->RmTmpFile();
180   int status=system(command.c_str());
181   if (status == -1)
182     {
183       MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
184               "(system command status -1)");
185       return Engines::Container::_nil();
186     }
187   else if (status == 217)
188     {
189       MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
190               "(system command status 217)");
191       return Engines::Container::_nil();
192     }
193   else
194     {
195       int count=TIME_OUT_TO_LAUNCH_CONT;
196       while ( CORBA::is_nil(ret) && count )
197         {
198 #ifndef WNT
199           sleep( 1 ) ;
200 #else
201           Sleep(1000);
202 #endif
203           count-- ;
204           if ( count != 10 )
205             MESSAGE( count << ". Waiting for FactoryServer on " << theMachine);
206           if(params.isMPI)
207             {
208               containerNameInNS = "/ContainerManager/id";
209               sprintf(idc,"%ld",id);
210               containerNameInNS += idc;
211             }
212           else
213             containerNameInNS =
214               _NS->BuildContainerNameForNS(params,theMachine.c_str());
215           SCRUTE(containerNameInNS);
216           CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
217           ret=Engines::Container::_narrow(obj);
218         }
219       if ( CORBA::is_nil(ret) )
220         {
221           MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed");
222         }
223       return ret;
224     }
225 }
226
227 //=============================================================================
228 /*! 
229  * 
230  */
231 //=============================================================================
232
233 Engines::MachineList *
234 SALOME_ContainerManager::
235 GetFittingResources(const Engines::MachineParameters& params,
236                     const char *componentName)
237 {
238   MESSAGE("SALOME_ContainerManager::GetFittingResources");
239   Engines::MachineList *ret=new Engines::MachineList;
240   vector<string> vec;
241   try
242     {
243       vec = _ResManager->GetFittingResources(params,componentName);
244     }
245   catch(const SALOME_Exception &ex)
246     {
247       INFOS("Caught exception.");
248       THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
249       //return ret;
250     }
251
252   //  MESSAGE("Machine list length "<<vec.size());
253   ret->length(vec.size());
254   for(unsigned int i=0;i<vec.size();i++)
255     {
256       (*ret)[i]=(vec[i]).c_str();
257     }
258   return ret;
259 }
260
261 //=============================================================================
262 /*! 
263  * 
264  */
265 //=============================================================================
266
267 char*
268 SALOME_ContainerManager::
269 FindBest(const Engines::MachineList& possibleComputers)
270 {
271   string theMachine=_ResManager->FindBest(possibleComputers);
272   return CORBA::string_dup(theMachine.c_str());
273 }
274
275 //=============================================================================
276 /*! 
277  * 
278  */
279 //=============================================================================
280
281 Engines::Container_ptr
282 SALOME_ContainerManager::
283 FindContainer(const Engines::MachineParameters& params,
284               const char *theMachine)
285 {
286   string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
287   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
288   if( !CORBA::is_nil(obj) )
289     return Engines::Container::_narrow(obj);
290   else
291     return Engines::Container::_nil();
292 }
293
294 //=============================================================================
295 /*! 
296  * 
297  */
298 //=============================================================================
299
300 Engines::Container_ptr
301 SALOME_ContainerManager::
302 FindContainer(const Engines::MachineParameters& params,
303               const Engines::MachineList& possibleComputers)
304 {
305   MESSAGE("FindContainer "<<possibleComputers.length());
306   for(unsigned int i=0;i<possibleComputers.length();i++)
307     {
308       MESSAGE("FindContainer possible " << possibleComputers[i]);
309       Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
310       if( !CORBA::is_nil(cont) )
311         return cont;
312     }
313   MESSAGE("FindContainer: not found");
314   return Engines::Container::_nil();
315 }
316
317 //=============================================================================
318 /*! 
319  * Get Id for container: a parallel container registers in Naming Service
320  * on the machine where is process 0. ContainerManager does'nt know the name
321  * of this machine before the launch of the parallel container. So to get
322  * the IOR of the parallel container in Naming Service, ContainerManager
323  * gives a unique Id. The parallel container registers his name under
324  * /ContainerManager/Id directory in NamingService
325  */
326 //=============================================================================
327
328
329 long SALOME_ContainerManager::GetIdForContainer(void)
330 {
331   _id++;
332   return _id;
333 }
334