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