1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : Superv_Component_i.hxx
24 // Author : André RIBES (EDF), Eric Fayolle (EDF)
27 #ifndef _SUPERV_COMPONENT_I_HXX_
28 #define _SUPERV_COMPONENT_I_HXX_
31 #include "base_port.hxx"
32 #include "uses_port.hxx"
33 #include "provides_port.hxx"
34 #include "port_factory.hxx"
36 #include "DSC_Exception.hxx"
39 /*! \class Superv_Component_i
40 * \brief This class implements DSC_User component.
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.
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.
49 * \note This class doesn't implement the init_service CORBA operation.
51 class Superv_Component_i :
53 virtual public POA_Engines::Superv_Component
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,
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,
68 bool regist = false );
69 virtual ~Superv_Component_i();
71 // Exceptions declarations.
72 // There are defined on the Superv_Component_i.cxx to avoid problems
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)
85 * \warning currently disabled.
87 virtual provides_port * create_provides_control_port()
91 * \warning currently disabled.
93 virtual provides_port * create_provides_data_and_control_port(const char* /*port_type*/)
97 * \warning currently disabled.
99 virtual uses_port * create_uses_control_port()
103 * \warning currently disabled.
105 virtual uses_port * create_uses_data_and_control_port(const char* /*port_type*/)
109 * This method permits to create a provides port provided by the platform.
110 * (See documentation of DSC for knoing these ports).
113 * \param port_fab_type type provides port.
114 * \return the provides port.
116 * \note It's user repsonsability to destroy the provides port.
118 virtual provides_port * create_provides_data_port(const std::string& port_fab_type);
122 * This method permits to create a uses port provided by the platform.
123 * (See documentation of DSC for knoing these ports).
126 * \param port_fab_type type uses port.
127 * \return the uses port.
129 * \note It's user repsonsability to destroy the uses port.
131 virtual uses_port * create_uses_data_port(const std::string& port_fab_type);
134 * Adds a port to the component. With this method only Salomé's provided DSC ports
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.
141 virtual void add_port(const char * port_fab_type,
142 const char * port_type,
143 const char * port_name);
146 * Adds a port to the component. With this method only Salomé's provided DSC ports
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.
154 template < typename SpecificPortType >
155 SpecificPortType * add_port(const char * port_fab_type,
156 const char * port_type,
157 const char * port_name);
160 * Adds a created provides port to the component.
162 * \param port the provides port.
163 * \param provides_port_name the name of the port in the component.
165 virtual void add_port(provides_port * port,
166 const char* provides_port_name);
169 * Adds a created uses port to the component.
171 * \param port the uses port.
172 * \param uses_port_name the name of the port in the component.
174 virtual void add_port(uses_port * port,
175 const char* uses_port_name);
178 * Gets the provides port already added in the component.
180 * \param port the provides port pointer.
181 * \param provides_port_name the name of the port.
183 virtual void get_port(provides_port *& port,
184 const char* provides_port_name);
187 * Gets the uses port already added in the component.
189 * \param port the uses port pointer.
190 * \param uses_port_name the name of the port.
192 virtual void get_port(uses_port *& port,
193 const char* uses_port_name);
196 * Gets the list of the ports of a service.
197 * If servicename is not set, all the ports of the component are
200 * \param port_names the ports's list.
201 * \param servicename service's name.
203 virtual void get_uses_port_names(std::vector<std::string> & port_names,
204 const std::string servicename="") const;
207 * Gets a port already added in the component.
209 * \param port_name the name of the port.
210 * \return a port's pointer.
212 template <typename SpecificPortType >
213 SpecificPortType * get_port( const char * port_name);
216 * \see DSC_Callbacks::provides_port_changed
218 virtual void provides_port_changed(const char* provides_port_name,
220 const Engines::DSC::Message message);
223 * \see DSC_Callbacks::uses_port_changed
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);
231 * Add a factory the component. If the factory_name is already
232 * used, the new library is not added.
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)
238 static void register_factory(const std::string & factory_name,
239 port_factory * factory_ptr);
242 * Get a factory from the component.
244 * \param factory_name name of the factory.
245 * \return factory pointer, NULL if the factory doesn't exist.
247 virtual port_factory * get_factory(const std::string & factory_name);
251 static long dscTimeOut;
252 static void setTimeOut();
253 void beginService(const char *serviceName);
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*/)
266 typedef std::map<std::string, port_factory*> factory_map_t;
267 static factory_map_t _factory_map;
269 /*-------------------------------------------------*/
270 // A Superv_Component port.
271 struct superv_port_t {
272 superv_port_t():u_ref(NULL),p_ref(NULL){};
275 if(u_ref)delete u_ref;
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);
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();
291 // For provides ports.
292 provides_port * p_ref;
295 typedef std::map<std::string, superv_port_t *> superv_ports;
297 /*-------------------------------------------------*/
298 /*-------------------------------------------------*/
300 superv_ports my_superv_ports;
301 superv_ports::iterator my_superv_ports_it;
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)
312 assert(port_fab_type);
315 SpecificPortType * retPort;
317 if (SALOME::VerbosityActivated())
318 std::cout << "---- Superv_Component_i::add_port : Mark 0 ---- " << port_name << "----" << std::endl;
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 " ));
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())
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;
337 retPort = dynamic_cast<SpecificPortType *>(port);
338 if (SALOME::VerbosityActivated())
339 std::cout << "---- Superv_Component_i::add_port : Mark 2 ---- " << retPort << "----" << std::endl;
341 if ( retPort == NULL ) { delete port;
342 throw BadCast( LOC("Can't cast to asked port type " ));
346 throw BadType(LOC(OSS()<< "port_type must be either 'provides' either 'uses' not "
353 template <typename SpecificPortType > SpecificPortType *
354 Superv_Component_i::get_port( const char * port_name)
359 SpecificPortType * retPort;
362 my_superv_ports_it = my_superv_ports.find(port_name);
363 if (my_superv_ports_it == my_superv_ports.end())
365 throw PortNotDefined( LOC(OSS()<< "Port " << port_name <<" does not exist"));
368 superv_port_t * superv_port = my_superv_ports[port_name];
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);
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);
379 throw UnexpectedState( LOC(OSS()<< "Internal Error superv_port struct is inconsistent "));
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."));
390 retPort = dynamic_cast<SpecificPortType *>(port);
391 if ( retPort == NULL ) {
392 throw BadCast( LOC("Can't cast to required port type " ));