Salome HOME
- Adding a new method to SALOME_ResourceManager:
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesCatalog_Handler.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME ResourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
23 //  File   : SALOME_ResourcesCatalog_Handler.cxx
24 //  Author : Estelle Deville
25 //  Module : SALOME
26 //$Header$
27 //
28 #include "SALOME_ResourcesCatalog_Handler.hxx"
29 #include "Basics_Utils.hxx"
30 #include <iostream>
31 #include <sstream>
32 #include <map>
33
34 using namespace std;
35
36 //=============================================================================
37 /*!
38  *  Constructor
39  *  \param listOfResources: map of ParserResourcesType to fill when parsing
40  */ 
41 //=============================================================================
42
43 SALOME_ResourcesCatalog_Handler::
44 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& resources_list): _resources_list(resources_list)
45 {
46   //XML tags initialisation
47   test_machine = "machine";
48   test_cluster = "cluster";
49   test_name = "name";
50   test_hostname = "hostname";
51   test_protocol = "protocol";
52   test_cluster_internal_protocol = "iprotocol";
53   test_mode = "mode";
54   test_batch = "batch";
55   test_mpi = "mpi";
56   test_user_name = "userName";
57   test_appli_path = "appliPath";
58   test_modules = "modules";
59   test_module_name = "moduleName";
60   test_components = "component";
61   test_component_name = "name";
62   test_os = "OS";
63   test_mem_in_mb = "memInMB";
64   test_cpu_freq_mhz = "CPUFreqMHz";
65   test_nb_of_nodes = "nbOfNodes";
66   test_nb_of_proc = "nbOfProc";
67   test_nb_of_proc_per_node = "nbOfProcPerNode";
68   test_batch_queue = "batchQueue";
69   test_user_commands = "userCommands";
70   test_use = "use";
71   test_members = "members";
72 }
73
74 //=============================================================================
75 /*!
76  *  Destructor
77  */ 
78 //=============================================================================
79
80 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
81 {
82   //  cout << "SALOME_ResourcesCatalog_Handler destruction") << endl;
83 }
84
85 //=============================================================================
86 /*!
87  *  Retrieves DS after the file parse.
88  */ 
89 //=============================================================================
90
91 const MapOfParserResourcesType&
92 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
93 {
94   return _resources_list;
95 }
96
97 //=============================================================================
98 /*!
99  *  Processes XML document and fills the list of resources
100  */ 
101 //=============================================================================
102
103 void SALOME_ResourcesCatalog_Handler::ProcessXmlDocument(xmlDocPtr theDoc)
104 {
105   // Empty private elements
106   _resources_list.clear();
107
108   // Get the document root node
109   xmlNodePtr aCurNode = xmlDocGetRootElement(theDoc);
110
111   aCurNode = aCurNode->xmlChildrenNode;
112  
113   // Processing the document nodes
114   while(aCurNode != NULL)
115   {
116     // Cas d'une machine ou d'une machine batch
117     if (!xmlStrcmp(aCurNode->name,(const xmlChar*)test_machine))
118     {
119       _resource.Clear();
120       bool Ok = ProcessMachine(aCurNode, _resource);
121       if (Ok)
122       {
123         // Adding a resource
124         if(_resource.HostName == "localhost")
125         {
126           _resource.HostName = Kernel_Utils::GetHostname();
127           if (_resource.Name == "localhost")
128           {
129             _resource.Name = Kernel_Utils::GetHostname();
130             _resource.DataForSort._Name = Kernel_Utils::GetHostname();
131           }
132         }
133         map<string, ParserResourcesType>::const_iterator iter = _resources_list.find(_resource.Name);
134         if (iter != _resources_list.end())
135           RES_INFOS("Warning resource " << _resource.Name << " already added, keep last resource found !");
136         _resources_list[_resource.Name] = _resource;
137       }
138     }
139     // Cas de la déclaration d'un cluster
140     if (!xmlStrcmp(aCurNode->name,(const xmlChar*)test_cluster))
141     {
142       _resource.Clear();
143       if(ProcessCluster(aCurNode, _resource))
144       {
145         map<string, ParserResourcesType>::const_iterator iter = _resources_list.find(_resource.Name);
146         if (iter != _resources_list.end())
147           RES_INFOS("Warning resource " << _resource.Name << " already added, keep last resource found !");
148         _resources_list[_resource.Name] = _resource;
149       }
150     }
151     aCurNode = aCurNode->next;
152   }
153
154 #ifdef _DEBUG_
155   for (map<string, ParserResourcesType>::const_iterator iter = _resources_list.begin();
156        iter != _resources_list.end();
157        iter++)
158   {
159     std::cerr << "************************************************" << std::endl;
160     std::cerr << "Resource " << (*iter).first << " found:" << std::endl;
161     std::cerr << " Name: " << (*iter).second.Name << std::endl;
162     std::cerr << " Hostname: " << (*iter).second.HostName << std::endl;
163     std::cerr << " Username: " << (*iter).second.UserName << std::endl;
164     std::cerr << " Appli path: " <<(*iter).second.AppliPath << std::endl;
165     std::cerr << " OS: " << (*iter).second.OS << std::endl;
166     std::cerr << " Protocol: " << (*iter).second.PrintAccessProtocolType() << std::endl;
167     std::cerr << " Internal Protocol: " <<(*iter).second.PrintClusterInternalProtocol() << std::endl;
168     std::cerr << " Mode: " << (*iter).second.PrintAccessModeType() << std::endl;
169     std::cerr << " Batch Type: " << (*iter).second.PrintBatchType() << std::endl;
170     std::cerr << " MPI Impl: " << (*iter).second.PrintMpiImplType() << std::endl;
171     std::cerr << "************************************************" << std::endl;
172   }
173 #endif
174 }
175
176 bool
177 SALOME_ResourcesCatalog_Handler::ProcessCluster(xmlNodePtr cluster_descr, ParserResourcesType & resource)
178 {
179   // Ajout d'un cluster
180   // hostname, use et nbOfProc sont obligatoires
181   if (xmlHasProp(cluster_descr, (const xmlChar*)test_hostname))
182   {
183     xmlChar* hostname = xmlGetProp(cluster_descr, (const xmlChar*)test_hostname);
184     resource.HostName = (const char*)hostname;
185     xmlFree(hostname);
186   }
187   else
188   {
189     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! found a cluster without a hostname" << std::endl;
190     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! this cluster will not be added" << std::endl;
191     return false;
192   }
193
194   if (xmlHasProp(cluster_descr, (const xmlChar*)test_name))
195   {
196     xmlChar* name = xmlGetProp(cluster_descr, (const xmlChar*)test_name);
197     resource.Name = (const char*)name;
198     resource.DataForSort._Name = (const char*)name;
199     xmlFree(name);
200   }
201   else
202   {
203     resource.Name = resource.HostName;
204     resource.DataForSort._Name = resource.HostName;
205     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! No Name found use Hostname for resource: " << _resource.Name << std::endl;
206   }
207
208   if (xmlHasProp(cluster_descr, (const xmlChar*)test_use))
209   {
210     xmlChar* use = xmlGetProp(cluster_descr, (const xmlChar*)test_use);
211     resource.use = (const char*)use;
212     xmlFree(use);
213   }
214   else
215   {
216     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! found a cluster without a use" << std::endl;
217     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! this cluster will not be added" << std::endl;
218     return false;
219   }
220
221   if (xmlHasProp(cluster_descr, (const xmlChar*)test_nb_of_proc))
222   {
223     xmlChar* nb_of_proc = xmlGetProp(cluster_descr, (const xmlChar*)test_nb_of_proc);
224     resource.nbOfProc = atoi((const char*)nb_of_proc);
225     xmlFree(nb_of_proc);
226   }
227   else
228   {
229     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! found a cluster without a nbOfProc" << std::endl;
230     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! this cluster will not be added" << std::endl;
231     return false;
232   }
233
234   if (xmlHasProp(cluster_descr, (const xmlChar*)test_mpi))
235   {
236     xmlChar* mpi = xmlGetProp(cluster_descr, (const xmlChar*)test_mpi);
237     std::string anMpi = (const char*)mpi;
238     xmlFree(mpi);
239     if (anMpi == "lam")
240       resource.mpi = lam;
241     else if (anMpi == "mpich1")
242       resource.mpi = mpich1;
243     else if (anMpi == "mpich2")
244       resource.mpi = mpich2;
245     else if (anMpi == "openmpi")
246       resource.mpi = openmpi;
247     else if  (anMpi == "slurm")
248       resource.mpi = slurm;
249     else if  (anMpi == "prun")
250       resource.mpi = prun;
251     else
252       resource.mpi = nompi;
253   }
254
255   // Parsing des membres du cluster 
256   xmlNodePtr aCurSubNode = cluster_descr->xmlChildrenNode;
257   while(aCurSubNode != NULL)
258   {
259     if (!xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_members))
260     {
261        xmlNodePtr members = aCurSubNode->xmlChildrenNode;
262        while (members != NULL)
263        {
264          // Process members
265          if (!xmlStrcmp(members->name, (const xmlChar*)test_machine))
266          {
267            ParserResourcesClusterMembersType new_member;
268            if (ProcessMember(members, new_member))
269              resource.ClusterMembersList.push_back(new_member);
270          }
271          members = members->next;
272        }
273     }
274     aCurSubNode = aCurSubNode->next;
275   }
276
277   // Test: Il faut au moins un membre pour que le cluster soit correct !
278   if (resource.ClusterMembersList.empty())
279   {
280     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! found a cluster without a member" << std::endl;
281     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessCluster : !!! Warning !!! this cluster will not be added" << std::endl;
282     return false;
283   }
284   return true;
285 }
286
287 bool
288 SALOME_ResourcesCatalog_Handler::ProcessMember(xmlNodePtr member_descr, ParserResourcesClusterMembersType & resource)
289 {
290   if (xmlHasProp(member_descr, (const xmlChar*)test_hostname))
291   {
292     xmlChar* hostname = xmlGetProp(member_descr, (const xmlChar*)test_hostname);
293     resource.HostName = (const char*)hostname;
294     xmlFree(hostname);
295   }
296   else
297   {
298     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a hostname" << std::endl;
299     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
300     return false;
301   }
302
303   if (xmlHasProp(member_descr, (const xmlChar*)test_protocol))
304   {
305     xmlChar* protocol= xmlGetProp(member_descr, (const xmlChar*)test_protocol);
306     switch (protocol[0])
307     {
308       case 'r':
309         resource.Protocol = rsh;
310         break;
311       case 's':
312         resource.Protocol = ssh;
313         break;
314       default:
315         std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine with a bad protocol" << std::endl;
316         std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
317         return false;
318     }
319     xmlFree(protocol);
320   }
321   else
322   {
323     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a protocol" << std::endl;
324     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
325     return false;
326   }
327
328   if (xmlHasProp(member_descr, (const xmlChar*)test_cluster_internal_protocol))
329   {
330     xmlChar* iprotocol= xmlGetProp(member_descr, (const xmlChar*)test_cluster_internal_protocol);
331     switch (iprotocol[0])
332     {
333       case 'r':
334         resource.ClusterInternalProtocol = rsh;
335         break;
336       case 's':
337         resource.ClusterInternalProtocol = ssh;
338         break;
339       default:
340         std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine with a bad protocol" << std::endl;
341         std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
342         return false;
343     }
344     xmlFree(iprotocol);
345   }
346   else
347   {
348     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a protocol" << std::endl;
349     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
350     return false;
351   }
352
353   if (xmlHasProp(member_descr, (const xmlChar*)test_user_name))
354   {
355     xmlChar* user_name= xmlGetProp(member_descr, (const xmlChar*)test_user_name);
356     resource.UserName = (const char*)user_name;
357     xmlFree(user_name);
358   }
359   else
360   {
361     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a user name" << std::endl;
362     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
363     return false;
364   }
365
366   if (xmlHasProp(member_descr, (const xmlChar*)test_nb_of_nodes))
367   {
368     xmlChar* nb_of_nodes = xmlGetProp(member_descr, (const xmlChar*)test_nb_of_nodes);
369     resource.DataForSort._nbOfNodes = atoi((const char*)nb_of_nodes);
370     xmlFree(nb_of_nodes);
371   }
372   else
373   {
374     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a nbOfNodes" << std::endl;
375     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
376     return false;
377   }
378
379   if (xmlHasProp(member_descr, (const xmlChar*)test_nb_of_proc_per_node))
380   {
381     xmlChar* nb_of_proc_per_node = xmlGetProp(member_descr, (const xmlChar*)test_nb_of_proc_per_node);
382     resource.DataForSort._nbOfProcPerNode = atoi((const char*)nb_of_proc_per_node);
383     xmlFree(nb_of_proc_per_node);
384   }
385   else
386   {
387     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a nbOfProcPerNode" << std::endl;
388     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
389     return false;
390   }
391
392   if (xmlHasProp(member_descr, (const xmlChar*)test_appli_path))
393   {
394     xmlChar* appli_path = xmlGetProp(member_descr, (const xmlChar*)test_appli_path);
395     resource.AppliPath = (const char*)appli_path;
396     xmlFree(appli_path);
397   }
398   else
399   {
400     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning found a machine without a AppliPath" << std::endl;
401     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMember : Warning this machine will not be added" << std::endl;
402     return false;
403   }
404   return true;
405 }
406
407 bool
408 SALOME_ResourcesCatalog_Handler::ProcessMachine(xmlNodePtr machine_descr, ParserResourcesType & resource)
409 {
410   if (xmlHasProp(machine_descr, (const xmlChar*)test_hostname))
411   {
412     xmlChar* hostname = xmlGetProp(machine_descr, (const xmlChar*)test_hostname);
413     resource.HostName = (const char*)hostname;
414     xmlFree(hostname);
415   }
416   else
417   {
418     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMachine : Warning found a machine without a hostname" << std::endl;
419     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMachine : Warning this machine will not be added" << std::endl;
420     return false;
421   }
422
423   if (xmlHasProp(machine_descr, (const xmlChar*)test_name))
424   {
425     xmlChar* name = xmlGetProp(machine_descr, (const xmlChar*)test_name);
426     resource.Name = (const char*)name;
427     resource.DataForSort._Name = (const char*)name;
428     xmlFree(name);
429   }
430   else
431   {
432     resource.Name = resource.HostName;
433     resource.DataForSort._Name = resource.HostName;
434     std::cerr << "SALOME_ResourcesCatalog_Handler::ProcessMachine : !!! Warning !!! No Name found use Hostname for resource: " << _resource.Name << std::endl;
435   }
436
437   if (xmlHasProp(machine_descr, (const xmlChar*)test_batch_queue))
438   {
439     xmlChar* batch_queue = xmlGetProp(machine_descr, (const xmlChar*)test_batch_queue);
440     resource.batchQueue = (const char*)batch_queue;
441     xmlFree(batch_queue);
442   }
443   else
444     resource.batchQueue = "";
445
446   if (xmlHasProp(machine_descr, (const xmlChar*)test_user_commands))
447   {
448     xmlChar* user_commands= xmlGetProp(machine_descr, (const xmlChar*)test_user_commands);
449     resource.userCommands = (const char*)user_commands;
450     xmlFree(user_commands);
451   }
452   else
453     resource.userCommands = "";
454
455   if (xmlHasProp(machine_descr, (const xmlChar*)test_protocol))
456   {
457     xmlChar* protocol= xmlGetProp(machine_descr, (const xmlChar*)test_protocol);
458     switch ( protocol[0])
459     {
460       case 'r':
461         resource.Protocol = rsh;
462         break;
463       case 's':
464         resource.Protocol = ssh;
465         break;
466       default:
467         // If it'not in all theses cases, the protocol is affected to rsh
468         resource.Protocol = rsh;
469         break;
470     }
471     xmlFree(protocol);
472   }
473   else
474     resource.Protocol = rsh;
475
476   if (xmlHasProp(machine_descr, (const xmlChar*)test_cluster_internal_protocol))
477   {
478     xmlChar* iprotocol= xmlGetProp(machine_descr, (const xmlChar*)test_cluster_internal_protocol);
479     switch ( iprotocol[0])
480     {
481       case 'r':
482         resource.ClusterInternalProtocol = rsh;
483         break;
484       case 's':
485         resource.ClusterInternalProtocol = ssh;
486         break;
487       default:
488         // If it'not in all theses cases, the protocol is affected to rsh
489         resource.ClusterInternalProtocol = rsh;
490         break;
491     }
492     xmlFree(iprotocol);
493   }
494   else
495     resource.ClusterInternalProtocol = resource.Protocol;
496
497   if (xmlHasProp(machine_descr, (const xmlChar*)test_mode))
498   {
499     xmlChar* mode=xmlGetProp(machine_descr, (const xmlChar*)test_mode);
500     switch ( mode[0] )
501     {
502       case 'i':
503         resource.Mode = interactive;
504         break;
505       case 'b':
506         resource.Mode = batch;
507         break;
508       default:
509         // If it'not in all theses cases, the mode is affected to interactive
510         resource.Mode = interactive;
511         break;
512     }
513     xmlFree(mode);
514   }
515   else
516     resource.Mode = interactive;
517
518   if (xmlHasProp(machine_descr, (const xmlChar*)test_batch))
519   {
520     xmlChar* batch = xmlGetProp(machine_descr, (const xmlChar*)test_batch);
521     std::string aBatch = (const char*)batch;
522     xmlFree(batch);
523     if (aBatch == "pbs")
524       resource.Batch = pbs;
525     else if  (aBatch == "lsf")
526       resource.Batch = lsf;
527     else if  (aBatch == "sge")
528       resource.Batch = sge;
529     else if  (aBatch == "ssh_batch")
530       resource.Batch = ssh_batch;
531     else
532       resource.Batch = none;
533   }
534
535   if (xmlHasProp(machine_descr, (const xmlChar*)test_mpi))
536   {
537     xmlChar* mpi = xmlGetProp(machine_descr, (const xmlChar*)test_mpi);
538     std::string anMpi = (const char*)mpi;
539     xmlFree(mpi);
540     if (anMpi == "lam")
541       resource.mpi = lam;
542     else if (anMpi == "mpich1")
543       resource.mpi = mpich1;
544     else if (anMpi == "mpich2")
545       resource.mpi = mpich2;
546     else if (anMpi == "openmpi")
547       resource.mpi = openmpi;
548     else if  (anMpi == "slurm")
549       resource.mpi = slurm;
550     else if  (anMpi == "prun")
551       resource.mpi = prun;
552     else
553       resource.mpi = nompi;
554   }
555
556   if (xmlHasProp(machine_descr, (const xmlChar*)test_user_name))
557   {
558     xmlChar* user_name= xmlGetProp(machine_descr, (const xmlChar*)test_user_name);
559     resource.UserName = (const char*)user_name;
560     xmlFree(user_name);
561   }
562
563   if (xmlHasProp(machine_descr, (const xmlChar*)test_appli_path))
564   {
565     xmlChar* appli_path = xmlGetProp(machine_descr, (const xmlChar*)test_appli_path);
566     resource.AppliPath = (const char*)appli_path;
567     xmlFree(appli_path);
568   }
569
570   if (xmlHasProp(machine_descr, (const xmlChar*)test_os))
571   {
572     xmlChar* os = xmlGetProp(machine_descr, (const xmlChar*)test_os);
573     resource.OS = (const char*)os;
574     xmlFree(os);
575   }
576
577   if (xmlHasProp(machine_descr, (const xmlChar*)test_mem_in_mb))
578   {
579     xmlChar* mem_in_mb = xmlGetProp(machine_descr, (const xmlChar*)test_mem_in_mb);
580     resource.DataForSort._memInMB = atoi((const char*)mem_in_mb);
581     xmlFree(mem_in_mb);
582   }
583
584   if (xmlHasProp(machine_descr, (const xmlChar*)test_cpu_freq_mhz))
585   {
586     xmlChar* cpu_freq_mhz = xmlGetProp(machine_descr, (const xmlChar*)test_cpu_freq_mhz);
587     resource.DataForSort._CPUFreqMHz = atoi((const char*)cpu_freq_mhz);
588     xmlFree(cpu_freq_mhz);
589   }
590
591   if (xmlHasProp(machine_descr, (const xmlChar*)test_nb_of_nodes))
592   {
593     xmlChar* nb_of_nodes = xmlGetProp(machine_descr, (const xmlChar*)test_nb_of_nodes);
594     resource.DataForSort._nbOfNodes = atoi((const char*)nb_of_nodes);
595     xmlFree(nb_of_nodes);
596   }
597
598   if (xmlHasProp(machine_descr, (const xmlChar*)test_nb_of_proc_per_node))
599   {
600     xmlChar* nb_of_proc_per_node = xmlGetProp(machine_descr, (const xmlChar*)test_nb_of_proc_per_node);
601     resource.DataForSort._nbOfProcPerNode = atoi((const char*)nb_of_proc_per_node);
602     xmlFree(nb_of_proc_per_node);
603   }
604
605   // Process children nodes
606   xmlNodePtr aCurSubNode = machine_descr->xmlChildrenNode;
607   while(aCurSubNode != NULL)
608   {
609     // Process components
610     if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_components) )
611     {
612       //If a component is given, it is in a module with the same name
613       //except if the module name is given
614       if (xmlHasProp(aCurSubNode, (const xmlChar*)test_component_name)) 
615       {
616         xmlChar* component_name = xmlGetProp(aCurSubNode, (const xmlChar*)test_component_name);
617         std::string aComponentName = (const char*)component_name;
618         _resource.ComponentsList.push_back(aComponentName);
619         if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name)) 
620         {
621           xmlChar* module_name = xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
622           std::string aModuleName = (const char*)module_name;
623           _resource.ModulesList.push_back(aModuleName);
624           xmlFree(module_name);
625         }
626         else
627           _resource.ModulesList.push_back(aComponentName);
628         xmlFree(component_name);
629       }
630     }
631     // Process modules
632     else if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_modules) )
633     {
634       // If a module is given, we create an entry in componentsList and modulesList
635       // with the same name (module == component)
636       if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name)) 
637       {
638         xmlChar* component_name = xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
639         std::string aComponentName = (const char*)component_name;
640         _resource.ComponentsList.push_back(aComponentName);
641         _resource.ModulesList.push_back(aComponentName);
642         xmlFree(component_name);
643       }
644     }
645     aCurSubNode = aCurSubNode->next;
646   }
647   return true;
648 }
649
650 //=============================================================================
651 /*!
652  *  Fill the document tree in xml file, used to write in an xml file.
653  *  \param theDoc document to fill.
654  */ 
655 //=============================================================================
656
657 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(xmlDocPtr theDoc)
658 {
659   // Node pointers
660   xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
661
662   root_node = xmlNewNode(NULL, BAD_CAST "resources");
663   xmlDocSetRootElement(theDoc, root_node);
664     
665   std::map<std::string, ParserResourcesType>::iterator iter = _resources_list.begin();
666   for (; iter != _resources_list.end(); iter++)
667   {
668     node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
669     RES_MESSAGE("Add resource name = " << (*iter).second.Name.c_str());
670     xmlNewProp(node, BAD_CAST test_name, BAD_CAST (*iter).second.Name.c_str());
671     xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).second.HostName.c_str());
672     xmlNewProp(node, BAD_CAST test_appli_path, BAD_CAST (*iter).second.AppliPath.c_str());
673     xmlNewProp(node, BAD_CAST test_batch_queue, BAD_CAST (*iter).second.batchQueue.c_str());
674     xmlNewProp(node, BAD_CAST test_user_commands, BAD_CAST (*iter).second.userCommands.c_str());
675
676     switch ((*iter).second.Protocol)
677     {
678       case rsh:
679         xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
680         break;
681       case ssh:
682         xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
683         break;
684       default:
685         xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
686     }
687
688     switch ((*iter).second.ClusterInternalProtocol)
689     {
690       case rsh:
691         xmlNewProp(node, BAD_CAST test_cluster_internal_protocol, BAD_CAST "rsh");
692         break;
693       case ssh:
694         xmlNewProp(node, BAD_CAST test_cluster_internal_protocol, BAD_CAST "ssh");
695         break;
696       default:
697         xmlNewProp(node, BAD_CAST test_cluster_internal_protocol, BAD_CAST "rsh");
698     }
699
700     switch ((*iter).second.Mode)
701     {
702       case interactive:
703         xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
704         break;
705       case batch:
706         xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
707         break;
708       default:
709         xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
710     }
711
712     switch ((*iter).second.Batch)
713     {
714       case pbs:
715         xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "pbs");
716         break;
717       case lsf:
718         xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "lsf");
719         break;
720       case sge:
721         xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "sge");
722         break;
723       case ssh_batch:
724         xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "ssh_batch");
725         break;
726       default:
727         xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "");
728     }
729
730     switch ((*iter).second.mpi)
731     {
732       case lam:
733         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "lam");
734         break;
735       case mpich1:
736         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich1");
737         break;
738       case mpich2:
739         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich2");
740         break;
741       case openmpi:
742         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "openmpi");
743         break;
744       case slurm:
745         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "slurm");
746         break;
747       case prun:
748         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "prun");
749         break;
750       default:
751         xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "");
752     }
753
754     xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
755
756     std::vector<std::string>::const_iterator iter2 = (*iter).second.ComponentsList.begin();
757     for(;iter2 != (*iter).second.ComponentsList.end(); iter2++)
758     {
759       node1 = xmlNewChild(node, NULL, BAD_CAST test_components, NULL);
760       xmlNewProp(node1, BAD_CAST test_component_name, BAD_CAST (*iter2).c_str());
761     }
762
763     xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
764     std::ostringstream mem_stream;
765     mem_stream << (*iter).second.DataForSort._memInMB;
766     xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST mem_stream.str().c_str());
767     std::ostringstream cpu_stream;
768     cpu_stream << (*iter).second.DataForSort._CPUFreqMHz;
769     xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST cpu_stream.str().c_str());
770     std::ostringstream nb_nodes_stream;
771     nb_nodes_stream << (*iter).second.DataForSort._nbOfNodes;
772     xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST nb_nodes_stream.str().c_str());
773     std::ostringstream nb_proc_per_nodes_stream;
774     nb_proc_per_nodes_stream << (*iter).second.DataForSort._nbOfProcPerNode;
775     xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST nb_proc_per_nodes_stream.str().c_str());
776   }
777 }