Salome HOME
DCQ : Merge with Ecole_ete_a6.
[modules/yacs.git] / src / ModuleCatalog / SALOME_ModuleCatalog_Acomponent_impl.cxx
1 //  SALOME ModuleCatalog : implementation of ModuleCatalog server which parsers xml description of modules
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_ModuleCatalog_Acomponent_impl.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "SALOME_ModuleCatalog_Acomponent_impl.hxx"
31
32 #include "Utils_ExceptHandlers.hxx"
33 UNEXPECT_CATCH(MC_NotFound, SALOME_ModuleCatalog::NotFound);
34
35 //----------------------------------------------------------------------
36 // Function : SALOME_ModuleCatalog_AcomponentImpl
37 // Purpose  : Constructor
38 //            Affect the component name, type,icone,  a bool to define 
39 //            if it's multistudy or not.
40 //            Affect too the constraint and the interfaces of the component
41 //            and the pathes prefixes for all computers
42 //----------------------------------------------------------------------  
43 SALOME_ModuleCatalog_AcomponentImpl::SALOME_ModuleCatalog_AcomponentImpl
44 (SALOME_ModuleCatalog::Component &C) : _Component(C)
45 {
46   BEGIN_OF("SALOME_ModuleCatalog_AcomponentImpl");
47
48   END_OF("SALOME_ModuleCatalog_AcomponentImpl");
49 }
50
51 //----------------------------------------------------------------------
52 // Function : ~SALOME_ModuleCatalog_AcomponentImpl
53 // Purpose  : Destructor 
54 //----------------------------------------------------------------------
55 SALOME_ModuleCatalog_AcomponentImpl::~SALOME_ModuleCatalog_AcomponentImpl()
56 {
57   BEGIN_OF("~SALOME_ModuleCatalog_AcomponentImpl");
58
59
60   END_OF("~SALOME_ModuleCatalog_AcomponentImpl");
61 }
62
63 //----------------------------------------------------------------------
64 // Function : GetInterfaceList
65 // Purpose  : get a list of the interfaces name of a component
66 //----------------------------------------------------------------------
67 SALOME_ModuleCatalog::ListOfInterfaces* 
68 SALOME_ModuleCatalog_AcomponentImpl::GetInterfaceList() 
69 {
70   BEGIN_OF("GetInterfaceList");
71
72   SALOME_ModuleCatalog::ListOfInterfaces_var _list 
73     = new SALOME_ModuleCatalog::ListOfInterfaces;
74
75   // All the interfaces are defined in _list_interfaces affected at the
76   // component creation
77   unsigned int _length_interfaces = _Component.interfaces.length();
78
79   _list->length(_length_interfaces);
80
81   // Parse all the interfaces to get their name
82   for (unsigned int ind = 0; ind < _length_interfaces; ind++)
83     {
84       _list[ind] = CORBA::string_dup(_Component.interfaces[ind].interfacename);
85       MESSAGE("The component " << _Component.name 
86               << " contains " << _list[ind] << " as interface");
87     }
88   
89   END_OF("GetInterfaceList");
90   return _list._retn();
91 }
92
93 //----------------------------------------------------------------------
94 // Function : GetInterface
95 // Purpose  : get one interface of a component
96 //----------------------------------------------------------------------
97 SALOME_ModuleCatalog::DefinitionInterface*
98 SALOME_ModuleCatalog_AcomponentImpl::GetInterface(const char* interfacename)
99                                      throw(SALOME_ModuleCatalog::NotFound)
100 {
101   BEGIN_OF("GetInterface");
102   SCRUTE(interfacename);
103
104   SALOME_ModuleCatalog::DefinitionInterface *_interface =
105           new SALOME_ModuleCatalog::DefinitionInterface;
106
107   bool _find = false ;
108   
109   // looking for the specified interface
110   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
111     {
112       SALOME_ModuleCatalog::DefinitionInterface &I
113         = _Component.interfaces[ind];
114
115       if (strcmp(interfacename, I.interfacename) == 0)
116         {
117           // wanted interface
118           _find = true ;
119           duplicate(*_interface, I);
120         }
121     }
122
123   SCRUTE(_find);
124   if (!_find)
125     {
126       // The interface was not found, the exception should be thrown
127       string message = "The interface";
128       message += interfacename;
129       message += " of the component ";
130       message += _Component.name;
131       message += " was not found"; 
132       MESSAGE(message)
133         throw SALOME_ModuleCatalog::NotFound(message.c_str());
134     }
135
136   END_OF("GetInterface");
137
138   return _interface;
139 }
140
141
142
143 //----------------------------------------------------------------------
144 // Function : GetServiceList
145 // Purpose  : get a list of the services name of an interface 
146 //            of a component
147 //----------------------------------------------------------------------
148 SALOME_ModuleCatalog::ListOfServices* 
149 SALOME_ModuleCatalog_AcomponentImpl::GetServiceList(const char* interfacename)
150                                      throw(SALOME_ModuleCatalog::NotFound)
151 {
152   BEGIN_OF("GetServiceList");
153   SCRUTE(interfacename);
154
155   SALOME_ModuleCatalog::ListOfServices_var _list 
156     = new SALOME_ModuleCatalog::ListOfServices;
157
158   // Variables initialisation
159   bool _find = false ;
160
161   // looking for the specified interface
162   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
163     {
164       SALOME_ModuleCatalog::DefinitionInterface & I = _Component.interfaces[ind];
165
166       if (strcmp(interfacename, I.interfacename) == 0)
167         {
168           _find = true ;
169           // wanted interface
170           // Get the list of services name for this interface
171           unsigned int _length_services = I.interfaceservicelist.length();
172           _list->length(_length_services);
173           for (unsigned int ind1 = 0; ind1 < _length_services ; ind1++)
174             {
175               _list[ind1] = CORBA::string_dup(I.interfaceservicelist[ind1].ServiceName);
176               MESSAGE("The interface " << interfacename << " of the component " 
177                       << _Component.name << " contains " << _list[ind1] << " as a service") 
178             }
179         }
180     }
181
182  if (!_find)
183     {
184       // The interface was not found, the exception should be thrown
185       string message = "The interface";
186       message += interfacename;
187       message += " of the component ";
188       message += _Component.name;
189       message += " was not found"; 
190       MESSAGE(message)
191         throw SALOME_ModuleCatalog::NotFound(message.c_str());
192     }
193
194   END_OF("GetServiceList");
195   return _list._retn();
196 }
197
198     
199 //----------------------------------------------------------------------
200 // Function : GetService
201 // Purpose  : get one service of an interface of a component
202 //----------------------------------------------------------------------
203 SALOME_ModuleCatalog::Service* 
204 SALOME_ModuleCatalog_AcomponentImpl::GetService(const char* interfacename, 
205                                                 const char* servicename) 
206                                      throw(SALOME_ModuleCatalog::NotFound)
207 {
208   BEGIN_OF("GetService");
209   SCRUTE(interfacename);
210   SCRUTE(servicename);
211
212   Unexpect aCatch( MC_NotFound );
213   SALOME_ModuleCatalog::Service *service = new SALOME_ModuleCatalog::Service;
214
215   // Variables initialization
216   bool _find = false ;
217   
218
219   // looking for the specified interface
220   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
221     {
222       SCRUTE(ind);
223       SCRUTE(_Component.interfaces[ind].interfacename);
224
225       SALOME_ModuleCatalog::DefinitionInterface &I = _Component.interfaces[ind];
226       if (strcmp(interfacename, I.interfacename) == 0)
227         {
228           // wanted interface
229           // looking for the specified service
230           for (unsigned int ind1 = 0; ind1 <  I.interfaceservicelist.length() ; ind1++)
231             {
232               SALOME_ModuleCatalog::Service &S = I.interfaceservicelist[ind1];
233               SCRUTE(ind1);
234               SCRUTE(S.ServiceName);
235
236               if (strcmp(servicename, S.ServiceName) == 0)
237               {
238                 // Wanted Service
239                 // Affect the service to be returned
240                 _find = true ;
241                 duplicate(*service, S);
242               }
243             }
244
245         }
246     }
247   
248   SCRUTE(_find);
249   if (!_find)
250     {
251       // The interface was not found, the exception should be thrown
252       string message = "The service";
253       message += servicename;
254       message += " of the interface ";
255       message += interfacename;
256       message += " of the component ";
257       message += _Component.name;
258       message += " was not found"; 
259       MESSAGE(message)
260         throw SALOME_ModuleCatalog::NotFound(message.c_str());
261     }
262
263   END_OF("GetService");
264   return service;
265 }
266
267 //----------------------------------------------------------------------
268 // Function : GetDefaultService
269 // Purpose  : get the default service of the interface
270 //----------------------------------------------------------------------
271 SALOME_ModuleCatalog::Service* 
272 SALOME_ModuleCatalog_AcomponentImpl::GetDefaultService(const char* interfacename) 
273                                      throw(SALOME_ModuleCatalog::NotFound)
274 {
275   BEGIN_OF("GetDefaultService");
276   SCRUTE(interfacename);
277
278   Unexpect aCatch( MC_NotFound );
279   SALOME_ModuleCatalog::Service *_service = new  SALOME_ModuleCatalog::Service;
280
281   // Variables initialisation
282   bool _find = false ;
283
284   // looking for the specified interface
285   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
286     {
287       if (strcmp(interfacename, _Component.interfaces[ind].interfacename) == 0)
288         {
289           // wanted interface
290           // looking for the defautl service of the wanted interface
291           for (unsigned int ind1 = 0; ind1 <  _Component.interfaces[ind].interfaceservicelist.length() ; ind1++)
292             {
293               if (_Component.interfaces[ind].interfaceservicelist[ind1].Servicebydefault)
294               {
295                 // Default Service
296                 // affect the service to be returned
297                 _find = true ;
298                 duplicate(*_service, _Component.interfaces[ind].interfaceservicelist[ind1]);
299               }
300             }
301
302         }
303     }
304
305   if (!_find)
306     {
307       // The service was not found, the exception should be thrown
308       string message = "The default service of the interface ";
309       message += interfacename;
310       message += " of the component ";
311       message += _Component.name;
312       message += " was not found";
313       MESSAGE(message)
314         throw SALOME_ModuleCatalog::NotFound(message.c_str());
315     }
316
317   END_OF("GetDefaultService");
318   return _service;
319 }
320
321 //----------------------------------------------------------------------
322 // Function : GetPathPrefix
323 // Purpose  : get the PathPrefix of a computer
324 //----------------------------------------------------------------------
325 char* 
326 SALOME_ModuleCatalog_AcomponentImpl::GetPathPrefix(const char* machinename) 
327                                      throw(SALOME_ModuleCatalog::NotFound)
328 {
329   BEGIN_OF("GetPathPrefix");
330   SCRUTE(machinename);
331   Unexpect aCatch( MC_NotFound );
332
333  // Variables initialisation
334   char* _path = NULL;
335   bool _find = false ;
336
337   // Parse all the path prefixes
338   // looking for the wanted computer
339   for (unsigned int ind = 0 ; ind < _Component.paths.length() ; ind++)
340     {
341       if (strcmp(machinename, _Component.paths[ind].machine) == 0)
342             {
343               _find = true ;
344               // Wanted computer
345               // affect the path to be returned
346                 const char* _temp = _Component.paths[ind].path ;
347                 _path = new char[strlen(_temp)+1];
348                 strcpy(_path,_temp);
349             }
350      }
351
352    SCRUTE(_find);
353    if (!_find)
354      {
355        // The computer was not found, the exception should be thrown
356        string message = "The computer ";
357        message += machinename;
358        message += " was not found in the catalog associated to the component ";
359        message += _Component.name;
360        MESSAGE(message)
361        throw SALOME_ModuleCatalog::NotFound(message.c_str());
362      }
363
364   END_OF("GetPathPrefix");
365   return _path;
366 }
367
368 //----------------------------------------------------------------------
369 // Function : constraint
370 // Purpose  : obtain the constraint affected to a component
371 //----------------------------------------------------------------------
372 char* SALOME_ModuleCatalog_AcomponentImpl::constraint() 
373 {
374   return CORBA::string_dup(_Component.constraint);
375 }
376
377 //----------------------------------------------------------------------
378 // Function : componentname
379 // Purpose  : obtain the componentname
380 //----------------------------------------------------------------------
381 char* SALOME_ModuleCatalog_AcomponentImpl::componentname()
382 {
383   return CORBA::string_dup(_Component.name);
384 }
385
386 //----------------------------------------------------------------------
387 // Function : componentusername
388 // Purpose  : obtain the componentusername
389 //----------------------------------------------------------------------
390 char* SALOME_ModuleCatalog_AcomponentImpl::componentusername()
391 {
392   return CORBA::string_dup(_Component.username);
393 }
394
395 //----------------------------------------------------------------------
396 // Function : multistudy
397 // Purpose  : define if a component can be multistudy or not
398 //----------------------------------------------------------------------
399 CORBA::Boolean SALOME_ModuleCatalog_AcomponentImpl::multistudy()
400 {
401   return _Component.multistudy ;
402 }
403
404
405 //----------------------------------------------------------------------
406 // Function : implementation type
407 // Purpose  : define if a component is implemented in C++ or Python
408 //----------------------------------------------------------------------
409 CORBA::Boolean SALOME_ModuleCatalog_AcomponentImpl::implementation_type()
410 {
411   return _Component.implementationType ;
412 }
413
414 //----------------------------------------------------------------------
415 // Function : component_type
416 // Purpose  : define the type of the component
417 //----------------------------------------------------------------------
418 SALOME_ModuleCatalog::ComponentType 
419 SALOME_ModuleCatalog_AcomponentImpl::component_type() 
420 {
421   return _Component.type;
422 }
423
424 //----------------------------------------------------------------------
425 // Function : icone
426 // Purpose  : obtain the icone affected to a component (for IAPP)
427 //----------------------------------------------------------------------
428 char* SALOME_ModuleCatalog_AcomponentImpl::component_icone() 
429 {
430   return CORBA::string_dup(_Component.icon);
431 }
432
433
434 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
435 (SALOME_ModuleCatalog::ServicesParameter & P_out,
436  const SALOME_ModuleCatalog::ServicesParameter &P_in)
437 {
438   // duplicate parameter name
439   P_out.Parametername = CORBA::string_dup(P_in.Parametername);
440   
441   // duplicate parameter type
442   P_out.Parametertype = CORBA::string_dup(P_in.Parametertype);
443 }
444
445
446 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
447 (SALOME_ModuleCatalog::ServicesDataStreamParameter & P_out,
448  const SALOME_ModuleCatalog::ServicesDataStreamParameter &P_in)
449 {
450   // duplicate parameter name
451   P_out.Parametername = CORBA::string_dup(P_in.Parametername);
452   
453   // duplicate parameter type
454   P_out.Parametertype = P_in.Parametertype;
455
456   // duplicate parameter dependency
457   P_out.Parameterdependency = P_in.Parameterdependency;
458
459 }
460
461 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
462 (SALOME_ModuleCatalog::Service & S_out,
463  const SALOME_ModuleCatalog::Service &S_in)
464 {
465   // type of node
466   S_out.TypeOfNode = S_in.TypeOfNode;
467
468     // duplicate service name
469   S_out.ServiceName = CORBA::string_dup(S_in.ServiceName);
470   
471   // duplicate service by default
472   S_out.Servicebydefault = S_in.Servicebydefault;
473
474   unsigned int _length;
475
476   // duplicate in Parameters
477   _length = S_in.ServiceinParameter.length();
478   S_out.ServiceinParameter.length(_length);
479
480   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
481     duplicate(S_out.ServiceinParameter[ind2],
482               S_in.ServiceinParameter[ind2]);
483   
484   // duplicate out Parameters
485   _length = S_in.ServiceoutParameter.length();
486   S_out.ServiceoutParameter.length(_length);
487   
488   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
489     duplicate(S_out.ServiceoutParameter[ind2],
490               S_in.ServiceoutParameter[ind2]);
491   
492   // duplicate in DataStreamParameters
493   _length = S_in.ServiceinDataStreamParameter.length();
494   S_out.ServiceinDataStreamParameter.length(_length);
495   
496   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
497     duplicate(S_out.ServiceinDataStreamParameter[ind2],
498               S_in.ServiceinDataStreamParameter[ind2]);
499   
500   // duplicate out DataStreamParameters
501   _length = S_in.ServiceoutDataStreamParameter.length();
502   SCRUTE(_length);
503   S_out.ServiceoutDataStreamParameter.length(_length);
504   
505   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
506     duplicate(S_out.ServiceoutDataStreamParameter[ind2],
507               S_in.ServiceoutDataStreamParameter[ind2]);
508 }
509
510
511 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
512 (SALOME_ModuleCatalog::DefinitionInterface & I_out,
513  const SALOME_ModuleCatalog::DefinitionInterface & I_in)
514 {
515   //duplicate interface name
516   I_out.interfacename = CORBA::string_dup(I_in.interfacename);
517   
518   // duplicate service list
519   unsigned int _length = I_in.interfaceservicelist.length();
520   SCRUTE(_length);
521   I_out.interfaceservicelist.length(_length);
522   
523   for (unsigned int ind1 = 0; ind1 < _length ; ind1 ++)
524     duplicate(I_out.interfaceservicelist[ind1],
525               I_in.interfaceservicelist[ind1]);
526 }
527
528