]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_ContainerManager.cxx
Salome HOME
CCAR: add a message in case of error when loading a component.
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SALOME_ContainerManager.hxx"
23 #include "SALOME_NamingService.hxx"
24 #include "SALOME_ModuleCatalog.hh"
25 #include "Basics_Utils.hxx"
26 #include "Basics_DirUtils.hxx"
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifndef WIN32
30 #include <unistd.h>
31 #endif
32 #include <vector>
33 #include "Utils_CorbaException.hxx"
34 #include "Batch_Date.hxx"
35 #include <sstream>
36
37 #ifdef WITH_PACO_PARALLEL
38 #include "PaCOPP.hxx"
39 #endif
40
41 #define TIME_OUT_TO_LAUNCH_CONT 61
42
43 using namespace std;
44
45 vector<Engines::Container_ptr> SALOME_ContainerManager::_batchLaunchedContainers;
46
47 vector<Engines::Container_ptr>::iterator SALOME_ContainerManager::_batchLaunchedContainersIter;
48
49 const char *SALOME_ContainerManager::_ContainerManagerNameInNS = 
50   "/ContainerManager";
51
52 //=============================================================================
53 /*! 
54  *  Constructor
55  *  \param orb
56  *  Define a CORBA single thread policy for the server, which avoid to deal
57  *  with non thread-safe usage like Change_Directory in SALOME naming service
58  */
59 //=============================================================================
60
61 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_ResourcesManager *rm, SALOME_NamingService *ns)
62 {
63   MESSAGE("constructor");
64   _NS = ns;
65   _ResManager = rm;
66
67   PortableServer::POAManager_var pman = poa->the_POAManager();
68   _orb = CORBA::ORB::_duplicate(orb) ;
69   CORBA::PolicyList policies;
70   policies.length(1);
71   PortableServer::ThreadPolicy_var threadPol = 
72     poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
73   policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
74
75   _poa = poa->create_POA("SThreadPOA",pman,policies);
76   threadPol->destroy();
77   PortableServer::ObjectId_var id = _poa->activate_object(this);
78   CORBA::Object_var obj = _poa->id_to_reference(id);
79   Engines::ContainerManager_var refContMan =
80     Engines::ContainerManager::_narrow(obj);
81
82   _NS->Register(refContMan,_ContainerManagerNameInNS);
83   _isAppliSalomeDefined = (getenv("APPLI") != 0);
84
85 #ifdef HAVE_MPI2
86 #ifdef WITHOPENMPI
87   if( getenv("OMPI_URI_FILE") != NULL ){
88     system("killall ompi-server");
89     string command;
90     command = "ompi-server -r ";
91     command += getenv("OMPI_URI_FILE");
92     int status=system(command.c_str());
93     if(status!=0)
94       throw SALOME_Exception("Error when launching ompi-server");
95   }
96 #endif
97 #endif
98
99   MESSAGE("constructor end");
100 }
101
102 //=============================================================================
103 /*! 
104  * destructor
105  */
106 //=============================================================================
107
108 SALOME_ContainerManager::~SALOME_ContainerManager()
109 {
110   MESSAGE("destructor");
111 #ifdef HAVE_MPI2
112 #ifdef WITHOPENMPI
113   if( getenv("OMPI_URI_FILE") != NULL )
114     system("killall ompi-server");
115 #endif
116 #endif
117 }
118
119 //=============================================================================
120 //! shutdown all the containers, then the ContainerManager servant
121 /*! CORBA method:
122  */
123 //=============================================================================
124
125 void SALOME_ContainerManager::Shutdown()
126 {
127   MESSAGE("Shutdown");
128   ShutdownContainers();
129   _NS->Destroy_Name(_ContainerManagerNameInNS);
130   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
131   _poa->deactivate_object(oid);
132 }
133
134 //=============================================================================
135 //! Loop on all the containers listed in naming service, ask shutdown on each
136 /*! CORBA Method:
137  */
138 //=============================================================================
139
140 void SALOME_ContainerManager::ShutdownContainers()
141 {
142   MESSAGE("ShutdownContainers");
143   bool isOK;
144   isOK = _NS->Change_Directory("/Containers");
145   if( isOK ){
146     vector<string> vec = _NS->list_directory_recurs();
147     list<string> lstCont;
148     for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++)
149       {
150         SCRUTE((*iter));
151         CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
152         try
153           {
154             Engines::Container_var cont=Engines::Container::_narrow(obj);
155             if(!CORBA::is_nil(cont))
156               lstCont.push_back((*iter));
157           }
158         catch(const CORBA::Exception& e)
159           {
160             // ignore this entry and continue
161           }
162       }
163     MESSAGE("Container list: ");
164     for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
165       SCRUTE((*iter));
166     }
167     for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
168     {
169       try
170       {
171         SCRUTE((*iter));
172         CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
173         Engines::Container_var cont=Engines::Container::_narrow(obj);
174         if(!CORBA::is_nil(cont))
175         {
176           MESSAGE("ShutdownContainers: " << (*iter));
177           cont->Shutdown();
178         }
179         else 
180           MESSAGE("ShutdownContainers: no container ref for " << (*iter));
181       }
182       catch(CORBA::SystemException& e)
183       {
184         INFOS("CORBA::SystemException ignored : " << e);
185       }
186       catch(CORBA::Exception&)
187       {
188         INFOS("CORBA::Exception ignored.");
189       }
190       catch(...)
191       {
192         INFOS("Unknown exception ignored.");
193       }
194     }
195   }
196 }
197
198 //=============================================================================
199 //! Give a suitable Container given constraints
200 /*! CORBA Method:
201  *  \param params            Machine Parameters required for the container
202  *  \return the container or nil
203  */
204 //=============================================================================
205
206 Engines::Container_ptr
207 SALOME_ContainerManager::GiveContainer(const Engines::MachineParameters& params)
208 {
209   char *valenv=getenv("SALOME_BATCH");
210   if(valenv)
211     if (strcmp(valenv,"1")==0)
212       {
213         if(_batchLaunchedContainers.empty())
214           fillBatchLaunchedContainers();
215
216         if (_batchLaunchedContainersIter == _batchLaunchedContainers.end())
217           _batchLaunchedContainersIter = _batchLaunchedContainers.begin();
218
219         Engines::Container_ptr rtn = Engines::Container::_duplicate(*_batchLaunchedContainersIter);
220         _batchLaunchedContainersIter++;
221         return rtn;
222       }
223   return StartContainer(params);
224 }
225
226 //=============================================================================
227 //! Start a suitable Container in a list of machines with constraints 
228 /*! C++ Method:
229  * Constraints are given by a machine parameters struct
230  *  \param params            Machine Parameters required for the container
231  *  \param possibleComputers list of machines usable for start
232  *  \param container_exe specific container executable (default=SALOME_Container)
233  */
234 //=============================================================================
235
236 Engines::Container_ptr
237 SALOME_ContainerManager::StartContainer(const Engines::MachineParameters& params,
238                const Engines::MachineList& possibleComputers,
239                const std::string& container_exe)
240 {
241 #ifdef WITH_PACO_PARALLEL
242   std::string parallelLib(params.parallelLib);
243   if (parallelLib != "")
244   {
245     Engines::MachineParameters myparams(params);
246     myparams.computerList=possibleComputers;
247     return StartParallelContainer(myparams);
248   }
249 #endif
250   string containerNameInNS;
251   Engines::Container_ptr ret = Engines::Container::_nil();
252
253   MESSAGE("SALOME_ContainerManager::StartContainer " << possibleComputers.length());
254
255   vector<string> lm;
256 // if mode is "get" keep only machines with existing containers 
257   if(std::string(params.mode.in())=="get")
258     {
259       for(unsigned int i=0;i<possibleComputers.length();i++)
260         {
261           Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
262           try
263             {
264               if(!cont->_non_existent())
265                 lm.push_back(string(possibleComputers[i]));
266             }
267           catch(CORBA::Exception&)
268             {
269               // CORBA::Exception ignored.
270             }
271         }
272     }
273   else
274     {
275       for(unsigned int i=0;i<possibleComputers.length();i++)
276         lm.push_back(string(possibleComputers[i]));
277     }
278
279   string theMachine;
280   try
281     {
282       theMachine=_ResManager->GetImpl()->Find(params.policy.in(),lm);
283     }
284   catch( const SALOME_Exception &ex )
285     {
286       MESSAGE(ex.what());
287       return Engines::Container::_nil();
288     }
289
290   //If the machine name is localhost use the real name
291   if(theMachine == "localhost")
292     theMachine=Kernel_Utils::GetHostname();
293
294   //check if an entry exists in Naming service
295   //if params.mode == "start" or "" shutdown the existing container before launching a new one with that name
296   //if params.mode == "getorstart" or "get" use the existing container
297   if(params.isMPI)
298     // A parallel container register on zero node in NS
299     containerNameInNS = _NS->BuildContainerNameForNS(params,GetMPIZeroNode(theMachine).c_str());
300   else
301     containerNameInNS = _NS->BuildContainerNameForNS(params,theMachine.c_str());
302
303   SCRUTE(containerNameInNS);
304   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
305   if ( !CORBA::is_nil(obj) )
306     {
307       try
308         {
309           Engines::Container_var cont=Engines::Container::_narrow(obj);
310           if(!cont->_non_existent())
311             {
312               if(std::string(params.mode.in())=="getorstart"||std::string(params.mode.in())=="get")
313                 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
314               else
315                 {
316                   INFOS("A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
317                   cont->Shutdown(); // shutdown the registered container if it exists
318                 }
319             }
320         }
321       catch(CORBA::Exception&)
322         {
323           INFOS("CORBA::Exception ignored.");
324         }
325     }
326
327   //try to launch a new container
328   MESSAGE("try to launch it on " << theMachine);
329
330   string command;
331   if(theMachine==""){
332     MESSAGE("SALOME_ContainerManager::StartContainer : no possible computer");
333     return Engines::Container::_nil();
334   }
335   else if(theMachine==Kernel_Utils::GetHostname())
336     command = BuildCommandToLaunchLocalContainer(params,container_exe);
337   else
338     command = BuildCommandToLaunchRemoteContainer(theMachine,params,container_exe);
339
340   //redirect stdout and stderr in a file
341 #ifdef WNT
342   string logFilename=getenv("TEMP");
343   logFilename += "\\";
344 #else
345   string logFilename="/tmp";
346   char* val = getenv("SALOME_TMP_DIR");
347   if(val)
348     {
349       struct stat file_info;
350       stat(val, &file_info);
351       bool is_dir = S_ISDIR(file_info.st_mode);
352       if (is_dir)logFilename=val;
353       else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
354     }
355   logFilename += "/";
356 #endif
357   logFilename += _NS->ContainerName(params)+"_"+ theMachine +"_"+getenv( "USER" )+".log" ;
358   command += " > " + logFilename + " 2>&1";
359 #ifdef WNT
360   command = "%PYTHONBIN% -c \"import win32pm ; win32pm.spawnpid(r'" + command + "', '')\"";
361 #else
362   command += " &";
363 #endif
364
365   // launch container with a system call
366   int status=system(command.c_str());
367
368   if (status == -1){
369     MESSAGE("SALOME_ContainerManager::StartContainer rsh failed (system command status -1)");
370     RmTmpFile(_TmpFileName); // command file can be removed here
371     return Engines::Container::_nil();
372   }
373   else if (status == 217){
374     MESSAGE("SALOME_ContainerManager::StartContainer rsh failed (system command status 217)");
375     RmTmpFile(_TmpFileName); // command file can be removed here
376     return Engines::Container::_nil();
377   }
378   else{
379     int count=TIME_OUT_TO_LAUNCH_CONT;
380     MESSAGE("count = "<<count);
381     while ( CORBA::is_nil(ret) && count ){
382 #ifndef WIN32
383       sleep( 1 ) ;
384 #else
385       Sleep(1000);
386 #endif
387       count-- ;
388       if ( count != 10 )
389         MESSAGE( count << ". Waiting for container on " << theMachine);
390
391       CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
392       ret=Engines::Container::_narrow(obj);
393     }
394     
395     if ( CORBA::is_nil(ret) )
396       {
397         MESSAGE("SALOME_ContainerManager::StartContainer rsh failed");
398       }
399     else
400       {
401         logFilename=":"+logFilename;
402         logFilename="@"+Kernel_Utils::GetHostname()+logFilename;
403         logFilename=getenv( "USER" )+logFilename;
404         ret->logfilename(logFilename.c_str());
405       }
406
407     RmTmpFile(_TmpFileName); // command file can be removed here
408     return ret;
409   }
410 }
411
412 //=============================================================================
413 //! Start a suitable Container given constraints 
414 /*! CORBA Method:
415  *  \param params            Machine Parameters required for the container
416  */
417 //=============================================================================
418
419 Engines::Container_ptr
420 SALOME_ContainerManager::StartContainer(const Engines::MachineParameters& params)
421 {
422   Engines::MachineList_var possibleComputers = _ResManager->GetFittingResources(params);
423
424   // Look into ModulCatalog if a specific container must be launched
425   CORBA::String_var container_exe;
426   int found=0;
427   try
428     {
429       CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
430       SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
431       if (CORBA::is_nil (Catalog))
432         return Engines::Container::_nil();
433       // Loop through component list
434       for(unsigned int i=0;i<params.componentList.length();i++)
435         {
436           const char* compoi = params.componentList[i];
437           SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
438           if (CORBA::is_nil (compoInfo))
439             {
440               continue;
441             }
442           SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
443           container_exe=compoInfo->implementation_name();
444           if(impl==SALOME_ModuleCatalog::CEXE)
445             {
446               if(found)
447                 {
448                   INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
449                   return Engines::Container::_nil();
450                 }
451               found=1;
452             }
453         }
454     }
455   catch (ServiceUnreachable&)
456     {
457       INFOS("Caught exception: Naming Service Unreachable");
458       return Engines::Container::_nil();
459     }
460   catch (...)
461     {
462       INFOS("Caught unknown exception.");
463       return Engines::Container::_nil();
464     }
465
466   if(found)
467     return StartContainer(params,possibleComputers,container_exe.in());
468   else
469     return StartContainer(params,possibleComputers);
470 }
471
472 //=============================================================================
473 //!  Find or start a suitable Container given some constraints
474 /*! CORBA Method:
475  *  \param params            Machine Parameters required for the container
476  *  \return the container or nil
477  */
478 //=============================================================================
479
480 Engines::Container_ptr
481 SALOME_ContainerManager::FindOrStartContainer(const Engines::MachineParameters& params)
482 {
483   Engines::Container_ptr ret = FindContainer(params,params.computerList);
484   if(!CORBA::is_nil(ret))
485     return ret;
486   MESSAGE("Container doesn't exist try to launch it ...");
487
488   return StartContainer(params);
489 }
490
491 //=============================================================================
492 //! Find a container given constraints (params) on a list of machines (possibleComputers)
493 /*!
494  *
495  */
496 //=============================================================================
497
498 Engines::Container_ptr
499 SALOME_ContainerManager::FindContainer(const Engines::MachineParameters& params,
500                                        const Engines::MachineList& possibleComputers)
501 {
502   MESSAGE("FindContainer "<<possibleComputers.length());
503   for(unsigned int i=0;i<possibleComputers.length();i++)
504     {
505       MESSAGE("FindContainer possible " << possibleComputers[i]);
506       Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
507       if( !CORBA::is_nil(cont) )
508         return cont;
509     }
510   MESSAGE("FindContainer: not found");
511   return Engines::Container::_nil();
512 }
513
514 //=============================================================================
515 //! Find a container given constraints (params) on a machine (theMachine)
516 /*!
517  *
518  */
519 //=============================================================================
520
521 Engines::Container_ptr
522 SALOME_ContainerManager::FindContainer(const Engines::MachineParameters& params,
523                                        const char *theMachine)
524 {
525   string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
526   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
527   try
528     {
529       if(obj->_non_existent())
530         return Engines::Container::_nil();
531       else
532         return Engines::Container::_narrow(obj);
533     }
534   catch(const CORBA::Exception& e)
535     {
536       return Engines::Container::_nil();
537     }
538 }
539
540 #ifdef WITH_PACO_PARALLEL
541 //=============================================================================
542 /*! CORBA Method:
543  *  Find or Start a suitable PaCO++ Parallel Container in a list of machines.
544  *  \param params            Machine Parameters required for the container
545  *  \return CORBA container reference.
546  */
547 //=============================================================================
548 Engines::Container_ptr
549 SALOME_ContainerManager::StartParallelContainer(const Engines::MachineParameters& params_const)
550 {
551   CORBA::Object_var obj;
552   PaCO::InterfaceManager_var container_proxy;
553   Engines::Container_ptr ret = Engines::Container::_nil();
554   Engines::MachineParameters params(params_const);
555
556   // Step 1 : Try to find a suitable container
557   // Currently not as good as could be since
558   // we have to verified the number of nodes of the container
559   // if a user tell that.
560   ret = FindContainer(params, params.computerList);
561   if(CORBA::is_nil(ret)) {
562     // Step 2 : Starting a new parallel container !
563     INFOS("[StartParallelContainer] Starting a PaCO++ parallel container");
564
565     // Step 3 : Choose a computer
566     std::string theMachine = _ResManager->FindFirst(params.computerList);
567     //If the machine name is localhost use the real name
568     if(theMachine == "localhost")
569       theMachine=Kernel_Utils::GetHostname();
570
571     if(theMachine == "") {
572       INFOS("[StartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
573       INFOS("[StartParallelContainer] No possible computer found");
574       INFOS("[StartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
575       return ret;
576     }
577     INFOS("[StartParallelContainer] on machine : " << theMachine);
578     params.hostname = CORBA::string_dup(theMachine.c_str());
579
580     // Step 4 : starting parallel container proxy
581     Engines::MachineParameters params_proxy(params);
582     std::string command_proxy;
583     SALOME_ContainerManager::actual_launch_machine_t proxy_machine;
584     try 
585     {
586       command_proxy = BuildCommandToLaunchParallelContainer("SALOME_ParallelContainerProxy", params_proxy, proxy_machine);
587     }
588     catch(const SALOME_Exception & ex)
589     {
590       INFOS("[StartParallelContainer] Exception in BuildCommandToLaunchParallelContainer");
591       INFOS(ex.what());
592       return ret;
593     }
594     params_proxy.nb_component_nodes = 0; // LaunchParallelContainer uses this value to know if it launches the proxy or the nodes
595     obj = LaunchParallelContainer(command_proxy, params_proxy, _NS->ContainerName(params_proxy), proxy_machine);
596     if (CORBA::is_nil(obj))
597     {
598       INFOS("[StartParallelContainer] LaunchParallelContainer for proxy returns NIL !");
599       return ret;
600     }
601     try 
602     {
603       container_proxy = PaCO::InterfaceManager::_narrow(obj);
604     }
605     catch(CORBA::SystemException& e)
606     {
607       INFOS("[StartParallelContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
608       INFOS("CORBA::SystemException : " << e);
609       return ret;
610     }
611     catch(CORBA::Exception& e)
612     {
613       INFOS("[StartParallelContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
614       INFOS("CORBA::Exception" << e);
615       return ret;
616     }
617     catch(...)
618     {
619       INFOS("[StartParallelContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
620       INFOS("Unknown exception !");
621       return ret;
622     }
623     if (CORBA::is_nil(container_proxy))
624     {
625       INFOS("[StartParallelContainer] PaCO::InterfaceManager::_narrow returns NIL !");
626       return ret;
627     }
628
629     // Step 5 : starting parallel container nodes
630     std::string command_nodes;
631     Engines::MachineParameters params_nodes(params);
632     SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
633     try 
634     {
635       command_nodes = BuildCommandToLaunchParallelContainer("SALOME_ParallelContainerNode", params_nodes, nodes_machines, proxy_machine[0]);
636     }
637     catch(const SALOME_Exception & ex)
638     {
639       INFOS("[StartParallelContainer] Exception in BuildCommandToLaunchParallelContainer");
640       INFOS(ex.what());
641       return ret;
642     }
643     std::string container_generic_node_name = _NS->ContainerName(params) + "Node";
644     obj = LaunchParallelContainer(command_nodes, params_nodes, container_generic_node_name, nodes_machines);
645     if (CORBA::is_nil(obj))
646     {
647       INFOS("[StartParallelContainer] LaunchParallelContainer for nodes returns NIL !");
648       // Il faut tuer le proxy
649       try 
650       {
651         Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
652         proxy->Shutdown();
653       }
654       catch (...)
655       {
656         INFOS("[StartParallelContainer] Exception catched from proxy Shutdown...");
657       }
658       return ret;
659     }
660
661     // Step 6 : connecting nodes and the proxy to actually create a parallel container
662     for (int i = 0; i < params.nb_component_nodes; i++) 
663     {
664       std::ostringstream tmp;
665       tmp << i;
666       std::string proc_number = tmp.str();
667       std::string container_node_name = container_generic_node_name + proc_number;
668
669       std::string theNodeMachine(nodes_machines[i]);
670       std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
671       obj = _NS->Resolve(containerNameInNS.c_str());
672       if (CORBA::is_nil(obj)) 
673       {
674         INFOS("[StartParallelContainer] CONNECTION FAILED From Naming Service !");
675         INFOS("[StartParallelContainer] Container name is " << containerNameInNS);
676         return ret;
677       }
678       try
679       {
680         MESSAGE("[StartParallelContainer] Deploying node : " << container_node_name);
681         PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
682         node->deploy();
683         MESSAGE("[StartParallelContainer] node " << container_node_name << " is deployed");
684       }
685       catch(CORBA::SystemException& e)
686       {
687         INFOS("[StartParallelContainer] Exception in deploying node : " << containerNameInNS);
688         INFOS("CORBA::SystemException : " << e);
689         return ret;
690       }
691       catch(CORBA::Exception& e)
692       {
693         INFOS("[StartParallelContainer] Exception in deploying node : " << containerNameInNS);
694         INFOS("CORBA::Exception" << e);
695         return ret;
696       }
697       catch(...)
698       {
699         INFOS("[StartParallelContainer] Exception in deploying node : " << containerNameInNS);
700         INFOS("Unknown exception !");
701         return ret;
702       }
703     }
704
705     // Step 7 : starting parallel container
706     try 
707     {
708       MESSAGE ("[StartParallelContainer] Starting parallel object");
709       container_proxy->start();
710       MESSAGE ("[StartParallelContainer] Parallel object is started");
711       ret = Engines::Container::_narrow(container_proxy);
712     }
713     catch(CORBA::SystemException& e)
714     {
715       INFOS("Caught CORBA::SystemException. : " << e);
716     }
717     catch(PortableServer::POA::ServantAlreadyActive&)
718     {
719       INFOS("Caught CORBA::ServantAlreadyActiveException");
720     }
721     catch(CORBA::Exception&)
722     {
723       INFOS("Caught CORBA::Exception.");
724     }
725     catch(std::exception& exc)
726     {
727       INFOS("Caught std::exception - "<<exc.what()); 
728     }
729     catch(...)
730     {
731       INFOS("Caught unknown exception.");
732     }
733   }
734   return ret;
735 }
736 #else
737 //=============================================================================
738 /*! CORBA Method:
739  *  Find or Start a suitable PaCO++ Parallel Container in a list of machines.
740  *  \param params            Machine Parameters required for the container
741  *  \return CORBA container reference.
742  */
743 //=============================================================================
744 Engines::Container_ptr
745 SALOME_ContainerManager::StartParallelContainer(const Engines::MachineParameters& params)
746 {
747   Engines::Container_ptr ret = Engines::Container::_nil();
748   INFOS("[StartParallelContainer] is disabled !");
749   INFOS("[StartParallelContainer] recompile SALOME Kernel to enable parallel extension");
750   return ret;
751 }
752 #endif
753
754 //=============================================================================
755 /*! This method launches the parallel container.
756  *  It will may be placed on the ressources manager.
757  *
758  * \param command to launch
759  * \param container's parameters
760  * \param name of the container
761  *
762  * \return CORBA container reference
763  */
764 //=============================================================================
765 CORBA::Object_ptr 
766 SALOME_ContainerManager::LaunchParallelContainer(const std::string& command, 
767                                                  const Engines::MachineParameters& params,
768                                                  const std::string& name,
769                                                  SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
770 {
771   CORBA::Object_ptr obj = CORBA::Object::_nil();
772   std::string containerNameInNS;
773   int count = TIME_OUT_TO_LAUNCH_CONT;
774
775   INFOS("[LaunchParallelContainer] Begin");
776   int status = system(command.c_str());
777   if (status == -1) {
778     INFOS("[LaunchParallelContainer] failed : system command status -1");
779     return obj;
780   }
781   else if (status == 217) {
782     INFOS("[LaunchParallelContainer] failed : system command status 217");
783     return obj;
784   }
785
786   if (params.nb_component_nodes == 0) 
787   {
788     std::string theMachine(vect_machine[0]);
789     // Proxy We have launch a proxy
790     containerNameInNS = _NS->BuildContainerNameForNS((char*) name.c_str(), theMachine.c_str());
791     INFOS("[LaunchParallelContainer]  Waiting for Parallel Container proxy " << containerNameInNS << " on " << theMachine);
792     while (CORBA::is_nil(obj) && count) 
793     {
794 #ifndef WIN32
795       sleep(1) ;
796 #else
797       Sleep(1000);
798 #endif
799       count-- ;
800       obj = _NS->Resolve(containerNameInNS.c_str());
801     }
802   }
803   else 
804   {
805     INFOS("[LaunchParallelContainer] launching the nodes of the parallel container");
806     // We are waiting all the nodes
807     for (int i = 0; i < params.nb_component_nodes; i++) 
808     {
809       obj = CORBA::Object::_nil();
810       std::string theMachine(vect_machine[i]);
811       // Name of the node
812       std::ostringstream tmp;
813       tmp << i;
814       std::string proc_number = tmp.str();
815       std::string container_node_name = name + proc_number;
816       containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
817       INFOS("[LaunchParallelContainer]  Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
818       while (CORBA::is_nil(obj) && count) {
819 #ifndef WIN32
820         sleep(1) ;
821 #else
822         Sleep(1000);
823 #endif
824         count-- ;
825         obj = _NS->Resolve(containerNameInNS.c_str());
826       }
827       if (CORBA::is_nil(obj))
828       {
829         INFOS("[LaunchParallelContainer] Launch of node failed (or not found) !");
830         return obj;
831       }
832     }
833   }
834   if (CORBA::is_nil(obj)) 
835     INFOS("[LaunchParallelContainer] failed");
836   
837   return obj;
838 }
839
840 void SALOME_ContainerManager::fillBatchLaunchedContainers()
841 {
842   _batchLaunchedContainers.clear();
843   _NS->Change_Directory("/Containers");
844   vector<string> vec = _NS->list_directory_recurs();
845   for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
846     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
847     Engines::Container_ptr cont=Engines::Container::_narrow(obj);
848     if(!CORBA::is_nil(cont)){
849       _batchLaunchedContainers.push_back(cont);
850     }
851   }
852   _batchLaunchedContainersIter=_batchLaunchedContainers.begin();
853 }
854
855 //=============================================================================
856 /*!
857  *  This is no longer valid (C++ container are also python containers)
858  */ 
859 //=============================================================================
860
861 bool isPythonContainer(const char* ContainerName)
862 {
863   bool ret = false;
864   int len = strlen(ContainerName);
865
866   if (len >= 2)
867     if (strcmp(ContainerName + len - 2, "Py") == 0)
868       ret = true;
869
870   return ret;
871 }
872
873 //=============================================================================
874 /*!
875  *  Builds the script to be launched
876  *
877  *  If SALOME Application not defined ($APPLI),
878  *  see BuildTempFileToLaunchRemoteContainer()
879  *
880  *  Else rely on distant configuration. Command is under the form (example):
881  *  ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
882  *                   SALOME_Container containerName &"
883
884  *  - where user is ommited if not specified in CatalogResources,
885  *  - where distant path is always relative to user@machine $HOME, and
886  *    equal to $APPLI if not specified in CatalogResources,
887  *  - where hostNS is the hostname of CORBA naming server (set by scripts to
888  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
889  *  - where portNS is the port used by CORBA naming server (set by scripts to
890  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
891  *  - where workingdir is the requested working directory for the container.
892  *    If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
893  */ 
894 //=============================================================================
895
896 string
897 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer
898 (const string& machine,
899  const Engines::MachineParameters& params, const std::string& container_exe)
900 {
901   string command;
902   int nbproc;
903           
904   if ( ! _isAppliSalomeDefined )
905     command = BuildTempFileToLaunchRemoteContainer(machine, params);
906
907   else
908     {
909       const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
910
911       if (params.isMPI)
912         {
913           if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
914             nbproc = 1;
915           else if ( params.nb_node == 0 )
916             nbproc = params.nb_proc_per_node;
917           else if ( params.nb_proc_per_node == 0 )
918             nbproc = params.nb_node;
919           else
920             nbproc = params.nb_node * params.nb_proc_per_node;
921         }
922
923       // "ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
924         //  SALOME_Container containerName &"
925
926       if (resInfo.Protocol == rsh)
927         command = "rsh ";
928       else if (resInfo.Protocol == ssh)
929         command = "ssh ";
930       else
931         throw SALOME_Exception("Unknown protocol");
932
933       if (resInfo.UserName != "")
934         {
935           command += resInfo.UserName;
936           command += "@";
937         }
938
939       command += machine;
940       command += " ";
941
942       if (resInfo.AppliPath != "")
943         command += resInfo.AppliPath; // path relative to user@machine $HOME
944       else
945         {
946           ASSERT(getenv("APPLI"));
947           command += getenv("APPLI"); // path relative to user@machine $HOME
948         }
949
950       command += "/runRemote.sh ";
951
952       ASSERT(getenv("NSHOST")); 
953       command += getenv("NSHOST"); // hostname of CORBA name server
954
955       command += " ";
956       ASSERT(getenv("NSPORT"));
957       command += getenv("NSPORT"); // port of CORBA name server
958
959       std::string wdir=params.workingdir.in();
960       if(wdir != "")
961         {
962           command += " WORKINGDIR ";
963           command += " '";
964           if(wdir == "$TEMPDIR")
965             wdir="\\$TEMPDIR";
966           command += wdir; // requested working directory
967           command += "'"; 
968         }
969
970       if(params.isMPI)
971         {
972           command += " mpirun -np ";
973           std::ostringstream o;
974           o << nbproc << " ";
975           command += o.str();
976 #ifdef WITHLAM
977           command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
978 #elif defined(WITHOPENMPI)
979           if( getenv("OMPI_URI_FILE") == NULL )
980             command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
981           else{
982             command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
983             command += getenv("OMPI_URI_FILE");
984           }
985 #endif        
986           command += " SALOME_MPIContainer ";
987         }
988       else
989         command += " " +container_exe+ " ";
990
991       command += _NS->ContainerName(params);
992       command += " -";
993       AddOmninamesParams(command);
994
995       MESSAGE("command =" << command);
996     }
997
998   return command;
999 }
1000
1001 //=============================================================================
1002 /*!
1003  *  builds the command to be launched.
1004  */ 
1005 //=============================================================================
1006
1007 string
1008 SALOME_ContainerManager::BuildCommandToLaunchLocalContainer
1009 (const Engines::MachineParameters& params, const std::string& container_exe)
1010 {
1011   _TmpFileName = BuildTemporaryFileName();
1012   string command;
1013   int nbproc = 0;
1014
1015   ofstream command_file( _TmpFileName.c_str() );
1016
1017   if (params.isMPI)
1018     {
1019       //command = "mpirun -np ";
1020       command_file << "mpirun -np ";
1021
1022       if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
1023         nbproc = 1;
1024       else if ( params.nb_node == 0 )
1025         nbproc = params.nb_proc_per_node;
1026       else if ( params.nb_proc_per_node == 0 )
1027         nbproc = params.nb_node;
1028       else
1029         nbproc = params.nb_node * params.nb_proc_per_node;
1030
1031       //std::ostringstream o;
1032
1033       //o << nbproc << " ";
1034       command_file << nbproc << " ";
1035
1036       //command += o.str();
1037 #ifdef WITHLAM
1038       //command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1039       command_file << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1040 #elif defined(WITHOPENMPI)
1041       //command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace ";
1042       if( getenv("OMPI_URI_FILE") == NULL )
1043         command_file << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1044       else
1045         {
1046           command_file << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1047           command_file << getenv("OMPI_URI_FILE");
1048         }
1049 #endif
1050
1051       if (isPythonContainer(params.container_name))
1052         //command += "pyMPI SALOME_ContainerPy.py ";
1053         command_file << " pyMPI SALOME_ContainerPy.py ";
1054       else
1055         //command += "SALOME_MPIContainer ";
1056         command_file << " SALOME_MPIContainer ";
1057     }
1058
1059   else
1060     {
1061       //command="";
1062       std::string wdir=params.workingdir.in();
1063       if(wdir != "")
1064         {
1065           // a working directory is requested
1066           if(wdir == "$TEMPDIR")
1067             {
1068               // a new temporary directory is requested
1069               string dir = Kernel_Utils::GetTmpDir();
1070 #ifdef WIN32
1071               //command += "cd /d "+ dir +";";
1072               command_file << "cd /d " << dir << endl;
1073 #else
1074               //command = "cd "+ dir +";";
1075               command_file << "cd " << dir << ";";
1076 #endif
1077
1078             }
1079           else
1080             {
1081               // a permanent directory is requested use it or create it
1082 #ifdef WIN32
1083               //command="mkdir " + wdir;
1084               command_file << "mkdir " + wdir << endl;
1085               command_file << "cd /D " + wdir << endl;
1086 #else
1087               //command="mkdir -p " + wdir + " && cd " + wdir + ";";
1088               command_file << "mkdir -p " << wdir << " && cd " << wdir + ";";
1089 #endif
1090             }
1091         }
1092       if (isPythonContainer(params.container_name))
1093         //command += "SALOME_ContainerPy.py ";
1094         command_file << "SALOME_ContainerPy.py ";
1095       else
1096         //command += container_exe + " ";
1097         command_file << container_exe + " ";
1098
1099     }
1100
1101   command_file << _NS->ContainerName(params);
1102   command_file << " -";
1103   AddOmninamesParams(command_file);
1104   command_file.close();
1105
1106 #ifndef WIN32
1107   chmod(_TmpFileName.c_str(), 0x1ED);
1108 #endif
1109   command = _TmpFileName;
1110
1111   MESSAGE("Command is file ... " << command);
1112   return command;
1113 }
1114
1115
1116 //=============================================================================
1117 /*!
1118  *  removes the generated temporary file in case of a remote launch.
1119  */ 
1120 //=============================================================================
1121
1122 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
1123 {
1124   int lenght = tmpFileName.size();
1125   if ( lenght  > 0)
1126     {
1127 #ifdef WIN32
1128       string command = "del /F ";
1129 #else
1130       string command = "rm ";      
1131 #endif
1132       if ( lenght > 4 )
1133         command += tmpFileName.substr(0, lenght - 3 );
1134       else
1135         command += tmpFileName;
1136       command += '*';
1137       system(command.c_str());
1138       //if dir is empty - remove it
1139       string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
1140       if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
1141         {
1142 #ifdef WIN32
1143           command = "del /F " + tmp_dir;
1144 #else
1145           command = "rmdir " + tmp_dir;
1146 #endif
1147           system(command.c_str());
1148         }
1149     }
1150 }
1151
1152 //=============================================================================
1153 /*!
1154  *   add to command all options relative to naming service.
1155  */ 
1156 //=============================================================================
1157
1158 void SALOME_ContainerManager::AddOmninamesParams(string& command) const
1159 {
1160   CORBA::String_var iorstr = _NS->getIORaddr();
1161   command += "ORBInitRef NameService=";
1162   command += iorstr;
1163 }
1164
1165
1166 //=============================================================================
1167 /*!
1168  *  add to command all options relative to naming service.
1169  */ 
1170 //=============================================================================
1171
1172 void SALOME_ContainerManager::AddOmninamesParams(ofstream& fileStream) const
1173 {
1174   CORBA::String_var iorstr = _NS->getIORaddr();
1175   fileStream << "ORBInitRef NameService=";
1176   fileStream << iorstr;
1177 }
1178
1179 //=============================================================================
1180 /*!
1181  *  generate a file name in /tmp directory
1182  */ 
1183 //=============================================================================
1184
1185 string SALOME_ContainerManager::BuildTemporaryFileName() const
1186 {
1187   //build more complex file name to support multiple salome session
1188   string aFileName = Kernel_Utils::GetTmpFileName();
1189 #ifndef WIN32
1190   aFileName += ".sh";
1191 #else
1192   aFileName += ".bat";
1193 #endif
1194   return aFileName;
1195 }
1196
1197
1198 //=============================================================================
1199 /*!
1200  *  Builds in a temporary file the script to be launched.
1201  *  
1202  *  Used if SALOME Application ($APPLI) is not defined.
1203  *  The command is build with data from CatalogResources, in which every path
1204  *  used on remote computer must be defined.
1205  */ 
1206 //=============================================================================
1207
1208 string
1209 SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer
1210 (const string& machine,
1211  const Engines::MachineParameters& params) throw(SALOME_Exception)
1212 {
1213   int status;
1214
1215   _TmpFileName = BuildTemporaryFileName();
1216   ofstream tempOutputFile;
1217   tempOutputFile.open(_TmpFileName.c_str(), ofstream::out );
1218   const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
1219   tempOutputFile << "#! /bin/sh" << endl;
1220
1221   // --- set env vars
1222
1223   tempOutputFile << "export SALOME_trace=local" << endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1224   //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1225
1226   // ! env vars
1227
1228   if (params.isMPI)
1229     {
1230       tempOutputFile << "mpirun -np ";
1231       int nbproc;
1232
1233       if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
1234         nbproc = 1;
1235       else if ( params.nb_node == 0 )
1236         nbproc = params.nb_proc_per_node;
1237       else if ( params.nb_proc_per_node == 0 )
1238         nbproc = params.nb_node;
1239       else
1240         nbproc = params.nb_node * params.nb_proc_per_node;
1241
1242       std::ostringstream o;
1243
1244       tempOutputFile << nbproc << " ";
1245 #ifdef WITHLAM
1246       tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1247 #elif defined(WITHOPENMPI)
1248       if( getenv("OMPI_URI_FILE") == NULL )
1249         tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1250       else{
1251         tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1252         tempOutputFile << getenv("OMPI_URI_FILE");
1253       }
1254 #endif
1255     }
1256
1257   tempOutputFile << getenv("KERNEL_ROOT_DIR") << "/bin/salome/";
1258
1259   if (params.isMPI)
1260     {
1261       if (isPythonContainer(params.container_name))
1262         tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1263       else
1264         tempOutputFile << " SALOME_MPIContainer ";
1265     }
1266
1267   else
1268     {
1269       if (isPythonContainer(params.container_name))
1270         tempOutputFile << "SALOME_ContainerPy.py ";
1271       else
1272         tempOutputFile << "SALOME_Container ";
1273     }
1274
1275   tempOutputFile << _NS->ContainerName(params) << " -";
1276   AddOmninamesParams(tempOutputFile);
1277   tempOutputFile << " &" << endl;
1278   tempOutputFile.flush();
1279   tempOutputFile.close();
1280 #ifndef WIN32
1281   chmod(_TmpFileName.c_str(), 0x1ED);
1282 #endif
1283
1284   // --- Build command
1285
1286   string command;
1287
1288   if (resInfo.Protocol == rsh)
1289     {
1290       command = "rsh ";
1291       string commandRcp = "rcp ";
1292       commandRcp += _TmpFileName;
1293       commandRcp += " ";
1294       commandRcp += machine;
1295       commandRcp += ":";
1296       commandRcp += _TmpFileName;
1297       status = system(commandRcp.c_str());
1298     }
1299
1300   else if (resInfo.Protocol == ssh)
1301     {
1302       command = "ssh ";
1303       string commandRcp = "scp ";
1304       commandRcp += _TmpFileName;
1305       commandRcp += " ";
1306       commandRcp += machine;
1307       commandRcp += ":";
1308       commandRcp += _TmpFileName;
1309       status = system(commandRcp.c_str());
1310     }
1311   else
1312     throw SALOME_Exception("Unknown protocol");
1313
1314   if(status)
1315     throw SALOME_Exception("Error of connection on remote host");    
1316
1317   command += machine;
1318   _CommandForRemAccess = command;
1319   command += " ";
1320   command += _TmpFileName;
1321
1322   SCRUTE(command);
1323
1324   return command;
1325
1326 }
1327
1328 //=============================================================================
1329 /*! Creates a command line that the container manager uses to launch
1330  * a parallel container.
1331  */ 
1332 //=============================================================================
1333 string 
1334 SALOME_ContainerManager::BuildCommandToLaunchParallelContainer(const std::string& exe_name,
1335                                                                const Engines::MachineParameters& params,
1336                                                                SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1337                                                                const std::string proxy_hostname)
1338 {
1339   // This method knows the differences between the proxy and the nodes.
1340   // nb_component_nodes is not used in the same way if it is a proxy or 
1341   // a node.
1342   
1343   //command = "gdb --args ";
1344   //command = "valgrind --tool=memcheck --log-file=val_log ";
1345   //command += real_exe_name;
1346
1347   // Step 0 : init some variables...
1348   std::string parallelLib(CORBA::string_dup(params.parallelLib));
1349   std::string real_exe_name  = exe_name + parallelLib;
1350   std::string machine_file_name("");
1351   bool remote = false;
1352   bool is_a_proxy = false; 
1353   std::string hostname(CORBA::string_dup(params.hostname));
1354
1355   std::ostringstream tmp_string;
1356   CORBA::Long nb_nodes = params.nb_component_nodes;
1357   tmp_string << nb_nodes;
1358   std::string nbproc = tmp_string.str();
1359
1360   Engines::MachineParameters_var rtn = new Engines::MachineParameters();
1361   rtn->container_name = params.container_name;
1362   rtn->hostname = params.hostname;
1363   rtn->OS = params.OS;
1364   rtn->mem_mb = params.mem_mb;
1365   rtn->cpu_clock = params.cpu_clock;
1366   rtn->nb_proc_per_node = params.nb_proc_per_node;
1367   rtn->nb_node = params.nb_node;
1368   rtn->isMPI = params.isMPI;
1369
1370   // Step 1 : local or remote launch ?
1371   if (hostname != std::string(Kernel_Utils::GetHostname()) )
1372   {
1373     MESSAGE("[BuildCommandToLaunchParallelContainer] remote machine case detected !");
1374     remote = true;
1375   }
1376
1377   // Step 2 : proxy or nodes launch ?
1378   std::string::size_type loc_proxy = exe_name.find("Proxy");
1379   if( loc_proxy != string::npos ) {
1380     is_a_proxy = true;
1381   } 
1382
1383   // Step 3 : Depending of the parallelLib, getting the machine file
1384   // ParallelLib Dummy has is own machine for this method
1385   if (remote)
1386   {
1387     if (is_a_proxy)
1388     {
1389       machine_file_name = _ResManager->getMachineFile(hostname,
1390                                                       1,
1391                                                       parallelLib);
1392     }
1393     else
1394     {
1395       machine_file_name = _ResManager->getMachineFile(hostname, 
1396                                                       params.nb_component_nodes,
1397                                                       parallelLib);
1398     }
1399     if (machine_file_name == "")
1400     {
1401       INFOS("[BuildCommandToLaunchParallelContainer] Error machine_file was not generated for machine " << hostname);
1402       throw SALOME_Exception("Error machine_file was not generated");
1403     }
1404     MESSAGE("[BuildCommandToLaunchParallelContainer] machine_file_name is : " << machine_file_name);
1405   }
1406
1407   // Step 4 : Log type choosen by the user
1408   std::string log_env("");
1409   char * get_val = getenv("PARALLEL_LOG");
1410   if (get_val)
1411     log_env = get_val;
1412   std::string command_begin("");
1413   std::string command_end("");
1414   if(log_env == "xterm")
1415   {
1416     command_begin = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1417     command_end   = "\"&";
1418   }
1419   else if(log_env == "xterm_debug")
1420   {
1421     command_begin = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1422     command_end   = "; cat \" &";
1423   }
1424   else
1425   {
1426     // default into a file...
1427     std::string logFilename = "/tmp/" + _NS->ContainerName(params) + "_" + hostname;
1428     if (is_a_proxy)
1429       logFilename += "_Proxy_";
1430     else
1431       logFilename += "_Node_";
1432     logFilename += std::string(getenv("USER")) + ".log";
1433     command_end = " > " + logFilename + " 2>&1 & ";
1434   }
1435
1436   // Step 5 : Building the command
1437   std::string command("");
1438   if (parallelLib == "Dummy")
1439   {
1440     if (is_a_proxy)
1441     {
1442       std::string command_remote("");
1443       if (remote)
1444       {
1445         std::string machine_name;
1446         std::ifstream machine_file(machine_file_name.c_str());
1447         std::getline(machine_file, machine_name);
1448         MESSAGE("[BuildCommandToLaunchParallelContainer] machine file name extracted is " << machine_name)
1449
1450         // We want to launch a command like : 
1451         // ssh user@machine distantPath/runRemote.sh hostNS portNS
1452         const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine_name);
1453         if (resInfo.Protocol == rsh)
1454           command_remote = "rsh ";
1455         else 
1456           command_remote = "ssh ";
1457         command_remote += resInfo.UserName;
1458         command_remote += "@";
1459         command_remote += machine_name;
1460         command_remote += " ";
1461         command_remote += resInfo.AppliPath; // path relative to user@machine $HOME
1462         command_remote += "/runRemote.sh ";
1463         ASSERT(getenv("NSHOST")); 
1464         command_remote += getenv("NSHOST"); // hostname of CORBA name server
1465         command_remote += " ";
1466         ASSERT(getenv("NSPORT"));
1467         command_remote += getenv("NSPORT"); // port of CORBA name server
1468         command_remote += " ";
1469
1470         hostname = machine_name;
1471       }
1472
1473       command =  real_exe_name;
1474       command += " " + _NS->ContainerName(rtn);
1475       command += " " + parallelLib;
1476       command += " " + hostname;
1477       command += " " + nbproc;
1478       command += " -";
1479       AddOmninamesParams(command);
1480
1481       command = command_begin + command_remote + command + command_end;
1482       vect_machine.push_back(hostname);
1483     }
1484     else
1485     {
1486       std::ifstream * machine_file = NULL;
1487       if (remote)
1488         machine_file = new std::ifstream(machine_file_name.c_str());
1489       for (int i= 0; i < nb_nodes; i++)
1490       {
1491         std::string command_remote("");
1492         if (remote)
1493         {
1494           std::string machine_name;
1495           std::getline(*machine_file, machine_name);
1496           MESSAGE("[BuildCommandToLaunchParallelContainer] machine file name extracted is " << machine_name)
1497
1498             // We want to launch a command like : 
1499             // ssh user@machine distantPath/runRemote.sh hostNS portNS
1500             const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine_name);
1501           if (resInfo.Protocol == rsh)
1502             command_remote = "rsh ";
1503           else 
1504             command_remote = "ssh ";
1505           command_remote += resInfo.UserName;
1506           command_remote += "@";
1507           command_remote += machine_name;
1508           command_remote += " ";
1509           command_remote += resInfo.AppliPath; // path relative to user@machine $HOME
1510           command_remote += "/runRemote.sh ";
1511           ASSERT(getenv("NSHOST")); 
1512           command_remote += getenv("NSHOST"); // hostname of CORBA name server
1513           command_remote += " ";
1514           ASSERT(getenv("NSPORT"));
1515           command_remote += getenv("NSPORT"); // port of CORBA name server
1516           command_remote += " ";
1517
1518           hostname = machine_name;
1519         }
1520
1521         std::ostringstream tmp;
1522         tmp << i;
1523         std::string proc_number = tmp.str();
1524
1525         std::string command_tmp("");
1526         command_tmp += real_exe_name;
1527         command_tmp += " " + _NS->ContainerName(rtn);
1528         command_tmp += " " + parallelLib;
1529         command_tmp += " " + proxy_hostname;
1530         command_tmp += " " + proc_number;
1531         command_tmp += " -";
1532         AddOmninamesParams(command_tmp);
1533
1534         // On change _Node_ par _Nodex_ pour avoir chaque noeud
1535         // sur un fichier
1536         std::string command_end_tmp = command_end;
1537         std::string::size_type loc_node = command_end_tmp.find("_Node_");
1538         if (loc_node != std::string::npos)
1539           command_end_tmp.insert(loc_node+5, proc_number);
1540         command += command_begin + command_remote + command_tmp + command_end_tmp;
1541         vect_machine.push_back(hostname);
1542       }
1543       if (machine_file)
1544         delete machine_file;
1545     }
1546   }
1547   else if (parallelLib == "Mpi")
1548   {
1549     // Step 0: if remote we have to copy the file
1550     // to the first machine of the file
1551     std::string remote_machine("");
1552     if (remote)
1553     {
1554       std::ifstream * machine_file = NULL;
1555       machine_file = new std::ifstream(machine_file_name.c_str());
1556       // Get first word of the line
1557       // For MPI implementation the first word is the 
1558       // machine name
1559       std::getline(*machine_file, remote_machine, ' ');
1560       machine_file->close();
1561       MESSAGE("[BuildCommandToLaunchParallelContainer] machine file name extracted is " << remote_machine)
1562
1563       // We want to launch a command like : 
1564       // scp mpi_machine_file user@machine:Path
1565       std::string command_remote("");
1566       const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(remote_machine);
1567       if (resInfo.Protocol == rsh)
1568         command_remote = "rcp ";
1569       else 
1570         command_remote = "scp ";
1571
1572       command_remote += machine_file_name;
1573       command_remote += " ";
1574       command_remote += resInfo.UserName;
1575       command_remote += "@";
1576       command_remote += remote_machine;
1577       command_remote += ":";
1578       command_remote += machine_file_name;
1579
1580       int status = system(command_remote.c_str());
1581       if (status == -1)
1582       {
1583         INFOS("copy of the mpi machine file failed !");
1584         return "";
1585       }
1586     }
1587
1588     if (is_a_proxy)
1589     {
1590       std::string command_remote("");
1591       if (remote)
1592       {
1593         // We want to launch a command like : 
1594         // ssh user@machine distantPath/runRemote.sh hostNS portNS
1595         const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(remote_machine);
1596         if (resInfo.Protocol == rsh)
1597           command_remote = "rsh ";
1598         else 
1599           command_remote = "ssh ";
1600         command_remote += resInfo.UserName;
1601         command_remote += "@";
1602         command_remote += remote_machine;
1603         command_remote += " ";
1604         command_remote += resInfo.AppliPath; // path relative to user@machine $HOME
1605         command_remote += "/runRemote.sh ";
1606         ASSERT(getenv("NSHOST")); 
1607         command_remote += getenv("NSHOST"); // hostname of CORBA name server
1608         command_remote += " ";
1609         ASSERT(getenv("NSPORT"));
1610         command_remote += getenv("NSPORT"); // port of CORBA name server
1611         command_remote += " ";
1612
1613         hostname = remote_machine;
1614       }
1615
1616       // We use Dummy proxy for MPI parallel containers
1617       real_exe_name  = exe_name + "Dummy";
1618       command =  real_exe_name;
1619       command += " " + _NS->ContainerName(rtn);
1620       command += " Dummy";
1621       command += " " + hostname;
1622       command += " " + nbproc;
1623       command += " -";
1624       AddOmninamesParams(command);
1625
1626       command = command_begin + command_remote + command + command_end;
1627       vect_machine.push_back(hostname);
1628     }
1629     else                                          
1630     {
1631       std::string command_remote("");
1632       if (remote)
1633       {
1634         const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(remote_machine);
1635         if (resInfo.Protocol == rsh)
1636           command_remote = "rsh ";
1637         else 
1638           command_remote = "ssh ";
1639         command_remote += resInfo.UserName;
1640         command_remote += "@";
1641         command_remote += remote_machine;
1642         command_remote += " ";
1643
1644         std::string new_real_exe_name("");
1645         new_real_exe_name += resInfo.AppliPath; // path relative to user@machine $HOME
1646         new_real_exe_name += "/runRemote.sh ";
1647         ASSERT(getenv("NSHOST")); 
1648         new_real_exe_name += getenv("NSHOST"); // hostname of CORBA name server
1649         new_real_exe_name += " ";
1650         ASSERT(getenv("NSPORT"));
1651         new_real_exe_name += getenv("NSPORT"); // port of CORBA name server
1652         new_real_exe_name += " ";
1653
1654         real_exe_name = new_real_exe_name + real_exe_name;
1655         hostname = remote_machine;
1656       }
1657
1658       const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(hostname);
1659       if (resInfo.mpi == lam)
1660       {
1661         command = "mpiexec -ssi boot ";
1662         if (resInfo.Protocol == rsh)
1663           command += "rsh ";
1664         else 
1665           command += "ssh ";
1666         command += "-machinefile " + machine_file_name + " "; 
1667         command += "-n " + nbproc + " ";
1668         command += real_exe_name;
1669         command += " " + _NS->ContainerName(rtn);
1670         command += " " + parallelLib;
1671         command += " " + proxy_hostname;
1672         command += " -";
1673         AddOmninamesParams(command);
1674       }
1675       else
1676       {
1677         command = "mpirun -np " + nbproc + " ";
1678         command += real_exe_name;
1679         command += " " + _NS->ContainerName(rtn);
1680         command += " " + parallelLib;
1681         command += " " + proxy_hostname;
1682         command += " -";
1683         AddOmninamesParams(command);
1684       }
1685
1686       command = command_begin + command_remote + command + command_end;
1687       for (int i= 0; i < nb_nodes; i++)
1688         vect_machine.push_back(proxy_hostname);
1689     }
1690   }
1691   else
1692   {
1693     std::string message("Unknown parallelLib : " + parallelLib);
1694     throw SALOME_Exception(message.c_str());
1695   }
1696
1697   MESSAGE("Parallel launch is: " << command);
1698   return command;
1699 }
1700
1701 string SALOME_ContainerManager::GetMPIZeroNode(string machine)
1702 {
1703   int status;
1704   string zeronode;
1705   string cmd;
1706   string tmpFile = BuildTemporaryFileName();
1707
1708   cmd = "ssh " + machine + " mpirun -np 1 hostname > " + tmpFile;
1709
1710   status = system(cmd.c_str());
1711   if( status == 0 ){
1712     ifstream fp(tmpFile.c_str(),ios::in);
1713     fp >> zeronode;
1714   }
1715
1716   RmTmpFile(tmpFile);
1717
1718   return zeronode;
1719 }