]> SALOME platform Git repositories - modules/kernel.git/blob - src/DSC/DSC_User/Superv_Component_i.hxx
Salome HOME
bos #42937: [CEA 41954] Integration of UB24.04 patches
[modules/kernel.git] / src / DSC / DSC_User / Superv_Component_i.hxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 //  File   : Superv_Component_i.hxx
24 //  Author : André RIBES (EDF), Eric Fayolle (EDF)
25 //  Module : KERNEL
26 //
27 #ifndef _SUPERV_COMPONENT_I_HXX_
28 #define _SUPERV_COMPONENT_I_HXX_
29
30 #include "DSC_i.hxx"
31 #include "base_port.hxx"
32 #include "uses_port.hxx"
33 #include "provides_port.hxx"
34 #include "port_factory.hxx"
35
36 #include "DSC_Exception.hxx"
37 #include <vector>
38
39 /*! \class Superv_Component_i
40  *  \brief This class implements DSC_User component.
41  *
42  *  This class allows a higher programming level than DSC_Basic. It enables
43  *  a programming level for service's developers who want to use DSC ports.
44  *
45  *  This class has two level for using and declare ports. The higher level proposes
46  *  operations to add ports that are provided by default by SALOME like Calcium ports.
47  *  It provides too some methods to add their own DSC_User ports.
48  *
49  *  \note This class doesn't implement the init_service CORBA operation.
50  */
51 class Superv_Component_i :
52   public Engines_DSC_i,
53   virtual public POA_Engines::Superv_Component
54 {
55 public:
56   Superv_Component_i(CORBA::ORB_ptr orb,
57                      PortableServer::POA_ptr poa,
58                      PortableServer::ObjectId * contId,
59                      const char *instanceName,
60                      const char *interfaceName,
61                      bool notif = false);
62   Superv_Component_i(CORBA::ORB_ptr orb,
63                      PortableServer::POA_ptr poa,
64                      Engines::Container_ptr container, 
65                      const char *instanceName,
66                      const char *interfaceName,
67                      bool notif = false,
68                      bool regist = false );
69   virtual ~Superv_Component_i();
70
71   // Exceptions declarations.
72   // There are defined on the Superv_Component_i.cxx to avoid problems
73   // from dlopen.
74   DSC_EXCEPTION(BadFabType)
75   DSC_EXCEPTION(BadType)
76   DSC_EXCEPTION(BadCast)
77   DSC_EXCEPTION(UnexpectedState)
78   DSC_EXCEPTION(PortAlreadyDefined)
79   DSC_EXCEPTION(PortNotDefined)
80   DSC_EXCEPTION(PortNotConnected)
81   DSC_EXCEPTION(NilPort)
82   DSC_EXCEPTION(BadProperty)
83   
84   /*!
85    * \warning currently disabled.
86    */
87   virtual provides_port * create_provides_control_port() 
88   {return NULL;}
89
90   /*!
91    * \warning currently disabled.
92    */
93   virtual provides_port * create_provides_data_and_control_port(const char* /*port_type*/) 
94   {return NULL;}
95
96   /*!
97    * \warning currently disabled.
98    */
99   virtual uses_port * create_uses_control_port()
100   {return NULL;}
101
102   /*!
103    * \warning currently disabled.
104    */
105   virtual uses_port * create_uses_data_and_control_port(const char* /*port_type*/)
106   {return NULL;}
107
108   /*!
109    * This method permits to create a provides port provided by the platform.
110    * (See documentation of DSC for knoing these ports).
111    *   
112    *
113    * \param port_fab_type type provides port.
114    * \return the provides port.
115    *
116    * \note It's user repsonsability to destroy the provides port.
117    */
118   virtual provides_port * create_provides_data_port(const std::string& port_fab_type);
119
120
121   /*!
122    * This method permits to create a uses port provided by the platform.
123    * (See documentation of DSC for knoing these ports).
124    *   
125    *
126    * \param port_fab_type type uses port.
127    * \return the uses port.
128    *
129    * \note It's user repsonsability to destroy the uses port.
130    */
131   virtual uses_port * create_uses_data_port(const std::string& port_fab_type);
132
133   /*!
134    * Adds a port to the component. With this method only Salomé's provided DSC ports
135    * can be added.
136    *
137    * \param port_fab_type type of the port.
138    * \param port_type uses or provides.
139    * \param port_name the name of the port in the component.
140    */
141   virtual void add_port(const char * port_fab_type,
142                         const char * port_type,
143                         const char * port_name);
144
145   /*!
146    * Adds a port to the component. With this method only Salomé's provided DSC ports
147    * can be added.
148    *
149    * \param port_fab_type type of the port.
150    * \param port_type uses or provides.
151    * \param port_name the name of the port in the component.
152    * \return the created port.   
153    */
154   template < typename SpecificPortType >  
155   SpecificPortType * add_port(const char * port_fab_type,
156                               const char * port_type,
157                               const char * port_name);
158
159   /*!
160    * Adds a created provides port to the component.
161    *
162    * \param port the provides port.
163    * \param provides_port_name the name of the port in the component.
164    */
165   virtual void add_port(provides_port * port, 
166                         const char* provides_port_name);
167
168   /*!
169    * Adds a created uses port to the component.
170    *
171    * \param port the uses port.
172    * \param uses_port_name the name of the port in the component.
173    */
174   virtual void add_port(uses_port * port, 
175                         const char* uses_port_name);
176
177   /*!
178    * Gets the provides port already added in the component.
179    *
180    * \param port the provides port pointer.
181    * \param provides_port_name the name of the port.
182    */
183   virtual void get_port(provides_port *& port, 
184                         const char* provides_port_name);
185   
186   /*!
187    * Gets the uses port already added in the component.
188    *
189    * \param port the uses port pointer.
190    * \param uses_port_name the name of the port.
191    */
192   virtual void get_port(uses_port *& port, 
193                         const char* uses_port_name);
194
195   /*!
196    * Gets the list of the ports of a service.
197    * If servicename is not set, all the ports of the component are 
198    * returned.
199    *
200    * \param port_names the ports's list.
201    * \param servicename service's name.
202    */
203   virtual void get_uses_port_names(std::vector<std::string> & port_names,
204                                    const std::string servicename="") const;
205
206   /*!
207    * Gets a port already added in the component.
208    *
209    * \param port_name the name of the port.
210    * \return a port's pointer.
211    */
212   template <typename SpecificPortType > 
213   SpecificPortType * get_port( const char * port_name);
214  
215   /*!
216    * \see DSC_Callbacks::provides_port_changed
217    */
218   virtual void provides_port_changed(const char* provides_port_name,
219                                      int connection_nbr,
220                                      const Engines::DSC::Message message);
221
222   /*!
223    * \see DSC_Callbacks::uses_port_changed
224    */
225   virtual void uses_port_changed(const char* uses_port_name,
226                                  Engines::DSC::uses_port * new_uses_port,
227                                  const Engines::DSC::Message message);
228
229
230   /*!
231    * Add a factory the component. If the factory_name is already
232    * used, the new library is not added.
233    *
234    * \param factory_name name of the factory (used by Superv_Component_i::create_provides_data_port
235    * and Superv_Component_i::create_uses_data_port)
236    * \param factory_ptr factory pointer (destroyed by the component)
237    */
238   static void register_factory(const std::string & factory_name,
239                                 port_factory * factory_ptr);
240
241   /*!
242    * Get a factory from the component. 
243    *
244    * \param factory_name name of the factory.
245    * \return factory pointer, NULL if the factory doesn't exist.
246    */
247   virtual port_factory * get_factory(const std::string & factory_name);
248
249   /*!
250    */
251   static long dscTimeOut;
252   static void setTimeOut();
253   void beginService(const char *serviceName);
254
255
256   // This method is implemented by default since it is a very specific usage.
257   // It also permits to not break compatibility with older components.
258   virtual CORBA::Boolean init_service_with_multiple(const char* /*service_name*/,
259                                                     const Engines::Superv_Component::seq_multiple_param & /*params*/)
260   {
261     return true;
262   }
263
264 private:
265   // Factory map
266   typedef std::map<std::string, port_factory*> factory_map_t;
267   static factory_map_t _factory_map;
268
269   /*-------------------------------------------------*/
270   // A Superv_Component port.
271   struct superv_port_t {
272     superv_port_t():u_ref(NULL),p_ref(NULL){};
273     ~superv_port_t()
274     {
275       if(u_ref)delete u_ref;
276       if(p_ref)
277         {
278           // do not delete CORBA servant : deactivate it and then call _remove_ref or delete
279           PortableServer::ServantBase* servant=dynamic_cast<PortableServer::ServantBase*>(p_ref);
280           if(servant)
281             {
282               PortableServer::POA_var poa =servant->_default_POA();
283               PortableServer::ObjectId_var oid = poa->servant_to_id(servant);
284               poa->deactivate_object(oid);
285               servant->_remove_ref();
286             }
287         }
288     };
289     // For uses ports.
290     uses_port * u_ref;
291     // For provides ports.
292     provides_port * p_ref;
293   };
294
295   typedef std::map<std::string, superv_port_t *> superv_ports;
296
297   /*-------------------------------------------------*/
298   /*-------------------------------------------------*/
299
300   superv_ports my_superv_ports;
301   superv_ports::iterator my_superv_ports_it;
302 };
303
304
305
306 template < typename SpecificPortType >  SpecificPortType * 
307 Superv_Component_i::add_port(const char * port_fab_type,
308                              const char * port_type,
309                              const char * port_name)
310   
311 {
312   assert(port_fab_type);
313   assert(port_type);
314   assert(port_name);
315   SpecificPortType * retPort; 
316
317   if (SALOME::VerbosityActivated())
318     std::cout << "---- Superv_Component_i::add_port :  Mark 0 ----  " << port_name << "----" << std::endl;
319     
320   std::string s_port_type(port_type);
321   if (s_port_type == "provides") {
322     provides_port * port = create_provides_data_port(port_fab_type);
323     add_port(port, port_name);
324     retPort = dynamic_cast<SpecificPortType *>(port);
325     if ( retPort == NULL ) { delete port;  
326       throw BadCast( LOC("Can't cast to asked port type " ));
327     }
328   }
329   else if (s_port_type == "uses") {
330     uses_port * port = create_uses_data_port(port_fab_type);
331     add_port(port, port_name);
332     if (SALOME::VerbosityActivated())
333     {
334       std::cout << "---- Superv_Component_i::add_port :  Mark 1 ----  " << port << "----" << std::endl;
335       std::cout << "---- Superv_Component_i::add_port :  Mark 1 ----   get_repository_id()" << port->get_repository_id() << std::endl;
336     }
337     retPort = dynamic_cast<SpecificPortType *>(port);
338     if (SALOME::VerbosityActivated())
339       std::cout << "---- Superv_Component_i::add_port :  Mark 2 ----  " << retPort << "----" << std::endl;
340
341     if ( retPort == NULL ) { delete port;  
342       throw BadCast( LOC("Can't cast to asked port type " ));
343     }
344   }
345   else
346     throw BadType(LOC(OSS()<< "port_type must be either 'provides' either 'uses' not "
347                       << port_type));
348   
349   return retPort;
350 }
351
352
353 template <typename SpecificPortType > SpecificPortType * 
354 Superv_Component_i::get_port( const char * port_name)
355   
356 {
357   assert(port_name);
358     
359   SpecificPortType * retPort;
360   base_port        * port;
361
362   my_superv_ports_it = my_superv_ports.find(port_name);
363   if (my_superv_ports_it == my_superv_ports.end())
364   {
365     throw PortNotDefined( LOC(OSS()<< "Port " << port_name <<" does not exist"));
366   }
367
368   superv_port_t * superv_port =  my_superv_ports[port_name];
369   try {
370     if ( superv_port->p_ref != NULL ) {
371       port = superv_port->p_ref;
372       Ports::Port_var portref=Engines_DSC_interface::get_provides_port(port_name, false); 
373     } 
374     else if ( superv_port->u_ref != NULL ) {
375       port = superv_port->u_ref;
376       Engines::DSC::uses_port * portseq=Engines_DSC_i::get_uses_port(port_name);
377       delete portseq;
378     } else {
379       throw UnexpectedState( LOC(OSS()<< "Internal Error superv_port struct is inconsistent "));
380     
381     }
382   } catch (const Engines::DSC::PortNotDefined&) {
383     throw PortNotDefined( LOC(OSS()<< "port "
384                               << port_name <<" does not exist."));
385   } catch (const Engines::DSC::PortNotConnected&) {
386     throw PortNotConnected( LOC(OSS()<< "port " << port_name 
387                                 << " is not connected."));
388   }
389   
390   retPort = dynamic_cast<SpecificPortType *>(port);
391   if ( retPort == NULL ) {
392     throw BadCast( LOC("Can't cast to required port type " ));
393   }
394
395   return retPort;
396 }
397
398
399 #endif