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