1 // SALOME ResourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SALOME_ResourcesCatalog_Handler.cxx
25 // Author : Estelle Deville
29 #include "SALOME_ResourcesCatalog_Handler.hxx"
33 #include "utilities.h"
37 //=============================================================================
40 * \param listOfResources: map of ParserResourcesType to fill when parsing
42 //=============================================================================
44 SALOME_ResourcesCatalog_Handler::
45 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& listOfResources):
46 _resources_list(listOfResources)
48 MESSAGE("SALOME_ResourcesCatalog_Handler creation");
49 //XML tags initialisation
50 test_machine = "machine";
51 test_resources = "resources";
53 test_hostname = "hostname";
55 test_protocol = "protocol";
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";
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";
70 //=============================================================================
74 //=============================================================================
76 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
78 // MESSAGE("SALOME_ResourcesCatalog_Handler destruction");
81 //=============================================================================
83 * Retrieves DS after the file parse.
85 //=============================================================================
87 const MapOfParserResourcesType&
88 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
90 return _resources_list;
93 //=============================================================================
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...)
100 //=============================================================================
102 bool SALOME_ResourcesCatalog_Handler::startDocument()
104 // MESSAGE("Begin parse document");
106 // --- Empty private elements
108 _resources_list.clear();
112 //=============================================================================
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 ?)
119 * \return true if no error was detected
121 //=============================================================================
124 SALOME_ResourcesCatalog_Handler::
125 startElement( const QString&,
128 const QXmlAttributes& attrs )
130 for (int i = 0;i < attrs.count();i++)
132 QString qName(attrs.localName(i));
133 std::string content(attrs.value(i).latin1());
135 if ((qName.compare(QString(test_hostname)) == 0))
136 _resource.DataForSort._hostName = content;
138 if ((qName.compare(QString(test_alias)) == 0))
139 _resource.Alias = content;
141 if ((qName.compare(QString(test_protocol)) == 0))
147 _resource.Protocol = rsh;
151 _resource.Protocol = ssh;
155 // If it'not in all theses cases, the protocol is affected to rsh
156 _resource.Protocol = rsh;
161 if ((qName.compare(QString(test_mode)) == 0))
167 _resource.Mode = interactive;
171 _resource.Mode = batch;
175 // If it'not in all theses cases, the mode is affected to interactive
176 _resource.Mode = interactive;
181 if ((qName.compare(QString(test_user_name)) == 0))
182 _resource.UserName = content;
184 if ((qName.compare(QString(test_appli_path)) == 0))
185 _resource.AppliPath = content;
187 if ((qName.compare(QString(test_module_name)) == 0))
188 previous_module_name = content;
190 if ((qName.compare(QString(test_module_path)) == 0))
191 previous_module_path = content;
193 if ((qName.compare(QString(test_pre_req_file_path)) == 0))
194 _resource.PreReqFilePath = content;
196 if ((qName.compare(QString(test_os)) == 0))
197 _resource.OS = content;
199 if ((qName.compare(QString(test_mem_in_mb)) == 0))
200 _resource.DataForSort._memInMB = atoi(content.c_str());
202 if ((qName.compare(QString(test_cpu_freq_mhz)) == 0))
203 _resource.DataForSort._CPUFreqMHz = atoi(content.c_str());
205 if ((qName.compare(QString(test_nb_of_nodes)) == 0))
206 _resource.DataForSort._nbOfNodes = atoi(content.c_str());
208 if ((qName.compare(QString(test_nb_of_proc_per_node)) == 0))
209 _resource.DataForSort._nbOfProcPerNode = atoi(content.c_str());
215 //=============================================================================
217 * Overload handler function endElement.
218 * \param QString argument by reference (not used here ?)
219 * \param QString argument by reference (not used here ?)
221 * \return true (if no error detected ...)
223 //=============================================================================
225 bool SALOME_ResourcesCatalog_Handler::
226 endElement(const QString&,
228 const QString& qName)
230 if ((qName.compare(QString(test_modules)) == 0))
231 _resource.ModulesPath[previous_module_name] = previous_module_path;
233 if ((qName.compare(QString(test_machine)) == 0)){
234 int nbnodes = _resource.DataForSort._nbOfNodes;
236 string clusterNode = _resource.DataForSort._hostName ;
237 for(int i=0;i<nbnodes;i++){
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()
251 _resources_list[_resource.DataForSort._hostName] = _resource;
257 //=============================================================================
259 * Overload handler function characters.
260 * fills the private attribute string 'content'.
262 * \return true (if no error detected ...)
264 //=============================================================================
266 bool SALOME_ResourcesCatalog_Handler::characters(const QString& chars)
268 content = (const char *)chars ;
272 //=============================================================================
274 * Overload handler function endDocument.
275 * Called after the document has been parsed.
276 * \return true (if no error detected ...)
278 //=============================================================================
280 bool SALOME_ResourcesCatalog_Handler::endDocument()
282 // for (map<string, ParserResourcesType>::const_iterator iter =
283 // _resources_list.begin();
284 // iter != _resources_list.end();
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);
296 // MESSAGE("This is the end of document");
300 //=============================================================================
302 * Overload handler function errorProtocol.
303 * \return the error message.
305 //=============================================================================
307 QString SALOME_ResourcesCatalog_Handler::errorProtocol()
309 INFOS(" ------------- error protocol !");
313 //=============================================================================
315 * Overload handler function fatalError.
316 * Fills the private string errorProt with details on error.
317 * \param exception from parser
318 * \return boolean (meaning ?)
320 //=============================================================================
323 SALOME_ResourcesCatalog_Handler::fatalError
324 (const QXmlParseException& exception)
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());
333 return QXmlDefaultHandler::fatalError( exception );
336 //=============================================================================
338 * Fill the document tree in xml file, used to write in an xml file.
339 * \param doc document to fill.
341 //=============================================================================
343 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(QDomDocument& doc)
345 QDomElement root = doc.createElement("resources");
346 doc.appendChild(root);
348 for (map<string, ParserResourcesType>::iterator iter =
349 _resources_list.begin();
350 iter != _resources_list.end();
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());
358 switch ((*iter).second.Protocol)
362 eltRoot.setAttribute((char *)test_protocol, "rsh");
366 eltRoot.setAttribute((char *)test_protocol, "ssh");
370 eltRoot.setAttribute((char *)test_protocol, "rsh");
373 switch ((*iter).second.Mode)
377 eltRoot.setAttribute((char *)test_mode, "interactive");
381 eltRoot.setAttribute((char *)test_mode, "batch");
385 eltRoot.setAttribute((char *)test_mode, "interactive");
388 eltRoot.setAttribute((char *)test_user_name,
389 (*iter).second.UserName.c_str());
391 for (map<string, string>::const_iterator iter2 =
392 (*iter).second.ModulesPath.begin();
393 iter2 != (*iter).second.ModulesPath.end();
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);
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);