Salome HOME
Fix compilation pb.
[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             _resource.DataForSort._hostName = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_hostname);
124           else
125             break;
126
127           if (xmlHasProp(aCurNode, (const xmlChar*)test_alias))
128             _resource.Alias = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_alias);
129           else
130             _resource.Alias = "";
131
132           switch ( ((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_protocol))[0] )
133             {
134             case 'r':
135               _resource.Protocol = rsh;
136               break;
137             case 's':
138               _resource.Protocol = ssh;
139               break;
140             default:
141               // If it'not in all theses cases, the protocol is affected to rsh
142               _resource.Protocol = rsh;
143               break;
144             }
145           
146           switch ( ((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_mode))[0] )
147             {
148             case 'i':
149               _resource.Mode = interactive;
150               break;
151             case 'b':
152               _resource.Mode = batch;
153               break;
154             default:
155               // If it'not in all theses cases, the mode is affected to interactive
156               _resource.Mode = interactive;
157               break;
158             }
159
160           if (xmlHasProp(aCurNode, (const xmlChar*)test_batch))
161             {
162               std::string aBatch = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_batch);
163               if (aBatch == "pbs")
164                 _resource.Batch = pbs;
165               else if  (aBatch == "lsf")
166                 _resource.Batch = lsf;
167               else if  (aBatch == "slurm")
168                 _resource.Batch = slurm;
169               else
170                 _resource.Batch = none;
171             }
172
173           if (xmlHasProp(aCurNode, (const xmlChar*)test_mpi))
174             {
175               std::string anMpi = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_mpi);
176               if (anMpi == "lam")
177                 _resource.mpi = lam;
178               else if (anMpi == "mpich1")
179                 _resource.mpi = mpich1;
180               else if (anMpi == "mpich2")
181                 _resource.mpi = mpich2;
182               else if (anMpi == "openmpi")
183                 _resource.mpi = openmpi;
184               else
185                 _resource.mpi = indif;
186             }
187
188           if (xmlHasProp(aCurNode, (const xmlChar*)test_user_name))
189             _resource.UserName = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_user_name);
190           
191           if (xmlHasProp(aCurNode, (const xmlChar*)test_appli_path))
192             _resource.AppliPath = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_appli_path);
193           
194           if (xmlHasProp(aCurNode, (const xmlChar*)test_os))
195             _resource.OS = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_os);
196           
197           if (xmlHasProp(aCurNode, (const xmlChar*)test_mem_in_mb))
198             _resource.DataForSort._memInMB = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_mem_in_mb));
199           
200           if (xmlHasProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz))
201             _resource.DataForSort._CPUFreqMHz = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz));
202           
203           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_nodes))
204             _resource.DataForSort._nbOfNodes = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_nodes));
205           
206           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node))
207             _resource.DataForSort._nbOfProcPerNode = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node));
208           
209           // Process modules
210           xmlNodePtr aCurSubNode = aCurNode->xmlChildrenNode;
211           while(aCurSubNode != NULL)
212             {
213               if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_modules) )
214                 {
215                   if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name)) 
216                     {
217                       std::string aModuleName = (const char*)xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
218                       _resource.ModulesList.push_back(aModuleName);
219                     }
220                 }
221               aCurSubNode = aCurSubNode->next;
222             }
223           
224           int aNbNodes = _resource.DataForSort._nbOfNodes;
225           if( aNbNodes > 1 ){
226             string clusterNode = _resource.DataForSort._hostName ;
227             for( int i=0; i < aNbNodes; i++ ){
228               char inode[64];
229               inode[0] = '\0' ;
230               sprintf(inode,"%s%d",clusterNode.c_str(),i+1);
231               std::string nodeName(inode);
232               _resource.DataForSort._hostName = nodeName ;
233               _resources_list[nodeName] = _resource;
234             }
235           }
236           else
237             _resources_list[_resource.DataForSort._hostName] = _resource;
238         }
239       
240       aCurNode = aCurNode->next;
241     }
242
243   // For debug only
244   if (MYDEBUG)
245     {
246       for (map<string, ParserResourcesType>::const_iterator iter =
247              _resources_list.begin();
248            iter != _resources_list.end();
249            iter++)
250         {
251           SCRUTE((*iter).second.Alias);
252           SCRUTE((*iter).second.UserName);
253           SCRUTE((*iter).second.AppliPath);
254           SCRUTE((*iter).second.OS);
255           SCRUTE((*iter).second.Protocol);
256           SCRUTE((*iter).second.Mode);
257         }
258       
259       MESSAGE("This is the end of document");
260     }
261 }
262
263
264 //=============================================================================
265 /*!
266  *  Fill the document tree in xml file, used to write in an xml file.
267  *  \param theDoc document to fill.
268  */ 
269 //=============================================================================
270
271 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(xmlDocPtr theDoc)
272 {
273   // Node pointers
274   xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
275   char string_buf[80];
276
277   root_node = xmlNewNode(NULL, BAD_CAST "resources");
278   xmlDocSetRootElement(theDoc, root_node);
279     
280   for (map<string, ParserResourcesType>::iterator iter =
281          _resources_list.begin();
282        iter != _resources_list.end();
283        iter++)
284     {
285       node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
286       xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).first.c_str());
287       xmlNewProp(node, BAD_CAST test_alias, BAD_CAST (*iter).second.Alias.c_str());
288       
289       switch ((*iter).second.Protocol)
290         {
291         case rsh:
292           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
293           break;
294         case ssh:
295           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
296           break;
297         default:
298           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
299         }
300
301       switch ((*iter).second.Mode)
302         {
303         case interactive:
304           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
305           break;
306         case batch:
307           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
308           break;
309         default:
310           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
311         }
312
313       switch ((*iter).second.Batch)
314         {
315         case pbs:
316           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "pbs");
317           break;
318         case lsf:
319           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "lsf");
320           break;
321         case slurm:
322           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "slurm");
323           break;
324         default:
325           xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "");
326         }
327
328       switch ((*iter).second.mpi)
329         {
330         case lam:
331           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "lam");
332           break;
333         case mpich1:
334           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich1");
335           break;
336         case mpich2:
337           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich2");
338           break;
339         case openmpi:
340           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "openmpi");
341           break;
342         default:
343           xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "");
344         }
345
346       xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
347
348      for (vector<string>::const_iterator iter2 =
349              (*iter).second.ModulesList.begin();
350            iter2 != (*iter).second.ModulesList.end();
351            iter2++)
352         {
353           node1 = xmlNewChild(node, NULL, BAD_CAST test_modules, NULL);
354           xmlNewProp(node1, BAD_CAST test_module_name, BAD_CAST (*iter2).c_str());
355         }
356
357       xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
358       xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._memInMB));
359       xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._CPUFreqMHz));
360       xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfNodes));
361       xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfProcPerNode));
362     }
363 }