Salome HOME
Porting to Mandrake 10.1 and new products:
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 <qdom.h>
33 #include "utilities.h"
34
35 using namespace std;
36
37 //----------------------------------------------------------------------
38 //Function : SALOME_ResourcesCatalog_Handler
39 //Purpose: Constructor
40 //----------------------------------------------------------------------
41 SALOME_ResourcesCatalog_Handler::SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& listOfResources):_resources_list(listOfResources)
42 {
43   MESSAGE("SALOME_ResourcesCatalog_Handler creation");
44   //XML tags initialisation
45   test_machine="machine";
46   test_resources="resources";
47
48   test_hostname="hostname";
49   test_alias="alias";
50   test_protocol="protocol";
51   test_mode="mode";
52   test_user_name="userName";
53   test_modules="modules";
54   test_module_name="moduleName";
55   test_module_path="modulePath";
56   test_pre_req_file_path="preReqFilePath";
57   test_os="OS";
58   test_mem_in_mb="memInMB";
59   test_cpu_freq_mhz="CPUFreqMHz";
60   test_nb_of_nodes="nbOfNodes";
61   test_nb_of_proc_per_node="nbOfProcPerNode";
62 }
63
64 //----------------------------------------------------------------------
65 //Function : ~SALOME_ResourcesCatalog_Handler
66 //Purpose: Destructor
67 //----------------------------------------------------------------------
68 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
69 {
70   MESSAGE("SALOME_ResourcesCatalog_Handler destruction");
71 }
72
73 //----------------------------------------------------------------------
74 //Function : GetResourcesAfterParsing
75 //Purpose: Retrieves DS after the file parse.
76 //----------------------------------------------------------------------
77 const MapOfParserResourcesType& SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
78 {
79   return _resources_list;
80 }
81
82 //----------------------------------------------------------------------
83 //Function : startDocument
84 //Purpose: overload handler function 
85 //----------------------------------------------------------------------
86 bool SALOME_ResourcesCatalog_Handler::startDocument()
87 {
88   MESSAGE("Begin parse document");
89   // Empty private elements
90   _resources_list.clear();
91   return true;
92 }
93
94 //----------------------------------------------------------------------
95 //Function : startElement
96 //Purpose: overload handler function 
97 //----------------------------------------------------------------------
98 bool SALOME_ResourcesCatalog_Handler::startElement(const QString&, 
99                                                const QString&,
100                                                const QString& name, 
101                                                const QXmlAttributes& attrs)
102 {
103   for(int i=0;i<attrs.count();i++)
104     {
105       QString qName(attrs.localName(i));
106       std::string content(attrs.value(i).latin1());
107       if((qName.compare(QString(test_hostname))==0))
108         _resource.DataForSort._hostName = content;
109       if((qName.compare(QString(test_alias))==0))
110         _resource.Alias = content;
111       if((qName.compare(QString(test_protocol))==0)){
112         switch(content[0]) {
113         case 'r':
114           _resource.Protocol = rsh;
115           break;
116         case 's':
117           _resource.Protocol = ssh;
118           break; 
119         default:
120           // If it'not in all theses cases, the protocol is affected to rsh
121           _resource.Protocol = rsh;
122           break;
123         }
124       }
125       if((qName.compare(QString(test_mode))==0))
126         {
127           switch(content[0]) {
128           case 'i':
129             _resource.Mode = interactive;
130             break;
131           case 'b':
132             _resource.Mode = batch;
133             break; 
134           default:
135             // If it'not in all theses cases, the mode is affected to interactive
136             _resource.Mode = interactive;
137             break;
138           }
139         }
140       if((qName.compare(QString(test_user_name))==0))
141         _resource.UserName = content;
142       if((qName.compare(QString(test_module_name))==0))
143         previous_module_name = content;
144       if((qName.compare(QString(test_module_path))==0))
145         previous_module_path = content;
146       if((qName.compare(QString(test_pre_req_file_path))==0))
147         _resource.PreReqFilePath = content;
148       if((qName.compare(QString(test_os))==0))
149         _resource.OS = content;
150       if((qName.compare(QString(test_mem_in_mb))==0))
151           _resource.DataForSort._memInMB = atoi(content.c_str());
152       if((qName.compare(QString(test_cpu_freq_mhz))==0))
153         _resource.DataForSort._CPUFreqMHz = atoi(content.c_str());
154       if((qName.compare(QString(test_nb_of_nodes))==0))
155         _resource.DataForSort._nbOfNodes = atoi(content.c_str());
156       if((qName.compare(QString(test_nb_of_proc_per_node))==0))
157         _resource.DataForSort._nbOfProcPerNode = atoi(content.c_str());
158     }
159   return true;
160 }
161
162 //----------------------------------------------------------------------
163 //Function : endElement
164 //Purpose: overload handler function 
165 //----------------------------------------------------------------------
166 bool SALOME_ResourcesCatalog_Handler::endElement(const QString&, const QString&,
167                                              const QString& qName)
168 {
169   if((qName.compare(QString(test_modules))==0))
170     _resource.ModulesPath[previous_module_name] = previous_module_path;
171   if((qName.compare(QString(test_machine))==0))
172     _resources_list[_resource.DataForSort._hostName]=_resource;
173   return true;
174 }
175
176 //----------------------------------------------------------------------
177 //Function : characters
178 //Purpose: overload handler function
179 //----------------------------------------------------------------------
180 bool SALOME_ResourcesCatalog_Handler::characters(const QString& chars)
181 {
182   content = (const char *)chars ;
183   return true;
184 }
185
186 //----------------------------------------------------------------------
187 //Function : endDocument
188 //Purpose: overload handler function 
189 //----------------------------------------------------------------------
190 bool SALOME_ResourcesCatalog_Handler::endDocument()
191 {
192   //_resources_list
193 //   for (unsigned int ind = 0; ind < _resources_list.size(); ind++)
194 //     {
195 //       MESSAGE("Resources name :"<<_resources_list[ind].Parsername);
196 //       MESSAGE("OS :"<<_resources_list[ind].ParserOS); 
197 //       MESSAGE("OS version :"<<_resources_list[ind].ParserOS_version);
198 //       for (unsigned int i = 0; i < _resources_list[ind].Parserprocs.size(); i++)
199 //      {
200 //        MESSAGE("Proc number :" << _resources_list[ind].Parserprocs[i].Parsernumber);
201 //        MESSAGE("Model name :" << _resources_list[ind].Parserprocs[i].Parsermodel_name);
202 //        MESSAGE("CPU(MHz) :" << _resources_list[ind].Parserprocs[i].Parsercpu_mhz);
203 //        MESSAGE("Cache :" << _resources_list[ind].Parserprocs[i].Parsercache_size);
204 //      }
205 //       for (unsigned int j = 0; j < _resources_list[ind].Parsercontainertype.size(); j++)
206 //      MESSAGE("Container Type :" << _resources_list[ind].Parsercontainertype[j]);
207 //     }
208   cout << "This is the end of document" << endl;
209   return true;
210 }
211
212 //----------------------------------------------------------------------
213 //Function : errorProtocol
214 //Purpose: overload handler function
215 //----------------------------------------------------------------------
216 QString SALOME_ResourcesCatalog_Handler::errorProtocol()
217 {
218   cout << "error prot !!!!!!!!!!!!!!!!!" << endl;
219   return errorProt;
220 }
221
222 //----------------------------------------------------------------------
223 //Function : fatalError
224 //Purpose: overload handler function
225 //----------------------------------------------------------------------
226 bool 
227 SALOME_ResourcesCatalog_Handler::fatalError(const QXmlParseException& exception)
228 {
229   cout << "fatal error !!!!!!!!!!!!!!!!!" << endl;
230     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
231     .arg( exception.message() )
232     .arg( exception.lineNumber() )
233     .arg( exception.columnNumber() );
234
235   return QXmlDefaultHandler::fatalError( exception );
236 }
237
238 //----------------------------------------------------------------------
239 //Function : FillDocument
240 //Purpose: Fill the document tree in xml file, used to write in xml file.
241 //----------------------------------------------------------------------
242 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(QDomDocument& doc)
243 {
244   QDomElement root = doc.createElement("resources");
245   doc.appendChild(root);
246   for(map<string, ParserResourcesType>::iterator iter=_resources_list.begin();iter!=_resources_list.end();iter++)
247     {
248       QDomElement eltRoot = doc.createElement(test_machine);
249       root.appendChild( eltRoot );
250       eltRoot.setAttribute((char *)test_hostname,(*iter).first.c_str());
251       eltRoot.setAttribute((char *)test_alias,(*iter).second.Alias.c_str());
252       switch((*iter).second.Protocol)
253         {
254         case rsh:
255           eltRoot.setAttribute((char *)test_protocol,"rsh");
256           break;
257         case ssh:
258           eltRoot.setAttribute((char *)test_protocol,"ssh");
259           break;
260         default:
261           eltRoot.setAttribute((char *)test_protocol,"rsh");
262         }
263       switch((*iter).second.Mode)
264         {
265         case interactive:
266           eltRoot.setAttribute((char *)test_mode,"interactive");
267           break;
268         case batch:
269           eltRoot.setAttribute((char *)test_mode,"batch");
270           break;
271         default:
272           eltRoot.setAttribute((char *)test_mode,"interactive");
273         }
274       eltRoot.setAttribute((char *)test_user_name,(*iter).second.UserName.c_str());
275       for(map<string, string>::const_iterator iter2=(*iter).second.ModulesPath.begin();iter2!=(*iter).second.ModulesPath.end();iter2++)
276         {
277           QDomElement rootForModulesPaths=doc.createElement(test_modules);
278           rootForModulesPaths.setAttribute(test_module_name,(*iter2).first.c_str());
279           rootForModulesPaths.setAttribute(test_module_path,(*iter2).second.c_str());
280           eltRoot.appendChild(rootForModulesPaths);
281         }
282       eltRoot.setAttribute(test_pre_req_file_path,(*iter).second.PreReqFilePath.c_str());
283       eltRoot.setAttribute(test_os,(*iter).second.OS.c_str());
284       eltRoot.setAttribute(test_mem_in_mb,(*iter).second.DataForSort._memInMB);
285       eltRoot.setAttribute(test_cpu_freq_mhz,(*iter).second.DataForSort._CPUFreqMHz);
286       eltRoot.setAttribute(test_nb_of_nodes,(*iter).second.DataForSort._nbOfNodes);
287       eltRoot.setAttribute(test_nb_of_proc_per_node,(*iter).second.DataForSort._nbOfProcPerNode);
288     }
289 }