1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SALOME ResourcesCatalog : implementation of catalog resources parsing (SALOME_ModuleCatalog.idl)
23 // File : SALOME_ResourcesCatalog_Handler.cxx
24 // Author : Estelle Deville
28 #include "SALOME_ResourcesCatalog_Handler.hxx"
29 #include "Basics_Utils.hxx"
35 //=============================================================================
38 * \param listOfResources: map of ParserResourcesType to fill when parsing
40 //=============================================================================
42 SALOME_ResourcesCatalog_Handler::
43 SALOME_ResourcesCatalog_Handler(MapOfParserResourcesType& resources_list,
44 MapOfParserResourcesType& resources_batch_list):
45 _resources_list(resources_list),
46 _resources_batch_list(resources_batch_list)
48 //XML tags initialisation
49 test_machine = "machine";
50 test_resources = "resources";
52 test_hostname = "hostname";
54 test_protocol = "protocol";
58 test_user_name = "userName";
59 test_appli_path = "appliPath";
60 test_modules = "modules";
61 test_module_name = "moduleName";
63 test_mem_in_mb = "memInMB";
64 test_cpu_freq_mhz = "CPUFreqMHz";
65 test_nb_of_nodes = "nbOfNodes";
66 test_nb_of_proc_per_node = "nbOfProcPerNode";
67 test_batch_queue = "batchQueue";
68 test_user_commands = "userCommands";
71 //=============================================================================
75 //=============================================================================
77 SALOME_ResourcesCatalog_Handler::~SALOME_ResourcesCatalog_Handler()
79 // cout << "SALOME_ResourcesCatalog_Handler destruction") << endl;
82 //=============================================================================
84 * Retrieves DS after the file parse.
86 //=============================================================================
88 const MapOfParserResourcesType&
89 SALOME_ResourcesCatalog_Handler::GetResourcesAfterParsing() const
91 return _resources_list;
94 //=============================================================================
96 * Processes XML document and fills the list of resources
98 //=============================================================================
100 void SALOME_ResourcesCatalog_Handler::ProcessXmlDocument(xmlDocPtr theDoc)
102 // Empty private elements
103 _resources_list.clear();
105 // Get the document root node
106 xmlNodePtr aCurNode = xmlDocGetRootElement(theDoc);
108 aCurNode = aCurNode->xmlChildrenNode;
110 // Processing the document nodes
111 while(aCurNode != NULL)
113 if ( !xmlStrcmp(aCurNode->name,(const xmlChar*)test_machine) )
116 if (xmlHasProp(aCurNode, (const xmlChar*)test_hostname))
118 xmlChar* hostname = xmlGetProp(aCurNode, (const xmlChar*)test_hostname);
119 _resource.DataForSort._hostName = (const char*)hostname;
120 _resource.HostName = (const char*)hostname;
126 if (xmlHasProp(aCurNode, (const xmlChar*)test_alias))
128 xmlChar* alias = xmlGetProp(aCurNode, (const xmlChar*)test_alias);
129 _resource.Alias = (const char*)alias;
133 _resource.Alias = "";
135 if (xmlHasProp(aCurNode, (const xmlChar*)test_batch_queue))
137 xmlChar* batch_queue = xmlGetProp(aCurNode, (const xmlChar*)test_batch_queue);
138 _resource.batchQueue = (const char*)batch_queue;
139 xmlFree(batch_queue);
142 _resource.batchQueue = "";
144 if (xmlHasProp(aCurNode, (const xmlChar*)test_user_commands))
146 xmlChar* user_commands= xmlGetProp(aCurNode, (const xmlChar*)test_user_commands);
147 _resource.userCommands = (const char*)user_commands;
148 xmlFree(user_commands);
151 _resource.userCommands = "";
153 if (xmlHasProp(aCurNode, (const xmlChar*)test_protocol))
155 xmlChar* protocol= xmlGetProp(aCurNode, (const xmlChar*)test_protocol);
156 switch ( protocol[0])
159 _resource.Protocol = rsh;
162 _resource.Protocol = ssh;
165 // If it'not in all theses cases, the protocol is affected to rsh
166 _resource.Protocol = rsh;
172 _resource.Protocol = rsh;
174 if (xmlHasProp(aCurNode, (const xmlChar*)test_mode))
176 xmlChar* mode=xmlGetProp(aCurNode, (const xmlChar*)test_mode);
180 _resource.Mode = interactive;
183 _resource.Mode = batch;
186 // If it'not in all theses cases, the mode is affected to interactive
187 _resource.Mode = interactive;
193 _resource.Mode = interactive;
195 if (xmlHasProp(aCurNode, (const xmlChar*)test_batch))
197 xmlChar* batch = xmlGetProp(aCurNode, (const xmlChar*)test_batch);
198 std::string aBatch = (const char*)batch;
201 _resource.Batch = pbs;
202 else if (aBatch == "lsf")
203 _resource.Batch = lsf;
204 else if (aBatch == "sge")
205 _resource.Batch = sge;
207 _resource.Batch = none;
210 if (xmlHasProp(aCurNode, (const xmlChar*)test_mpi))
212 xmlChar* mpi = xmlGetProp(aCurNode, (const xmlChar*)test_mpi);
213 std::string anMpi = (const char*)mpi;
217 else if (anMpi == "mpich1")
218 _resource.mpi = mpich1;
219 else if (anMpi == "mpich2")
220 _resource.mpi = mpich2;
221 else if (anMpi == "openmpi")
222 _resource.mpi = openmpi;
223 else if (anMpi == "slurm")
224 _resource.mpi = slurm;
225 else if (anMpi == "prun")
226 _resource.mpi = prun;
228 _resource.mpi = nompi;
231 if (xmlHasProp(aCurNode, (const xmlChar*)test_user_name))
233 xmlChar* user_name= xmlGetProp(aCurNode, (const xmlChar*)test_user_name);
234 _resource.UserName = (const char*)user_name;
238 if (xmlHasProp(aCurNode, (const xmlChar*)test_appli_path))
240 xmlChar* appli_path = xmlGetProp(aCurNode, (const xmlChar*)test_appli_path);
241 _resource.AppliPath = (const char*)appli_path;
245 if (xmlHasProp(aCurNode, (const xmlChar*)test_os))
247 xmlChar* os = xmlGetProp(aCurNode, (const xmlChar*)test_os);
248 _resource.OS = (const char*)os;
252 if (xmlHasProp(aCurNode, (const xmlChar*)test_mem_in_mb))
254 xmlChar* mem_in_mb = xmlGetProp(aCurNode, (const xmlChar*)test_mem_in_mb);
255 _resource.DataForSort._memInMB = atoi((const char*)mem_in_mb);
259 if (xmlHasProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz))
261 xmlChar* cpu_freq_mhz = xmlGetProp(aCurNode, (const xmlChar*)test_cpu_freq_mhz);
262 _resource.DataForSort._CPUFreqMHz = atoi((const char*)cpu_freq_mhz);
263 xmlFree(cpu_freq_mhz);
266 if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_nodes))
268 xmlChar* nb_of_nodes = xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_nodes);
269 _resource.DataForSort._nbOfNodes = atoi((const char*)nb_of_nodes);
270 xmlFree(nb_of_nodes);
273 if (xmlHasProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node))
275 xmlChar* nb_of_proc_per_node = xmlGetProp(aCurNode, (const xmlChar*)test_nb_of_proc_per_node);
276 _resource.DataForSort._nbOfProcPerNode = atoi((const char*)nb_of_proc_per_node);
277 xmlFree(nb_of_proc_per_node);
281 xmlNodePtr aCurSubNode = aCurNode->xmlChildrenNode;
282 while(aCurSubNode != NULL)
284 if ( !xmlStrcmp(aCurSubNode->name, (const xmlChar*)test_modules) )
286 if (xmlHasProp(aCurSubNode, (const xmlChar*)test_module_name))
288 xmlChar* module_name = xmlGetProp(aCurSubNode, (const xmlChar*)test_module_name);
289 std::string aModuleName = (const char*)module_name;
290 _resource.ModulesList.push_back(aModuleName);
291 xmlFree(module_name);
294 aCurSubNode = aCurSubNode->next;
297 // There is two lists
298 // _resources_list for interactive resources
299 // _resources_batch_list for batch resources
300 // This choice is done with Mode parameter
301 if (_resource.Mode == interactive)
303 int aNbNodes = _resource.DataForSort._nbOfNodes;
305 string clusterNode = _resource.DataForSort._hostName ;
306 for( int i=0; i < aNbNodes; i++ ){
309 sprintf(inode,"%s%d",clusterNode.c_str(),i+1);
310 std::string nodeName(inode);
311 _resource.DataForSort._hostName = nodeName ;
312 _resource.HostName = nodeName ;
313 _resources_list[nodeName] = _resource;
318 _resources_list[_resource.HostName] = _resource;
319 if(_resource.HostName == "localhost")
320 _resources_list[Kernel_Utils::GetHostname()] = _resource;
324 _resources_batch_list[_resource.HostName] = _resource;
326 aCurNode = aCurNode->next;
330 for (map<string, ParserResourcesType>::const_iterator iter =
331 _resources_list.begin();
332 iter != _resources_list.end();
335 std::cerr << (*iter).second.HostName << std::endl;
336 std::cerr << (*iter).second.Alias << std::endl;
337 std::cerr << (*iter).second.UserName << std::endl;
338 std::cerr << (*iter).second.AppliPath << std::endl;
339 std::cerr << (*iter).second.OS << std::endl;
340 std::cerr << (*iter).second.Protocol << std::endl;
341 std::cerr << (*iter).second.Mode << std::endl;
348 //=============================================================================
350 * Fill the document tree in xml file, used to write in an xml file.
351 * \param theDoc document to fill.
353 //=============================================================================
355 void SALOME_ResourcesCatalog_Handler::PrepareDocToXmlFile(xmlDocPtr theDoc)
358 xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
361 root_node = xmlNewNode(NULL, BAD_CAST "resources");
362 xmlDocSetRootElement(theDoc, root_node);
364 for (map<string, ParserResourcesType>::iterator iter =
365 _resources_list.begin();
366 iter != _resources_list.end();
369 node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
370 xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).second.HostName.c_str());
371 xmlNewProp(node, BAD_CAST test_alias, BAD_CAST (*iter).second.Alias.c_str());
372 xmlNewProp(node, BAD_CAST test_batch_queue, BAD_CAST (*iter).second.batchQueue.c_str());
373 xmlNewProp(node, BAD_CAST test_user_commands, BAD_CAST (*iter).second.userCommands.c_str());
375 switch ((*iter).second.Protocol)
378 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
381 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
384 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
387 switch ((*iter).second.Mode)
390 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
393 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
396 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
399 switch ((*iter).second.Batch)
402 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "pbs");
405 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "lsf");
408 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "sge");
411 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "");
414 switch ((*iter).second.mpi)
417 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "lam");
420 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich1");
423 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich2");
426 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "openmpi");
429 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "slurm");
432 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "prun");
435 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "");
438 xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
440 for (vector<string>::const_iterator iter2 =
441 (*iter).second.ModulesList.begin();
442 iter2 != (*iter).second.ModulesList.end();
445 node1 = xmlNewChild(node, NULL, BAD_CAST test_modules, NULL);
446 xmlNewProp(node1, BAD_CAST test_module_name, BAD_CAST (*iter2).c_str());
449 xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
450 xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._memInMB));
451 xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._CPUFreqMHz));
452 xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfNodes));
453 xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfProcPerNode));
455 for (map<string, ParserResourcesType>::iterator iter =
456 _resources_batch_list.begin();
457 iter != _resources_batch_list.end();
460 node = xmlNewChild(root_node, NULL, BAD_CAST test_machine, NULL);
461 xmlNewProp(node, BAD_CAST test_hostname, BAD_CAST (*iter).second.HostName.c_str());
462 xmlNewProp(node, BAD_CAST test_alias, BAD_CAST (*iter).second.Alias.c_str());
464 switch ((*iter).second.Protocol)
467 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
470 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "ssh");
473 xmlNewProp(node, BAD_CAST test_protocol, BAD_CAST "rsh");
476 switch ((*iter).second.Mode)
479 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
482 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "batch");
485 xmlNewProp(node, BAD_CAST test_mode, BAD_CAST "interactive");
488 switch ((*iter).second.Batch)
491 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "pbs");
494 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "lsf");
497 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "sge");
500 xmlNewProp(node, BAD_CAST test_batch, BAD_CAST "");
503 switch ((*iter).second.mpi)
506 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "lam");
509 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich1");
512 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "mpich2");
515 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "openmpi");
518 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "slurm");
521 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "prun");
524 xmlNewProp(node, BAD_CAST test_mpi, BAD_CAST "");
527 xmlNewProp(node, BAD_CAST test_user_name, BAD_CAST (*iter).second.UserName.c_str());
529 for (vector<string>::const_iterator iter2 =
530 (*iter).second.ModulesList.begin();
531 iter2 != (*iter).second.ModulesList.end();
534 node1 = xmlNewChild(node, NULL, BAD_CAST test_modules, NULL);
535 xmlNewProp(node1, BAD_CAST test_module_name, BAD_CAST (*iter2).c_str());
538 xmlNewProp(node, BAD_CAST test_os, BAD_CAST (*iter).second.OS.c_str());
539 xmlNewProp(node, BAD_CAST test_mem_in_mb, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._memInMB));
540 xmlNewProp(node, BAD_CAST test_cpu_freq_mhz, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._CPUFreqMHz));
541 xmlNewProp(node, BAD_CAST test_nb_of_nodes, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfNodes));
542 xmlNewProp(node, BAD_CAST test_nb_of_proc_per_node, BAD_CAST sprintf(string_buf, "%u", (*iter).second.DataForSort._nbOfProcPerNode));