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