OpenRTM-aist  2.0.1
PortBase.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_PORTBASE_H
21 #define RTC_PORTBASE_H
22 
23 #include <rtm/RTC.h>
24 
25 #include <mutex>
26 #include <rtm/idl/RTCSkel.h>
27 #include <rtm/CORBA_SeqUtil.h>
28 #include <rtm/NVUtil.h>
29 #include <rtm/SystemLogger.h>
31 
32 #include <iostream>
33 #include <rtm/DirectPortBase.h>
34 
35 namespace RTC
36 {
37  class ConnectionCallback;
38 
129  class PortBase
130  : public virtual POA_RTC::PortService,
131  public virtual PortableServer::RefCountServantBase
132  {
133  public:
159  explicit PortBase(const char* name = "");
160 
178  ~PortBase() override;
179 
231  PortProfile* get_port_profile() override;
232 
258  const PortProfile& getPortProfile() const;
259 
312  ConnectorProfileList* get_connector_profiles() override;
313 
353  ConnectorProfile* get_connector_profile(const char* connector_id) override;
354 
512  ReturnCode_t connect(ConnectorProfile& connector_profile) override;
513 
629  ReturnCode_t notify_connect(ConnectorProfile& connector_profile) override;
630 
696  ReturnCode_t disconnect(const char* connector_id) override;
697 
786  ReturnCode_t notify_disconnect(const char* connector_id) override;
787 
807  ReturnCode_t disconnect_all() override;
808 
809  //============================================================
810  // Local operations
811  //============================================================
828  virtual void activateInterfaces() = 0;
829 
846  virtual void deactivateInterfaces() = 0;
847 
867  void setName(const char* name);
868 
886  const char* getName() const;
887 
905  const PortProfile& getProfile() const;
906 
928  void setPortRef(PortService_ptr port_ref);
929 
951  PortService_ptr getPortRef() const;
952 
972  void setOwner(RTObject_ptr owner);
973 
974  //============================================================
975  // callbacks
976  //============================================================
1021  void setOnPublishInterfaces(ConnectionCallback* on_publish);
1022 
1067  void setOnSubscribeInterfaces(ConnectionCallback* on_subscribe);
1068 
1120  void setOnConnected(ConnectionCallback* on_connected);
1121 
1167  void setOnUnsubscribeInterfaces(ConnectionCallback* on_unsubscribe);
1168 
1212  void setOnDisconnected(ConnectionCallback* on_disconnected);
1213 
1238  void setOnConnectionLost(ConnectionCallback* on_connection_lost);
1239 
1260  void setPortConnectListenerHolder(PortConnectListeners* portconnListeners);
1261 
1275  virtual DirectPortBase* getDirectPort();
1276  //============================================================
1277  // protected operations
1278  //============================================================
1279  protected:
1346  virtual ReturnCode_t
1347  publishInterfaces(ConnectorProfile& connector_profile) = 0;
1348 
1375  virtual ReturnCode_t connectNext(ConnectorProfile& connector_profile);
1376 
1403  virtual ReturnCode_t disconnectNext(ConnectorProfile& cprof);
1404 
1468  virtual ReturnCode_t
1469  subscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
1470 
1508  virtual void
1509  unsubscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
1510 
1526  virtual void setConnectionLimit(int limit_value);
1527 
1548  virtual ReturnCode_t _publishInterfaces();
1549  //============================================================
1550  // protected utility functions
1551  //============================================================
1578  static bool isEmptyId(const ConnectorProfile& connector_profile) ;
1579 
1599  static std::string getUUID() ;
1600 
1620  static void setUUID(ConnectorProfile& connector_profile) ;
1621 
1647  bool isExistingConnId(const char* id);
1648 
1678  ConnectorProfile findConnProfile(const char* id);
1679 
1708  CORBA::Long findConnProfileIndex(const char* id);
1709 
1737  void updateConnectorProfile(const ConnectorProfile& connector_profile);
1738 
1768  bool eraseConnectorProfile(const char* id);
1769 
1819  bool appendInterface(const char* instance_name, const char* type_name,
1820  PortInterfacePolarity pol);
1821 
1851  bool deleteInterface(const char* name, PortInterfacePolarity pol);
1852 
1876  template <class ValueType>
1877  void addProperty(const char* key, ValueType value)
1878  {
1880  NVUtil::newNV(key, value));
1881  }
1882 
1908  void appendProperty(const char* key, const char* value)
1909  {
1910  NVUtil::appendStringValue(m_profile.properties, key, value);
1911  }
1920  protected:
1938  void updateConnectors();
1939 
1957 #ifndef ORB_IS_RTORB
1958  bool checkPorts(::RTC::PortServiceList& ports);
1959 #else // ORB_IS_RTORB
1960  bool checkPorts(RTC_PortServiceList& ports);
1961 #endif // ORB_IS_RTORB
1962 
1963 
1964  inline void onNotifyConnect(const char* portname,
1965  RTC::ConnectorProfile& profile)
1966  {
1967  if (m_portconnListeners != nullptr)
1968  {
1970  PortConnectListenerType::ON_NOTIFY_CONNECT, portname, profile);
1971  }
1972  }
1973 
1974  inline void onNotifyDisconnect(const char* portname,
1975  RTC::ConnectorProfile& profile)
1976  {
1977  if (m_portconnListeners != nullptr)
1978  {
1981  }
1982  }
1983  inline void onUnsubscribeInterfaces(const char* portname,
1984  RTC::ConnectorProfile& profile)
1985  {
1986  if (m_portconnListeners != nullptr)
1987  {
1990  }
1991  }
1992 
1993  inline void onPublishInterfaces(const char* portname,
1994  RTC::ConnectorProfile& profile,
1995  ReturnCode_t ret)
1996  {
1997  if (m_portconnListeners != nullptr)
1998  {
2001  profile, ret);
2002  }
2003  }
2004 
2005  inline void onConnectNextport(const char* portname,
2006  RTC::ConnectorProfile& profile,
2007  ReturnCode_t ret)
2008  {
2009  if (m_portconnListeners != nullptr)
2010  {
2013  profile, ret);
2014  }
2015  }
2016 
2017  inline void onSubscribeInterfaces(const char* portname,
2018  RTC::ConnectorProfile& profile,
2019  ReturnCode_t ret)
2020  {
2021  if (m_portconnListeners != nullptr)
2022  {
2025  profile, ret);
2026  }
2027  }
2028 
2029  inline void onConnected(const char* portname,
2030  RTC::ConnectorProfile& profile,
2031  ReturnCode_t ret)
2032  {
2033  if (m_portconnListeners != nullptr)
2034  {
2036  PortConnectRetListenerType::ON_CONNECTED, portname, profile, ret);
2037  }
2038  }
2039 
2040  inline void onDisconnectNextport(const char* portname,
2041  RTC::ConnectorProfile& profile,
2042  ReturnCode_t ret)
2043  {
2044  if (m_portconnListeners != nullptr)
2045  {
2047  PortConnectRetListenerType::ON_DISCONNECT_NEXT, portname, profile, ret);
2048  }
2049  }
2050 
2051  inline void onDisconnected(const char* portname,
2052  RTC::ConnectorProfile& profile,
2053  ReturnCode_t ret)
2054  {
2055  if (m_portconnListeners != nullptr)
2056  {
2058  PortConnectRetListenerType::ON_DISCONNECTED, portname, profile, ret);
2059  }
2060  }
2061 
2062  protected:
2089  mutable Logger rtclog;
2097  PortProfile m_profile;
2098 
2106  RTC::PortService_var m_objref;
2114  mutable std::mutex m_profile_mutex;
2115  mutable std::mutex m_connectorsMutex;
2116 
2124  std::string m_ownerInstanceName = "unknown";
2125 
2134 
2207 
2223 
2238 
2240 
2241  //============================================================
2242  // Functor
2243  //============================================================
2252  {
2253  explicit find_conn_id(const char* id) : m_id(id) {}
2254  bool operator()(const ConnectorProfile& cprof)
2255  {
2256  return m_id == std::string(cprof.connector_id);
2257  }
2258  std::string m_id;
2259  }; // struct find_conn_id
2260 
2269  {
2270  explicit find_port_ref(PortService_ptr port_ref) : m_port(port_ref) {}
2271 #ifdef ORB_IS_ORBEXPRESS
2272  bool operator()(PortService_var port_ref)
2273  {
2274  return m_port->_is_equivalent(port_ref.in());
2275  }
2276 #else
2277  bool operator()(PortService_ptr port_ref)
2278  {
2279  return m_port->_is_equivalent(port_ref);
2280  }
2281 #endif
2282  PortService_ptr m_port;
2283  }; // struct find_port_ref
2284 
2293  {
2294  find_interface(const char* name, PortInterfacePolarity pol)
2295  : m_name(name), m_pol(pol)
2296  {}
2297 
2298  bool operator()(const PortInterfaceProfile& prof)
2299  {
2300  CORBA::String_var name(CORBA::string_dup(prof.instance_name));
2301  return ((m_name == static_cast<const char *>(name))
2302  && (m_pol == prof.polarity));
2303  }
2304  std::string m_name;
2305  PortInterfacePolarity m_pol;
2306  }; // struct find_interface
2307  }; // class PortBase
2308 } // namespace RTC
2309 
2310 #endif // RTC_PORTBASE_H
RTC::PortBase::onSubscribeInterfaces
void onSubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2017
RTC::PortConnectListenerType::ON_NOTIFY_DISCONNECT
@ ON_NOTIFY_DISCONNECT
RTC::PortBase::onDisconnectNextport
void onDisconnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2040
RTC::PortBase::onNotifyConnect
void onNotifyConnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1964
RTC::PortBase::deactivateInterfaces
virtual void deactivateInterfaces()=0
Deactivate all Port interfaces.
RTC::PortBase::setOnConnectionLost
void setOnConnectionLost(ConnectionCallback *on_connection_lost)
Setting callback called on connection lost.
RTC::PortBase::onDisconnected
void onDisconnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2051
RTC::PortBase::setOnPublishInterfaces
void setOnPublishInterfaces(ConnectionCallback *on_publish)
Setting callback called on publish interfaces.
RTC::PortBase::m_connectionLimit
int m_connectionLimit
The maximum number of connections.
Definition: PortBase.h:2133
RTC::PortBase::setConnectionLimit
virtual void setConnectionLimit(int limit_value)
Set the maximum number of connections.
RTC::PortBase::getUUID
static std::string getUUID()
Generate the UUID.
RTC::PortConnectListenerType::ON_UNSUBSCRIBE_INTERFACES
@ ON_UNSUBSCRIBE_INTERFACES
RTC::PortBase::m_onConnectionLost
ConnectionCallback * m_onConnectionLost
Callback functor objects.
Definition: PortBase.h:2222
RTC::PortBase::setPortConnectListenerHolder
void setPortConnectListenerHolder(PortConnectListeners *portconnListeners)
Setting PortConnectListener holder.
RTC::PortBase::m_onPublishInterfaces
ConnectionCallback * m_onPublishInterfaces
Callback functor objects.
Definition: PortBase.h:2149
RTC::PortBase::get_connector_profiles
ConnectorProfileList * get_connector_profiles() override
[CORBA interface] Get the ConnectorProfileList of the Port
RTC::PortBase::unsubscribeInterfaces
virtual void unsubscribeInterfaces(const ConnectorProfile &connector_profile)=0
Disconnect interface connection.
RTC::PortBase::isExistingConnId
bool isExistingConnId(const char *id)
Check whether the given id exists in stored ConnectorProfiles.
RTC::PortBase::m_portconnListeners
PortConnectListeners * m_portconnListeners
PortConnectListener holder.
Definition: PortBase.h:2237
RTC::PortBase::appendProperty
void appendProperty(const char *key, const char *value)
Append NameValue data to PortProfile's properties.
Definition: PortBase.h:1908
RTC::PortBase::eraseConnectorProfile
bool eraseConnectorProfile(const char *id)
Delete the ConnectorProfile.
RTC::PortBase::find_port_ref::m_port
PortService_ptr m_port
Definition: PortBase.h:2282
RTC::PortBase::activateInterfaces
virtual void activateInterfaces()=0
Activate all Port interfaces.
RTC::PortBase::onNotifyDisconnect
void onNotifyDisconnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1974
RTC::PortBase::m_objref
RTC::PortService_var m_objref
Object Reference of the Port.
Definition: PortBase.h:2106
RTC::PortBase::find_interface::find_interface
find_interface(const char *name, PortInterfacePolarity pol)
Definition: PortBase.h:2294
RTC::PortBase::find_interface
Functor to find interface from name and polarity.
Definition: PortBase.h:2292
RTC::PortBase::m_directport
DirectPortBase * m_directport
Definition: PortBase.h:2239
RTC::PortBase::onUnsubscribeInterfaces
void onUnsubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1983
RTC::PortConnectRetListenerType::ON_PUBLISH_INTERFACES
@ ON_PUBLISH_INTERFACES
RTC::PortBase::~PortBase
~PortBase() override
Destructor.
RTC::PortBase::find_conn_id::find_conn_id
find_conn_id(const char *id)
Definition: PortBase.h:2253
RTC::PortBase::m_profile_mutex
std::mutex m_profile_mutex
Mutex of PortProfile.
Definition: PortBase.h:2114
RTC::PortBase::m_onUnsubscribeInterfaces
ConnectionCallback * m_onUnsubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2192
RTC::PortBase::find_interface::operator()
bool operator()(const PortInterfaceProfile &prof)
Definition: PortBase.h:2298
CORBA_SeqUtil.h
CORBA sequence utility template functions.
RTC::PortBase::m_onDisconnected
ConnectionCallback * m_onDisconnected
Callback functor objects.
Definition: PortBase.h:2206
CORBA_SeqUtil::push_back
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
Definition: CORBA_SeqUtil.h:175
RTC::PortBase::deleteInterface
bool deleteInterface(const char *name, PortInterfacePolarity pol)
Delete the interface registration from the PortInterfaceProfile.
RTC::PortBase::onPublishInterfaces
void onPublishInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:1993
RTC::PortBase::find_port_ref::find_port_ref
find_port_ref(PortService_ptr port_ref)
Definition: PortBase.h:2270
RTC::PortBase::setOnDisconnected
void setOnDisconnected(ConnectionCallback *on_disconnected)
Setting callback called on disconnected.
RTC::ConnectionCallback
Callback functor abstract for connect/notify_connect() funcs.
Definition: PortCallback.h:55
RTC::PortConnectListenerType::ON_NOTIFY_CONNECT
@ ON_NOTIFY_CONNECT
RTC::PortBase::isEmptyId
static bool isEmptyId(const ConnectorProfile &connector_profile)
Check whether connector_id of ConnectorProfile is empty.
RTC::DirectPortBase
Definition: DirectPortBase.h:46
RTC::PortConnectListeners
PortConnectListeners class.
Definition: PortConnectListener.h:486
RTC::PortBase::find_port_ref::operator()
bool operator()(PortService_ptr port_ref)
Definition: PortBase.h:2277
NVUtil::newNV
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:80
RTC::PortBase::updateConnectorProfile
void updateConnectorProfile(const ConnectorProfile &connector_profile)
Append or update the ConnectorProfile list.
RTC::PortBase::setOnUnsubscribeInterfaces
void setOnUnsubscribeInterfaces(ConnectionCallback *on_unsubscribe)
Setting callback called on unsubscribe interfaces.
RTC::PortBase::m_properties
coil::Properties m_properties
Properties.
Definition: PortBase.h:1919
RTC::PortBase::find_conn_id
Functor to find a ConnectorProfile named id.
Definition: PortBase.h:2251
RTC::PortConnectRetListenerType::ON_DISCONNECT_NEXT
@ ON_DISCONNECT_NEXT
RTC::PortBase::setOnSubscribeInterfaces
void setOnSubscribeInterfaces(ConnectionCallback *on_subscribe)
Setting callback called on publish interfaces.
RTC::PortBase::getPortProfile
const PortProfile & getPortProfile() const
Get the PortProfile of the Port.
RTC::Logger
Logger class.
Definition: SystemLogger.h:99
RTC::PortConnectListeners::notify
bool notify(PortConnectListenerType type, const char *portname, RTC::ConnectorProfile &profile)
Definition: PortConnectListener.h:625
RTC::PortBase::find_port_ref
Functor to find the object reference that is identical port_ref.
Definition: PortBase.h:2268
RTC::PortBase::getName
const char * getName() const
Get the name of this Port.
RTC::PortBase::connectNext
virtual ReturnCode_t connectNext(ConnectorProfile &connector_profile)
Call notify_connect() of the next Port.
RTC::PortBase::_publishInterfaces
virtual ReturnCode_t _publishInterfaces()
Publish interface information.
RTC::PortBase::m_ownerInstanceName
std::string m_ownerInstanceName
Instance name.
Definition: PortBase.h:2124
RTC::PortConnectRetListenerType::ON_CONNECT_NEXTPORT
@ ON_CONNECT_NEXTPORT
RTC::PortBase::addProperty
void addProperty(const char *key, ValueType value)
Add NameValue data to PortProfile's properties.
Definition: PortBase.h:1877
RTC::PortBase::getPortRef
PortService_ptr getPortRef() const
Get the object reference of this Port.
RTC::PortBase::disconnect
ReturnCode_t disconnect(const char *connector_id) override
[CORBA interface] Disconnect the Port
NVUtil::appendStringValue
bool appendStringValue(SDOPackage::NVList &nv, const char *name, const char *value)
Append the specified string to element of NVList.
PortConnectListener.h
port's internal action listener classes
RTC::PortBase
Port base class.
Definition: PortBase.h:129
RTC::PortBase::rtclog
Logger rtclog
Logger stream.
Definition: PortBase.h:2089
RTC::PortBase::subscribeInterfaces
virtual ReturnCode_t subscribeInterfaces(const ConnectorProfile &connector_profile)=0
Publish interface information.
RTC::PortBase::onConnectNextport
void onConnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2005
NVUtil.h
NameValue and NVList utility functions.
RTC::PortBase::setPortRef
void setPortRef(PortService_ptr port_ref)
Set the object reference of this Port.
RTC::PortBase::onConnected
void onConnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2029
RTC::PortBase::m_onSubscribeInterfaces
ConnectionCallback * m_onSubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2163
RTC::PortBase::updateConnectors
void updateConnectors()
Disconnect ports that doesn't exist.
RTC::PortBase::find_interface::m_pol
PortInterfacePolarity m_pol
Definition: PortBase.h:2305
RTC::PortBase::find_conn_id::m_id
std::string m_id
Definition: PortBase.h:2258
RTC::PortBase::m_onConnected
ConnectionCallback * m_onConnected
Callback functor objects.
Definition: PortBase.h:2178
RTC::PortBase::appendInterface
bool appendInterface(const char *instance_name, const char *type_name, PortInterfacePolarity pol)
Append an interface to the PortInterfaceProfile.
RTC::PortBase::getProfile
const PortProfile & getProfile() const
Get the PortProfile of the Port.
RTC::PortBase::disconnectNext
virtual ReturnCode_t disconnectNext(ConnectorProfile &cprof)
Call notify_disconnect() of the next Port.
RTC::PortBase::publishInterfaces
virtual ReturnCode_t publishInterfaces(ConnectorProfile &connector_profile)=0
Publish interface information.
RTC::PortBase::getDirectPort
virtual DirectPortBase * getDirectPort()
RTC::PortBase::findConnProfile
ConnectorProfile findConnProfile(const char *id)
Find ConnectorProfile with id.
RTC::PortBase::notify_connect
ReturnCode_t notify_connect(ConnectorProfile &connector_profile) override
[CORBA interface] Notify the Ports connection
RTC::PortBase::setUUID
static void setUUID(ConnectorProfile &connector_profile)
Generate and set the UUID to the ConnectorProfile.
RTC::PortBase::checkPorts
bool checkPorts(::RTC::PortServiceList &ports)
Existence of ports.
RTC::PortConnectRetListenerType::ON_CONNECTED
@ ON_CONNECTED
RTC::PortConnectRetListenerType::ON_DISCONNECTED
@ ON_DISCONNECTED
RTC
RT-Component.
RTC::PortBase::get_connector_profile
ConnectorProfile * get_connector_profile(const char *connector_id) override
[CORBA interface] Get the ConnectorProfile
RTC::PortBase::setName
void setName(const char *name)
Set the name of this Port.
RTC::PortBase::m_connectorsMutex
std::mutex m_connectorsMutex
Definition: PortBase.h:2115
RTC::Properties
coil::Properties Properties
Definition: RTC.h:72
RTC::PortBase::isExistingMarshalingType
bool isExistingMarshalingType(coil::Properties &con_prop)
Whether the specified serializer can be used.
RTC.h
RTComponent header.
RTC::PortBase::connect
ReturnCode_t connect(ConnectorProfile &connector_profile) override
[CORBA interface] Connect the Port
RTC::PortBase::find_conn_id::operator()
bool operator()(const ConnectorProfile &cprof)
Definition: PortBase.h:2254
RTC::PortBase::setOwner
void setOwner(RTObject_ptr owner)
Set the owner RTObject of the Port.
RTC::PortConnectRetListenerType::ON_SUBSCRIBE_INTERFACES
@ ON_SUBSCRIBE_INTERFACES
RTC::PortBase::find_interface::m_name
std::string m_name
Definition: PortBase.h:2304
RTC::PortBase::PortBase
PortBase(const char *name="")
Constructor.
RTC::PortBase::get_port_profile
PortProfile * get_port_profile() override
[CORBA interface] Get the PortProfile of the Port
RTC::PortBase::setOnConnected
void setOnConnected(ConnectionCallback *on_connected)
Setting callback called on connection established.
SystemLogger.h
RT component logger class.
RTC::PortBase::findConnProfileIndex
CORBA::Long findConnProfileIndex(const char *id)
Find ConnectorProfile with id.
RTC::PortBase::disconnect_all
ReturnCode_t disconnect_all() override
[CORBA interface] Disconnect the All Ports
RTC::PortBase::notify_disconnect
ReturnCode_t notify_disconnect(const char *connector_id) override
[CORBA interface] Notify the Ports disconnection
RTC::PortBase::m_profile
PortProfile m_profile
PortProfile of the Port.
Definition: PortBase.h:2097
DirectPortBase.h
DirectPortBase class.