Back to index

salome-kernel  6.5.0
Superv_Component_i.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : Superv_Component_i.hxx
00024 //  Author : André RIBES (EDF), Eric Fayolle (EDF)
00025 //  Module : KERNEL
00026 //
00027 #ifndef _SUPERV_COMPONENT_I_HXX_
00028 #define _SUPERV_COMPONENT_I_HXX_
00029 
00030 #include "DSC_i.hxx"
00031 #include "base_port.hxx"
00032 #include "uses_port.hxx"
00033 #include "provides_port.hxx"
00034 #include "port_factory.hxx"
00035 
00036 #include "DSC_Exception.hxx"
00037 #include <vector>
00038 
00039 //#define MYDEBUG
00040 
00053 class Superv_Component_i :
00054   public Engines_DSC_i,
00055   virtual public POA_Engines::Superv_Component
00056 {
00057 public:
00058   Superv_Component_i(CORBA::ORB_ptr orb,
00059                      PortableServer::POA_ptr poa,
00060                      PortableServer::ObjectId * contId,
00061                      const char *instanceName,
00062                      const char *interfaceName,
00063                      bool notif = false);
00064   Superv_Component_i(CORBA::ORB_ptr orb,
00065                      PortableServer::POA_ptr poa,
00066                      Engines::Container_ptr container, 
00067                      const char *instanceName,
00068                      const char *interfaceName,
00069                      bool notif = false,
00070          bool regist = true );
00071   virtual ~Superv_Component_i();
00072 
00073   // Exceptions declarations.
00074   // There are defined on the Superv_Component_i.cxx to avoid problems
00075   // from dlopen.
00076   DSC_EXCEPTION(BadFabType);
00077   DSC_EXCEPTION(BadType);
00078   DSC_EXCEPTION(BadCast);
00079   DSC_EXCEPTION(UnexpectedState);
00080   DSC_EXCEPTION(PortAlreadyDefined);
00081   DSC_EXCEPTION(PortNotDefined);
00082   DSC_EXCEPTION(PortNotConnected);
00083   DSC_EXCEPTION(NilPort);
00084   DSC_EXCEPTION(BadProperty);
00085   
00089   virtual provides_port * create_provides_control_port() 
00090   {return NULL;}
00091 
00095   virtual provides_port * create_provides_data_and_control_port(const char* port_type) 
00096   {return NULL;}
00097 
00101   virtual uses_port * create_uses_control_port()
00102   {return NULL;}
00103 
00107   virtual uses_port * create_uses_data_and_control_port(const char* port_type)
00108   {return NULL;}
00109 
00120   virtual provides_port * create_provides_data_port(const std::string& port_fab_type)
00121     throw (BadFabType);
00122 
00123 
00134   virtual uses_port * create_uses_data_port(const std::string& port_fab_type)
00135     throw (BadFabType); 
00136 
00145   virtual void add_port(const char * port_fab_type,
00146                         const char * port_type,
00147                         const char * port_name)
00148     throw (PortAlreadyDefined, BadFabType, BadType, BadProperty);
00149 
00159   template < typename SpecificPortType >  
00160   SpecificPortType * add_port(const char * port_fab_type,
00161                               const char * port_type,
00162                               const char * port_name)
00163     throw (PortAlreadyDefined, BadFabType, BadType, BadCast, BadProperty);
00164 
00171   virtual void add_port(provides_port * port, 
00172                         const char* provides_port_name)
00173     throw (PortAlreadyDefined, NilPort, BadProperty);
00174 
00181   virtual void add_port(uses_port * port, 
00182                         const char* uses_port_name)
00183     throw (PortAlreadyDefined, NilPort, BadProperty);
00184 
00191   virtual void get_port(provides_port *& port, 
00192                         const char* provides_port_name)
00193     throw (PortNotDefined, PortNotConnected);
00194   
00201   virtual void get_port(uses_port *& port, 
00202                         const char* uses_port_name)
00203     throw (PortNotDefined, PortNotConnected);
00204 
00213   virtual void get_uses_port_names(std::vector<std::string> & port_names,
00214                                    const std::string servicename="") const;
00215 
00222   template <typename SpecificPortType > 
00223   SpecificPortType * get_port( const char * port_name)
00224     throw (PortNotDefined, PortNotConnected, BadCast, UnexpectedState);
00225  
00229   virtual void provides_port_changed(const char* provides_port_name,
00230                                      int connection_nbr,
00231                                      const Engines::DSC::Message message);
00232 
00236   virtual void uses_port_changed(const char* uses_port_name,
00237                                  Engines::DSC::uses_port * new_uses_port,
00238                                  const Engines::DSC::Message message);
00239 
00240 
00249   static void register_factory(const std::string & factory_name,
00250                                 port_factory * factory_ptr);
00251 
00258   virtual port_factory * get_factory(const std::string & factory_name);
00259 
00262   static long dscTimeOut;
00263   static void setTimeOut();
00264   void beginService(const char *serviceName);
00265 
00266 private:   
00267   // Factory map
00268   typedef std::map<std::string, port_factory*> factory_map_t;
00269   static factory_map_t _factory_map;
00270 
00271   /*-------------------------------------------------*/
00272   // A Superv_Component port.
00273   struct superv_port_t {
00274     superv_port_t():u_ref(NULL),p_ref(NULL){};
00275     ~superv_port_t()
00276     {
00277       if(u_ref)delete u_ref;
00278       if(p_ref)
00279         {
00280           // do not delete CORBA servant : deactivate it and then call _remove_ref or delete
00281           PortableServer::ServantBase* servant=dynamic_cast<PortableServer::ServantBase*>(p_ref);
00282           if(servant)
00283             {
00284               PortableServer::POA_var poa =servant->_default_POA();
00285               PortableServer::ObjectId_var oid = poa->servant_to_id(servant);
00286               poa->deactivate_object(oid);
00287               servant->_remove_ref();
00288             }
00289         }
00290     };
00291     // For uses ports.
00292     uses_port * u_ref;
00293     // For provides ports.
00294     provides_port * p_ref;
00295   };
00296 
00297   typedef std::map<std::string, superv_port_t *> superv_ports;
00298 
00299   /*-------------------------------------------------*/
00300   /*-------------------------------------------------*/
00301 
00302   superv_ports my_superv_ports;
00303   superv_ports::iterator my_superv_ports_it;
00304 };
00305 
00306 
00307 
00308 template < typename SpecificPortType >  SpecificPortType * 
00309 Superv_Component_i::add_port(const char * port_fab_type,
00310                              const char * port_type,
00311                              const char * port_name)
00312   throw (PortAlreadyDefined, BadFabType, BadType, BadCast, BadProperty)
00313 {
00314   assert(port_fab_type);
00315   assert(port_type);
00316   assert(port_name);
00317   SpecificPortType * retPort; 
00318 
00319 #ifdef MYDEBUG
00320   std::cout << "---- Superv_Component_i::add_port :  Mark 0 ----  " << port_name << "----" << std::endl;
00321 #endif
00322     
00323   std::string s_port_type(port_type);
00324   if (s_port_type == "provides") {
00325     provides_port * port = create_provides_data_port(port_fab_type);
00326     add_port(port, port_name);
00327     retPort = dynamic_cast<SpecificPortType *>(port);
00328     if ( retPort == NULL ) { delete port;  
00329       throw BadCast( LOC("Can't cast to asked port type " ));
00330     }
00331   }
00332   else if (s_port_type == "uses") {
00333     uses_port * port = create_uses_data_port(port_fab_type);
00334     add_port(port, port_name);
00335 #ifdef MYDEBUG
00336     std::cout << "---- Superv_Component_i::add_port :  Mark 1 ----  " << port << "----" << std::endl;
00337     std::cout << "---- Superv_Component_i::add_port :  Mark 1 ----   get_repository_id()" << port->get_repository_id() << std::endl;
00338 #endif
00339     retPort = dynamic_cast<SpecificPortType *>(port);
00340 #ifdef MYDEBUG
00341     std::cout << "---- Superv_Component_i::add_port :  Mark 2 ----  " << retPort << "----" << std::endl;
00342 #endif
00343     if ( retPort == NULL ) { delete port;  
00344       throw BadCast( LOC("Can't cast to asked port type " ));
00345     }
00346   }
00347   else
00348     throw BadType(LOC(OSS()<< "port_type must be either 'provides' either 'uses' not "
00349                       << port_type));
00350   
00351   return retPort;
00352 };
00353 
00354 
00355 template <typename SpecificPortType > SpecificPortType * 
00356 Superv_Component_i::get_port( const char * port_name)
00357   throw (PortNotDefined, PortNotConnected, BadCast, UnexpectedState)
00358 {
00359   assert(port_name);
00360     
00361   SpecificPortType * retPort;
00362   base_port        * port;
00363 
00364   my_superv_ports_it = my_superv_ports.find(port_name);
00365   if (my_superv_ports_it == my_superv_ports.end())
00366   {
00367     throw PortNotDefined( LOC(OSS()<< "Port " << port_name <<" does not exist"));
00368   }
00369 
00370   superv_port_t * superv_port =  my_superv_ports[port_name];
00371   try {
00372     if ( superv_port->p_ref != NULL ) {
00373       port = superv_port->p_ref;
00374       Ports::Port_var portref=Engines_DSC_interface::get_provides_port(port_name, false); 
00375     } 
00376     else if ( superv_port->u_ref != NULL ) {
00377       port = superv_port->u_ref;
00378       Engines::DSC::uses_port * portseq=Engines_DSC_i::get_uses_port(port_name);
00379       delete portseq;
00380     } else {
00381       throw UnexpectedState( LOC(OSS()<< "Internal Error superv_port struct is inconsistent "));
00382     
00383     }
00384   } catch (const Engines::DSC::PortNotDefined&) {
00385     throw PortNotDefined( LOC(OSS()<< "port "
00386                               << port_name <<" does not exist."));
00387   } catch (const Engines::DSC::PortNotConnected&) {
00388     throw PortNotConnected( LOC(OSS()<< "port " << port_name 
00389                                 << " is not connected."));
00390   }
00391   
00392   retPort = dynamic_cast<SpecificPortType *>(port);
00393   if ( retPort == NULL ) {
00394     throw BadCast( LOC("Can't cast to required port type " ));
00395   }
00396 
00397   return retPort;
00398 };
00399 
00400 
00401 #endif