Salome HOME
merge from BR_For40_DSC 28 jan 08
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesCatalog_Handler.cxx
1 //  SALOME ResourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : SALOME_ResourcesCatalog_Handler.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //$Header$
28
29 #include "SALOME_ResourcesCatalog_Handler.hxx"
30 #include <iostream>
31 #include <map>
32 #include "utilities.h"
33
34 using namespace std;
35
36 #ifdef _DEBUG_
37 static int MYDEBUG = 1;
38 #else
39 static int MYDEBUG = 0;
40 #endif
41
42 //=============================================================================
43 /*!
44  *  Constructor
45  *  \param listOfResources: map of ParserResourcesType to fill when parsing
46  */ 
47 //=============================================================================
48
49 SALOME_ResourcesCatalog_Handler::
50 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& listOfResources):
51     _resources_list(listOfResources)
52 {
53   MESSAGE("SALOME_ResourcesCatalog_Handler creation");
54   //XML tags initialisation
55   test_machine = "machine";
56   test_resources = "resources";
57
58   test_hostname = "hostname";
59   test_alias = "alias";
60   test_protocol = "protocol";
61   test_mode = "mode";
62   test_batch = "batch";
63   test_mpi = "mpi";
64   test_user_name = "userName";
65   test_appli_path = "appliPath";
66   test_modules = "modules";
67   test_module_name = "moduleName";
68   test_os = "OS";
69   test_mem_in_mb = "memInMB";
70   test_cpu_freq_mhz = "CPUFreqMHz";
71   test_nb_of_nodes = "nbOfNodes";
72   test_nb_of_proc_per_node = "nbOfProcPerNode";
73 }
74
75 //=============================================================================
76 /*!
77  *  Destructor
78  */ 
79 //=============================================================================
80
81 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
82 {
83   //  MESSAGE("SALOME_ResourcesCatalog_Handler destruction");
84 }
85
86 //=============================================================================
87 /*!
88  *  Retrieves DS after the file parse.
89  */ 
90 //=============================================================================
91
92 const MapOfParserResourcesType&
93 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
94 {
95   return _resources_list;
96 }
97
98 //=============================================================================
99 /*!
100  *  Processes XML document and fills the list of resources
101  */ 
102 //=============================================================================
103
104 void SALOME_ResourcesCatalog_Handler::ProcessXmlDocument(xmlDocPtr theDoc)
105 {
106   if (MYDEBUG) MESSAGE("Begin parse document");
107
108   // Empty private elements
109   _resources_list.clear();
110
111   // Get the document root node
112   xmlNodePtr aCurNode = xmlDocGetRootElement(theDoc);
113
114   aCurNode = aCurNode->xmlChildrenNode;
115   
116   // Processing the document nodes
117   while(aCurNode != NULL)
118     {
119       if ( !xmlStrcmp(aCurNode->name,(const xmlChar*)test_machine) )
120         {
121           _resource.Clear();
122           if (xmlHasProp(aCurNode, (const xmlChar*)test_hostname))
123             {
124               xmlChar* hostname = xmlGetProp(aCurNode, (const xmlChar*)test_hostname);
125               _resource.DataForSort._hostName = (const char*)hostname;
126               xmlFree(hostname);
127             }
128           else
129             break;
130
131           if (xmlHasProp(aCurNode, (const xmlChar*)test_alias))
132             {
133               xmlChar* alias = xmlGetProp(aCurNode, (const xmlChar*)test_alias);
134               _resource.Alias = (const char*)alias;
135               xmlFree(alias);
136             }
137           else
138             _resource.Alias = "";
139
140           if (xmlHasProp(aCurNode, (const xmlChar*)test_protocol))
141             {
142               xmlChar* protocol= xmlGetProp(aCurNode, (const xmlChar*)test_protocol);
143               switch ( protocol[0])
144                 {
145                 case 'r':
146                   _resource.Protocol = rsh;
147                   break;
148                 case 's':
149                   _resource.Protocol = ssh;
150                   break;
151                 default:
152                   // If it'not in all theses cases, the protocol is affected to rsh
153                   _resource.Protocol = rsh;
154                   break;
155                 }
156               xmlFree(protocol);
157             }
158           else
159             _resource.Protocol = rsh;
160           
161           if (xmlHasProp(aCurNode, (const xmlChar*)test_mode))
162             {
163               xmlChar* mode=xmlGetProp(aCurNode, (const xmlChar*)test_mode);
164               switch ( mode[0] )
165                 {
166                 case 'i':
167                   _resource.Mode = interactive;
168                   break;
169                 case 'b':
170                   _resource.Mode = batch;
171                   break;
172                 default:
173                   // If it'not in all theses cases, the mode is affected to interactive
174                   _resource.Mode = interactive;
175                   break;
176                 }
177               xmlFree(mode);
178             }
179           else
180             _resource.Mode = interactive;
181
182           if (xmlHasProp(aCurNode, (const xmlChar*)test_batch))
183             {
184               xmlChar* batch = xmlGetProp(aCurNode, (const xmlChar*)test_batch);
185               std::string aBatch = (const char*)batch;
186               xmlFree(batch);
187               if (aBatch == "pbs")
188                 _resource.Batch = pbs;
189               else if  (aBatch == "lsf")
190                 _resource.Batch = lsf;
191               else if  (aBatch == "slurm")
192                 _resource.Batch = slurm;
193               else
194                 _resource.Batch = none;
195             }
196
197           if (xmlHasProp(aCurNode, (const xmlChar*)test_mpi))
198             {
199               xmlChar* mpi = xmlGetProp(aCurNode, (const xmlChar*)test_mpi);
200               std::string anMpi = (const char*)mpi;
201               xmlFree(mpi);
202               if (anMpi == "lam")
203                 _resource.mpi = lam;
204               else if (anMpi == "mpich1")
205                 _resource.mpi = mpich1;
206               else if (anMpi == "mpich2")
207                 _resource.mpi = mpich2;
208               else if (anMpi == "openmpi")
209                 _resource.mpi = openmpi;
210               else
211                 _resource.mpi = indif;
212             }
213
214           if (xmlHasProp(aCurNode, (const xmlChar*)test_user_name))
215             {
216               xmlChar* user_name= xmlGetProp(aCurNode, (const xmlChar*)test_user_name);
217               _resource.UserName = (const char*)user_name;
218               xmlFree(user_name);
219             }
220           
221           if (xmlHasProp(aCurNode, (const xmlChar*)test_appli_path))
222             {
223               xmlChar* appli_path = xmlGetProp(aCurNode, (const xmlChar*)test_appli_path);
224               _resource.AppliPath = (const char*)appli_path;
225               xmlFree(appli_path);
226             }
227           
228           if (xmlHasProp(aCurNode, (const xmlChar*)test_os))
229             {
230               xmlChar* os = xmlGetProp(aCurNode, (const xmlChar*)test_os);
231               _resource.OS = (const char*)os;
232               xmlFree(os);
233             }
234           
235           if (xmlHasProp(aCurNode, (const xmlChar*)test_mem_in_mb))
236             {
237               xmlChar* mem_in_mb = xmlGetProp(aCurNode, (const xmlChar*)test_mem_in_mb);
238               _resource.DataForSort._memInMB = atoi((const char*)mem_in_mb);
239               xmlFree(mem_in_mb);
240             }
241           
242           if (xmlHasProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz))
243             {
244               xmlChar* cpu_freq_mhz = xmlGetProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz);
245               _resource.DataForSort._CPUFreqMHz = atoi((const char*)cpu_freq_mhz);
246               xmlFree(cpu_freq_mhz);
247             }
248           
249           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_nodes))
250             {
251               xmlChar* nb_of_nodes = xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_nodes);
252               _resource.DataForSort._nbOfNodes = atoi((const char*)nb_of_nodes);
253               xmlFree(nb_of_nodes);
254             }
255           
256           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node))
257             {
258               xmlChar* nb_of_proc_per_node = xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node);
259               _resource.DataForSort._nbOfProcPerNode = atoi((const char*)nb_of_proc_per_node);
260               xmlFree(nb_of_proc_per_node);
261             }
262           
263           // Process modules
264           xmlNodePtr aCurSubNode = aCurNode->xmlChildrenNode;
265           while(aCurSubNode != NULL)
266             {
267               if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_modules) )
268                 {
269                   if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name)) 
270                     {
271                       xmlChar* module_name = xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
272                       std::string aModuleName = (const char*)module_name;
273                       _resource.ModulesList.push_back(aModuleName);
274                       xmlFree(module_name);
275                     }
276                 }
277               aCurSubNode = aCurSubNode->next;
278             }
279           
280           int aNbNodes = _resource.DataForSort._nbOfNodes;
281           if( aNbNodes > 1 ){
282             string clusterNode = _resource.DataForSort._hostName ;
283             for( int i=0; i < aNbNodes; i++ ){
284               char inode[64];
285               inode[0] = '\0' ;
286               sprintf(inode,"%s%d",clusterNode.c_str(),i+1);
287               std::string nodeName(inode);
288               _resource.DataForSort._hostName = nodeName ;
289               _resources_list[nodeName] = _resource;
290             }
291           }
292           else
293             _resources_list[_resource.DataForSort._hostName] = _resource;
294         }
295       
296       aCurNode = aCurNode->next;
297     }
298
299   // For debug only
300   if (MYDEBUG)
301     {
302       for (map<string, ParserResourcesType>::const_iterator iter =
303              _resources_list.begin();
304            iter != _resources_list.end();
305            iter++)
306         {
307           SCRUTE((*iter).second.Alias);
308           SCRUTE((*iter).second.UserName);
309           SCRUTE((*iter).second.AppliPath);
310           SCRUTE((*iter).second.OS);
311           SCRUTE((*iter).second.Protocol);
312           SCRUTE((*iter).second.Mode);
313         }
314       
315       MESSAGE("This is the end of document");
316     }
317 }
318
319
320 //=============================================================================
321 /*!
322  *  Fill the document tree in xml file, used to write in an xml file.
323  *  \param theDoc document to fill.
324  */ 
325 //=============================================================================
326
327 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(xmlDocPtr theDoc)
328 {
329   // Node pointers
330   xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
331   char string_buf[80];
332
333   root_node = xmlNewNode(NULL, BAD_CAST "resources");
334   xmlDocSetRootElement(theDoc, root_node);
335     
336   for (map<string, ParserResourcesType>::iterator iter =
337          _resources_list.begin();
338        iter != _resources_list.end();
339        iter++)
340     {
341       node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
342       xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).first.c_str());
343       xmlNewProp(node, BAD_CAST test_alias, BAD_CAST (*iter).second.Alias.c_str());
344       
345       switch ((*iter).second.Protocol)
346         {
347         case rsh:
348           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
349           break;
350         case ssh:
351           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
352           break;
353         default:
354           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
355         }
356
357       switch ((*iter).second.Mode)
358         {
359         case interactive:
360           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
361           break;
362         case batch:
363           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
364           break;
365         default:
366           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
367         }
368
369       switch ((*iter).second.Batch)
370         {
371         case pbs:
372           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "pbs");
373           break;
374         case lsf:
375           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "lsf");
376           break;
377         case slurm:
378           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "slurm");
379           break;
380         default:
381           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "");
382         }
383
384       switch ((*iter).second.mpi)
385         {
386         case lam:
387           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "lam");
388           break;
389         case mpich1:
390           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich1");
391           break;
392         case mpich2:
393           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich2");
394           break;
395         case openmpi:
396           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "openmpi");
397           break;
398         default:
399           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "");
400         }
401
402       xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
403
404      for (vector<string>::const_iterator iter2 =
405              (*iter).second.ModulesList.begin();
406            iter2 != (*iter).second.ModulesList.end();
407            iter2++)
408         {
409           node1 = xmlNewChild(node, NULL, BAD_CAST test_modules, NULL);
410           xmlNewProp(node1, BAD_CAST test_module_name, BAD_CAST (*iter2).c_str());
411         }
412
413       xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
414       xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._memInMB));
415       xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._CPUFreqMHz));
416       xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfNodes));
417       xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfProcPerNode));
418     }
419 }