Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[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 /*!
39  *  Constructor
40  *  \param listOfResources: map of ParserResourcesType to fill when parsing
41  */ 
42 //=============================================================================
43
44 SALOME_ResourcesCatalog_Handler::
45 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& listOfResources):
46     _resources_list(listOfResources)
47 {
48   MESSAGE("SALOME_ResourcesCatalog_Handler creation");
49   //XML tags initialisation
50   test_machine = "machine";
51   test_resources = "resources";
52
53   test_hostname = "hostname";
54   test_alias = "alias";
55   test_protocol = "protocol";
56   test_mode = "mode";
57   test_user_name = "userName";
58   test_appli_path = "appliPath";
59   test_modules = "modules";
60   test_module_name = "moduleName";
61   test_module_path = "modulePath";
62   test_pre_req_file_path = "preReqFilePath";
63   test_os = "OS";
64   test_mem_in_mb = "memInMB";
65   test_cpu_freq_mhz = "CPUFreqMHz";
66   test_nb_of_nodes = "nbOfNodes";
67   test_nb_of_proc_per_node = "nbOfProcPerNode";
68 }
69
70 //=============================================================================
71 /*!
72  *  Destructor
73  */ 
74 //=============================================================================
75
76 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
77 {
78   MESSAGE("SALOME_ResourcesCatalog_Handler destruction");
79 }
80
81 //=============================================================================
82 /*!
83  *  Retrieves DS after the file parse.
84  */ 
85 //=============================================================================
86
87 const MapOfParserResourcesType&
88 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
89   {
90     return _resources_list;
91   }
92
93 //=============================================================================
94 /*!
95  *  Overload handler function startDocument.
96  *  Called before an xml file is parsed.
97  *  Clears the list of resources.
98  *  \return true (if no error detected...)
99  */ 
100 //=============================================================================
101
102 bool SALOME_ResourcesCatalog_Handler::startDocument()
103 {
104   MESSAGE("Begin parse document");
105
106   // --- Empty private elements
107
108   _resources_list.clear();
109   return true;
110 }
111
112 //=============================================================================
113 /*!
114  *  Overload handler function startElement.
115  *    \param QString argument by reference (not used here ?)
116  *    \param QString argument by reference (not used here ?)
117  *    \param name                          (not used here ?)
118  *    \param atts
119  *    \return true if no error was detected
120  */ 
121 //=============================================================================
122
123 bool
124 SALOME_ResourcesCatalog_Handler::
125 startElement( const QString&,
126               const QString&,
127               const QString& name,
128               const QXmlAttributes& attrs )
129 {
130   for (int i = 0;i < attrs.count();i++)
131     {
132       QString qName(attrs.localName(i));
133       std::string content(attrs.value(i).latin1());
134
135       if ((qName.compare(QString(test_hostname)) == 0))
136         _resource.DataForSort._hostName = content;
137
138       if ((qName.compare(QString(test_alias)) == 0))
139         _resource.Alias = content;
140
141       if ((qName.compare(QString(test_protocol)) == 0))
142         {
143           switch (content[0])
144             {
145
146             case 'r':
147               _resource.Protocol = rsh;
148               break;
149
150             case 's':
151               _resource.Protocol = ssh;
152               break;
153
154             default:
155               // If it'not in all theses cases, the protocol is affected to rsh
156               _resource.Protocol = rsh;
157               break;
158             }
159         }
160
161       if ((qName.compare(QString(test_mode)) == 0))
162         {
163           switch (content[0])
164             {
165
166             case 'i':
167               _resource.Mode = interactive;
168               break;
169
170             case 'b':
171               _resource.Mode = batch;
172               break;
173
174             default:
175               // If it'not in all theses cases, the mode is affected to interactive
176               _resource.Mode = interactive;
177               break;
178             }
179         }
180
181       if ((qName.compare(QString(test_user_name)) == 0))
182         _resource.UserName = content;
183
184       if ((qName.compare(QString(test_appli_path)) == 0))
185         _resource.AppliPath = content;
186
187       if ((qName.compare(QString(test_module_name)) == 0))
188         previous_module_name = content;
189
190       if ((qName.compare(QString(test_module_path)) == 0))
191         previous_module_path = content;
192
193       if ((qName.compare(QString(test_pre_req_file_path)) == 0))
194         _resource.PreReqFilePath = content;
195
196       if ((qName.compare(QString(test_os)) == 0))
197         _resource.OS = content;
198
199       if ((qName.compare(QString(test_mem_in_mb)) == 0))
200         _resource.DataForSort._memInMB = atoi(content.c_str());
201
202       if ((qName.compare(QString(test_cpu_freq_mhz)) == 0))
203         _resource.DataForSort._CPUFreqMHz = atoi(content.c_str());
204
205       if ((qName.compare(QString(test_nb_of_nodes)) == 0))
206         _resource.DataForSort._nbOfNodes = atoi(content.c_str());
207
208       if ((qName.compare(QString(test_nb_of_proc_per_node)) == 0))
209         _resource.DataForSort._nbOfProcPerNode = atoi(content.c_str());
210     }
211
212   return true;
213 }
214
215 //=============================================================================
216 /*!
217  *  Overload handler function endElement.
218  *     \param QString argument by reference  (not used here ?)
219  *     \param QString argument by reference  (not used here ?)
220  *     \param qName 
221  *     \return true (if no error detected ...)
222  */ 
223 //=============================================================================
224
225 bool SALOME_ResourcesCatalog_Handler::
226 endElement(const QString&,
227            const QString&,
228            const QString& qName)
229 {
230   if ((qName.compare(QString(test_modules)) == 0))
231     _resource.ModulesPath[previous_module_name] = previous_module_path;
232
233   if ((qName.compare(QString(test_machine)) == 0))
234     _resources_list[_resource.DataForSort._hostName] = _resource;
235
236   return true;
237 }
238
239 //=============================================================================
240 /*!
241  *  Overload handler function characters.
242  *  fills the private attribute string 'content'.
243  *     \param chars  
244  *     \return true (if no error detected ...)
245  */ 
246 //=============================================================================
247
248 bool SALOME_ResourcesCatalog_Handler::characters(const QString& chars)
249 {
250   content = (const char *)chars ;
251   return true;
252 }
253
254 //=============================================================================
255 /*!
256  *  Overload handler function endDocument.
257  *  Called after the document has been parsed.
258  *     \return true (if no error detected ...)
259  */ 
260 //=============================================================================
261
262 bool SALOME_ResourcesCatalog_Handler::endDocument()
263 {
264   for (map<string, ParserResourcesType>::const_iterator iter =
265          _resources_list.begin();
266        iter != _resources_list.end();
267        iter++)
268     {
269       SCRUTE((*iter).second.Alias);
270       SCRUTE((*iter).second.UserName);
271       SCRUTE((*iter).second.AppliPath);
272       SCRUTE((*iter).second.PreReqFilePath);
273       SCRUTE((*iter).second.OS);
274       SCRUTE((*iter).second.Protocol);
275       SCRUTE((*iter).second.Mode);
276    }
277   
278   MESSAGE("This is the end of document");
279   return true;
280 }
281
282 //=============================================================================
283 /*!
284  *  Overload handler function errorProtocol.
285  *  \return the error message.
286  */ 
287 //=============================================================================
288
289 QString SALOME_ResourcesCatalog_Handler::errorProtocol()
290 {
291   INFOS(" ------------- error protocol !");
292   return errorProt;
293 }
294
295 //=============================================================================
296 /*!
297  *  Overload handler function fatalError.
298  *  Fills the private string errorProt with details on error.
299  *     \param exception from parser
300  *     \return boolean (meaning ?)
301  */
302 //=============================================================================
303
304 bool
305 SALOME_ResourcesCatalog_Handler::fatalError
306 (const QXmlParseException& exception)
307 {
308   INFOS(" ------------- fatal error !");
309   errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
310                .arg( exception.message() )
311                .arg( exception.lineNumber() )
312                .arg( exception.columnNumber() );
313
314   return QXmlDefaultHandler::fatalError( exception );
315 }
316
317 //=============================================================================
318 /*!
319  *  Fill the document tree in xml file, used to write in an xml file.
320  *  \param doc document to fill.
321  */ 
322 //=============================================================================
323
324 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(QDomDocument& doc)
325 {
326   QDomElement root = doc.createElement("resources");
327   doc.appendChild(root);
328
329   for (map<string, ParserResourcesType>::iterator iter =
330          _resources_list.begin();
331        iter != _resources_list.end();
332        iter++)
333     {
334       QDomElement eltRoot = doc.createElement(test_machine);
335       root.appendChild( eltRoot );
336       eltRoot.setAttribute((char *)test_hostname, (*iter).first.c_str());
337       eltRoot.setAttribute((char *)test_alias, (*iter).second.Alias.c_str());
338
339       switch ((*iter).second.Protocol)
340         {
341
342         case rsh:
343           eltRoot.setAttribute((char *)test_protocol, "rsh");
344           break;
345
346         case ssh:
347           eltRoot.setAttribute((char *)test_protocol, "ssh");
348           break;
349
350         default:
351           eltRoot.setAttribute((char *)test_protocol, "rsh");
352         }
353
354       switch ((*iter).second.Mode)
355         {
356
357         case interactive:
358           eltRoot.setAttribute((char *)test_mode, "interactive");
359           break;
360
361         case batch:
362           eltRoot.setAttribute((char *)test_mode, "batch");
363           break;
364
365         default:
366           eltRoot.setAttribute((char *)test_mode, "interactive");
367         }
368
369       eltRoot.setAttribute((char *)test_user_name,
370                            (*iter).second.UserName.c_str());
371
372       for (map<string, string>::const_iterator iter2 =
373              (*iter).second.ModulesPath.begin();
374            iter2 != (*iter).second.ModulesPath.end();
375            iter2++)
376         {
377           QDomElement rootForModulesPaths = doc.createElement(test_modules);
378           rootForModulesPaths.setAttribute(test_module_name,
379                                            (*iter2).first.c_str());
380           rootForModulesPaths.setAttribute(test_module_path,
381                                            (*iter2).second.c_str());
382           eltRoot.appendChild(rootForModulesPaths);
383         }
384
385       eltRoot.setAttribute(test_pre_req_file_path,
386                            (*iter).second.PreReqFilePath.c_str());
387       eltRoot.setAttribute(test_os, (*iter).second.OS.c_str());
388       eltRoot.setAttribute(test_mem_in_mb,
389                            (*iter).second.DataForSort._memInMB);
390       eltRoot.setAttribute(test_cpu_freq_mhz,
391                            (*iter).second.DataForSort._CPUFreqMHz);
392       eltRoot.setAttribute(test_nb_of_nodes,
393                            (*iter).second.DataForSort._nbOfNodes);
394       eltRoot.setAttribute(test_nb_of_proc_per_node,
395                            (*iter).second.DataForSort._nbOfProcPerNode);
396     }
397 }