]> SALOME platform Git repositories - modules/kernel.git/blob - src/RessourcesCatalog/SALOME_RessourcesCatalog_impl.cxx
Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/kernel.git] / src / RessourcesCatalog / SALOME_RessourcesCatalog_impl.cxx
1 //  SALOME RessourcesCatalog : 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_RessourcesCatalog_impl.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27
28 using namespace std;
29 #include "SALOME_RessourcesCatalog_impl.hxx"
30 #include <fstream>
31
32 //----------------------------------------------------------------------
33 // Function : SALOME_RessourcesCatalogImpl
34 // Purpose  : Constructor 
35 //----------------------------------------------------------------------
36 SALOME_RessourcesCatalogImpl::SALOME_RessourcesCatalogImpl(int argc, char** argv)
37 {
38   MESSAGE("Ressources Catalog creation");
39
40   // Empty used variables
41   _ressourceslist.resize(0);
42
43   // Parse the arguments given at server run
44   if (!_parseArguments(argc, argv,&_path_ressources))
45     MESSAGE( "Error while argument parsing" )
46
47   // Test existency of files
48   if (_path_ressources == NULL)
49     MESSAGE( "Error the ressources catalog should be indicated" )
50   else
51     {
52       MESSAGE("Parse ressources catalog");
53       // Affect the variable _ressourceslist 
54       _parse_xml_file(_path_ressources,_ressourceslist);
55
56       // Verification of _datatype_list content
57       if(!_verify_ressources(_ressourceslist))
58         MESSAGE( "Error while parsing the ressources catalog" )
59       else MESSAGE("Ressources Catalog OK");
60     }
61 }
62
63 //----------------------------------------------------------------------
64 // Function : ~SALOME_RessourcesCatalogImpl
65 // Purpose  : Destructor 
66 //----------------------------------------------------------------------
67 SALOME_RessourcesCatalogImpl::~SALOME_RessourcesCatalogImpl()
68 {
69   MESSAGE("Ressources Catalog Destruction");
70 }
71
72 //----------------------------------------------------------------------
73 // Function : GetComputerList
74 // Purpose  : get ressources list
75 //----------------------------------------------------------------------
76 SALOME_RessourcesCatalog::ListOfComputer* 
77 SALOME_RessourcesCatalogImpl::GetComputerList()
78 {
79   MESSAGE("Begin of GetComputerList");
80   SALOME_RessourcesCatalog::ListOfComputer_var _list = new SALOME_RessourcesCatalog::ListOfComputer;
81   _list->length(_ressourceslist.size());
82  for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
83    {
84      _list[ind]=CORBA::string_dup(_ressourceslist[ind].Parsername.c_str());
85    }
86   return _list._retn();
87 }
88
89
90 //----------------------------------------------------------------------
91 // Function : GetRessourcesList
92 // Purpose  : get a data type name list
93 //----------------------------------------------------------------------
94 SALOME_RessourcesCatalog::ListOfContainerType* 
95 SALOME_RessourcesCatalogImpl::GetContainerTypeList(const char* computer) 
96                               throw(SALOME_RessourcesCatalog::NotFound)
97 {
98   MESSAGE("Begin of GetContainerTypeList");
99   SALOME_RessourcesCatalog::ListOfContainerType_var _list = 
100     new SALOME_RessourcesCatalog::ListOfContainerType;
101
102   bool find = false ;
103
104   // Looking for ressources named "computer" 
105   // If found, get container type list
106   // If not found, exception is thrown
107
108   for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
109     {
110      if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
111         {
112           MESSAGE("Ressources named " << computer << " found in the ressources catalog");
113             find = true;
114             
115             _list->length(_ressourceslist[ind].Parsercontainertype.size());
116             for (unsigned int ind1=0; ind1 < _ressourceslist[ind].Parsercontainertype.size();ind1++)
117               {
118              // get container type list
119                 switch(_ressourceslist[ind].Parsercontainertype[ind1]){
120                 case Cpp:
121                   _list[ind1] = SALOME_RessourcesCatalog::Cpp;
122                   break;
123                 case python:
124                   _list[ind1] = SALOME_RessourcesCatalog::python;
125                   break;
126                 case NP:
127                   _list[ind1] = SALOME_RessourcesCatalog::NP;
128                   break;
129                 }
130               }
131         }
132     }
133
134   if (!find)
135     {
136       // The ressources was not found, the exception should be thrown
137       MESSAGE( "The ressource " << computer <<  " was not found in the ressources catalog" )
138       throw SALOME_RessourcesCatalog::NotFound() ;
139     }
140   return _list._retn();
141 }
142
143 //----------------------------------------------------------------------
144 // Function : GetComputerInfo
145 // Purpose  : get ressources information
146 //----------------------------------------------------------------------
147 SALOME_RessourcesCatalog::computer_info*
148 SALOME_RessourcesCatalogImpl::GetComputerInfo(const char* computer)
149                             throw(SALOME_RessourcesCatalog::NotFound)
150 {
151   MESSAGE("Begin of GetComputerInfo");
152   SALOME_RessourcesCatalog::computer_info_var _computer_info = 
153     new SALOME_RessourcesCatalog::computer_info;
154
155   bool find = false ;
156
157   // Looking for ressources named "computer" 
158   // If found, get computer info
159   // If not found, exception is thrown
160    for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
161     {
162      if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
163         {
164           MESSAGE("Ressources named " << computer << " found in the ressources catalog");
165             find = true;
166
167             _computer_info->name = CORBA::string_dup(_ressourceslist[ind].Parsername.c_str());
168             _computer_info->OS = CORBA::string_dup(_ressourceslist[ind].ParserOS.c_str());
169             _computer_info->OS_version = CORBA::string_dup(_ressourceslist[ind].ParserOS_version.c_str());
170             _computer_info->procs = _duplicate_procs(_ressourceslist[ind].Parserprocs);    
171         }
172     }
173  
174   if (!find)
175     {
176       // The ressources was not found, the exception should be thrown
177       MESSAGE( "The ressource " << computer <<  " was not found in the ressources catalog" )
178       throw SALOME_RessourcesCatalog::NotFound() ;
179     }
180   return _computer_info._retn();
181 }
182
183
184
185 //----------------------------------------------------------------------
186 // Function : _parse_xml_file
187 // Purpose  : parse one module catalog 
188 //----------------------------------------------------------------------
189 void 
190 SALOME_RessourcesCatalogImpl::_parse_xml_file(const char* file,
191                                             ListOfParserressources& ressourceslist) 
192 {
193   SALOME_RessourcesCatalog_Handler* handler = new SALOME_RessourcesCatalog_Handler();
194   QFile xmlFile(file);
195
196   QXmlInputSource source(xmlFile);
197
198   QXmlSimpleReader reader;
199   reader.setContentHandler( handler );
200   reader.setErrorHandler( handler );
201   reader.parse( source );
202   xmlFile.close();
203   ressourceslist = _ressources_list;
204 }
205
206
207 //----------------------------------------------------------------------
208 // Function : _verify_ressources
209 // Purpose  : verify ressources from the catalog parsing
210 //            Verify that a computer is'nt defined twice in the catalog
211 //----------------------------------------------------------------------
212 bool
213 SALOME_RessourcesCatalogImpl::_verify_ressources(ListOfParserressources ressourceslist)
214 {
215     bool _return_value = true;
216     bool _bool = false ;
217     vector<string> _machine_list;
218     _machine_list.resize(0);
219
220   // Fill a list of all computers indicated in the ressources list
221   for (unsigned int ind = 0; ind < ressourceslist.size(); ind++)
222           _machine_list.push_back(ressourceslist[ind].Parsername);   
223
224   // Parse if a computer name is twice in the list of computers
225   for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
226     {
227      for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
228        {
229          if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
230            {
231              MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the ressources list")
232              _return_value = false;
233           }
234        }
235     }
236
237    return _return_value;
238 }
239
240
241 //----------------------------------------------------------------------
242 // Function : _parseArguments
243 // Purpose  : parse arguments to get general and personal catalog files
244 //----------------------------------------------------------------------
245 bool
246 SALOME_RessourcesCatalogImpl::_parseArguments(int argc, char **argv, 
247                                               char **_path)
248 {
249   bool _return_value = true;
250   *_path = NULL;
251   for (int ind = 0; ind < argc ; ind++)
252     {
253
254       if (strcmp(argv[ind],"-help") == 0)
255         {
256           INFOS( "Usage: " << argv[0] << " -common 'path to ressources catalog' -ORBInitRef NameService=corbaname::localhost");
257             _return_value = false ;
258         }
259       if (strcmp(argv[ind],"-common") == 0)
260         {
261           if (ind + 1 <= argc)
262             {
263               // General catalog file
264               *_path= argv[ind + 1] ;
265               ifstream _path_file(*_path);
266               if (!_path_file)
267                 {
268                   MESSAGE( "Sorry the file " << *_path << " can't be open" )
269                   *_path = NULL;
270                   _return_value = false;
271                 }
272             }
273         }
274      
275     }
276   return _return_value;
277 }
278
279 //----------------------------------------------------------------------
280 // Function : _duplicate_procs
281 // Purpose  : create a list of processors information from the catalog parsing
282 //----------------------------------------------------------------------
283 SALOME_RessourcesCatalog::ListOfProc
284 SALOME_RessourcesCatalogImpl::_duplicate_procs(ListOfParserProc list_procs)
285 {
286   SALOME_RessourcesCatalog::ListOfProc _list_procs;
287   unsigned int _length = list_procs.size();
288   _list_procs.length(_length);
289  
290   for (unsigned int ind = 0; ind < _length; ind++)
291     {
292       //duplicate processor number
293       _list_procs[ind].number = list_procs[ind].Parsernumber;
294
295       //duplicate model name
296       _list_procs[ind].model_name = CORBA::string_dup(list_procs[ind].Parsermodel_name.c_str());
297
298       //duplicate cpu
299       _list_procs[ind].cpu_mhz = list_procs[ind].Parsercpu_mhz;
300
301       // duplicate cache size
302       _list_procs[ind].cache_size = list_procs[ind].Parsercache_size;
303     }
304   return _list_procs;
305 }