Salome HOME
Porting KERNEL on new XML reader.
[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_user_name = "userName";
63   test_appli_path = "appliPath";
64   test_modules = "modules";
65   test_module_name = "moduleName";
66   test_module_path = "modulePath";
67   test_pre_req_file_path = "preReqFilePath";
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           if (xmlHasProp(aCurNode, (const xmlChar*)test_hostname))
122             _resource.DataForSort._hostName = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_hostname);
123           else
124             break;
125
126           if (xmlHasProp(aCurNode, (const xmlChar*)test_alias))
127             _resource.Alias = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_alias);
128           else
129             _resource.Alias = "";
130
131           switch ( ((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_protocol))[0] )
132             {
133             case 'r':
134               _resource.Protocol = rsh;
135               break;
136             case 's':
137               _resource.Protocol = ssh;
138               break;
139             default:
140               // If it'not in all theses cases, the protocol is affected to rsh
141               _resource.Protocol = rsh;
142               break;
143             }
144           
145           switch ( ((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_mode))[0] )
146             {
147             case 'i':
148               _resource.Mode = interactive;
149               break;
150             case 'b':
151               _resource.Mode = batch;
152               break;
153             default:
154               // If it'not in all theses cases, the mode is affected to interactive
155               _resource.Mode = interactive;
156               break;
157             }
158
159           if (xmlHasProp(aCurNode, (const xmlChar*)test_user_name))
160             _resource.UserName = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_user_name);
161           
162           if (xmlHasProp(aCurNode, (const xmlChar*)test_appli_path))
163             _resource.AppliPath = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_appli_path);
164           
165           if (xmlHasProp(aCurNode, (const xmlChar*)test_pre_req_file_path))
166             _resource.PreReqFilePath = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_pre_req_file_path);
167           
168           if (xmlHasProp(aCurNode, (const xmlChar*)test_os))
169             _resource.OS = (const char*)xmlGetProp(aCurNode, (const xmlChar*)test_os);
170           
171           if (xmlHasProp(aCurNode, (const xmlChar*)test_mem_in_mb))
172             _resource.DataForSort._memInMB = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_mem_in_mb));
173           
174           if (xmlHasProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz))
175             _resource.DataForSort._CPUFreqMHz = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz));
176           
177           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_nodes))
178             _resource.DataForSort._nbOfNodes = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_nodes));
179           
180           if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node))
181             _resource.DataForSort._nbOfProcPerNode = atoi((const char*)xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node));
182           
183           // Process modules
184           xmlNodePtr aCurSubNode = aCurNode->xmlChildrenNode;
185           while(aCurSubNode != NULL)
186             {
187               if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_modules) )
188                 {
189                   if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name) && 
190                       xmlHasProp(aCurSubNode, (const xmlChar*)test_module_path)) 
191                     {
192                       std::string aModuleName = (const char*)xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
193                       std::string aModulePath = (const char*)xmlGetProp(aCurSubNode, (const xmlChar*)test_module_path);
194                       _resource.ModulesPath[aModuleName] = aModulePath;
195                     }
196                 }
197               aCurSubNode = aCurSubNode->next;
198             }
199           
200           int aNbNodes = _resource.DataForSort._nbOfNodes;
201           if( aNbNodes > 1 ){
202             string clusterNode = _resource.DataForSort._hostName ;
203             for( int i=0; i < aNbNodes; i++ ){
204               char inode[64];
205               inode[0] = '\0' ;
206               sprintf(inode,"%s%d",clusterNode.c_str(),i+1);
207               std::string nodeName(inode);
208               _resource.DataForSort._hostName = nodeName ;
209               _resources_list[nodeName] = _resource;
210             }
211           }
212           else
213             _resources_list[_resource.DataForSort._hostName] = _resource;
214         }
215       
216       aCurNode = aCurNode->next;
217     }
218
219   // For debug only
220   if (MYDEBUG)
221     {
222       for (map<string, ParserResourcesType>::const_iterator iter =
223              _resources_list.begin();
224            iter != _resources_list.end();
225            iter++)
226         {
227           SCRUTE((*iter).second.Alias);
228           SCRUTE((*iter).second.UserName);
229           SCRUTE((*iter).second.AppliPath);
230           SCRUTE((*iter).second.PreReqFilePath);
231           SCRUTE((*iter).second.OS);
232           SCRUTE((*iter).second.Protocol);
233           SCRUTE((*iter).second.Mode);
234         }
235       
236       MESSAGE("This is the end of document");
237     }
238 }
239
240
241 //=============================================================================
242 /*!
243  *  Fill the document tree in xml file, used to write in an xml file.
244  *  \param theDoc document to fill.
245  */ 
246 //=============================================================================
247
248 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(xmlDocPtr theDoc)
249 {
250   // Node pointers
251   xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
252   char string_buf[80];
253
254   root_node = xmlNewNode(NULL, BAD_CAST "resources");
255   xmlDocSetRootElement(theDoc, root_node);
256     
257   for (map<string, ParserResourcesType>::iterator iter =
258          _resources_list.begin();
259        iter != _resources_list.end();
260        iter++)
261     {
262       node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
263       xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).first.c_str());
264       xmlNewProp(node, BAD_CAST test_alias, BAD_CAST (*iter).second.Alias.c_str());
265       
266       switch ((*iter).second.Protocol)
267         {
268         case rsh:
269           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
270           break;
271         case ssh:
272           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
273           break;
274         default:
275           xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
276         }
277
278       switch ((*iter).second.Mode)
279         {
280         case interactive:
281           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
282           break;
283         case batch:
284           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
285           break;
286         default:
287           xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
288         }
289
290       xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
291
292       for (map<string, string>::const_iterator iter2 =
293              (*iter).second.ModulesPath.begin();
294            iter2 != (*iter).second.ModulesPath.end();
295            iter2++)
296         {
297           node1 = xmlNewChild(node, NULL, BAD_CAST test_modules, NULL);
298           xmlNewProp(node1, BAD_CAST test_module_name, BAD_CAST (*iter2).first.c_str());
299           xmlNewProp(node1, BAD_CAST test_module_path, BAD_CAST (*iter2).second.c_str());
300         }
301
302       xmlNewProp(node, BAD_CAST test_pre_req_file_path, BAD_CAST (*iter).second.PreReqFilePath.c_str());
303       xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
304       xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._memInMB));
305       xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._CPUFreqMHz));
306       xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfNodes));
307       xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfProcPerNode));
308     }
309 }