Back to index

salome-kernel  6.5.0
Superv_Component_i.cxx
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.cxx
00024 //  Author : André RIBES (EDF), Eric Fayolle (EDF)
00025 //  Module : KERNEL
00026 //
00027 #include "Superv_Component_i.hxx"
00028 
00029 DSC_EXCEPTION_CXX(Superv_Component_i,BadFabType);
00030 DSC_EXCEPTION_CXX(Superv_Component_i,BadType);
00031 DSC_EXCEPTION_CXX(Superv_Component_i,BadCast);
00032 DSC_EXCEPTION_CXX(Superv_Component_i,UnexpectedState);
00033 DSC_EXCEPTION_CXX(Superv_Component_i,PortAlreadyDefined);
00034 DSC_EXCEPTION_CXX(Superv_Component_i,PortNotDefined);
00035 DSC_EXCEPTION_CXX(Superv_Component_i,PortNotConnected);
00036 DSC_EXCEPTION_CXX(Superv_Component_i,NilPort);
00037 DSC_EXCEPTION_CXX(Superv_Component_i,BadProperty);
00038 
00039 std::map<std::string, port_factory*> Superv_Component_i::_factory_map;
00040 long Superv_Component_i::dscTimeOut=0;
00041 
00042 
00043 Superv_Component_i::Superv_Component_i(CORBA::ORB_ptr orb,
00044                                        PortableServer::POA_ptr poa,
00045                                        PortableServer::ObjectId * contId,
00046                                        const char *instanceName,
00047                                        const char *interfaceName,
00048                                        bool notif) : 
00049   Engines_DSC_i(orb, poa, contId, instanceName, interfaceName) 
00050 {
00051 #ifdef MYDEBUG
00052   std::cerr << "--Superv_Component_i : MARK 1 ----  " << instanceName << "----" << std::endl;
00053 #endif
00054   setTimeOut();
00055 }
00056 Superv_Component_i::Superv_Component_i(CORBA::ORB_ptr orb,
00057                                        PortableServer::POA_ptr poa,
00058                                        Engines::Container_ptr container, 
00059                                        const char *instanceName,
00060                                        const char *interfaceName,
00061                                        bool notif,
00062                                        bool regist) : 
00063   Engines_DSC_i(orb, poa, container, instanceName, interfaceName,notif,regist) 
00064 {
00065 #ifdef MYDEBUG
00066   std::cerr << "--Superv_Component_i : MARK 1 ----  " << instanceName << "----" << std::endl;
00067 #endif
00068   setTimeOut();
00069 }
00070 
00071   
00072 Superv_Component_i::~Superv_Component_i() 
00073 {
00074   my_superv_ports_it = my_superv_ports.begin();
00075   for(;my_superv_ports_it != my_superv_ports.end();my_superv_ports_it++) 
00076     delete my_superv_ports_it->second;
00077 }
00078 
00079 void 
00080 Superv_Component_i::register_factory(const std::string & factory_name,
00081                                      port_factory * factory_ptr) 
00082 {
00083   factory_map_t::iterator it = _factory_map.find(factory_name);
00084 
00085   if (it == _factory_map.end() )
00086   {
00087     _factory_map[factory_name] = factory_ptr;
00088   }
00089 }
00090 
00091 port_factory *
00092 Superv_Component_i::get_factory(const std::string & factory_name) 
00093 {
00094   port_factory * rtn_factory = NULL;
00095   factory_map_t::iterator it = _factory_map.find(factory_name);
00096 
00097   if (it != _factory_map.end() )
00098   {
00099     rtn_factory = _factory_map[factory_name];
00100   }
00101 
00102   return rtn_factory;
00103 }
00104 
00105 provides_port *
00106 Superv_Component_i::create_provides_data_port(const std::string& port_fab_type)
00107   throw (BadFabType)
00108 { 
00109   provides_port * rtn_port = NULL;
00110   std::string factory_name;
00111   std::string type_name;
00112   int search_result;
00113 
00114   search_result = port_fab_type.find("_");
00115   factory_name = port_fab_type.substr(0,search_result);
00116   type_name = port_fab_type.substr(search_result+1, port_fab_type.length());
00117 
00118   port_factory * factory = get_factory(factory_name);
00119   if (factory) {
00120     rtn_port = factory->create_data_servant(type_name);
00121   }
00122 
00123   if (rtn_port == NULL)
00124     throw  BadFabType( LOC(OSS()<< "No way to get the port factory "
00125                            <<port_fab_type));
00126 
00127   return rtn_port;
00128 }
00129 
00130 uses_port *
00131 Superv_Component_i::create_uses_data_port(const std::string& port_fab_type) 
00132 throw (BadFabType)
00133 {
00134   uses_port * rtn_proxy = NULL;
00135   std::string factory_name;
00136   std::string type_name;
00137   int search_result;
00138 
00139   search_result = port_fab_type.find("_");
00140   factory_name = port_fab_type.substr(0,search_result);
00141   type_name = port_fab_type.substr(search_result+1, port_fab_type.length());
00142 
00143   port_factory * factory = get_factory(factory_name);
00144   if (factory) {
00145     rtn_proxy = factory->create_data_proxy(type_name);
00146   }
00147   
00148   if (rtn_proxy == NULL)
00149    throw BadFabType( LOC(OSS()<< "No way to get the port factory "
00150                           <<port_fab_type));
00151 
00152   return rtn_proxy;
00153 }
00154 
00155 void
00156 Superv_Component_i::add_port(const char * port_fab_type,
00157                              const char * port_type,
00158                              const char * port_name)
00159   throw (PortAlreadyDefined, BadFabType, BadType, BadProperty)
00160 {
00161   assert(port_fab_type);
00162   assert(port_type);
00163   assert(port_name);
00164 
00165   std::string s_port_type(port_type);
00166   if (s_port_type == "provides") {
00167     provides_port * port = create_provides_data_port(port_fab_type);
00168     add_port(port, port_name);
00169   }
00170   else if (s_port_type == "uses") {
00171 #ifdef MYDEBUG
00172     std::cerr << "---- Superv_Component_i::add_port : MARK 1 ---- "  << std::endl;
00173 #endif
00174     uses_port * port = create_uses_data_port(port_fab_type);
00175 #ifdef MYDEBUG
00176     std::cerr << "---- Superv_Component_i::add_port : MARK 2 ---- "  << std::endl;
00177 #endif
00178     add_port(port, port_name);
00179   }
00180   else
00181     throw BadType( LOC(OSS()<< "port_type must be either 'provides' either 'uses' not "
00182                        << port_type));
00183 
00184 }
00185 
00186 void 
00187 Superv_Component_i::add_port(provides_port * port, 
00188                              const char* provides_port_name) 
00189   throw (PortAlreadyDefined, NilPort, BadProperty)
00190 {
00191   assert(port);
00192   assert(provides_port_name);
00193 
00194   try {
00195 
00196     Ports::PortProperties_var portproperties=port->get_port_properties();
00197     Ports::Port_var portref=port->get_port_ref();
00198     Engines_DSC_interface::add_provides_port(portref, 
00199                                              provides_port_name,
00200                                              portproperties);
00201 
00202     superv_port_t * new_superv_port = new superv_port_t();
00203     new_superv_port->p_ref = port;
00204     my_superv_ports[provides_port_name] = new_superv_port;
00205 
00206   } 
00207   catch (const Engines::DSC::PortAlreadyDefined&) {
00208     throw PortAlreadyDefined( LOC(OSS()<< "provides port "
00209                                   << provides_port_name <<" already exist."));
00210   } 
00211   catch (const Engines::DSC::NilPort&) {
00212     throw NilPort( LOC(OSS()<< "provides port pointer is nul."));
00213   }
00214   catch (const Engines::DSC::BadProperty&) {
00215     throw BadProperty( LOC(OSS()<< "Property is not well defined"));
00216   }
00217 }
00218 
00219 void
00220 Superv_Component_i::add_port(uses_port * port, 
00221                              const char* uses_port_name) 
00222   throw (PortAlreadyDefined, NilPort, BadProperty)
00223 {
00224   assert(port);
00225   assert(uses_port_name);
00226 
00227   try {
00228     Ports::PortProperties_var portproperties=port->get_port_properties();
00229     Engines_DSC_interface::add_uses_port(port->get_repository_id(), 
00230                                          uses_port_name,
00231                                          portproperties);
00232     superv_port_t * new_superv_port = new superv_port_t();
00233     new_superv_port->u_ref = port;
00234     my_superv_ports[uses_port_name] = new_superv_port;
00235   } 
00236   catch (const Engines::DSC::PortAlreadyDefined&) {
00237     throw PortAlreadyDefined( LOC(OSS()<< "uses port " 
00238                                   << uses_port_name <<" already exists."));
00239   } 
00240   catch (const Engines::DSC::NilPort&) {
00241     throw NilPort( LOC(OSS()<< "uses port pointer is nul."));
00242   }
00243   catch (const Engines::DSC::BadProperty&) {
00244     throw BadProperty( LOC(OSS()<< "Property is not well defined"));
00245   }
00246 }
00247 
00248 void
00249 Superv_Component_i::get_port(provides_port *& port,
00250                              const char * provides_port_name)
00251   throw (PortNotDefined,PortNotConnected)
00252 {
00253   assert(provides_port_name);
00254 
00255   try {
00256     Ports::Port_var portref=Engines_DSC_interface::get_provides_port(provides_port_name, false);
00257     port = my_superv_ports[provides_port_name]->p_ref;
00258   } catch (const Engines::DSC::PortNotDefined&) {
00259     throw PortNotDefined( LOC(OSS()<< "provides  port "
00260                               << provides_port_name <<" does not exist."));
00261   } catch (const Engines::DSC::PortNotConnected&) {
00262     throw PortNotConnected( LOC(OSS()<< "provides port " << provides_port_name 
00263                                 << " is not connected."));
00264   }
00265 }
00266 
00267 void
00268 Superv_Component_i::get_port(uses_port *& port,
00269                              const char * uses_port_name)
00270   throw (PortNotDefined, PortNotConnected)
00271 {
00272   assert(uses_port_name);
00273 
00274   try {
00275     Engines::DSC::uses_port * portseq=Engines_DSC_i::get_uses_port(uses_port_name);
00276     delete portseq;
00277     port = my_superv_ports[uses_port_name]->u_ref;
00278   } catch (const Engines::DSC::PortNotDefined&) {    
00279     throw PortNotDefined( LOC(OSS()<< "uses port "
00280                               << uses_port_name <<" does not exist."));
00281   } catch (const Engines::DSC::PortNotConnected&) {
00282     throw PortNotConnected( LOC(OSS()<< "uses port " << uses_port_name 
00283                                 << " is not connected."));
00284   }
00285 }
00286 
00287 
00288 
00289 void
00290 Superv_Component_i::provides_port_changed(const char* provides_port_name,
00291                                           int connection_nbr,
00292                                           const Engines::DSC::Message message)
00293 {
00294   my_superv_ports_it = my_superv_ports.find(provides_port_name);
00295   if (my_superv_ports_it !=  my_superv_ports.end())
00296     my_superv_ports[provides_port_name]->p_ref->provides_port_changed(connection_nbr,
00297                                                                       message);
00298 }
00299 
00300 void
00301 Superv_Component_i::uses_port_changed(const char* uses_port_name,
00302                                       Engines::DSC::uses_port * new_uses_port,
00303                                       const Engines::DSC::Message message)
00304 {
00305   my_superv_ports_it = my_superv_ports.find(uses_port_name);
00306   if (my_superv_ports_it !=  my_superv_ports.end())
00307     my_superv_ports[uses_port_name]->u_ref->uses_port_changed(new Engines::DSC::uses_port(*new_uses_port),
00308                                                               message);
00309   //delete the copy made by the caller
00310   delete new_uses_port;
00311 }
00312 
00313 
00314 
00315 void
00316 Superv_Component_i::get_uses_port_names(std::vector<std::string> & port_names,
00317                                         const std::string servicename) const {
00318 
00319   port_names.reserve(my_superv_ports.size());
00320 
00321   superv_ports::const_iterator it;
00322 
00323   for (it=my_superv_ports.begin(); it!=my_superv_ports.end();++it)
00324     if( (*it).second->p_ref == NULL ) port_names.push_back((*it).first);
00325 }
00326 
00327 void Superv_Component_i::setTimeOut()
00328 {
00329   char* valenv=getenv("DSC_TIMEOUT");
00330   if(valenv)
00331     {
00332       std::istringstream iss(valenv);
00333       long temp;
00334       if (iss >> temp)
00335         if(temp >=0)
00336           Superv_Component_i::dscTimeOut=temp;
00337     }
00338 }
00339 
00340 void Superv_Component_i::beginService(const char *serviceName)
00341 {
00342   Engines_DSC_i::beginService(serviceName);
00343   setTimeOut();
00344 }