Salome HOME
0023299: [CEA] Finalize multi-study removal
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 #include "SALOME_ContainerManager.hxx"
24 #include "SALOME_ResourcesManager.hxx"
25 #include "SALOME_LoadRateManager.hxx"
26 #include "SALOME_NamingService.hxx"
27 #include "SALOME_ResourcesManager_Client.hxx"
28 #include "SALOME_ModuleCatalog.hh"
29 #include "Basics_Utils.hxx"
30 #include "Basics_DirUtils.hxx"
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <signal.h>
34 #ifndef WIN32
35 #include <unistd.h>
36 #endif
37 #include <vector>
38 #include "Utils_CorbaException.hxx"
39 #include <sstream>
40 #include <string>
41
42 #include <SALOMEconfig.h>
43 #include CORBA_CLIENT_HEADER(SALOME_Session)
44
45 #ifdef HAVE_MPI2
46 #include <mpi.h>
47 #endif
48
49 #ifdef WIN32
50 #include <process.h>
51 #define getpid _getpid
52 #endif
53
54 #ifdef WITH_PACO_PARALLEL
55 #include "PaCOPP.hxx"
56 #endif
57
58 const int SALOME_ContainerManager::TIME_OUT_TO_LAUNCH_CONT=60;
59
60 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
61   "/ContainerManager";
62
63 omni_mutex SALOME_ContainerManager::_numInstanceMutex;
64
65 Utils_Mutex SALOME_ContainerManager::_getenvMutex;
66
67 Utils_Mutex SALOME_ContainerManager::_systemMutex;
68
69 //=============================================================================
70 /*!
71  *  Constructor
72  *  \param orb
73  *  Define a CORBA single thread policy for the server, which avoid to deal
74  *  with non thread-safe usage like Change_Directory in SALOME naming service
75  */
76 //=============================================================================
77
78 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService *ns)
79   : _nbprocUsed(1)
80 {
81   MESSAGE("constructor");
82   _NS = ns;
83   _resManager = new SALOME_ResourcesManager_Client(ns);
84
85   PortableServer::POAManager_var pman = poa->the_POAManager();
86   _orb = CORBA::ORB::_duplicate(orb) ;
87   CORBA::PolicyList policies;
88   policies.length(1);
89   PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
90   policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
91
92   _poa = poa->create_POA("MThreadPOA",pman,policies);
93   threadPol->destroy();
94   PortableServer::ObjectId_var id = _poa->activate_object(this);
95   CORBA::Object_var obj = _poa->id_to_reference(id);
96   Engines::ContainerManager_var refContMan =
97     Engines::ContainerManager::_narrow(obj);
98
99   _NS->Register(refContMan,_ContainerManagerNameInNS);
100   _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
101
102 #ifdef HAVE_MPI2
103 #ifdef OPEN_MPI
104   _pid_mpiServer = -1;
105   // the urifile name depends on pid of the process
106   std::stringstream urifile;
107   urifile << GetenvThreadSafeAsString("HOME") << "/.urifile_" << getpid();
108   setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
109   if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
110     // get the pid of all ompi-server
111     std::set<pid_t> thepids1 = getpidofprogram("ompi-server");
112     // launch a new ompi-server
113     std::string command;
114     command = "ompi-server -r ";
115     command += GetenvThreadSafeAsString("OMPI_URI_FILE");
116     int status=SystemThreadSafe(command.c_str());
117     if(status!=0)
118       throw SALOME_Exception("Error when launching ompi-server");
119     // get the pid of all ompi-server
120     std::set<pid_t> thepids2 = getpidofprogram("ompi-server");
121     // my ompi-server is the new one
122     std::set<pid_t>::const_iterator it;
123     for(it=thepids2.begin();it!=thepids2.end();it++)
124       if(thepids1.find(*it) == thepids1.end())
125         _pid_mpiServer = *it;
126     if(_pid_mpiServer < 0)
127       throw SALOME_Exception("Error when getting ompi-server id");
128   }
129 #elif defined(MPICH)
130   _pid_mpiServer = -1;
131   // get the pid of all hydra_nameserver
132   std::set<pid_t> thepids1 = getpidofprogram("hydra_nameserver");
133   // launch a new hydra_nameserver
134   std::string command;
135   command = "hydra_nameserver &";
136   SystemThreadSafe(command.c_str());
137   // get the pid of all hydra_nameserver
138   std::set<pid_t> thepids2 = getpidofprogram("hydra_nameserver");
139   // my hydra_nameserver is the new one
140   std::set<pid_t>::const_iterator it;
141   for(it=thepids2.begin();it!=thepids2.end();it++)
142     if(thepids1.find(*it) == thepids1.end())
143       _pid_mpiServer = *it;
144 #endif
145 #endif
146
147   MESSAGE("constructor end");
148 }
149
150 //=============================================================================
151 /*!
152  * destructor
153  */
154 //=============================================================================
155
156 SALOME_ContainerManager::~SALOME_ContainerManager()
157 {
158   MESSAGE("destructor");
159   delete _resManager;
160 #ifdef HAVE_MPI2
161 #ifdef OPEN_MPI
162   if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
163     // kill my ompi-server
164     if( kill(_pid_mpiServer,SIGTERM) != 0 )
165       throw SALOME_Exception("Error when killing ompi-server");
166     // delete my urifile
167     int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
168     if(status!=0)
169       throw SALOME_Exception("Error when removing urifile");
170   }
171 #elif defined(MPICH)
172   // kill my hydra_nameserver
173   if(_pid_mpiServer > -1)
174     if( kill(_pid_mpiServer,SIGTERM) != 0 )
175       throw SALOME_Exception("Error when killing hydra_nameserver");
176 #endif
177 #endif
178 }
179
180 //=============================================================================
181 //! shutdown all the containers, then the ContainerManager servant
182 /*! CORBA method:
183  */
184 //=============================================================================
185
186 void SALOME_ContainerManager::Shutdown()
187 {
188   MESSAGE("Shutdown");
189   ShutdownContainers();
190   _NS->Destroy_Name(_ContainerManagerNameInNS);
191   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
192   _poa->deactivate_object(oid);
193 }
194
195 //=============================================================================
196 //! Loop on all the containers listed in naming service, ask shutdown on each
197 /*! CORBA Method:
198  */
199 //=============================================================================
200
201 void SALOME_ContainerManager::ShutdownContainers()
202 {
203   MESSAGE("ShutdownContainers");
204
205   SALOME::Session_var session = SALOME::Session::_nil();
206   CORBA::Long pid = 0;
207   CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
208   if (!CORBA::is_nil(objS))
209   {
210     session = SALOME::Session::_narrow(objS);
211     if (!CORBA::is_nil(session))
212       pid = session->getPID();
213   }
214
215   bool isOK;
216   isOK = _NS->Change_Directory("/Containers");
217   if( isOK ){
218     std::vector<std::string> vec = _NS->list_directory_recurs();
219     std::list<std::string> lstCont;
220     for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
221       {
222         SCRUTE((*iter));
223         CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
224         try
225           {
226             Engines::Container_var cont=Engines::Container::_narrow(obj);
227             if(!CORBA::is_nil(cont) && pid != cont->getPID())
228               lstCont.push_back((*iter));
229           }
230         catch(const CORBA::Exception& e)
231           {
232             // ignore this entry and continue
233           }
234       }
235     MESSAGE("Container list: ");
236     for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
237       SCRUTE((*iter));
238     }
239     for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
240     {
241       try
242       {
243         SCRUTE((*iter));
244         CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
245         Engines::Container_var cont=Engines::Container::_narrow(obj);
246         if(!CORBA::is_nil(cont))
247         {
248           MESSAGE("ShutdownContainers: " << (*iter));
249           cont->Shutdown();
250         }
251         else
252           MESSAGE("ShutdownContainers: no container ref for " << (*iter));
253       }
254       catch(CORBA::SystemException& e)
255       {
256         INFOS("CORBA::SystemException ignored : " << e);
257       }
258       catch(CORBA::Exception&)
259       {
260         INFOS("CORBA::Exception ignored.");
261       }
262       catch(...)
263       {
264         INFOS("Unknown exception ignored.");
265       }
266     }
267   }
268 }
269
270 //=============================================================================
271 //! Give a suitable Container given constraints
272 /*! CORBA Method:
273  *  \param params Container Parameters required for the container
274  *  \return the container or nil
275  */
276 //=============================================================================
277 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
278 {
279   std::string machFile;
280   Engines::Container_ptr ret(Engines::Container::_nil());
281
282   // Step 0: Default mode is start
283   Engines::ContainerParameters local_params(params);
284   if (std::string(local_params.mode.in()) == "")
285     local_params.mode = CORBA::string_dup("start");
286   std::string mode = local_params.mode.in();
287   MESSAGE("[GiveContainer] starting with mode: " << mode);
288
289   // Step 1: Find Container for find and findorstart mode
290   if (mode == "find" || mode == "findorstart")
291   {
292     ret = FindContainer(params, params.resource_params.resList);
293     if(!CORBA::is_nil(ret))
294       return ret;
295     else
296     {
297       if (mode == "find")
298       {
299         MESSAGE("[GiveContainer] no container found");
300         return ret;
301       }
302       else
303       {
304         mode = "start";
305       }
306     }
307   }
308
309   // Step 2: Get all possibleResources from the parameters
310   // Consider only resources that can run containers
311   resourceParams resource_params = resourceParameters_CORBAtoCPP(local_params.resource_params);
312   resource_params.can_run_containers = true;
313   std::vector<std::string> possibleResources = _resManager->GetFittingResources(resource_params);
314   MESSAGE("[GiveContainer] - length of possible resources " << possibleResources.size());
315   std::vector<std::string> local_resources;
316
317   // Step 3: if mode is "get" keep only machines with existing containers
318   if(mode == "get")
319   {
320     for(unsigned int i=0; i < possibleResources.size(); i++)
321     {
322       Engines::Container_ptr cont = FindContainer(params, possibleResources[i]);
323       try
324       {
325         if(!cont->_non_existent())
326           local_resources.push_back(possibleResources[i]);
327       }
328       catch(CORBA::Exception&) {}
329     }
330
331     // if local_resources is empty, we cannot give a container
332     if (local_resources.size() == 0)
333     {
334       MESSAGE("[GiveContainer] cannot find a container for mode get");
335       return ret;
336     }
337   }
338   else
339     local_resources = possibleResources;
340
341   // Step 4: select the resource where to get/start the container
342   bool resource_available = true;
343   std::string resource_selected;
344   std::vector<std::string> resources = local_resources;
345   while (resource_available)
346   {
347     if (resources.size() == 0)
348       resource_available = false;
349     else
350     {
351       try
352       {
353         resource_selected = _resManager->Find(params.resource_params.policy.in(), resources);
354         // Remove resource_selected from vector
355         std::vector<std::string>::iterator it;
356         for (it=resources.begin() ; it < resources.end(); it++ )
357           if (*it == resource_selected)
358           {
359             resources.erase(it);
360             break;
361           }
362       }
363       catch(const SALOME_Exception &ex)
364       {
365         MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
366         return ret;
367       }
368       MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
369
370       // Step 5: Create container name
371       ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
372       std::string hostname(resource_definition.HostName);
373       std::string containerNameInNS;
374       if(params.isMPI){
375         int nbproc;
376         if ( params.nb_proc <= 0 )
377           nbproc = 1;
378         else
379           nbproc = params.nb_proc;
380         try
381         {
382           if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
383             machFile = machinesFile(nbproc);
384         }
385         catch(const SALOME_Exception & ex)
386         {
387           std::string err_msg = ex.what();
388           err_msg += params.container_name;
389           INFOS(err_msg.c_str());
390           return ret;
391         }
392         // A mpi parallel container register on zero node in NS
393         std::string mpiZeroNode = GetMPIZeroNode(resource_selected,machFile).c_str();
394         containerNameInNS = _NS->BuildContainerNameForNS(params, mpiZeroNode.c_str());
395       }
396       else
397         containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
398       MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
399
400       // Step 6: check if the name exists in naming service
401       //if params.mode == "getorstart" or "get" use the existing container
402       //if params.mode == "start" shutdown the existing container before launching a new one with that name
403
404       { // critical section
405         Utils_Locker lock (&_giveContainerMutex1);
406         CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
407         if (!CORBA::is_nil(obj))
408           {
409             try
410             {
411                 Engines::Container_var cont=Engines::Container::_narrow(obj);
412                 if(!cont->_non_existent())
413                   {
414                     if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
415                         return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
416                     }
417                     else
418                       {
419                         INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
420                         cont->Shutdown(); // shutdown the registered container if it exists
421                       }
422                   }
423             }
424             catch(CORBA::Exception&)
425             {
426                 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
427             }
428           }
429       } // end critical section
430       Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
431       if (!CORBA::is_nil(cont))
432       {
433         INFOS("[GiveContainer] container " << containerNameInNS << " launched");
434         return cont._retn();
435       }
436       else
437       {
438         INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
439       }
440     }
441   }
442
443   // We were not able to launch the container
444   INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
445   std::vector<std::string>::iterator it;
446   for (it=local_resources.begin() ; it < local_resources.end(); it++ )
447     INFOS("[GiveContainer] " << *it)
448   return ret;
449 }
450
451 Engines::Container_ptr
452 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
453                                          const std::string & resource_selected,
454                                          const std::string & hostname,
455                                          const std::string & machFile,
456                                          const std::string & containerNameInNS)
457 {
458   std::string user,command,logFilename,tmpFileName;
459   int status;
460   Engines::Container_ptr ret(Engines::Container::_nil());
461   {//start of critical section
462     Utils_Locker lock (&_giveContainerMutex1);
463     // Step 1: type of container: PaCO, Exe, Mpi or Classic
464     // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
465     // TODO -> separates Mpi from Classic/Exe
466     // Classic or Exe ?
467     std::string container_exe = "SALOME_Container"; // Classic container
468     Engines::ContainerParameters local_params(params);
469     int found=0;
470     try
471     {
472         CORBA::String_var container_exe_tmp;
473         CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
474         SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
475         if (CORBA::is_nil (Catalog))
476           {
477             INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
478             return ret;
479           }
480         // Loop through component list
481         for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
482           {
483             const char* compoi = local_params.resource_params.componentList[i];
484             SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
485             if (CORBA::is_nil (compoInfo))
486               {
487                 continue;
488               }
489             SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
490             container_exe_tmp=compoInfo->implementation_name();
491             if(impl==SALOME_ModuleCatalog::CEXE)
492               {
493                 if(found)
494                   {
495                     INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
496                     return Engines::Container::_nil();
497                   }
498                 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
499                 container_exe = container_exe_tmp.in();
500                 found=1;
501               }
502           }
503     }
504     catch (ServiceUnreachable&)
505     {
506         INFOS("Caught exception: Naming Service Unreachable");
507         return ret;
508     }
509     catch (...)
510     {
511         INFOS("Caught unknown exception.");
512         return ret;
513     }
514
515     // Step 2: test resource
516     // Only if an application directory is set
517     if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
518       {
519         // Preparing remote command
520         std::string command = "";
521         const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_selected));
522         command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
523         if (resInfo.AppliPath != "")
524           command += resInfo.AppliPath;
525         else
526           {
527             ASSERT(GetenvThreadSafe("APPLI"));
528             command += GetenvThreadSafeAsString("APPLI");
529           }
530         command += "/runRemote.sh ";
531         ASSERT(GetenvThreadSafe("NSHOST"));
532         command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
533         command += " ";
534         ASSERT(GetenvThreadSafe("NSPORT"));
535         command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
536         command += " \"ls /tmp >/dev/null 2>&1\"";
537
538         // Launch remote command
539         int status = SystemThreadSafe(command.c_str());
540         if (status != 0)
541           {
542             // Error on resource - cannot launch commands
543             INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
544             INFOS("[LaunchContainer] Command was " << command);
545 #ifndef WIN32
546             INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
547 #endif
548             return Engines::Container::_nil();
549           }
550       }
551
552     // Step 3: start a new container
553     // Check if a PaCO container
554     // PaCO++
555     if (std::string(local_params.parallelLib.in()) != "")
556       {
557         ret = StartPaCOPPContainer(params, resource_selected);
558         return ret;
559       }
560     // Other type of containers...
561     MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
562     // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
563     if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
564       command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
565     // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
566     else if(hostname == Kernel_Utils::GetHostname())
567       command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
568     // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
569     else
570       command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
571
572     //redirect stdout and stderr in a file
573 #ifdef WIN32
574     logFilename=GetenvThreadSafeAsString("TEMP");
575     logFilename += "\\";
576     user = GetenvThreadSafeAsString( "USERNAME" );
577 #else
578     user = GetenvThreadSafeAsString( "USER" );
579     if (user.empty())
580       user = GetenvThreadSafeAsString( "LOGNAME" );
581     logFilename="/tmp";
582     char* val = GetenvThreadSafe("SALOME_TMP_DIR");
583     if(val)
584       {
585         struct stat file_info;
586         stat(val, &file_info);
587         bool is_dir = S_ISDIR(file_info.st_mode);
588         if (is_dir)logFilename=val;
589         else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
590       }
591     logFilename += "/";
592 #endif
593     logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
594     std::ostringstream tmp;
595     tmp << "_" << getpid();
596     logFilename += tmp.str();
597     logFilename += ".log" ;
598     command += " > " + logFilename + " 2>&1";
599     MakeTheCommandToBeLaunchedASync(command);
600
601     // launch container with a system call
602     status=SystemThreadSafe(command.c_str());
603   }//end of critical of section
604
605   if (status == -1)
606     {
607       INFOS("[LaunchContainer] command failed (system command status -1): " << command);
608       RmTmpFile(tmpFileName); // command file can be removed here
609       return Engines::Container::_nil();
610     }
611   else if (status == 217)
612     {
613       INFOS("[LaunchContainer] command failed (system command status 217): " << command);
614       RmTmpFile(tmpFileName); // command file can be removed here
615       return Engines::Container::_nil();
616     }
617   else
618     {
619       // Step 4: Wait for the container
620       int count(GetTimeOutToLoaunchServer());
621       INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
622       while (CORBA::is_nil(ret) && count)
623         {
624           SleepInSecond(1);
625           count--;
626           MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
627           CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
628           ret=Engines::Container::_narrow(obj);
629         }
630       if (CORBA::is_nil(ret))
631         {
632           INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
633         }
634       else
635         {
636           // Setting log file name
637           logFilename=":"+logFilename;
638           logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
639           logFilename=user+logFilename;
640           ret->logfilename(logFilename.c_str());
641           RmTmpFile(tmpFileName); // command file can be removed here
642         }
643     }
644   return ret;
645 }
646
647 //=============================================================================
648 //! Find a container given constraints (params) on a list of machines (possibleComputers)
649 //! agy : this method is ThreadSafe
650 /*!
651  *
652  */
653 //=============================================================================
654
655 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
656 {
657   MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
658   for(unsigned int i=0; i < possibleResources.length();i++)
659     {
660       Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
661       if(!CORBA::is_nil(cont))
662         return cont;
663     }
664   MESSAGE("[FindContainer] no container found");
665   return Engines::Container::_nil();
666 }
667
668 //=============================================================================
669 //! Find a container given constraints (params) on a machine (theMachine)
670 //! agy : this method is ThreadSafe
671 /*!
672  *
673  */
674 //=============================================================================
675
676 Engines::Container_ptr
677 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
678 {
679   ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
680   std::string hostname(resource_definition.HostName);
681   std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
682   MESSAGE("[FindContainer] Try to find a container  " << containerNameInNS << " on resource " << resource);
683   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
684   try
685   {
686     if(obj->_non_existent())
687       return Engines::Container::_nil();
688     else
689       return Engines::Container::_narrow(obj);
690   }
691   catch(const CORBA::Exception& e)
692   {
693     return Engines::Container::_nil();
694   }
695 }
696
697
698 bool isPythonContainer(const char* ContainerName);
699
700 //=============================================================================
701 /*!
702  *  This is no longer valid (C++ container are also python containers)
703  */
704 //=============================================================================
705 bool isPythonContainer(const char* ContainerName)
706 {
707   return false; // VSR 02/08/2013: Python containers are no more supported
708   bool ret = false;
709   int len = strlen(ContainerName);
710
711   if (len >= 2)
712     if (strcmp(ContainerName + len - 2, "Py") == 0)
713       ret = true;
714
715   return ret;
716 }
717
718 //=============================================================================
719 /*!
720  *  Builds the script to be launched
721  *
722  *  If SALOME Application not defined ($APPLI),
723  *  see BuildTempFileToLaunchRemoteContainer()
724  *
725  *  Else rely on distant configuration. Command is under the form (example):
726  *  ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
727  *                   SALOME_Container containerName &"
728
729  *  - where user is ommited if not specified in CatalogResources,
730  *  - where distant path is always relative to user@machine $HOME, and
731  *    equal to $APPLI if not specified in CatalogResources,
732  *  - where hostNS is the hostname of CORBA naming server (set by scripts to
733  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
734  *  - where portNS is the port used by CORBA naming server (set by scripts to
735  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
736  *  - where workingdir is the requested working directory for the container.
737  *    If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
738  */
739 //=============================================================================
740
741 std::string
742 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
743 {
744   std::string command,tmpFileName;
745   if (!_isAppliSalomeDefined)
746     command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
747   else
748   {
749     int nbproc;
750     const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
751
752     if (params.isMPI)
753     {
754       if ( params.nb_proc <= 0 )
755         nbproc = 1;
756       else
757         nbproc = params.nb_proc;
758     }
759
760     // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
761     //  SALOME_Container containerName &"
762     command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
763
764     if (resInfo.AppliPath != "")
765       command += resInfo.AppliPath; // path relative to user@machine $HOME
766     else
767     {
768       ASSERT(GetenvThreadSafe("APPLI"));
769       command += GetenvThreadSafeAsString("APPLI"); // path relative to user@machine $HOME
770     }
771
772     command += "/runRemote.sh ";
773
774     ASSERT(GetenvThreadSafe("NSHOST"));
775     command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
776
777     command += " ";
778     ASSERT(GetenvThreadSafe("NSPORT"));
779     command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
780
781     std::string wdir = params.workingdir.in();
782     if(wdir != "")
783     {
784       command += " WORKINGDIR ";
785       command += " '";
786       if(wdir == "$TEMPDIR")
787         wdir="\\$TEMPDIR";
788       command += wdir; // requested working directory
789       command += "'";
790     }
791
792     if(params.isMPI)
793     {
794       command += " mpirun -np ";
795       std::ostringstream o;
796       o << nbproc << " ";
797       command += o.str();
798 #ifdef LAM_MPI
799       command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
800 #elif defined(OPEN_MPI)
801       if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
802         command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
803       else{
804         command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
805         command += GetenvThreadSafeAsString("OMPI_URI_FILE");
806       }
807 #elif defined(MPICH)
808       command += "-nameserver " + Kernel_Utils::GetHostname();
809 #endif
810       command += " SALOME_MPIContainer ";
811     }
812     else
813       command += " " +container_exe+ " ";
814
815     command += _NS->ContainerName(params);
816     command += " -";
817     AddOmninamesParams(command);
818
819     MESSAGE("command =" << command);
820   }
821
822   return command;
823 }
824
825 //=============================================================================
826 /*!
827  *  builds the command to be launched.
828  */
829 //=============================================================================
830 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
831 {
832   tmpFileName = BuildTemporaryFileName();
833   std::string command;
834   int nbproc = 0;
835
836   std::ostringstream o;
837
838   if (params.isMPI)
839     {
840       o << "mpirun -np ";
841
842       if ( params.nb_proc <= 0 )
843         nbproc = 1;
844       else
845         nbproc = params.nb_proc;
846
847       o << nbproc << " ";
848
849       if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
850         o << "-machinefile " << machinesFile << " ";
851
852 #ifdef LAM_MPI
853       o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
854 #elif defined(OPEN_MPI)
855       if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
856         o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
857       else
858         {
859           o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
860           o << GetenvThreadSafeAsString("OMPI_URI_FILE");
861         }
862 #elif defined(MPICH)
863       o << "-nameserver " + Kernel_Utils::GetHostname();
864 #endif
865
866       if (isPythonContainer(params.container_name))
867         o << " pyMPI SALOME_ContainerPy.py ";
868       else
869         o << " SALOME_MPIContainer ";
870     }
871
872   else
873     {
874       std::string wdir=params.workingdir.in();
875       if(wdir != "")
876         {
877           // a working directory is requested
878           if(wdir == "$TEMPDIR")
879             {
880               // a new temporary directory is requested
881               std::string dir = Kernel_Utils::GetTmpDir();
882 #ifdef WIN32
883               o << "cd /d " << dir << std::endl;
884 #else
885               o << "cd " << dir << ";";
886 #endif
887
888             }
889           else
890             {
891               // a permanent directory is requested use it or create it
892 #ifdef WIN32
893               o << "mkdir " + wdir << std::endl;
894               o << "cd /D " + wdir << std::endl;
895 #else
896               o << "mkdir -p " << wdir << " && cd " << wdir + ";";
897 #endif
898             }
899         }
900
901       if (isPythonContainer(params.container_name))
902         o << "SALOME_ContainerPy.py ";
903       else
904         o << container_exe + " ";
905
906     }
907
908   o << _NS->ContainerName(params);
909   o << " -";
910   AddOmninamesParams(o);
911
912   std::ofstream command_file( tmpFileName.c_str() );
913   command_file << o.str();
914   command_file.close();
915
916 #ifndef WIN32
917   chmod(tmpFileName.c_str(), 0x1ED);
918 #endif
919   command = tmpFileName;
920
921   MESSAGE("Command is file ... " << command);
922   MESSAGE("Command is ... " << o.str());
923   return command;
924 }
925
926
927 //=============================================================================
928 /*!
929  *  removes the generated temporary file in case of a remote launch.
930  *  This method is thread safe
931  */
932 //=============================================================================
933
934 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
935 {
936   int lenght = tmpFileName.size();
937   if ( lenght  > 0)
938     {
939 #ifdef WIN32
940       std::string command = "del /F ";
941 #else
942       std::string command = "rm ";
943 #endif
944       if ( lenght > 4 )
945         command += tmpFileName.substr(0, lenght - 3 );
946       else
947         command += tmpFileName;
948       command += '*';
949       SystemThreadSafe(command.c_str());
950       //if dir is empty - remove it
951       std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
952       if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
953         {
954 #ifdef WIN32
955           command = "del /F " + tmp_dir;
956 #else
957           command = "rmdir " + tmp_dir;
958 #endif
959           SystemThreadSafe(command.c_str());
960         }
961     }
962 }
963
964 //=============================================================================
965 /*!
966  *   add to command all options relative to naming service.
967  */
968 //=============================================================================
969
970 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
971 {
972   std::ostringstream oss;
973   AddOmninamesParams(oss);
974   command+=oss.str();
975 }
976
977 //=============================================================================
978 /*!
979  *  add to command all options relative to naming service.
980  */
981 //=============================================================================
982
983 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
984 {
985   AddOmninamesParams(fileStream,_NS);
986 }
987
988 //=============================================================================
989 /*!
990  *  add to command all options relative to naming service.
991  */
992 //=============================================================================
993
994 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
995 {
996   CORBA::String_var iorstr(ns->getIORaddr());
997   fileStream << "ORBInitRef NameService=";
998   fileStream << iorstr;
999 }
1000
1001 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
1002 {
1003 #ifdef WIN32
1004     command = "%PYTHONBIN% -c \"import subprocess ; subprocess.Popen(r'" + command + "').pid\"";
1005 #else
1006     command += " &";
1007 #endif
1008 }
1009
1010 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
1011 {
1012   int count(TIME_OUT_TO_LAUNCH_CONT);
1013   if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
1014     {
1015       std::string new_count_str(GetenvThreadSafeAsString("TIMEOUT_TO_LAUNCH_CONTAINER"));
1016       int new_count;
1017       std::istringstream ss(new_count_str);
1018       if (!(ss >> new_count))
1019         {
1020           INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
1021         }
1022       else
1023         count = new_count;
1024     }
1025   return count;
1026 }
1027
1028 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
1029 {
1030 #ifndef WIN32
1031   sleep( ellapseTimeInSecond ) ;
1032 #else
1033   int timeInMS(1000*ellapseTimeInSecond);
1034   Sleep(timeInMS);
1035 #endif
1036 }
1037
1038 //=============================================================================
1039 /*!
1040  *  generate a file name in /tmp directory
1041  */
1042 //=============================================================================
1043
1044 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1045 {
1046   //build more complex file name to support multiple salome session
1047   std::string aFileName = Kernel_Utils::GetTmpFileName();
1048 #ifndef WIN32
1049   aFileName += ".sh";
1050 #else
1051   aFileName += ".bat";
1052 #endif
1053   return aFileName;
1054 }
1055
1056 //=============================================================================
1057 /*!
1058  *  Builds in a temporary file the script to be launched.
1059  *
1060  *  Used if SALOME Application ($APPLI) is not defined.
1061  *  The command is build with data from CatalogResources, in which every path
1062  *  used on remote computer must be defined.
1063  */
1064 //=============================================================================
1065
1066 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1067 {
1068   int status;
1069
1070   tmpFileName = BuildTemporaryFileName();
1071   std::ofstream tempOutputFile;
1072   tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1073   const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1074   tempOutputFile << "#! /bin/sh" << std::endl;
1075
1076   // --- set env vars
1077
1078   tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1079   //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1080
1081   // ! env vars
1082
1083   if (params.isMPI)
1084     {
1085       tempOutputFile << "mpirun -np ";
1086       int nbproc;
1087
1088       if ( params.nb_proc <= 0 )
1089         nbproc = 1;
1090       else
1091         nbproc = params.nb_proc;
1092
1093       std::ostringstream o;
1094
1095       tempOutputFile << nbproc << " ";
1096 #ifdef LAM_MPI
1097       tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1098 #elif defined(OPEN_MPI)
1099       if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1100         tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1101       else{
1102         tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1103         tempOutputFile << GetenvThreadSafeAsString("OMPI_URI_FILE");
1104       }
1105 #elif defined(MPICH)
1106       tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1107 #endif
1108     }
1109
1110   tempOutputFile << GetenvThreadSafeAsString("KERNEL_ROOT_DIR") << "/bin/salome/";
1111
1112   if (params.isMPI)
1113     {
1114       if (isPythonContainer(params.container_name))
1115         tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1116       else
1117         tempOutputFile << " SALOME_MPIContainer ";
1118     }
1119
1120   else
1121     {
1122       if (isPythonContainer(params.container_name))
1123         tempOutputFile << "SALOME_ContainerPy.py ";
1124       else
1125         tempOutputFile << "SALOME_Container ";
1126     }
1127
1128   tempOutputFile << _NS->ContainerName(params) << " -";
1129   AddOmninamesParams(tempOutputFile);
1130   tempOutputFile << " &" << std::endl;
1131   tempOutputFile.flush();
1132   tempOutputFile.close();
1133 #ifndef WIN32
1134   chmod(tmpFileName.c_str(), 0x1ED);
1135 #endif
1136
1137   // --- Build command
1138
1139   std::string command;
1140
1141   if (resInfo.Protocol == rsh)
1142     {
1143       command = "rsh ";
1144       std::string commandRcp = "rcp ";
1145       commandRcp += tmpFileName;
1146       commandRcp += " ";
1147       commandRcp += resInfo.HostName;
1148       commandRcp += ":";
1149       commandRcp += tmpFileName;
1150       status = SystemThreadSafe(commandRcp.c_str());
1151     }
1152
1153   else if (resInfo.Protocol == ssh)
1154     {
1155       command = "ssh ";
1156       std::string commandRcp = "scp ";
1157       commandRcp += tmpFileName;
1158       commandRcp += " ";
1159       commandRcp += resInfo.HostName;
1160       commandRcp += ":";
1161       commandRcp += tmpFileName;
1162       status = SystemThreadSafe(commandRcp.c_str());
1163     }
1164
1165   else if (resInfo.Protocol == srun)
1166     {
1167       command = "srun -n 1 -N 1 --share --nodelist=";
1168       std::string commandRcp = "rcp ";
1169       commandRcp += tmpFileName;
1170       commandRcp += " ";
1171       commandRcp += resInfo.HostName;
1172       commandRcp += ":";
1173       commandRcp += tmpFileName;
1174       status = SystemThreadSafe(commandRcp.c_str());
1175     }
1176   else
1177     throw SALOME_Exception("Unknown protocol");
1178
1179   if(status)
1180     throw SALOME_Exception("Error of connection on remote host");
1181
1182   command += resInfo.HostName;
1183   command += " ";
1184   command += tmpFileName;
1185
1186   SCRUTE(command);
1187
1188   return command;
1189
1190 }
1191
1192 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1193 {
1194   int status;
1195   std::string zeronode;
1196   std::string command;
1197   std::string tmpFile = BuildTemporaryFileName();
1198   const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1199   
1200   if(resInfo.Protocol == sh)
1201   {
1202     return resInfo.HostName;
1203   }
1204
1205   if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1206     {
1207       if (_isAppliSalomeDefined)
1208         {
1209
1210           if (resInfo.Protocol == rsh)
1211             command = "rsh ";
1212           else if (resInfo.Protocol == ssh)
1213             command = "ssh ";
1214           else if (resInfo.Protocol == srun)
1215             command = "srun -n 1 -N 1 --share --nodelist=";
1216           else
1217             throw SALOME_Exception("Unknown protocol");
1218
1219           if (resInfo.UserName != "")
1220             {
1221               command += "-l ";
1222               command += resInfo.UserName;
1223               command += " ";
1224             }
1225
1226           command += resInfo.HostName;
1227           command += " ";
1228
1229           if (resInfo.AppliPath != "")
1230             command += resInfo.AppliPath; // path relative to user@machine $HOME
1231           else
1232             {
1233               ASSERT(GetenvThreadSafe("APPLI"));
1234               command += GetenvThreadSafeAsString("APPLI"); // path relative to user@machine $HOME
1235             }
1236
1237           command += "/runRemote.sh ";
1238
1239           ASSERT(GetenvThreadSafe("NSHOST"));
1240           command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
1241
1242           command += " ";
1243           ASSERT(GetenvThreadSafe("NSPORT"));
1244           command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1245
1246           command += " mpirun -np 1 hostname -s > " + tmpFile;
1247         }
1248       else
1249         command = "mpirun -np 1 hostname -s > " + tmpFile;
1250     }
1251   else
1252     command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1253
1254   status = SystemThreadSafe(command.c_str());
1255   if( status == 0 ){
1256     std::ifstream fp(tmpFile.c_str(),std::ios::in);
1257     while(fp >> zeronode);
1258   }
1259
1260   RmTmpFile(tmpFile);
1261
1262   return zeronode;
1263 }
1264
1265 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1266 {
1267   std::string tmp;
1268   std::string nodesFile = GetenvThreadSafeAsString("LIBBATCH_NODEFILE");
1269   std::string machinesFile = Kernel_Utils::GetTmpFileName();
1270   std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1271   std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1272
1273   _numInstanceMutex.lock();
1274
1275   for(int i=0;i<_nbprocUsed;i++)
1276     fpi >> tmp;
1277
1278   for(int i=0;i<nbproc;i++)
1279     if( fpi >> tmp )
1280       fpo << tmp << std::endl;
1281     else
1282       throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1283
1284   _nbprocUsed += nbproc;
1285   fpi.close();
1286   fpo.close();
1287
1288   _numInstanceMutex.unlock();
1289
1290   return machinesFile;
1291
1292 }
1293
1294 std::set<pid_t> SALOME_ContainerManager::getpidofprogram(const std::string program)
1295 {
1296   std::set<pid_t> thepids;
1297   std::string tmpFile = Kernel_Utils::GetTmpFileName();
1298   std::string cmd;
1299   std::string thepid;
1300   cmd = "pidof " + program + " > " + tmpFile;
1301   SystemThreadSafe(cmd.c_str());
1302   std::ifstream fpi(tmpFile.c_str(),std::ios::in);
1303   while(fpi >> thepid){
1304     thepids.insert(atoi(thepid.c_str()));
1305   }
1306   return thepids;
1307 }
1308
1309 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1310                                                                   const std::string & hostname,
1311                                                                   const std::string & username)
1312 {
1313   std::ostringstream command;
1314   switch (protocol)
1315   {
1316   case rsh:
1317     command << "rsh ";
1318     if (username != "")
1319     {
1320       command << "-l " << username << " ";
1321     }
1322     command << hostname << " ";
1323     break;
1324   case ssh:
1325     command << "ssh ";
1326     if (username != "")
1327     {
1328       command << "-l " << username << " ";
1329     }
1330     command << hostname << " ";
1331     break;
1332   case srun:
1333     // no need to redefine the user with srun, the job user is taken by default
1334     // (note: for srun, user id can be specified with " --uid=<user>")
1335     command << "srun -n 1 -N 1 --share --nodelist=" << hostname << " ";
1336     break;
1337   case pbsdsh:
1338     command << "pbsdsh -o -h " << hostname << " ";
1339     break;
1340   case blaunch:
1341     command << "blaunch -no-shell " << hostname << " ";
1342     break;
1343   default:
1344     throw SALOME_Exception("Unknown protocol");
1345   }
1346
1347   return command.str();
1348 }
1349
1350 bool
1351 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1352 {
1353   bool result = true;
1354
1355   // Step 1 : check ContainerParameters
1356   // Check container_name, has to be defined
1357   if (std::string(params.container_name.in()) == "")
1358   {
1359     INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1360     result = false;
1361   }
1362   // Check parallelLib
1363   std::string parallelLib = params.parallelLib.in();
1364   if (parallelLib != "Mpi" && parallelLib != "Dummy")
1365   {
1366     INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1367     INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1368     INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1369     result = false;
1370   }
1371   // Check nb_proc
1372   if (params.nb_proc <= 0)
1373   {
1374     INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1375     result = false;
1376   }
1377
1378   // Step 2 : check resource_selected
1379   const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1380   //std::string protocol = resource_definition->protocol.in();
1381   std::string username = resource_definition.UserName;
1382   std::string applipath = resource_definition.AppliPath;
1383
1384   //if (protocol == "" || username == "" || applipath == "")
1385   if (username == "" || applipath == "")
1386   {
1387     INFOS("[checkPaCOParameters] resource selected is not well defined");
1388     INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1389     INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1390     INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1391     INFOS("[checkPaCOParameters] resource username: " << username);
1392     INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1393     result = false;
1394   }
1395
1396   return result;
1397 }
1398
1399 /*
1400  * :WARNING: Do not directly convert returned value to std::string
1401  * This function may return NULL if env variable is not defined.
1402  * And std::string(NULL) causes undefined behavior.
1403  * Use GetenvThreadSafeAsString to properly get a std::string.
1404 */
1405 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1406 {// getenv is not thread safe. See man 7 pthread.
1407   Utils_Locker lock (&_getenvMutex);
1408   return getenv(name);
1409 }
1410
1411 /*
1412  * Return env variable as a std::string.
1413  * Return empty string if env variable is not set.
1414  */
1415 std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
1416 {
1417   char* var = GetenvThreadSafe(name);
1418   return var ? std::string(var) : std::string();
1419 }
1420
1421 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1422 {
1423   Utils_Locker lock (&_systemMutex);
1424   return system(command);
1425 }
1426
1427 #ifdef WITH_PACO_PARALLEL
1428
1429 //=============================================================================
1430 /*! CORBA Method:
1431  *  Start a suitable PaCO++ Parallel Container in a list of machines.
1432  *  \param params           Container Parameters required for the container
1433  *  \return CORBA container reference.
1434  */
1435 //=============================================================================
1436 Engines::Container_ptr
1437 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1438                                               std::string resource_selected)
1439 {
1440   CORBA::Object_var obj;
1441   PaCO::InterfaceManager_var container_proxy;
1442   Engines::Container_ptr ret = Engines::Container::_nil();
1443   Engines::ContainerParameters params(params_const);
1444   params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1445
1446   // Step 0 : Check parameters
1447   if (!checkPaCOParameters(params, resource_selected))
1448   {
1449     INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1450     return ret;
1451   }
1452
1453   // Step 1 : Starting a new parallel container !
1454   INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1455   INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1456
1457   // Step 2 : Get a MachineFile for the parallel container
1458   std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1459                                                               params.nb_proc,
1460                                                               params.parallelLib.in());
1461
1462   if (machine_file_name == "")
1463   {
1464     INFOS("[StartPaCOPPContainer] Machine file generation failed");
1465     return ret;
1466   }
1467
1468   // Step 3 : starting parallel container proxy
1469   std::string command_proxy("");
1470   std::string proxy_machine;
1471   try
1472   {
1473     command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1474   }
1475   catch(const SALOME_Exception & ex)
1476   {
1477     INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1478     INFOS(ex.what());
1479     return ret;
1480   }
1481   obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1482   if (CORBA::is_nil(obj))
1483   {
1484     INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1485     return ret;
1486   }
1487   container_proxy = PaCO::InterfaceManager::_narrow(obj);
1488   MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1489
1490   // Step 4 : starting parallel container nodes
1491   std::string command_nodes("");
1492   SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1493   try
1494   {
1495     command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1496   }
1497   catch(const SALOME_Exception & ex)
1498   {
1499     INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1500     INFOS(ex.what());
1501     return ret;
1502   }
1503
1504   std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1505   bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1506   if (!result)
1507   {
1508     INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1509     // Il faut tuer le proxy
1510     try
1511     {
1512       Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1513       proxy->Shutdown();
1514     }
1515     catch (...)
1516     {
1517       INFOS("[StarPaCOPPContainer] Exception catched from proxy Shutdown...");
1518     }
1519     return ret;
1520   }
1521
1522   // Step 4 : connecting nodes and the proxy to actually create a parallel container
1523   for (int i = 0; i < params.nb_proc; i++)
1524   {
1525     std::ostringstream tmp;
1526     tmp << i;
1527     std::string proc_number = tmp.str();
1528     std::string container_node_name = container_generic_node_name + proc_number;
1529
1530     std::string theNodeMachine(nodes_machines[i]);
1531     std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1532     obj = _NS->Resolve(containerNameInNS.c_str());
1533     if (CORBA::is_nil(obj))
1534     {
1535       INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1536       INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1537       return ret;
1538     }
1539     try
1540     {
1541       MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1542       PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1543       node->deploy();
1544       MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1545     }
1546     catch(CORBA::SystemException& e)
1547     {
1548       INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1549       INFOS("CORBA::SystemException : " << e);
1550       return ret;
1551     }
1552     catch(CORBA::Exception& e)
1553     {
1554       INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1555       INFOS("CORBA::Exception" << e);
1556       return ret;
1557     }
1558     catch(...)
1559     {
1560       INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1561       INFOS("Unknown exception !");
1562       return ret;
1563     }
1564   }
1565
1566   // Step 5 : starting parallel container
1567   try
1568   {
1569     MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1570     container_proxy->start();
1571     MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1572     ret = Engines::Container::_narrow(container_proxy);
1573   }
1574   catch(CORBA::SystemException& e)
1575   {
1576     INFOS("Caught CORBA::SystemException. : " << e);
1577   }
1578   catch(PortableServer::POA::ServantAlreadyActive&)
1579   {
1580     INFOS("Caught CORBA::ServantAlreadyActiveException");
1581   }
1582   catch(CORBA::Exception&)
1583   {
1584     INFOS("Caught CORBA::Exception.");
1585   }
1586   catch(std::exception& exc)
1587   {
1588     INFOS("Caught std::exception - "<<exc.what());
1589   }
1590   catch(...)
1591   {
1592     INFOS("Caught unknown exception.");
1593   }
1594   return ret;
1595 }
1596
1597 std::string
1598 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1599                                                                 std::string machine_file_name,
1600                                                                 std::string & proxy_hostname)
1601 {
1602   // In the proxy case, we always launch a Dummy Proxy
1603   std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1604   std::string container_name = params.container_name.in();
1605
1606   // Convert nb_proc in string
1607   std::ostringstream tmp_string;
1608   tmp_string << params.nb_proc;
1609   std::string nb_proc_str = tmp_string.str();
1610
1611   // Get resource definition
1612   ParserResourcesType resource_definition =
1613       _resManager->GetResourceDefinition(params.resource_params.name.in());
1614
1615   // Choose hostname
1616   std::string hostname;
1617   std::ifstream machine_file(machine_file_name.c_str());
1618   std::getline(machine_file, hostname, ' ');
1619   size_t found = hostname.find('\n');
1620   if (found!=std::string::npos)
1621     hostname.erase(found, 1); // Remove \n
1622   proxy_hostname = hostname;
1623   MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1624
1625   // Remote execution
1626   bool remote_execution = false;
1627   if (hostname != std::string(Kernel_Utils::GetHostname()))
1628   {
1629     MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1630     remote_execution = true;
1631   }
1632
1633   // Log environnement
1634   std::string log_type("");
1635   char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1636   if (get_val)
1637     log_type = get_val;
1638
1639   // Generating the command
1640   std::string command_begin("");
1641   std::string command_end("");
1642   std::ostringstream command;
1643
1644   LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1645   command << command_begin;
1646
1647   // Adding connection command
1648   // We can only have a remote execution with
1649   // a SALOME application
1650   if (remote_execution)
1651   {
1652     ASSERT(GetenvThreadSafe("NSHOST"));
1653     ASSERT(GetenvThreadSafe("NSPORT"));
1654
1655     command << resource_definition.getAccessProtocolTypeStr();
1656     command << " -l ";
1657     command << resource_definition.UserName;
1658     command << " " << hostname;
1659     command << " " << resource_definition.AppliPath;
1660     command << "/runRemote.sh ";
1661     command << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1662     command << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1663   }
1664
1665   command << exe_name;
1666   command << " " << container_name;
1667   command << " Dummy";
1668   command << " " << hostname;
1669   command << " " << nb_proc_str;
1670   command << " -";
1671   AddOmninamesParams(command);
1672
1673   // Final command
1674   command << command_end;
1675   MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1676
1677   return command.str();
1678 }
1679
1680 std::string
1681 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1682                                                                const std::string & machine_file_name,
1683                                                                SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1684                                                                const std::string & proxy_hostname)
1685 {
1686   // Name of exe
1687   std::string exe_name = "SALOME_ParallelContainerNode";
1688   exe_name += params.parallelLib.in();
1689   std::string container_name = params.container_name.in();
1690
1691   // Convert nb_proc in string
1692   std::ostringstream nb_proc_stream;
1693   nb_proc_stream << params.nb_proc;
1694
1695   // Get resource definition
1696   ParserResourcesType resource_definition =
1697       _resManager->GetResourceDefinition(params.resource_params.name.in());
1698
1699   // Log environnement
1700   std::string log_type("");
1701   char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1702   if (get_val)
1703     log_type = get_val;
1704
1705   // Now the command is different according to paralleLib
1706   std::ostringstream command_nodes;
1707   std::ifstream machine_file(machine_file_name.c_str());
1708   if (std::string(params.parallelLib.in()) == "Dummy")
1709   {
1710     for (int i= 0; i < params.nb_proc; i++)
1711     {
1712       // Choose hostname
1713       std::string hostname;
1714       std::getline(machine_file, hostname);
1715       MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1716
1717       // Remote execution
1718       bool remote_execution = false;
1719       if (hostname != std::string(Kernel_Utils::GetHostname()))
1720       {
1721         MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1722         remote_execution = true;
1723       }
1724
1725       // For each node we have a new command
1726       // Generating the command
1727       std::ostringstream command_node_stream;
1728       std::string command_node_begin("");
1729       std::string command_node_end("");
1730       std::ostringstream node_number;
1731       node_number << i;
1732       std::string container_node_name = container_name + node_number.str();
1733       LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1734
1735       // Adding connection command
1736       // We can only have a remote execution with
1737       // a SALOME application
1738       if (remote_execution)
1739       {
1740         ASSERT(GetenvThreadSafe("NSHOST"));
1741         ASSERT(GetenvThreadSafe("NSPORT"));
1742
1743         command_node_stream << resource_definition.getAccessProtocolTypeStr();
1744         command_node_stream << " -l ";
1745         command_node_stream << resource_definition.UserName;
1746         command_node_stream << " " << hostname;
1747         command_node_stream << " " << resource_definition.AppliPath;
1748         command_node_stream << "/runRemote.sh ";
1749         command_node_stream << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1750         command_node_stream << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1751       }
1752
1753       command_node_stream << exe_name;
1754       command_node_stream << " " << container_name;
1755       command_node_stream << " " << params.parallelLib.in();
1756       command_node_stream << " " << proxy_hostname;
1757       command_node_stream << " " << node_number.str();
1758       command_node_stream << " -";
1759       AddOmninamesParams(command_node_stream);
1760
1761       command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1762       vect_machine.push_back(hostname);
1763     }
1764   }
1765
1766   else if (std::string(params.parallelLib.in()) == "Mpi")
1767   {
1768     // Choose hostname
1769     std::string hostname;
1770     std::getline(machine_file, hostname, ' ');
1771     MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1772
1773     // Remote execution
1774     bool remote_execution = false;
1775     if (hostname != std::string(Kernel_Utils::GetHostname()))
1776     {
1777       MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1778       remote_execution = true;
1779     }
1780
1781     // In case of Mpi and Remote, we copy machine_file in the applipath
1782     // scp mpi_machine_file user@machine:Path
1783     std::ostringstream command_remote_stream;
1784     std::string::size_type last = machine_file_name.find_last_of("/");
1785     if (last == std::string::npos)
1786       last = -1;
1787
1788     if (resource_definition.Protocol == rsh)
1789       command_remote_stream << "rcp ";
1790     else
1791       command_remote_stream << "scp ";
1792     command_remote_stream << machine_file_name << " ";
1793     command_remote_stream << resource_definition.UserName << "@";
1794     command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1795     command_remote_stream <<  "/" << machine_file_name.substr(last+1);
1796
1797     int status = SystemThreadSafe(command_remote_stream.str().c_str());
1798     if (status == -1)
1799     {
1800       INFOS("copy of the MPI machine file failed ! - sorry !");
1801       return "";
1802     }
1803
1804     // Generating the command
1805     std::string command_begin("");
1806     std::string command_end("");
1807
1808     LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1809     command_nodes << command_begin;
1810
1811     // Adding connection command
1812     // We can only have a remote execution with
1813     // a SALOME application
1814     if (remote_execution)
1815     {
1816       ASSERT(GetenvThreadSafe("NSHOST"));
1817       ASSERT(GetenvThreadSafe("NSPORT"));
1818
1819       command_nodes << resource_definition.getAccessProtocolTypeStr();
1820       command_nodes << " -l ";
1821       command_nodes << resource_definition.UserName;
1822       command_nodes << " " << hostname;
1823       command_nodes << " " << resource_definition.AppliPath;
1824       command_nodes << "/runRemote.sh ";
1825       command_nodes << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1826       command_nodes << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1827     }
1828
1829     if (resource_definition.mpi == lam)
1830     {
1831       command_nodes << "mpiexec -ssi boot ";
1832       command_nodes << "-machinefile "  << machine_file_name << " ";
1833       command_nodes <<  "-n " << params.nb_proc;
1834     }
1835     else
1836     {
1837       command_nodes << "mpirun -np " << params.nb_proc;
1838     }
1839     command_nodes << " " << exe_name;
1840     command_nodes << " " << container_name;
1841     command_nodes << " " << params.parallelLib.in();
1842     command_nodes << " " << proxy_hostname;
1843     command_nodes << " -";
1844     AddOmninamesParams(command_nodes);
1845
1846     // We don't put hostname, because nodes are registered in the resource of the proxy
1847     for (int i= 0; i < params.nb_proc; i++)
1848       vect_machine.push_back(proxy_hostname);
1849
1850     command_nodes << command_end;
1851   }
1852   return command_nodes.str();
1853 }
1854
1855 void
1856 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1857                                           const std::string & exe_type,
1858                                           const std::string & container_name,
1859                                           const std::string & hostname,
1860                                           std::string & begin,
1861                                           std::string & end)
1862 {
1863   if(log_type == "xterm")
1864   {
1865     begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1866     end   = "\"&";
1867   }
1868   else if(log_type == "xterm_debug")
1869   {
1870     begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1871     end   = "; cat \" &";
1872   }
1873   else
1874   {
1875     // default into a file...
1876     std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1877     std::string user = GetenvThreadSafeAsString("USER");
1878     if (user.empty())
1879       user = GetenvThreadSafeAsString("LOGNAME");
1880     logFilename += user + ".log";
1881     end = " > " + logFilename + " 2>&1 & ";
1882   }
1883 }
1884
1885 CORBA::Object_ptr
1886 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1887                                                   const Engines::ContainerParameters& params,
1888                                                   const std::string & hostname)
1889 {
1890   PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1891
1892   MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1893   int status = SystemThreadSafe(command.c_str());
1894   if (status == -1) {
1895     INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1896     return container_proxy;
1897   }
1898   else if (status == 217) {
1899     INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1900     return container_proxy;
1901   }
1902
1903   int count(GetTimeOutToLoaunchServer());
1904   CORBA::Object_var obj = CORBA::Object::_nil();
1905   std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1906                                                                hostname.c_str());
1907   MESSAGE("[LaunchParallelContainer]  Waiting for Parallel Container proxy : " << containerNameInNS);
1908
1909   while (CORBA::is_nil(obj) && count)
1910   {
1911     sleep(1);
1912     count--;
1913     obj = _NS->Resolve(containerNameInNS.c_str());
1914   }
1915
1916   try
1917   {
1918     container_proxy = PaCO::InterfaceManager::_narrow(obj);
1919   }
1920   catch(CORBA::SystemException& e)
1921   {
1922     INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1923     INFOS("CORBA::SystemException : " << e);
1924     return container_proxy;
1925   }
1926   catch(CORBA::Exception& e)
1927   {
1928     INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1929     INFOS("CORBA::Exception" << e);
1930     return container_proxy;
1931   }
1932   catch(...)
1933   {
1934     INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1935     INFOS("Unknown exception !");
1936     return container_proxy;
1937   }
1938   if (CORBA::is_nil(container_proxy))
1939   {
1940     INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1941     return container_proxy;
1942   }
1943   return obj._retn();
1944 }
1945
1946 //=============================================================================
1947 /*! This method launches the parallel container.
1948  *  It will may be placed on the ressources manager.
1949  *
1950  * \param command to launch
1951  * \param container's parameters
1952  * \param name of the container
1953  *
1954  * \return CORBA container reference
1955  */
1956 //=============================================================================
1957 bool
1958 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1959                                                  const Engines::ContainerParameters& params,
1960                                                  const std::string& name,
1961                                                  SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1962 {
1963   INFOS("[LaunchPaCONodeContainer] Launch command");
1964   int status = SystemThreadSafe(command.c_str());
1965   if (status == -1) {
1966     INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1967     return false;
1968   }
1969   else if (status == 217) {
1970     INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1971     return false;
1972   }
1973
1974   INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1975   // We are waiting all the nodes
1976   for (int i = 0; i < params.nb_proc; i++)
1977   {
1978     CORBA::Object_var obj = CORBA::Object::_nil();
1979     std::string theMachine(vect_machine[i]);
1980     // Name of the node
1981     std::ostringstream tmp;
1982     tmp << i;
1983     std::string proc_number = tmp.str();
1984     std::string container_node_name = name + proc_number;
1985     std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1986     INFOS("[LaunchPaCONodeContainer]  Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1987     int count(GetTimeOutToLoaunchServer());
1988     while (CORBA::is_nil(obj) && count) {
1989       SleepInSecond(1);
1990       count-- ;
1991       obj = _NS->Resolve(containerNameInNS.c_str());
1992     }
1993     if (CORBA::is_nil(obj))
1994     {
1995       INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
1996       return false;
1997     }
1998   }
1999   return true;
2000 }
2001
2002 #else
2003
2004 Engines::Container_ptr
2005 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
2006                                               std::string resource_selected)
2007 {
2008   Engines::Container_ptr ret = Engines::Container::_nil();
2009   INFOS("[StarPaCOPPContainer] is disabled !");
2010   INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
2011   return ret;
2012 }
2013
2014 std::string
2015 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
2016                                                                 std::string machine_file_name,
2017                                                                 std::string & proxy_hostname)
2018 {
2019   return "";
2020 }
2021
2022 std::string
2023 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
2024                                                                const std::string & machine_file_name,
2025                                                                SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
2026                                                                const std::string & proxy_hostname)
2027 {
2028   return "";
2029 }
2030 void
2031 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
2032                                           const std::string & exe_type,
2033                                           const std::string & container_name,
2034                                           const std::string & hostname,
2035                                           std::string & begin,
2036                                           std::string & end)
2037 {
2038 }
2039
2040 CORBA::Object_ptr
2041 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
2042                                                   const Engines::ContainerParameters& params,
2043                                                   const std::string& hostname)
2044 {
2045   CORBA::Object_ptr ret = CORBA::Object::_nil();
2046   return ret;
2047 }
2048
2049 bool
2050 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2051                         const Engines::ContainerParameters& params,
2052                         const std::string& name,
2053                         SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
2054 {
2055   return false;
2056 }
2057 #endif
2058