PortAdmin.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <functional>
00020 #include <rtm/RTC.h>
00021 #include <rtm/PortAdmin.h>
00022 #include <rtm/CORBA_SeqUtil.h>
00023 
00024 namespace RTC
00025 {
00033   struct PortAdmin::find_port_name
00034   {
00035     find_port_name(const char* name) : m_name(name) {};
00036     bool operator()(const PortService_ptr& p)
00037     {
00038       try
00039         {
00040 #ifndef ORB_IS_RTORB
00041           PortProfile_var prof(p->get_port_profile());
00042           std::string name(prof->name);
00043 #else // ORB_IS_RTORB
00044           PortProfile *pp;
00045           pp = p->get_port_profile();
00046           std::string name( pp->name);
00047           delete pp;
00048 #endif // ORB_IS_RTORB
00049           return m_name == name;
00050         }
00051       catch (...)
00052         {
00053           return false;
00054         }
00055       return false;
00056     }
00057     const std::string m_name;
00058   };
00059 
00060   struct PortAdmin::find_port
00061   {
00062     find_port(const PortService_ptr& p) : m_port(p) {};
00063     bool operator()(const PortService_ptr& p)
00064     {
00065       return m_port->_is_equivalent(p);
00066     }
00067     const PortService_ptr m_port;
00068   };
00069   
00077   struct PortAdmin::del_port
00078   {
00079     PortAdmin* m_pa;
00080     del_port(PortAdmin* pa) : m_pa(pa) {};
00081     void operator()(PortBase* p)
00082     {
00083       m_pa->removePort(*p);
00084     }
00085   };
00086   
00094   PortAdmin::PortAdmin(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
00095     : m_pORB(CORBA::ORB::_duplicate(orb)),
00096       m_pPOA(PortableServer::POA::_duplicate(poa)),
00097       rtclog("portadmin")
00098   {
00099   }
00100   
00108   PortServiceList* PortAdmin::getPortServiceList() const
00109   {
00110     PortServiceList_var ports;
00111     ports = new PortServiceList(m_portRefs);
00112     return ports._retn();
00113   }
00114   
00122   PortProfileList PortAdmin::getPortProfileList() const
00123   {
00124 
00125 #ifndef ORB_IS_RTORB
00126     PortProfileList port_profs;
00127     //    port_prof_collect p(port_profs);
00128     port_prof_collect2 p(port_profs);
00129     //    m_portServants.for_each(p);
00130     ::CORBA_SeqUtil::for_each(m_portRefs, p);
00131 #else // ORB_IS_RTORB
00132     CORBA::ULong len = m_portRefs.length();
00133 
00134     PortProfileList port_profs = PortProfileList(len);
00135     PortProfile *pp;
00136 
00137     for (CORBA::ULong i (0); i < len; ++i)
00138       {
00139         try
00140           {
00141             pp = m_portRefs[i]->get_port_profile();
00142             port_profs[i] = *(pp);
00143             delete pp;
00144           }
00145         catch (...)
00146           {
00147             ;
00148           }
00149       }
00150 #endif // ORB_IS_RTORB
00151     return port_profs;
00152   }
00153   
00161   PortService_ptr PortAdmin::getPortRef(const char* port_name) const
00162   {
00163     CORBA::Long index;
00164     index = CORBA_SeqUtil::find(m_portRefs, find_port_name(port_name));
00165     if (index >= 0) 
00166       {//throw NotFound(port_name);
00167         return m_portRefs[index];
00168       }
00169     return RTC::PortService::_nil();
00170   }
00171   
00179   PortBase* PortAdmin::getPort(const char* port_name) const
00180   {
00181     return m_portServants.find(port_name);
00182   }
00183   
00191   bool PortAdmin::addPort(PortBase& port)
00192   {
00193     // Check for duplicate
00194     if (CORBA_SeqUtil::find(m_portRefs, find_port_name(port.getName())) != -1)
00195       {
00196         return false;
00197       }
00198 
00199     // Store Port's ref to PortServiceList
00200     CORBA_SeqUtil::push_back(m_portRefs,
00201                              RTC::PortService::_duplicate(port.getPortRef()));
00202     
00203     // Store Port servant
00204     return m_portServants.registerObject(&port);
00205   }
00206 
00214   bool PortAdmin::addPort(PortService_ptr port)
00215   {
00216     // Check for duplicate
00217     try
00218       {
00219         PortProfile_var prof(port->get_port_profile());
00220         std::string name(prof->name);
00221         
00222 // Why RtORB delete _var object explicitly?
00223 #ifdef ORB_IS_RTORB
00224         delete prof._retn();
00225 #endif
00226         if (CORBA_SeqUtil::find(m_portRefs, find_port_name(name.c_str())) != -1)
00227           {
00228             return false;
00229           }
00230       }
00231     catch (...)
00232       {
00233         return false;
00234       }
00235     CORBA_SeqUtil::push_back(m_portRefs, RTC::PortService::_duplicate(port));
00236     return true;
00237   }
00238 
00239   void PortAdmin::registerPort(PortBase& port)
00240   {
00241     if (!addPort(port))
00242       {
00243         RTC_ERROR(("registerPort(PortBase&) failed."));
00244       }
00245   }
00246   
00247   void PortAdmin::registerPort(PortService_ptr port)
00248   {
00249     if (!addPort(port))
00250       {
00251         RTC_ERROR(("registerPort(PortService_ptr) failed."));
00252       }
00253   }
00254   
00262   bool PortAdmin::removePort(PortBase& port)
00263   {
00264     try
00265       {
00266         port.disconnect_all();
00267         // port.shutdown();
00268         
00269         const char* tmp(port.getProfile().name);
00270         CORBA_SeqUtil::erase_if(m_portRefs, find_port_name(tmp));
00271         
00272         PortableServer::ObjectId_var oid = m_pPOA->servant_to_id(&port);
00273         m_pPOA->deactivate_object(oid);
00274         port.setPortRef(RTC::PortService::_nil());
00275         
00276         return m_portServants.unregisterObject(tmp) == NULL ? false : true;
00277       }
00278     catch (...)
00279       {
00280         return false;
00281       }
00282   }
00283 
00284   bool PortAdmin::removePort(PortService_ptr port)
00285   {
00286     try
00287       {
00288         CORBA_SeqUtil::erase_if(m_portRefs, find_port(port));
00289         return true;
00290       }
00291     catch (...)
00292       {
00293         return false;
00294       }
00295   }
00296 
00297   void PortAdmin::deletePort(PortBase& port)
00298   {
00299     if (!removePort(port))
00300       {
00301         RTC_ERROR(("deletePort(PortBase&) failed."));
00302       }
00303   }
00304   void PortAdmin::deletePort(PortService_ptr port)
00305   {
00306     if (!removePort(port))
00307       {
00308         RTC_ERROR(("deletePort(PortService_ptr) failed."));
00309       }
00310   }
00311 
00312   
00320   void PortAdmin::deletePortByName(const char* port_name)
00321   {
00322     if (!port_name) return;
00323     PortBase& p(*m_portServants.find(port_name));
00324     removePort(p);
00325   }
00326 
00334   void PortAdmin::activatePorts()
00335   {
00336     std::vector<PortBase*> ports;
00337     ports = m_portServants.getObjects();
00338     for (int i(0), len(ports.size()); i < len; ++i)
00339       {
00340         ports[i]->activateInterfaces();
00341       }
00342   }
00343 
00351   void PortAdmin::deactivatePorts()
00352   {
00353     std::vector<PortBase*> ports;
00354     ports = m_portServants.getObjects();
00355     for (int i(0), len(ports.size()); i < len; ++i)
00356       {
00357         ports[i]->deactivateInterfaces();
00358       }
00359   }
00360 
00368   void PortAdmin::finalizePorts()
00369   {
00370     deactivatePorts();
00371     std::vector<PortBase*> ports;
00372     ports = m_portServants.getObjects();
00373     for_each(ports.begin(), ports.end(), del_port(this));
00374   }
00375 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:16