OpenRTM-aist  2.1.0
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  //============================================================
1022 
1068 
1120  void setOnConnected(ConnectionCallback* on_connected);
1121 
1168 
1212  void setOnDisconnected(ConnectionCallback* on_disconnected);
1213 
1238  void setOnConnectionLost(ConnectionCallback* on_connection_lost);
1239 
1261 
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:
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
CORBA sequence utility template functions.
DirectPortBase class.
NameValue and NVList utility functions.
port's internal action listener classes
RTComponent header.
RT component logger class.
Callback functor abstract for connect/notify_connect() funcs.
Definition: PortCallback.h:56
Definition: DirectPortBase.h:47
Logger class.
Definition: SystemLogger.h:101
Port base class.
Definition: PortBase.h:132
ReturnCode_t disconnect(const char *connector_id) override
[CORBA interface] Disconnect the Port
PortProfile m_profile
PortProfile of the Port.
Definition: PortBase.h:2097
std::mutex m_connectorsMutex
Definition: PortBase.h:2115
static bool isEmptyId(const ConnectorProfile &connector_profile)
Check whether connector_id of ConnectorProfile is empty.
int m_connectionLimit
The maximum number of connections.
Definition: PortBase.h:2133
PortConnectListeners * m_portconnListeners
PortConnectListener holder.
Definition: PortBase.h:2237
ConnectorProfile * get_connector_profile(const char *connector_id) override
[CORBA interface] Get the ConnectorProfile
ReturnCode_t notify_disconnect(const char *connector_id) override
[CORBA interface] Notify the Ports disconnection
void setOnUnsubscribeInterfaces(ConnectionCallback *on_unsubscribe)
Setting callback called on unsubscribe interfaces.
void setOwner(RTObject_ptr owner)
Set the owner RTObject of the Port.
bool isExistingConnId(const char *id)
Check whether the given id exists in stored ConnectorProfiles.
DirectPortBase * m_directport
Definition: PortBase.h:2239
void onConnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2005
void onNotifyDisconnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1974
ReturnCode_t notify_connect(ConnectorProfile &connector_profile) override
[CORBA interface] Notify the Ports connection
ConnectorProfileList * get_connector_profiles() override
[CORBA interface] Get the ConnectorProfileList of the Port
void onDisconnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2040
ConnectorProfile findConnProfile(const char *id)
Find ConnectorProfile with id.
ReturnCode_t disconnect_all() override
[CORBA interface] Disconnect the All Ports
ConnectionCallback * m_onConnectionLost
Callback functor objects.
Definition: PortBase.h:2222
ConnectionCallback * m_onUnsubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2192
bool eraseConnectorProfile(const char *id)
Delete the ConnectorProfile.
std::mutex m_profile_mutex
Mutex of PortProfile.
Definition: PortBase.h:2114
void setName(const char *name)
Set the name of this Port.
void onConnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2029
const char * getName() const
Get the name of this Port.
void addProperty(const char *key, ValueType value)
Add NameValue data to PortProfile's properties.
Definition: PortBase.h:1877
ConnectionCallback * m_onConnected
Callback functor objects.
Definition: PortBase.h:2178
ConnectionCallback * m_onSubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2163
const PortProfile & getProfile() const
Get the PortProfile of the Port.
RTC::PortService_var m_objref
Object Reference of the Port.
Definition: PortBase.h:2106
void onNotifyConnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1964
void onDisconnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2051
std::string m_ownerInstanceName
Instance name.
Definition: PortBase.h:2124
void onPublishInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:1993
void setPortRef(PortService_ptr port_ref)
Set the object reference of this Port.
bool appendInterface(const char *instance_name, const char *type_name, PortInterfacePolarity pol)
Append an interface to the PortInterfaceProfile.
CORBA::Long findConnProfileIndex(const char *id)
Find ConnectorProfile with id.
PortBase(const char *name="")
Constructor.
void setOnConnected(ConnectionCallback *on_connected)
Setting callback called on connection established.
coil::Properties m_properties
Properties.
Definition: PortBase.h:1919
ConnectionCallback * m_onPublishInterfaces
Callback functor objects.
Definition: PortBase.h:2149
virtual void unsubscribeInterfaces(const ConnectorProfile &connector_profile)=0
Disconnect interface connection.
virtual void deactivateInterfaces()=0
Deactivate all Port interfaces.
void setOnDisconnected(ConnectionCallback *on_disconnected)
Setting callback called on disconnected.
static void setUUID(ConnectorProfile &connector_profile)
Generate and set the UUID to the ConnectorProfile.
virtual void setConnectionLimit(int limit_value)
Set the maximum number of connections.
void setPortConnectListenerHolder(PortConnectListeners *portconnListeners)
Setting PortConnectListener holder.
void setOnConnectionLost(ConnectionCallback *on_connection_lost)
Setting callback called on connection lost.
void updateConnectors()
Disconnect ports that doesn't exist.
void updateConnectorProfile(const ConnectorProfile &connector_profile)
Append or update the ConnectorProfile list.
virtual ReturnCode_t disconnectNext(ConnectorProfile &cprof)
Call notify_disconnect() of the next Port.
Logger rtclog
Logger stream.
Definition: PortBase.h:2089
bool checkPorts(::RTC::PortServiceList &ports)
Existence of ports.
ConnectionCallback * m_onDisconnected
Callback functor objects.
Definition: PortBase.h:2206
ReturnCode_t connect(ConnectorProfile &connector_profile) override
[CORBA interface] Connect the Port
PortProfile * get_port_profile() override
[CORBA interface] Get the PortProfile of the Port
const PortProfile & getPortProfile() const
Get the PortProfile of the Port.
void appendProperty(const char *key, const char *value)
Append NameValue data to PortProfile's properties.
Definition: PortBase.h:1908
void setOnSubscribeInterfaces(ConnectionCallback *on_subscribe)
Setting callback called on publish interfaces.
virtual ReturnCode_t connectNext(ConnectorProfile &connector_profile)
Call notify_connect() of the next Port.
virtual ReturnCode_t publishInterfaces(ConnectorProfile &connector_profile)=0
Publish interface information.
virtual void activateInterfaces()=0
Activate all Port interfaces.
virtual ReturnCode_t _publishInterfaces()
Publish interface information.
bool isExistingMarshalingType(coil::Properties &con_prop)
Whether the specified serializer can be used.
virtual DirectPortBase * getDirectPort()
~PortBase() override
Destructor.
bool deleteInterface(const char *name, PortInterfacePolarity pol)
Delete the interface registration from the PortInterfaceProfile.
void onUnsubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1983
void onSubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2017
PortService_ptr getPortRef() const
Get the object reference of this Port.
virtual ReturnCode_t subscribeInterfaces(const ConnectorProfile &connector_profile)=0
Publish interface information.
static std::string getUUID()
Generate the UUID.
void setOnPublishInterfaces(ConnectionCallback *on_publish)
Setting callback called on publish interfaces.
PortConnectListeners class.
Definition: PortConnectListener.h:487
bool notify(PortConnectListenerType type, const char *portname, RTC::ConnectorProfile &profile)
Definition: PortConnectListener.h:625
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
Definition: CORBA_SeqUtil.h:175
bool appendStringValue(SDOPackage::NVList &nv, const char *name, const char *value)
Append the specified string to element of NVList.
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:80
RT-Component.
coil::Properties Properties
Definition: RTC.h:72
Functor to find a ConnectorProfile named id.
Definition: PortBase.h:2252
find_conn_id(const char *id)
Definition: PortBase.h:2253
bool operator()(const ConnectorProfile &cprof)
Definition: PortBase.h:2254
std::string m_id
Definition: PortBase.h:2258
Functor to find interface from name and polarity.
Definition: PortBase.h:2293
bool operator()(const PortInterfaceProfile &prof)
Definition: PortBase.h:2298
std::string m_name
Definition: PortBase.h:2304
PortInterfacePolarity m_pol
Definition: PortBase.h:2305
find_interface(const char *name, PortInterfacePolarity pol)
Definition: PortBase.h:2294
Functor to find the object reference that is identical port_ref.
Definition: PortBase.h:2269
find_port_ref(PortService_ptr port_ref)
Definition: PortBase.h:2270
PortService_ptr m_port
Definition: PortBase.h:2282
bool operator()(PortService_ptr port_ref)
Definition: PortBase.h:2277