1 // SALOME RessourcesCatalog : 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_RessourcesCatalog_impl.cxx
25 // Author : Estelle Deville
28 #include "SALOME_RessourcesCatalog_impl.hxx"
31 #include "Utils_ExceptHandlers.hxx"
33 UNEXPECT_CATCH(RC_NotFound, SALOME_RessourcesCatalog::NotFound);
35 //----------------------------------------------------------------------
36 // Function : SALOME_RessourcesCatalogImpl
37 // Purpose : Constructor
38 //----------------------------------------------------------------------
39 SALOME_RessourcesCatalogImpl::SALOME_RessourcesCatalogImpl(int argc, char** argv)
41 MESSAGE("Ressources Catalog creation");
43 // Empty used variables
44 _ressourceslist.resize(0);
46 // Parse the arguments given at server run
47 if (!_parseArguments(argc, argv,&_path_ressources))
48 MESSAGE( "Error while argument parsing" )
50 // Test existency of files
51 if (_path_ressources == NULL)
52 MESSAGE( "Error the ressources catalog should be indicated" )
55 MESSAGE("Parse ressources catalog");
56 // Affect the variable _ressourceslist
57 _parse_xml_file(_path_ressources,_ressourceslist);
59 // Verification of _datatype_list content
60 if(!_verify_ressources(_ressourceslist))
61 MESSAGE( "Error while parsing the ressources catalog" )
62 else MESSAGE("Ressources Catalog OK");
66 //----------------------------------------------------------------------
67 // Function : ~SALOME_RessourcesCatalogImpl
68 // Purpose : Destructor
69 //----------------------------------------------------------------------
70 SALOME_RessourcesCatalogImpl::~SALOME_RessourcesCatalogImpl()
72 MESSAGE("Ressources Catalog Destruction");
75 //----------------------------------------------------------------------
76 // Function : GetComputerList
77 // Purpose : get ressources list
78 //----------------------------------------------------------------------
79 SALOME_RessourcesCatalog::ListOfComputer*
80 SALOME_RessourcesCatalogImpl::GetComputerList()
82 MESSAGE("Begin of GetComputerList");
83 SALOME_RessourcesCatalog::ListOfComputer_var _list = new SALOME_RessourcesCatalog::ListOfComputer;
84 _list->length(_ressourceslist.size());
85 for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
87 _list[ind]=CORBA::string_dup(_ressourceslist[ind].Parsername.c_str());
93 //----------------------------------------------------------------------
94 // Function : GetRessourcesList
95 // Purpose : get a data type name list
96 //----------------------------------------------------------------------
97 SALOME_RessourcesCatalog::ListOfContainerType*
98 SALOME_RessourcesCatalogImpl::GetContainerTypeList(const char* computer)
99 throw(SALOME_RessourcesCatalog::NotFound)
101 Unexpect aCatch(RC_NotFound);
102 MESSAGE("Begin of GetContainerTypeList");
103 SALOME_RessourcesCatalog::ListOfContainerType_var _list =
104 new SALOME_RessourcesCatalog::ListOfContainerType;
108 // Looking for ressources named "computer"
109 // If found, get container type list
110 // If not found, exception is thrown
112 for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
114 if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
116 MESSAGE("Ressources named " << computer << " found in the ressources catalog");
119 _list->length(_ressourceslist[ind].Parsercontainertype.size());
120 for (unsigned int ind1=0; ind1 < _ressourceslist[ind].Parsercontainertype.size();ind1++)
122 // get container type list
123 switch(_ressourceslist[ind].Parsercontainertype[ind1]){
125 _list[ind1] = SALOME_RessourcesCatalog::Cpp;
128 _list[ind1] = SALOME_RessourcesCatalog::python;
131 _list[ind1] = SALOME_RessourcesCatalog::NP;
140 // The ressources was not found, the exception should be thrown
141 MESSAGE( "The ressource " << computer << " was not found in the ressources catalog" )
142 throw SALOME_RessourcesCatalog::NotFound() ;
144 return _list._retn();
147 //----------------------------------------------------------------------
148 // Function : GetComputerInfo
149 // Purpose : get ressources information
150 //----------------------------------------------------------------------
151 SALOME_RessourcesCatalog::computer_info*
152 SALOME_RessourcesCatalogImpl::GetComputerInfo(const char* computer)
153 throw(SALOME_RessourcesCatalog::NotFound)
155 MESSAGE("Begin of GetComputerInfo");
156 Unexpect aCatch(RC_NotFound);
157 SALOME_RessourcesCatalog::computer_info_var _computer_info =
158 new SALOME_RessourcesCatalog::computer_info;
162 // Looking for ressources named "computer"
163 // If found, get computer info
164 // If not found, exception is thrown
165 for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
167 if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
169 MESSAGE("Ressources named " << computer << " found in the ressources catalog");
172 _computer_info->name = CORBA::string_dup(_ressourceslist[ind].Parsername.c_str());
173 _computer_info->OS = CORBA::string_dup(_ressourceslist[ind].ParserOS.c_str());
174 _computer_info->OS_version = CORBA::string_dup(_ressourceslist[ind].ParserOS_version.c_str());
175 _computer_info->procs = _duplicate_procs(_ressourceslist[ind].Parserprocs);
181 // The ressources was not found, the exception should be thrown
182 MESSAGE( "The ressource " << computer << " was not found in the ressources catalog" )
183 throw SALOME_RessourcesCatalog::NotFound() ;
185 return _computer_info._retn();
190 //----------------------------------------------------------------------
191 // Function : _parse_xml_file
192 // Purpose : parse one module catalog
193 //----------------------------------------------------------------------
195 SALOME_RessourcesCatalogImpl::_parse_xml_file(const char* file,
196 ListOfParserressources& ressourceslist)
198 SALOME_RessourcesCatalog_Handler* handler = new SALOME_RessourcesCatalog_Handler();
201 QXmlInputSource source(xmlFile);
203 QXmlSimpleReader reader;
204 reader.setContentHandler( handler );
205 reader.setErrorHandler( handler );
206 reader.parse( source );
208 ressourceslist = _ressources_list;
212 //----------------------------------------------------------------------
213 // Function : _verify_ressources
214 // Purpose : verify ressources from the catalog parsing
215 // Verify that a computer is'nt defined twice in the catalog
216 //----------------------------------------------------------------------
218 SALOME_RessourcesCatalogImpl::_verify_ressources(ListOfParserressources ressourceslist)
220 bool _return_value = true;
222 vector<string> _machine_list;
223 _machine_list.resize(0);
225 // Fill a list of all computers indicated in the ressources list
226 for (unsigned int ind = 0; ind < ressourceslist.size(); ind++)
227 _machine_list.push_back(ressourceslist[ind].Parsername);
229 // Parse if a computer name is twice in the list of computers
231 for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
233 for (ind = 0; ind < _machine_list.size(); ind++)
236 for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
238 if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
240 MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the ressources list")
241 _return_value = false;
246 return _return_value;
250 //----------------------------------------------------------------------
251 // Function : _parseArguments
252 // Purpose : parse arguments to get general and personal catalog files
253 //----------------------------------------------------------------------
255 SALOME_RessourcesCatalogImpl::_parseArguments(int argc, char **argv,
258 bool _return_value = true;
260 for (int ind = 0; ind < argc ; ind++)
263 if (strcmp(argv[ind],"-help") == 0)
265 MESSAGE( "Usage: " << argv[0] << " -common 'path to ressources catalog' -ORBInitRef NameService=corbaname::localhost");
266 _return_value = false ;
268 if (strcmp(argv[ind],"-common") == 0)
272 // General catalog file
273 *_path= argv[ind + 1] ;
274 ifstream _path_file(*_path);
277 MESSAGE( "Sorry the file " << *_path << " can't be open" )
279 _return_value = false;
285 return _return_value;
288 //----------------------------------------------------------------------
289 // Function : _duplicate_procs
290 // Purpose : create a list of processors information from the catalog parsing
291 //----------------------------------------------------------------------
292 SALOME_RessourcesCatalog::ListOfProc
293 SALOME_RessourcesCatalogImpl::_duplicate_procs(ListOfParserProc list_procs)
295 SALOME_RessourcesCatalog::ListOfProc _list_procs;
296 unsigned int _length = list_procs.size();
297 _list_procs.length(_length);
299 for (unsigned int ind = 0; ind < _length; ind++)
301 //duplicate processor number
302 _list_procs[ind].number = list_procs[ind].Parsernumber;
304 //duplicate model name
305 _list_procs[ind].model_name = CORBA::string_dup(list_procs[ind].Parsermodel_name.c_str());
308 _list_procs[ind].cpu_mhz = list_procs[ind].Parsercpu_mhz;
310 // duplicate cache size
311 _list_procs[ind].cache_size = list_procs[ind].Parsercache_size;