Salome HOME
Merging with WPDev
[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 <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     int nbnodes = _resource.DataForSort._nbOfNodes;
235     if( nbnodes > 1 ){
236       string clusterNode = _resource.DataForSort._hostName ;
237       for(int i=0;i<nbnodes;i++){
238         char inode[64];
239         inode[0] = '\0' ;
240         sprintf(inode,"%s%d",clusterNode.c_str(),i+1);
241         std::string nodeName(inode);
242 //        _resource.DataForSort._nbOfNodes = 1;
243         _resource.DataForSort._hostName = nodeName ;
244         _resources_list[nodeName] = _resource;
245         //cout << "SALOME_ResourcesCatalog_Handler::endElement _resources_list["
246         //     << nodeName << "] = _resource " << _resource.DataForSort._hostName.c_str()
247         //     << endl ;
248       }
249     }
250     else
251       _resources_list[_resource.DataForSort._hostName] = _resource;
252   }
253
254   return true;
255 }
256
257 //=============================================================================
258 /*!
259  *  Overload handler function characters.
260  *  fills the private attribute string 'content'.
261  *     \param chars  
262  *     \return true (if no error detected ...)
263  */ 
264 //=============================================================================
265
266 bool SALOME_ResourcesCatalog_Handler::characters(const QString& chars)
267 {
268   content = (const char *)chars ;
269   return true;
270 }
271
272 //=============================================================================
273 /*!
274  *  Overload handler function endDocument.
275  *  Called after the document has been parsed.
276  *     \return true (if no error detected ...)
277  */ 
278 //=============================================================================
279
280 bool SALOME_ResourcesCatalog_Handler::endDocument()
281 {
282 //   for (map<string, ParserResourcesType>::const_iterator iter =
283 //          _resources_list.begin();
284 //        iter != _resources_list.end();
285 //        iter++)
286 //     {
287 //       SCRUTE((*iter).second.Alias);
288 //       SCRUTE((*iter).second.UserName);
289 //       SCRUTE((*iter).second.AppliPath);
290 //       SCRUTE((*iter).second.PreReqFilePath);
291 //       SCRUTE((*iter).second.OS);
292 //       SCRUTE((*iter).second.Protocol);
293 //       SCRUTE((*iter).second.Mode);
294 //    }
295   
296 //  MESSAGE("This is the end of document");
297   return true;
298 }
299
300 //=============================================================================
301 /*!
302  *  Overload handler function errorProtocol.
303  *  \return the error message.
304  */ 
305 //=============================================================================
306
307 QString SALOME_ResourcesCatalog_Handler::errorProtocol()
308 {
309   INFOS(" ------------- error protocol !");
310   return errorProt;
311 }
312
313 //=============================================================================
314 /*!
315  *  Overload handler function fatalError.
316  *  Fills the private string errorProt with details on error.
317  *     \param exception from parser
318  *     \return boolean (meaning ?)
319  */
320 //=============================================================================
321
322 bool
323 SALOME_ResourcesCatalog_Handler::fatalError
324 (const QXmlParseException& exception)
325 {
326   INFOS(" ------------- fatal error !");
327   errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
328                .arg( exception.message() )
329                .arg( exception.lineNumber() )
330                .arg( exception.columnNumber() );
331   INFOS("parser error: " << errorProt.latin1());
332
333   return QXmlDefaultHandler::fatalError( exception );
334 }
335
336 //=============================================================================
337 /*!
338  *  Fill the document tree in xml file, used to write in an xml file.
339  *  \param doc document to fill.
340  */ 
341 //=============================================================================
342
343 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(QDomDocument& doc)
344 {
345   QDomElement root = doc.createElement("resources");
346   doc.appendChild(root);
347
348   for (map<string, ParserResourcesType>::iterator iter =
349          _resources_list.begin();
350        iter != _resources_list.end();
351        iter++)
352     {
353       QDomElement eltRoot = doc.createElement(test_machine);
354       root.appendChild( eltRoot );
355       eltRoot.setAttribute((char *)test_hostname, (*iter).first.c_str());
356       eltRoot.setAttribute((char *)test_alias, (*iter).second.Alias.c_str());
357
358       switch ((*iter).second.Protocol)
359         {
360
361         case rsh:
362           eltRoot.setAttribute((char *)test_protocol, "rsh");
363           break;
364
365         case ssh:
366           eltRoot.setAttribute((char *)test_protocol, "ssh");
367           break;
368
369         default:
370           eltRoot.setAttribute((char *)test_protocol, "rsh");
371         }
372
373       switch ((*iter).second.Mode)
374         {
375
376         case interactive:
377           eltRoot.setAttribute((char *)test_mode, "interactive");
378           break;
379
380         case batch:
381           eltRoot.setAttribute((char *)test_mode, "batch");
382           break;
383
384         default:
385           eltRoot.setAttribute((char *)test_mode, "interactive");
386         }
387
388       eltRoot.setAttribute((char *)test_user_name,
389                            (*iter).second.UserName.c_str());
390
391       for (map<string, string>::const_iterator iter2 =
392              (*iter).second.ModulesPath.begin();
393            iter2 != (*iter).second.ModulesPath.end();
394            iter2++)
395         {
396           QDomElement rootForModulesPaths = doc.createElement(test_modules);
397           rootForModulesPaths.setAttribute(test_module_name,
398                                            (*iter2).first.c_str());
399           rootForModulesPaths.setAttribute(test_module_path,
400                                            (*iter2).second.c_str());
401           eltRoot.appendChild(rootForModulesPaths);
402         }
403
404       eltRoot.setAttribute(test_pre_req_file_path,
405                            (*iter).second.PreReqFilePath.c_str());
406       eltRoot.setAttribute(test_os, (*iter).second.OS.c_str());
407       eltRoot.setAttribute(test_mem_in_mb,
408                            (*iter).second.DataForSort._memInMB);
409       eltRoot.setAttribute(test_cpu_freq_mhz,
410                            (*iter).second.DataForSort._CPUFreqMHz);
411       eltRoot.setAttribute(test_nb_of_nodes,
412                            (*iter).second.DataForSort._nbOfNodes);
413       eltRoot.setAttribute(test_nb_of_proc_per_node,
414                            (*iter).second.DataForSort._nbOfProcPerNode);
415     }
416 }