Salome HOME
merge from branch BR_For40_DSC tag mergeto_V4_1_0_maintainance_29may08
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "SALOME_ContainerManager.hxx"
21 #include "SALOME_NamingService.hxx"
22 #include "OpUtil.hxx"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifndef WNT
26 #include <unistd.h>
27 #endif
28 #include <vector>
29 #include "Utils_CorbaException.hxx"
30 #include "Batch_Date.hxx"
31
32 #ifdef WITH_PACO_PARALLEL
33 #include "PaCO++.h"
34 #endif
35
36 #define TIME_OUT_TO_LAUNCH_CONT 21
37
38 using namespace std;
39
40 vector<Engines::Container_ptr> SALOME_ContainerManager::_batchLaunchedContainers;
41
42 vector<Engines::Container_ptr>::iterator SALOME_ContainerManager::_batchLaunchedContainersIter;
43
44 const char *SALOME_ContainerManager::_ContainerManagerNameInNS = 
45   "/ContainerManager";
46
47 //=============================================================================
48 /*! 
49  *  Constructor
50  *  \param orb
51  *  Define a CORBA single thread policy for the server, which avoid to deal
52  *  with non thread-safe usage like Change_Directory in SALOME naming service
53  */
54 //=============================================================================
55
56 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_ResourcesManager *rm, SALOME_NamingService *ns)
57 {
58   MESSAGE("constructor");
59   _NS = ns;
60   _ResManager = rm;
61   _id=0;
62
63   PortableServer::POAManager_var pman = poa->the_POAManager();
64   _orb = CORBA::ORB::_duplicate(orb) ;
65   CORBA::PolicyList policies;
66   policies.length(1);
67   PortableServer::ThreadPolicy_var threadPol = 
68     poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
69   policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
70
71   _poa = poa->create_POA("SThreadPOA",pman,policies);
72   threadPol->destroy();
73   PortableServer::ObjectId_var id = _poa->activate_object(this);
74   CORBA::Object_var obj = _poa->id_to_reference(id);
75   Engines::ContainerManager_var refContMan =
76     Engines::ContainerManager::_narrow(obj);
77
78   _NS->Register(refContMan,_ContainerManagerNameInNS);
79   _MpiStarted = false;
80   _isAppliSalomeDefined = (getenv("APPLI") != 0);
81   MESSAGE("constructor end");
82 }
83
84 //=============================================================================
85 /*! 
86  * destructor
87  */
88 //=============================================================================
89
90 SALOME_ContainerManager::~SALOME_ContainerManager()
91 {
92   MESSAGE("destructor");
93 }
94
95 //=============================================================================
96 /*! CORBA method:
97  *  shutdown all the containers, then the ContainerManager servant
98  */
99 //=============================================================================
100
101 void SALOME_ContainerManager::Shutdown()
102 {
103   MESSAGE("Shutdown");
104   ShutdownContainers();
105   _NS->Destroy_Name(_ContainerManagerNameInNS);
106   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
107   _poa->deactivate_object(oid);
108   //_remove_ref() has already been done at creation
109   //_remove_ref();
110 }
111
112 //=============================================================================
113 /*! CORBA Method:
114  *  Loop on all the containers listed in naming service, ask shutdown on each
115  */
116 //=============================================================================
117
118 void SALOME_ContainerManager::ShutdownContainers()
119 {
120   MESSAGE("ShutdownContainers");
121   bool isOK;
122   isOK = _NS->Change_Directory("/Containers");
123   if( isOK ){
124     vector<string> vec = _NS->list_directory_recurs();
125     list<string> lstCont;
126     for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
127       SCRUTE((*iter));
128       CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
129       Engines::Container_var cont=Engines::Container::_narrow(obj);
130       if(!CORBA::is_nil(cont)){
131         lstCont.push_back((*iter));
132       }
133     }
134     MESSAGE("Container list: ");
135     for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
136       SCRUTE((*iter));
137     }
138     for(list<string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
139       SCRUTE((*iter));
140       CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
141       Engines::Container_var cont=Engines::Container::_narrow(obj);
142       if(!CORBA::is_nil(cont))
143         {
144           MESSAGE("ShutdownContainers: " << (*iter));
145           try
146             {
147               cont->Shutdown();
148             }
149           catch(CORBA::SystemException& e)
150             {
151               INFOS("CORBA::SystemException ignored : " << e);
152             }
153           catch(CORBA::Exception&)
154             {
155               INFOS("CORBA::Exception ignored.");
156             }
157           catch(...)
158             {
159               INFOS("Unknown exception ignored.");
160             }
161         }
162       else 
163         MESSAGE("ShutdownContainers: no container ref for " << (*iter));
164     }
165   }
166 }
167
168 //=============================================================================
169 /*! CORBA Method:
170  *  Find a suitable Container in a list of machines, or start one
171  *  \param params            Machine Parameters required for the container
172  *  \param possibleComputers list of machines usable for find or start
173  */
174 //=============================================================================
175
176 Engines::Container_ptr
177 SALOME_ContainerManager::
178 FindOrStartContainer(const Engines::MachineParameters& params,
179                      const Engines::MachineList& possibleComputers)
180 {
181   Engines::Container_ptr ret = FindContainer(params,possibleComputers);
182   if(!CORBA::is_nil(ret))
183     return ret;
184   MESSAGE("Container doesn't exist try to launch it ...");
185
186   return StartContainer(params,possibleComputers,Engines::P_FIRST);
187
188 }
189
190 //=============================================================================
191 /*! CORBA Method:
192  *  Start a suitable Container in a list of machines
193  *  \param params            Machine Parameters required for the container
194  *  \param possibleComputers list of machines usable for start
195  */
196 //=============================================================================
197
198 Engines::Container_ptr
199 SALOME_ContainerManager::
200 StartContainer(const Engines::MachineParameters& params,
201                const Engines::MachineList& possibleComputers,
202                Engines::ResPolicy policy)
203 {
204 #ifdef WITH_PACO_PARALLEL
205   std::string parallelLib(params.parallelLib);
206   if (parallelLib != "")
207     return FindOrStartParallelContainer(params, possibleComputers);
208 #endif
209   long id;
210   string containerNameInNS;
211   char idc[3*sizeof(long)];
212   Engines::Container_ptr ret = Engines::Container::_nil();
213
214   MESSAGE("SALOME_ContainerManager::StartContainer " <<
215           possibleComputers.length());
216
217   vector<string> lm;
218   for(int i=0;i<possibleComputers.length();i++)
219     lm.push_back(string(possibleComputers[i]));
220
221   string theMachine;
222   try{
223     switch(policy){
224     case Engines::P_FIRST:
225       theMachine=_ResManager->GetImpl()->FindFirst(lm);
226       break;
227     case Engines::P_CYCL:
228       theMachine=_ResManager->GetImpl()->FindNext(lm);
229       break;
230     case Engines::P_BEST:
231       theMachine=_ResManager->GetImpl()->FindBest(lm);
232       break;
233     }
234   }
235   catch( const SALOME_Exception &ex ){
236     MESSAGE(ex.what());
237     return Engines::Container::_nil();
238   }
239
240   //If the machine name is localhost use the real name
241   if(theMachine == "localhost")
242     theMachine=GetHostname();
243
244   MESSAGE("try to launch it on " << theMachine);
245
246   // Get Id for container: a parallel container registers in Naming Service
247   // on the machine where is process 0. ContainerManager does'nt know the name
248   // of this machine before the launch of the parallel container. So to get
249   // the IOR of the parallel container in Naming Service, ContainerManager
250   // gives a unique Id. The parallel container registers his name under
251   // /ContainerManager/Id directory in NamingService
252
253   id = GetIdForContainer();
254
255   string command;
256   if(theMachine==""){
257     MESSAGE("SALOME_ContainerManager::StartContainer : " <<
258             "no possible computer");
259     return Engines::Container::_nil();
260   }
261   else if(theMachine==GetHostname())
262     command = BuildCommandToLaunchLocalContainer(params,id);
263   else
264     command = BuildCommandToLaunchRemoteContainer(theMachine,params,id);
265
266   RmTmpFile();
267
268   //check if an entry exists in Naming service
269   if(params.isMPI)
270     {
271       containerNameInNS = "/ContainerManager/id";
272       sprintf(idc,"%ld",id);
273       containerNameInNS += idc;
274     }
275   else
276     containerNameInNS = _NS->BuildContainerNameForNS(params,theMachine.c_str());
277
278   SCRUTE(containerNameInNS);
279   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
280   if ( !CORBA::is_nil(obj) )
281     {
282       // unregister the registered container if it exists
283       _NS->Destroy_Name(containerNameInNS.c_str());
284       // unregister component instances ???
285       //Engines::Container_var cont=Engines::Container::_narrow(obj);
286     }
287
288   //redirect stdout and stderr in a file
289   string logFilename="/tmp/"+_NS->ContainerName(params)+"_"+ theMachine +"_"+getenv( "USER" )+".log" ;
290   command += " > " + logFilename + " 2>&1 &";
291
292   // launch container with a system call
293   int status=system(command.c_str());
294   if (status == -1){
295     MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
296             "(system command status -1)");
297     return Engines::Container::_nil();
298   }
299   else if (status == 217){
300     MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed " <<
301             "(system command status 217)");
302     return Engines::Container::_nil();
303   }
304   else{
305     int count=TIME_OUT_TO_LAUNCH_CONT;
306     MESSAGE("count = "<<count);
307     while ( CORBA::is_nil(ret) && count ){
308 #ifndef WNT
309       sleep( 1 ) ;
310 #else
311       Sleep(1000);
312 #endif
313       count-- ;
314       if ( count != 10 )
315         MESSAGE( count << ". Waiting for container on " << theMachine);
316
317       CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
318       ret=Engines::Container::_narrow(obj);
319     }
320     
321     if ( CORBA::is_nil(ret) )
322       {
323         MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed");
324       }
325     else
326       {
327         logFilename=":"+logFilename;
328         logFilename="@"+GetHostname()+logFilename;
329         logFilename=getenv( "USER" )+logFilename;
330         ret->logfilename(logFilename.c_str());
331       }
332
333     return ret;
334   }
335 }
336
337 //=============================================================================
338 /*! CORBA Method:
339  *  Start a suitable Container in a list of machines
340  *  \param params            Machine Parameters required for the container
341  *  \param possibleComputers list of machines usable for start
342  */
343 //=============================================================================
344
345 Engines::Container_ptr
346 SALOME_ContainerManager::
347 StartContainer(const Engines::MachineParameters& params,
348                Engines::ResPolicy policy,
349                const Engines::CompoList& componentList)
350 {
351   Engines::MachineList_var possibleComputers = _ResManager->GetFittingResources(params,componentList);
352   return StartContainer(params,possibleComputers,policy);
353 }
354
355 #ifdef WITH_PACO_PARALLEL
356 //=============================================================================
357 /*! CORBA Method:
358  *  Find or Start a suitable PaCO++ Parallel Container in a list of machines.
359  *  \param params            Machine Parameters required for the container
360  *  \param possibleComputers list of machines usable for find or start
361  *
362  *  \return CORBA container reference.
363  */
364 //=============================================================================
365 Engines::Container_ptr
366 SALOME_ContainerManager::
367 FindOrStartParallelContainer(const Engines::MachineParameters& params_const,
368                              const Engines::MachineList& possibleComputers)
369 {
370   CORBA::Object_var obj;
371   PaCO::InterfaceManager_var proxy;
372   Engines::Container_ptr ret = Engines::Container::_nil();
373   Engines::MachineParameters params(params_const);
374
375   // Step 1 : Try to find a suitable container
376   // Currently not as good as could be since
377   // we have to verified the number of nodes of the container
378   // if a user tell that.
379   ret = FindContainer(params, possibleComputers);
380
381   if(CORBA::is_nil(ret)) {
382     // Step 2 : Starting a new parallel container
383     INFOS("[FindOrStartParallelContainer] Starting a parallel container");
384
385     // Step 2.1 : Choose a computer
386     string theMachine = _ResManager->FindFirst(possibleComputers);
387     if(theMachine == "") {
388       INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
389       INFOS("[FindOrStartParallelContainer] No possible computer found");
390       INFOS("[FindOrStartParallelContainer] !!!!!!!!!!!!!!!!!!!!!!!!!!");
391     }
392     else {
393       INFOS("[FindOrStartParallelContainer] on machine : " << theMachine);
394       string command;
395       if(theMachine == GetHostname()) {
396         // Step 3 : starting parallel container proxy
397         params.hostname = CORBA::string_dup(theMachine.c_str());
398         Engines::MachineParameters params_proxy(params);
399         try {
400           command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerProxy", params_proxy, "xterm");
401         }
402         catch(const SALOME_Exception & ex){
403           MESSAGE(ex.what());
404           return Engines::Container::_nil();
405         }
406         // LaunchParallelContainer uses this value to know if it launches the proxy or the nodes
407         params_proxy.nb_component_nodes = 0;
408         obj = LaunchParallelContainer(command, params_proxy, _NS->ContainerName(params));
409         ret = Engines::Container::_narrow(obj);
410         proxy = PaCO::InterfaceManager::_narrow(obj);
411
412         // Step 4 : starting parallel container nodes
413         command = BuildCommandToLaunchLocalParallelContainer("SALOME_ParallelContainerNode", params, "xterm");
414         string name = _NS->ContainerName(params) + "Node";
415         LaunchParallelContainer(command, params, name);
416         // Step 5 : connecting nodes and the proxy to actually create a parallel container
417         try {
418           for (int i = 0; i < params.nb_component_nodes; i++) {
419
420             char buffer [5];
421 #ifndef WNT
422             snprintf(buffer,5,"%d",i);
423 #else
424             _snprintf(buffer,5,"%d",i);
425 #endif
426             string name_cont = name + string(buffer);
427
428             string theNodeMachine(CORBA::string_dup(params.hostname));
429             string containerNameInNS = _NS->BuildContainerNameForNS(name_cont.c_str(),theNodeMachine.c_str());
430             int count = TIME_OUT_TO_LAUNCH_CONT;
431             obj = _NS->Resolve(containerNameInNS.c_str());
432             while (CORBA::is_nil(obj) && count) {
433               INFOS("[FindOrStartParallelContainer] CONNECTION FAILED !!!!!!!!!!!!!!!!!!!!!!!!");
434 #ifndef WNT
435               sleep(1) ;
436 #else
437               Sleep(1000);
438 #endif
439               count-- ;
440               obj = _NS->Resolve(containerNameInNS.c_str());
441             }
442
443             PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
444             MESSAGE("[FindOrStartParallelContainer] Deploying node : " << name);
445             node->deploy();
446           }
447           proxy->start();
448         }
449         catch(CORBA::SystemException& e)
450         {
451           INFOS("Caught CORBA::SystemException. : " << e);
452         }
453         catch(PortableServer::POA::ServantAlreadyActive&)
454         {
455           INFOS("Caught CORBA::ServantAlreadyActiveException");
456         }
457         catch(CORBA::Exception&)
458         {
459           INFOS("Caught CORBA::Exception.");
460         }
461         catch(std::exception& exc)
462         {
463           INFOS("Caught std::exception - "<<exc.what()); 
464         }
465         catch(...)
466         {
467           INFOS("Caught unknown exception.");
468         }
469         INFOS("[FindOrStartParallelContainer] node " << name << " deployed");
470       }
471       else {
472         INFOS("[FindOrStartParallelContainer] Currently parallel containers are launched only on the local host");
473       }
474     }
475 }
476 return ret;
477 }
478 #else
479 //=============================================================================
480 /*! CORBA Method:
481  *  Find or Start a suitable PaCO++ Parallel Container in a list of machines.
482  *  \param params            Machine Parameters required for the container
483  *  \param possibleComputers list of machines usable for find or start
484  *
485  *  \return CORBA container reference.
486  */
487 //=============================================================================
488 Engines::Container_ptr
489 SALOME_ContainerManager::
490 FindOrStartParallelContainer(const Engines::MachineParameters& params,
491                              const Engines::MachineList& possibleComputers)
492 {
493   Engines::Container_ptr ret = Engines::Container::_nil();
494   INFOS("[FindOrStartParallelContainer] is disabled !");
495   INFOS("[FindOrStartParallelContainer] recompile SALOME Kernel to enable parallel extension");
496   return ret;
497 }
498 #endif
499
500 //=============================================================================
501 /*! CORBA Method:
502  *  Give a suitable Container in a list of machines
503  *  \param params            Machine Parameters required for the container
504  *  \param possibleComputers list of machines usable for start
505  */
506 //=============================================================================
507
508 Engines::Container_ptr
509 SALOME_ContainerManager::
510 GiveContainer(const Engines::MachineParameters& params,
511                Engines::ResPolicy policy,
512                const Engines::CompoList& componentList)
513 {
514   char *valenv=getenv("SALOME_BATCH");
515   if(valenv)
516     if (strcmp(valenv,"1")==0)
517       {
518         if(_batchLaunchedContainers.empty())
519           fillBatchLaunchedContainers();
520
521         if (_batchLaunchedContainersIter == _batchLaunchedContainers.end())
522           _batchLaunchedContainersIter = _batchLaunchedContainers.begin();
523
524         Engines::Container_ptr rtn = Engines::Container::_duplicate(*_batchLaunchedContainersIter);
525         _batchLaunchedContainersIter++;
526         return rtn;
527       }
528   return StartContainer(params,policy,componentList);
529 }
530
531 //=============================================================================
532 /*! 
533  * 
534  */
535 //=============================================================================
536
537 Engines::Container_ptr
538 SALOME_ContainerManager::
539 FindContainer(const Engines::MachineParameters& params,
540               const char *theMachine)
541 {
542   string containerNameInNS(_NS->BuildContainerNameForNS(params,theMachine));
543   CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
544   if( !CORBA::is_nil(obj) )
545     return Engines::Container::_narrow(obj);
546   else
547     return Engines::Container::_nil();
548 }
549
550 //=============================================================================
551 /*! 
552  * 
553  */
554 //=============================================================================
555
556 Engines::Container_ptr
557 SALOME_ContainerManager::
558 FindContainer(const Engines::MachineParameters& params,
559               const Engines::MachineList& possibleComputers)
560 {
561   MESSAGE("FindContainer "<<possibleComputers.length());
562   for(unsigned int i=0;i<possibleComputers.length();i++)
563     {
564       MESSAGE("FindContainer possible " << possibleComputers[i]);
565       Engines::Container_ptr cont = FindContainer(params,possibleComputers[i]);
566       if( !CORBA::is_nil(cont) )
567         return cont;
568     }
569   MESSAGE("FindContainer: not found");
570   return Engines::Container::_nil();
571 }
572
573 //=============================================================================
574 /*! This method launches the parallel container.
575  *  It will may be placed on the ressources manager.
576  *
577  * \param command to launch
578  * \param container's parameters
579  * \param name of the container
580  *
581  * \return CORBA container reference
582  */
583 //=============================================================================
584 CORBA::Object_ptr 
585 SALOME_ContainerManager::LaunchParallelContainer(const std::string& command, 
586                                                  const Engines::MachineParameters& params,
587                                                  const std::string& name)
588 {
589   CORBA::Object_ptr obj = CORBA::Object::_nil();
590   string containerNameInNS;
591   MESSAGE("[LaunchParallelContainer] : command to launch...");
592   MESSAGE(command);
593   if (params.nb_component_nodes == 0) {
594     INFOS("[LaunchParallelContainer] launching the proxy of the parallel container");
595     int status = system(command.c_str());
596     if (status == -1) {
597       INFOS("[LaunchParallelContainer] failed : system command status -1");
598     }
599     else if (status == 217) {
600       INFOS("[LaunchParallelContainer] failed : system command status 217");
601     }
602
603     int count = TIME_OUT_TO_LAUNCH_CONT;
604     string theMachine(CORBA::string_dup(params.hostname));
605     containerNameInNS = _NS->BuildContainerNameForNS((char*) name.c_str(),theMachine.c_str());
606
607     INFOS("[LaunchParallelContainer]  Waiting for Parallel Container proxy on " << theMachine);
608     while (CORBA::is_nil(obj) && count) {
609 #ifndef WNT
610       sleep(1) ;
611 #else
612       Sleep(1000);
613 #endif
614       count-- ;
615       obj = _NS->Resolve(containerNameInNS.c_str());
616     }
617   }
618   else {
619     INFOS("[LaunchParallelContainer] launching the nodes of the parallel container");
620     int status = system(command.c_str());
621     if (status == -1) {
622       INFOS("[LaunchParallelContainer] failed : system command status -1");
623     }
624     else if (status == 217) {
625       INFOS("[LaunchParallelContainer] failed : system command status 217");
626     }
627     // We are waiting all the nodes
628     for (int i = 0; i < params.nb_component_nodes; i++) {
629       obj = CORBA::Object::_nil();
630       int count = TIME_OUT_TO_LAUNCH_CONT;
631
632       // Name of the node
633       char buffer [5];
634 #ifndef WNT
635       snprintf(buffer,5,"%d",i);
636 #else
637       _snprintf(buffer,5,"%d",i);
638 #endif
639
640       string name_cont = name + string(buffer);
641
642       // I don't like this...
643       string theMachine(CORBA::string_dup(params.hostname));
644       containerNameInNS = _NS->BuildContainerNameForNS((char*) name_cont.c_str(),theMachine.c_str());
645       cerr << "[LaunchContainer]  Waiting for Parllel Container node " << containerNameInNS << " on " << theMachine << endl;
646       while (CORBA::is_nil(obj) && count) {
647 #ifndef WNT
648         sleep(1) ;
649 #else
650         Sleep(1000);
651 #endif
652         count-- ;
653         obj = _NS->Resolve(containerNameInNS.c_str());
654       }
655     }
656   }
657
658   if ( CORBA::is_nil(obj) ) {
659     INFOS("[LaunchParallelContainer] failed");
660   }
661   return obj;
662 }
663
664 //=============================================================================
665 /*! 
666  * Get Id for container: a parallel container registers in Naming Service
667  * on the machine where is process 0. ContainerManager does'nt know the name
668  * of this machine before the launch of the parallel container. So to get
669  * the IOR of the parallel container in Naming Service, ContainerManager
670  * gives a unique Id. The parallel container registers his name under
671  * /ContainerManager/Id directory in NamingService
672  */
673 //=============================================================================
674
675
676 long SALOME_ContainerManager::GetIdForContainer(void)
677 {
678   _id++;
679   return _id;
680 }
681
682 void SALOME_ContainerManager::fillBatchLaunchedContainers()
683 {
684   _batchLaunchedContainers.clear();
685   _NS->Change_Directory("/Containers");
686   vector<string> vec = _NS->list_directory_recurs();
687   for(vector<string>::iterator iter = vec.begin();iter!=vec.end();iter++){
688     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
689     Engines::Container_ptr cont=Engines::Container::_narrow(obj);
690     if(!CORBA::is_nil(cont)){
691       _batchLaunchedContainers.push_back(cont);
692     }
693   }
694   _batchLaunchedContainersIter=_batchLaunchedContainers.begin();
695 }
696
697 //=============================================================================
698 /*!
699  *  This is no longer valid (C++ container are also python containers)
700  */ 
701 //=============================================================================
702
703 bool isPythonContainer(const char* ContainerName)
704 {
705   bool ret = false;
706   int len = strlen(ContainerName);
707
708   if (len >= 2)
709     if (strcmp(ContainerName + len - 2, "Py") == 0)
710       ret = true;
711
712   return ret;
713 }
714
715 //=============================================================================
716 /*!
717  *  Builds the script to be launched
718  *
719  *  If SALOME Application not defined ($APPLI),
720  *  see BuildTempFileToLaunchRemoteContainer()
721  *
722  *  Else rely on distant configuration. Command is under the form (example):
723  *  ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
724  *                   SALOME_Container containerName &"
725
726  *  - where user is ommited if not specified in CatalogResources,
727  *  - where distant path is always relative to user@machine $HOME, and
728  *    equal to $APPLI if not specified in CatalogResources,
729  *  - where hostNS is the hostname of CORBA naming server (set by scripts to
730  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
731  *  - where portNS is the port used by CORBA naming server (set by scripts to
732  *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
733  *  - where workingdir is the requested working directory for the container.
734  *    If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
735  */ 
736 //=============================================================================
737
738 string
739 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer
740 (const string& machine,
741  const Engines::MachineParameters& params, const long id)
742 {
743   string command;
744   int nbproc;
745   char idc[3*sizeof(long)];
746           
747   if ( ! _isAppliSalomeDefined )
748     command = BuildTempFileToLaunchRemoteContainer(machine, params);
749
750   else
751     {
752       const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
753
754       if (params.isMPI)
755         {
756           if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
757             nbproc = 1;
758           else if ( params.nb_node == 0 )
759             nbproc = params.nb_proc_per_node;
760           else if ( params.nb_proc_per_node == 0 )
761             nbproc = params.nb_node;
762           else
763             nbproc = params.nb_node * params.nb_proc_per_node;
764         }
765
766       // "ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
767       //  SALOME_Container containerName &"
768
769       if (resInfo.Protocol == rsh)
770         command = "rsh ";
771       else if (resInfo.Protocol == ssh)
772         command = "ssh ";
773       else
774         throw SALOME_Exception("Unknown protocol");
775
776       if (resInfo.UserName != "")
777         {
778           command += resInfo.UserName;
779           command += "@";
780         }
781
782       command += machine;
783       command += " ";
784
785       if (resInfo.AppliPath != "")
786         command += resInfo.AppliPath; // path relative to user@machine $HOME
787       else
788         {
789           ASSERT(getenv("APPLI"));
790           command += getenv("APPLI"); // path relative to user@machine $HOME
791         }
792
793       command += "/runRemote.sh ";
794
795       ASSERT(getenv("NSHOST")); 
796       command += getenv("NSHOST"); // hostname of CORBA name server
797
798       command += " ";
799       ASSERT(getenv("NSPORT"));
800       command += getenv("NSPORT"); // port of CORBA name server
801
802       std::string wdir=params.workingdir.in();
803       if(wdir != "")
804         {
805           command += " WORKINGDIR ";
806           command += " '";
807           if(wdir == "$TEMPDIR")
808             wdir="\\$TEMPDIR";
809           command += wdir; // requested working directory
810           command += "'"; 
811         }
812
813       if(params.isMPI)
814         {
815           command += " mpirun -np ";
816           std::ostringstream o;
817           o << nbproc << " ";
818           command += o.str();
819 #ifdef WITHLAM
820           command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
821 #endif  
822           command += " SALOME_MPIContainer ";
823         }
824       else
825         command += " SALOME_Container ";
826
827       command += _NS->ContainerName(params);
828       command += " -id ";
829       sprintf(idc,"%ld",id);
830       command += idc;
831       command += " -";
832       AddOmninamesParams(command);
833
834       MESSAGE("command =" << command);
835     }
836
837   return command;
838 }
839
840 //=============================================================================
841 /*!
842  *  builds the command to be launched.
843  */ 
844 //=============================================================================
845
846 string
847 SALOME_ContainerManager::BuildCommandToLaunchLocalContainer
848 (const Engines::MachineParameters& params, const long id)
849 {
850   _TmpFileName = "";
851   string command;
852   int nbproc = 0;
853   char idc[3*sizeof(long)];
854
855   if (params.isMPI)
856     {
857       command = "mpirun -np ";
858
859       if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
860         nbproc = 1;
861       else if ( params.nb_node == 0 )
862         nbproc = params.nb_proc_per_node;
863       else if ( params.nb_proc_per_node == 0 )
864         nbproc = params.nb_node;
865       else
866         nbproc = params.nb_node * params.nb_proc_per_node;
867
868       std::ostringstream o;
869
870       o << nbproc << " ";
871
872       command += o.str();
873 #ifdef WITHLAM
874       command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
875 #endif
876
877       if (isPythonContainer(params.container_name))
878         command += "pyMPI SALOME_ContainerPy.py ";
879       else
880         command += "SALOME_MPIContainer ";
881     }
882
883   else
884     {
885       command="";
886       std::string wdir=params.workingdir.in();
887       if(wdir != "")
888         {
889           // a working directory is requested
890           if(wdir == "$TEMPDIR")
891             {
892               // a new temporary directory is requested
893               char dir[]="/tmp/salomeXXXXXX";
894               char* mdir=mkdtemp(dir);
895               if(mdir==NULL)
896                 std::cerr << "Problem in mkdtemp " << dir << " " << mdir << std::endl;
897               else
898                 command="cd "+std::string(dir)+";";
899             }
900           else
901             {
902               // a permanent directory is requested use it or create it
903               command="mkdir -p " + wdir + " && cd " + wdir + ";";
904             }
905         }
906       if (isPythonContainer(params.container_name))
907         command += "SALOME_ContainerPy.py ";
908       else
909         command += "SALOME_Container ";
910     }
911
912   command += _NS->ContainerName(params);
913   command += " -id ";
914   sprintf(idc,"%ld",id);
915   command += idc;
916   command += " -";
917   AddOmninamesParams(command);
918
919   MESSAGE("Command is ... " << command);
920   return command;
921 }
922
923
924 //=============================================================================
925 /*!
926  *  removes the generated temporary file in case of a remote launch.
927  */ 
928 //=============================================================================
929
930 void SALOME_ContainerManager::RmTmpFile()
931 {
932   if (_TmpFileName != "")
933     {
934 #ifndef WNT
935       string command = "rm ";
936 #else
937       string command = "del /F ";
938 #endif
939       command += _TmpFileName;
940       char *temp = strdup(command.c_str());
941       int lgthTemp = strlen(temp);
942       temp[lgthTemp - 3] = '*';
943       temp[lgthTemp - 2] = '\0';
944       system(temp);
945       free(temp);
946     }
947 }
948
949 //=============================================================================
950 /*!
951  *   add to command all options relative to naming service.
952  */ 
953 //=============================================================================
954
955 void SALOME_ContainerManager::AddOmninamesParams(string& command) const
956   {
957     CORBA::String_var iorstr = _NS->getIORaddr();
958     command += "ORBInitRef NameService=";
959     command += iorstr;
960   }
961
962
963 //=============================================================================
964 /*!
965  *  add to command all options relative to naming service.
966  */ 
967 //=============================================================================
968
969 void SALOME_ContainerManager::AddOmninamesParams(ofstream& fileStream) const
970   {
971     CORBA::String_var iorstr = _NS->getIORaddr();
972     fileStream << "ORBInitRef NameService=";
973     fileStream << iorstr;
974   }
975
976 //=============================================================================
977 /*!
978  *  generate a file name in /tmp directory
979  */ 
980 //=============================================================================
981
982 string SALOME_ContainerManager::BuildTemporaryFileName() const
983   {
984     //build more complex file name to support multiple salome session
985     char *temp = new char[19];
986     strcpy(temp, "/tmp/command");
987     strcat(temp, "XXXXXX");
988 #ifndef WNT
989
990     mkstemp(temp);
991 #else
992
993     char aPID[80];
994     itoa(getpid(), aPID, 10);
995     strcat(temp, aPID);
996 #endif
997
998     string command(temp);
999     delete [] temp;
1000     command += ".sh";
1001     return command;
1002   }
1003
1004
1005 //=============================================================================
1006 /*!
1007  *  Builds in a temporary file the script to be launched.
1008  *  
1009  *  Used if SALOME Application ($APPLI) is not defined.
1010  *  The command is build with data from CatalogResources, in which every path
1011  *  used on remote computer must be defined.
1012  */ 
1013 //=============================================================================
1014
1015 string
1016 SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer
1017 (const string& machine,
1018  const Engines::MachineParameters& params) throw(SALOME_Exception)
1019 {
1020   int status;
1021
1022   _TmpFileName = BuildTemporaryFileName();
1023   ofstream tempOutputFile;
1024   tempOutputFile.open(_TmpFileName.c_str(), ofstream::out );
1025   const ParserResourcesType& resInfo = _ResManager->GetImpl()->GetResourcesList(machine);
1026   tempOutputFile << "#! /bin/sh" << endl;
1027
1028   // --- set env vars
1029
1030   tempOutputFile << "export SALOME_trace=local" << endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1031   //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1032
1033   // ! env vars
1034
1035   if (params.isMPI)
1036     {
1037       tempOutputFile << "mpirun -np ";
1038       int nbproc;
1039
1040       if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
1041         nbproc = 1;
1042       else if ( params.nb_node == 0 )
1043         nbproc = params.nb_proc_per_node;
1044       else if ( params.nb_proc_per_node == 0 )
1045         nbproc = params.nb_node;
1046       else
1047         nbproc = params.nb_node * params.nb_proc_per_node;
1048
1049       std::ostringstream o;
1050
1051       tempOutputFile << nbproc << " ";
1052 #ifdef WITHLAM
1053       tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1054 #endif
1055     }
1056
1057   tempOutputFile << getenv("KERNEL_ROOT_DIR") << "/bin/salome/";
1058
1059   if (params.isMPI)
1060     {
1061       if (isPythonContainer(params.container_name))
1062         tempOutputFile << "pyMPI SALOME_ContainerPy.py ";
1063       else
1064         tempOutputFile << "SALOME_MPIContainer ";
1065     }
1066
1067   else
1068     {
1069       if (isPythonContainer(params.container_name))
1070         tempOutputFile << "SALOME_ContainerPy.py ";
1071       else
1072         tempOutputFile << "SALOME_Container ";
1073     }
1074
1075   tempOutputFile << _NS->ContainerName(params) << " -";
1076   AddOmninamesParams(tempOutputFile);
1077   tempOutputFile << " &" << endl;
1078   tempOutputFile.flush();
1079   tempOutputFile.close();
1080   chmod(_TmpFileName.c_str(), 0x1ED);
1081
1082   // --- Build command
1083
1084   string command;
1085
1086   if (resInfo.Protocol == rsh)
1087     {
1088       command = "rsh ";
1089       string commandRcp = "rcp ";
1090       commandRcp += _TmpFileName;
1091       commandRcp += " ";
1092       commandRcp += machine;
1093       commandRcp += ":";
1094       commandRcp += _TmpFileName;
1095       status = system(commandRcp.c_str());
1096     }
1097
1098   else if (resInfo.Protocol == ssh)
1099     {
1100       command = "ssh ";
1101       string commandRcp = "scp ";
1102       commandRcp += _TmpFileName;
1103       commandRcp += " ";
1104       commandRcp += machine;
1105       commandRcp += ":";
1106       commandRcp += _TmpFileName;
1107       status = system(commandRcp.c_str());
1108     }
1109   else
1110     throw SALOME_Exception("Unknown protocol");
1111
1112   if(status)
1113     throw SALOME_Exception("Error of connection on remote host");    
1114
1115   command += machine;
1116   _CommandForRemAccess = command;
1117   command += " ";
1118   command += _TmpFileName;
1119
1120   SCRUTE(command);
1121
1122   return command;
1123
1124 }
1125
1126 //=============================================================================
1127 /*! Creates a command line that the container manager uses to launch
1128  * a parallel container.
1129  */ 
1130 //=============================================================================
1131 string 
1132 SALOME_ContainerManager::BuildCommandToLaunchLocalParallelContainer(const std::string& exe_name,
1133                                                                     const Engines::MachineParameters& params,
1134                                                                     const std::string& log)
1135 {
1136   // This method knows the differences between the proxy and the nodes.
1137   // nb_component_nodes is not used in the same way if it is a proxy or 
1138   // a node.
1139
1140   string command;
1141   string parallelLib(CORBA::string_dup(params.parallelLib));
1142   string hostname(CORBA::string_dup(params.hostname));
1143   int par = exe_name.find("Proxy");
1144   int nbproc = params.nb_component_nodes;
1145   char buffer [33];
1146   sprintf(buffer,"%d",nbproc);
1147
1148   Engines::MachineParameters_var rtn = new Engines::MachineParameters();
1149   rtn->container_name = params.container_name;
1150   rtn->hostname = params.hostname;
1151   rtn->OS = params.OS;
1152   rtn->mem_mb = params.mem_mb;
1153   rtn->cpu_clock = params.cpu_clock;
1154   rtn->nb_proc_per_node = params.nb_proc_per_node;
1155   rtn->nb_node = params.nb_node;
1156   rtn->isMPI = params.isMPI;
1157
1158   string real_exe_name  = exe_name + parallelLib;
1159
1160   if (parallelLib == "Dummy")
1161   {
1162     //command = "gdb --args ";
1163     //command = "valgrind --tool=memcheck --log-file=val_log ";
1164     //command += real_exe_name;
1165
1166     command = real_exe_name;
1167
1168     command += " " + _NS->ContainerName(rtn);
1169     command += " " + parallelLib;
1170     command += " " + hostname;
1171     command += " -";
1172     AddOmninamesParams(command);
1173   }
1174
1175   else if (parallelLib == "Mpi")
1176   {
1177     // Step 1 : check if MPI is started
1178     if (_MpiStarted == false)
1179     {
1180       startMPI();
1181     }
1182
1183     if (par < 0)
1184     {
1185       // Nodes case
1186
1187       command = "mpiexec -np " + string(buffer) + " ";
1188 //      command += "gdb --args ";
1189       command += real_exe_name;
1190       command += " " + _NS->ContainerName(rtn);
1191       command += " " + parallelLib;
1192       command += " " + hostname;
1193       command += " -";
1194       AddOmninamesParams(command);
1195     }
1196     else                                          
1197     {
1198       // Proxy case
1199       command = "mpiexec -np 1 ";
1200       command += real_exe_name;
1201       command += " " + _NS->ContainerName(rtn);
1202       command += " " + string(buffer);
1203       command += " " + parallelLib;
1204       command += " " + hostname;
1205       command += " -";
1206       AddOmninamesParams(command);
1207     }
1208   }
1209   else
1210   {
1211     std::string message("Unknown parallelLib" + parallelLib);
1212     throw SALOME_Exception(message.c_str());
1213   }
1214
1215   // log choice
1216   if (log == "default")
1217   {
1218     command += " > /tmp/";
1219     command += _NS->ContainerName(rtn);
1220     command += "_";
1221     command += GetHostname();
1222     command += "_";
1223     command += getenv( "USER" ) ;
1224     command += ".log 2>&1 &" ;
1225   }
1226   if (log == "xterm")
1227   {
1228     command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;  " 
1229               + command + " \" &";
1230 //            + command + "; echo $LD_LIBRARY_PATH; cat \" &";
1231   }
1232   return command;
1233
1234 /*  if (log == "xterm")
1235   {
1236     command = "/usr/X11R6/bin/xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH; echo $LD_LIBRARY_PATH; echo $PATH; " + command + "; cat \" &";
1237   }
1238 */
1239 /*  command = "cd ; rm " + fichier_commande + "; touch " + \
1240              fichier_commande + "; echo \" export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; " + \
1241              command + " >& /tmp/ribes_" + fichier_commande + " & \" > " + fichier_commande + ";";
1242   command += "ssh cn01 sh " + fichier_commande + " &";
1243   cerr << "La commande : " << command << endl;
1244 */
1245 }
1246
1247 void SALOME_ContainerManager::startMPI()
1248 {
1249   cerr << "----------------------------------------------" << endl;
1250   cerr << "----------------------------------------------" << endl;
1251   cerr << "----------------------------------------------" << endl;
1252   cerr << "-Only Lam on Localhost is currently supported-" << endl;
1253   cerr << "----------------------------------------------" << endl;
1254   cerr << "----------------------------------------------" << endl;
1255   cerr << "----------------------------------------------" << endl;
1256
1257   int status = system("lamboot");
1258   if (status == -1)
1259   {
1260     INFOS("lamboot failed : system command status -1");
1261   }
1262   else if (status == 217)
1263   {
1264     INFOS("lamboot failed : system command status 217");
1265   }
1266   else
1267   {
1268     _MpiStarted = true;
1269   }
1270 }
1271