Salome HOME
DCQ : Merge with Ecole_ete_a6.
[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 #include "Utils_ExceptHandlers.hxx"
33 UNEXPECT_CATCH(RC_NotFound, SALOME_RessourcesCatalog::NotFound);
34
35 //----------------------------------------------------------------------
36 // Function : SALOME_RessourcesCatalogImpl
37 // Purpose  : Constructor 
38 //----------------------------------------------------------------------
39 SALOME_RessourcesCatalogImpl::SALOME_RessourcesCatalogImpl(int argc, char** argv)
40 {
41   MESSAGE("Ressources Catalog creation");
42
43   // Empty used variables
44   _ressourceslist.resize(0);
45
46   // Parse the arguments given at server run
47   if (!_parseArguments(argc, argv,&_path_ressources))
48     MESSAGE( "Error while argument parsing" )
49
50   // Test existency of files
51   if (_path_ressources == NULL)
52     MESSAGE( "Error the ressources catalog should be indicated" )
53   else
54     {
55       MESSAGE("Parse ressources catalog");
56       // Affect the variable _ressourceslist 
57       _parse_xml_file(_path_ressources,_ressourceslist);
58
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");
63     }
64 }
65
66 //----------------------------------------------------------------------
67 // Function : ~SALOME_RessourcesCatalogImpl
68 // Purpose  : Destructor 
69 //----------------------------------------------------------------------
70 SALOME_RessourcesCatalogImpl::~SALOME_RessourcesCatalogImpl()
71 {
72   MESSAGE("Ressources Catalog Destruction");
73 }
74
75 //----------------------------------------------------------------------
76 // Function : GetComputerList
77 // Purpose  : get ressources list
78 //----------------------------------------------------------------------
79 SALOME_RessourcesCatalog::ListOfComputer* 
80 SALOME_RessourcesCatalogImpl::GetComputerList()
81 {
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++)
86    {
87      _list[ind]=CORBA::string_dup(_ressourceslist[ind].Parsername.c_str());
88    }
89   return _list._retn();
90 }
91
92
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)
100 {
101   Unexpect aCatch(RC_NotFound);
102   MESSAGE("Begin of GetContainerTypeList");
103   SALOME_RessourcesCatalog::ListOfContainerType_var _list = 
104     new SALOME_RessourcesCatalog::ListOfContainerType;
105
106   bool find = false ;
107
108   // Looking for ressources named "computer" 
109   // If found, get container type list
110   // If not found, exception is thrown
111
112   for (unsigned int ind=0; ind < _ressourceslist.size();ind++)
113     {
114      if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
115         {
116           MESSAGE("Ressources named " << computer << " found in the ressources catalog");
117             find = true;
118             
119             _list->length(_ressourceslist[ind].Parsercontainertype.size());
120             for (unsigned int ind1=0; ind1 < _ressourceslist[ind].Parsercontainertype.size();ind1++)
121               {
122              // get container type list
123                 switch(_ressourceslist[ind].Parsercontainertype[ind1]){
124                 case Cpp:
125                   _list[ind1] = SALOME_RessourcesCatalog::Cpp;
126                   break;
127                 case python:
128                   _list[ind1] = SALOME_RessourcesCatalog::python;
129                   break;
130                 case NP:
131                   _list[ind1] = SALOME_RessourcesCatalog::NP;
132                   break;
133                 }
134               }
135         }
136     }
137
138   if (!find)
139     {
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() ;
143     }
144   return _list._retn();
145 }
146
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)
154 {
155   MESSAGE("Begin of GetComputerInfo");
156   Unexpect aCatch(RC_NotFound);
157   SALOME_RessourcesCatalog::computer_info_var _computer_info = 
158     new SALOME_RessourcesCatalog::computer_info;
159
160   bool find = false ;
161
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++)
166     {
167      if (strcmp((_ressourceslist[ind].Parsername).c_str(),computer) == 0)
168         {
169           MESSAGE("Ressources named " << computer << " found in the ressources catalog");
170             find = true;
171
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);    
176         }
177     }
178  
179   if (!find)
180     {
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() ;
184     }
185   return _computer_info._retn();
186 }
187
188
189
190 //----------------------------------------------------------------------
191 // Function : _parse_xml_file
192 // Purpose  : parse one module catalog 
193 //----------------------------------------------------------------------
194 void 
195 SALOME_RessourcesCatalogImpl::_parse_xml_file(const char* file,
196                                             ListOfParserressources& ressourceslist) 
197 {
198   SALOME_RessourcesCatalog_Handler* handler = new SALOME_RessourcesCatalog_Handler();
199   QFile xmlFile(file);
200
201   QXmlInputSource source(xmlFile);
202
203   QXmlSimpleReader reader;
204   reader.setContentHandler( handler );
205   reader.setErrorHandler( handler );
206   reader.parse( source );
207   xmlFile.close();
208   ressourceslist = _ressources_list;
209 }
210
211
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 //----------------------------------------------------------------------
217 bool
218 SALOME_RessourcesCatalogImpl::_verify_ressources(ListOfParserressources ressourceslist)
219 {
220     bool _return_value = true;
221     bool _bool = false ;
222     vector<string> _machine_list;
223     _machine_list.resize(0);
224
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);   
228
229   // Parse if a computer name is twice in the list of computers
230   for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
231     {
232      for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
233        {
234          if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
235            {
236              MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the ressources list")
237              _return_value = false;
238           }
239        }
240     }
241
242    return _return_value;
243 }
244
245
246 //----------------------------------------------------------------------
247 // Function : _parseArguments
248 // Purpose  : parse arguments to get general and personal catalog files
249 //----------------------------------------------------------------------
250 bool
251 SALOME_RessourcesCatalogImpl::_parseArguments(int argc, char **argv, 
252                                               char **_path)
253 {
254   bool _return_value = true;
255   *_path = NULL;
256   for (int ind = 0; ind < argc ; ind++)
257     {
258
259       if (strcmp(argv[ind],"-help") == 0)
260         {
261           MESSAGE( "Usage: " << argv[0] << " -common 'path to ressources catalog' -ORBInitRef NameService=corbaname::localhost");
262             _return_value = false ;
263         }
264       if (strcmp(argv[ind],"-common") == 0)
265         {
266           if (ind + 1 <= argc)
267             {
268               // General catalog file
269               *_path= argv[ind + 1] ;
270               ifstream _path_file(*_path);
271               if (!_path_file)
272                 {
273                   MESSAGE( "Sorry the file " << *_path << " can't be open" )
274                   *_path = NULL;
275                   _return_value = false;
276                 }
277             }
278         }
279      
280     }
281   return _return_value;
282 }
283
284 //----------------------------------------------------------------------
285 // Function : _duplicate_procs
286 // Purpose  : create a list of processors information from the catalog parsing
287 //----------------------------------------------------------------------
288 SALOME_RessourcesCatalog::ListOfProc
289 SALOME_RessourcesCatalogImpl::_duplicate_procs(ListOfParserProc list_procs)
290 {
291   SALOME_RessourcesCatalog::ListOfProc _list_procs;
292   unsigned int _length = list_procs.size();
293   _list_procs.length(_length);
294  
295   for (unsigned int ind = 0; ind < _length; ind++)
296     {
297       //duplicate processor number
298       _list_procs[ind].number = list_procs[ind].Parsernumber;
299
300       //duplicate model name
301       _list_procs[ind].model_name = CORBA::string_dup(list_procs[ind].Parsermodel_name.c_str());
302
303       //duplicate cpu
304       _list_procs[ind].cpu_mhz = list_procs[ind].Parsercpu_mhz;
305
306       // duplicate cache size
307       _list_procs[ind].cache_size = list_procs[ind].Parsercache_size;
308     }
309   return _list_procs;
310 }