PortBase.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifndef PortBase_h
00021 #define PortBase_h
00022 
00023 #include <rtm/RTC.h>
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <coil/Guard.h>
00028 #include <coil/Mutex.h>
00029 #include <rtm/idl/RTCSkel.h>
00030 #include <rtm/CORBA_SeqUtil.h>
00031 #include <rtm/NVUtil.h>
00032 #include <rtm/SystemLogger.h>
00033 
00034 #include <iostream>
00035 
00036 #ifdef WIN32
00037 #pragma warning( disable : 4290 )
00038 #endif
00039 
00040 namespace RTC
00041 {
00132   class PortBase
00133     : public virtual POA_RTC::PortService,
00134       public virtual PortableServer::RefCountServantBase
00135   {
00136   public:
00160     PortBase(const char* name = "");
00161     
00177     virtual ~PortBase(void);
00178     
00220     virtual PortProfile* get_port_profile()
00221       throw (CORBA::SystemException);
00222     
00242     const PortProfile& getPortProfile() const;
00243     
00283     virtual ConnectorProfileList* get_connector_profiles()
00284       throw (CORBA::SystemException);
00285     
00313     virtual ConnectorProfile* get_connector_profile(const char* connector_id)
00314       throw (CORBA::SystemException);
00315     
00449     virtual ReturnCode_t connect(ConnectorProfile& connector_profile)
00450       throw (CORBA::SystemException);
00451     
00488     virtual ReturnCode_t notify_connect(ConnectorProfile& connector_profile)
00489       throw (CORBA::SystemException);
00490     
00523     virtual ReturnCode_t disconnect(const char* connector_id)
00524       throw (CORBA::SystemException);
00525     
00565     virtual ReturnCode_t notify_disconnect(const char* connector_id)
00566       throw (CORBA::SystemException);
00567     
00587     virtual ReturnCode_t disconnect_all()
00588       throw (CORBA::SystemException);
00589     
00590     //============================================================
00591     // Local operations
00592     //============================================================
00609     virtual void activateInterfaces() = 0;
00610 
00627     virtual void deactivateInterfaces() = 0;
00628 
00648     void setName(const char* name);
00649     
00667     const PortProfile& getProfile() const;
00668     
00690     void setPortRef(PortService_ptr port_ref);
00691     
00713     PortService_ptr getPortRef();
00714     
00734     void setOwner(RTObject_ptr owner);
00735 
00736     //============================================================
00737     // protected operations
00738     //============================================================
00739   protected:
00806     virtual ReturnCode_t
00807     publishInterfaces(ConnectorProfile& connector_profile) = 0;
00808     
00835     virtual ReturnCode_t connectNext(ConnectorProfile& connector_profile);
00836     
00863     virtual ReturnCode_t disconnectNext(ConnectorProfile& connector_profile);
00864     
00928     virtual ReturnCode_t
00929     subscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
00930     
00968     virtual void
00969     unsubscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
00970     
00971     //============================================================
00972     // protected utility functions
00973     //============================================================
01000     bool isEmptyId(const ConnectorProfile& connector_profile) const;
01001     
01021     const std::string getUUID() const;
01022     
01042     void setUUID(ConnectorProfile& connector_profile) const;
01043     
01069     bool isExistingConnId(const char* id);
01070     
01100     ConnectorProfile findConnProfile(const char* id);
01101     
01130     CORBA::Long findConnProfileIndex(const char* id);
01131     
01159     void updateConnectorProfile(const ConnectorProfile& connector_profile);
01160     
01190     bool eraseConnectorProfile(const char* id);
01191     
01241     bool appendInterface(const char* name, const char* type_name,
01242                          PortInterfacePolarity pol);
01243     
01273     bool deleteInterface(const char* name, PortInterfacePolarity pol);
01274     
01298     template <class ValueType>
01299     void addProperty(const char* key, ValueType value)
01300     {
01301       CORBA_SeqUtil::push_back(m_profile.properties,
01302                                NVUtil::newNV(key, value));
01303     }
01304 
01305     void appendProperty(const char* key, const char* value)
01306     {
01307       NVUtil::appendStringValue(m_profile.properties, key, value);
01308     }
01309     
01310   protected:
01311     mutable Logger rtclog;
01319     PortProfile m_profile;
01320     
01328     RTC::PortService_var m_objref;
01329     mutable coil::Mutex m_profile_mutex;
01330     typedef coil::Guard<coil::Mutex> Guard;
01331     
01332     //============================================================
01333     // Functor
01334     //============================================================
01342     struct if_name
01343     {
01344       if_name(const char* name) : m_name(name) {};
01345       bool operator()(const PortInterfaceProfile& prof)
01346       {
01347         return m_name == std::string(prof.instance_name);
01348       }
01349       std::string m_name;
01350     };  // struct if_name
01351     
01359     struct find_conn_id
01360     {
01361       find_conn_id(const char* id) : m_id(id) {};
01362       bool operator()(const ConnectorProfile& cprof)
01363       {
01364         return m_id == std::string(cprof.connector_id);
01365       }
01366       std::string m_id;
01367     };  // struct find_conn_id
01368     
01376     struct find_port_ref
01377     {
01378       find_port_ref(PortService_ptr port_ref) : m_port(port_ref) {};
01379       bool operator()(PortService_ptr port_ref)
01380       {
01381         return m_port->_is_equivalent(port_ref);
01382       }
01383       PortService_ptr m_port;
01384     };  // struct find_port_ref
01385     
01393     struct connect_func
01394     {
01395       PortService_var port_ref;
01396       ConnectorProfile connector_profile;
01397       ReturnCode_t return_code;
01398       
01399       connect_func() {};
01400       connect_func(PortService_ptr p, ConnectorProfile& prof)
01401         : port_ref(p), connector_profile(prof), return_code(RTC::RTC_OK) {};
01402       void operator()(PortService_ptr p)
01403       {
01404         if (!port_ref->_is_equivalent(p))
01405           {
01406             ReturnCode_t retval;
01407             retval = p->notify_connect(connector_profile);
01408             if (retval != RTC::RTC_OK)
01409               {
01410                 return_code = retval;
01411               }
01412           }
01413       }
01414     };  // struct connect_func
01415     
01423     struct disconnect_func
01424     {
01425       PortService_var port_ref;
01426       ConnectorProfile connector_profile;
01427       ReturnCode_t return_code;
01428       
01429       disconnect_func() : return_code(RTC::RTC_OK) {};
01430       disconnect_func(PortService_ptr p, ConnectorProfile& prof)
01431         : port_ref(p), connector_profile(prof), return_code(RTC::RTC_OK) {};
01432       void operator()(PortService_ptr p)
01433       {
01434         if (!port_ref->_is_equivalent(p))
01435           {
01436             ReturnCode_t retval;
01437             retval = p->disconnect(connector_profile.connector_id);
01438             if (retval != RTC::RTC_OK)
01439               {
01440                 return_code = retval;
01441               }
01442           }
01443       }
01444     };  // struct disconnect_func
01445     
01453     struct disconnect_all_func
01454     {
01455       ReturnCode_t return_code;
01456       PortBase* port;
01457       
01458       disconnect_all_func() {};
01459       disconnect_all_func(PortBase* p) 
01460         : return_code(RTC::RTC_OK), port(p) {};
01461       void operator()(ConnectorProfile& p)
01462       {
01463         ReturnCode_t retval;
01464         retval = port->disconnect(p.connector_id);
01465         if (retval != RTC::RTC_OK)
01466           {
01467             return_code = retval;
01468           }
01469       }
01470     };  // struct disconnect_all_func
01471     
01479     struct find_interface
01480     {
01481       find_interface(const char* name, PortInterfacePolarity pol)
01482         : m_name(name), m_pol(pol)
01483       {}
01484       
01485       bool operator()(const PortInterfaceProfile& prof)
01486       {
01487         std::string name(CORBA::string_dup(prof.instance_name));
01488         return ((m_name == name) && (m_pol == prof.polarity));
01489       }
01490       std::string m_name;
01491       PortInterfacePolarity m_pol;
01492     };  // struct find_interface
01493   };  // class PortBase
01494 };  // namespace RTC
01495 
01496 #ifdef WIN32
01497 #pragma warning( default : 4290 )
01498 #endif
01499 
01500 #endif // PortBase_h

Generated on Sun May 24 14:08:45 2009 for OpenRTM by  doxygen 1.5.3