OpenRTM-aist  1.2.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 <string>
26 #include <vector>
27 #include <coil/Guard.h>
28 #include <coil/Mutex.h>
29 #include <rtm/idl/RTCSkel.h>
30 #include <rtm/CORBA_SeqUtil.h>
31 #include <rtm/NVUtil.h>
32 #include <rtm/SystemLogger.h>
34 #include <iostream>
35 #include <rtm/DirectPortBase.h>
36 
37 #ifdef WIN32
38 #pragma warning( disable : 4290 )
39 #endif
40 
41 namespace RTC
42 {
43  class ConnectionCallback;
44 
135  class PortBase
136  : public virtual POA_RTC::PortService,
137  public virtual PortableServer::RefCountServantBase
138  {
139  public:
165  PortBase(const char* name = "");
166 
184  virtual ~PortBase(void);
185 
237  virtual PortProfile* get_port_profile()
238  throw (CORBA::SystemException);
239 
265  const PortProfile& getPortProfile() const;
266 
319  virtual ConnectorProfileList* get_connector_profiles()
320  throw (CORBA::SystemException);
321 
361  virtual ConnectorProfile* get_connector_profile(const char* connector_id)
362  throw (CORBA::SystemException);
363 
521  virtual ReturnCode_t connect(ConnectorProfile& connector_profile)
522  throw (CORBA::SystemException);
523 
639  virtual ReturnCode_t notify_connect(ConnectorProfile& connector_profile)
640  throw (CORBA::SystemException);
641 
707  virtual ReturnCode_t disconnect(const char* connector_id)
708  throw (CORBA::SystemException);
709 
798  virtual ReturnCode_t notify_disconnect(const char* connector_id)
799  throw (CORBA::SystemException);
800 
820  virtual ReturnCode_t disconnect_all()
821  throw (CORBA::SystemException);
822 
823  //============================================================
824  // Local operations
825  //============================================================
842  virtual void activateInterfaces() = 0;
843 
860  virtual void deactivateInterfaces() = 0;
861 
881  void setName(const char* name);
882 
900  const char* getName() const;
901 
919  const PortProfile& getProfile() const;
920 
942  void setPortRef(PortService_ptr port_ref);
943 
965  PortService_ptr getPortRef() const;
966 
986  void setOwner(RTObject_ptr owner);
987 
988  //============================================================
989  // callbacks
990  //============================================================
1035  void setOnPublishInterfaces(ConnectionCallback* on_publish);
1036 
1081  void setOnSubscribeInterfaces(ConnectionCallback* on_subscribe);
1082 
1134  void setOnConnected(ConnectionCallback* on_connected);
1135 
1181  void setOnUnsubscribeInterfaces(ConnectionCallback* on_subscribe);
1182 
1226  void setOnDisconnected(ConnectionCallback* on_disconnected);
1227 
1252  void setOnConnectionLost(ConnectionCallback* on_connection_lost);
1253 
1274  void setPortConnectListenerHolder(PortConnectListeners* portconnListeners);
1275 
1289  virtual DirectPortBase* getDirectPort();
1290  //============================================================
1291  // protected operations
1292  //============================================================
1293  protected:
1360  virtual ReturnCode_t
1361  publishInterfaces(ConnectorProfile& connector_profile) = 0;
1362 
1389  virtual ReturnCode_t connectNext(ConnectorProfile& connector_profile);
1390 
1417  virtual ReturnCode_t disconnectNext(ConnectorProfile& connector_profile);
1418 
1482  virtual ReturnCode_t
1483  subscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
1484 
1522  virtual void
1523  unsubscribeInterfaces(const ConnectorProfile& connector_profile) = 0;
1524 
1540  virtual void setConnectionLimit(int limit_value);
1541 
1562  virtual ReturnCode_t _publishInterfaces(void);
1563  //============================================================
1564  // protected utility functions
1565  //============================================================
1592  bool isEmptyId(const ConnectorProfile& connector_profile) const;
1593 
1613  const std::string getUUID() const;
1614 
1634  void setUUID(ConnectorProfile& connector_profile) const;
1635 
1661  bool isExistingConnId(const char* id);
1662 
1692  ConnectorProfile findConnProfile(const char* id);
1693 
1722  CORBA::Long findConnProfileIndex(const char* id);
1723 
1751  void updateConnectorProfile(const ConnectorProfile& connector_profile);
1752 
1782  bool eraseConnectorProfile(const char* id);
1783 
1833  bool appendInterface(const char* name, const char* type_name,
1834  PortInterfacePolarity pol);
1835 
1865  bool deleteInterface(const char* name, PortInterfacePolarity pol);
1866 
1890  template <class ValueType>
1891  void addProperty(const char* key, ValueType value)
1892  {
1894  NVUtil::newNV(key, value));
1895  }
1896 
1922  void appendProperty(const char* key, const char* value)
1923  {
1924  NVUtil::appendStringValue(m_profile.properties, key, value);
1925  }
1934  protected:
1952  void updateConnectors();
1953 
1971 #ifndef ORB_IS_RTORB
1972  bool checkPorts(::RTC::PortServiceList& ports);
1973 #else // ORB_IS_RTORB
1974  bool checkPorts(RTC_PortServiceList& ports);
1975 #endif // ORB_IS_RTORB
1976 
1977 
1978  inline void onNotifyConnect(const char* portname,
1979  RTC::ConnectorProfile& profile)
1980  {
1981  if (m_portconnListeners != NULL)
1982  {
1984  portconnect_[ON_NOTIFY_CONNECT].notify(portname, profile);
1985  }
1986  }
1987 
1988  inline void onNotifyDisconnect(const char* portname,
1989  RTC::ConnectorProfile& profile)
1990  {
1991  if (m_portconnListeners != NULL)
1992  {
1994  portconnect_[ON_NOTIFY_DISCONNECT].notify(portname, profile);
1995  }
1996  }
1997  inline void onUnsubscribeInterfaces(const char* portname,
1998  RTC::ConnectorProfile& profile)
1999  {
2000  if (m_portconnListeners != NULL)
2001  {
2003  portconnect_[ON_UNSUBSCRIBE_INTERFACES].notify(portname, profile);
2004  }
2005  }
2006 
2007  inline void onPublishInterfaces(const char* portname,
2008  RTC::ConnectorProfile& profile,
2009  ReturnCode_t ret)
2010  {
2011  if (m_portconnListeners != NULL)
2012  {
2014  portconnret_[ON_PUBLISH_INTERFACES].notify(portname,
2015  profile, ret);
2016  }
2017  }
2018 
2019  inline void onConnectNextport(const char* portname,
2020  RTC::ConnectorProfile& profile,
2021  ReturnCode_t ret)
2022  {
2023  if (m_portconnListeners != NULL)
2024  {
2026  portconnret_[ON_CONNECT_NEXTPORT].notify(portname,
2027  profile, ret);
2028  }
2029  }
2030 
2031  inline void onSubscribeInterfaces(const char* portname,
2032  RTC::ConnectorProfile& profile,
2033  ReturnCode_t ret)
2034  {
2035  if (m_portconnListeners != NULL)
2036  {
2038  portconnret_[ON_SUBSCRIBE_INTERFACES].notify(portname,
2039  profile, ret);
2040  }
2041  }
2042 
2043  inline void onConnected(const char* portname,
2044  RTC::ConnectorProfile& profile,
2045  ReturnCode_t ret)
2046  {
2047  if (m_portconnListeners != NULL)
2048  {
2050  portconnret_[ON_CONNECTED].notify(portname, profile, ret);
2051  }
2052  }
2053 
2054  inline void onDisconnectNextport(const char* portname,
2055  RTC::ConnectorProfile& profile,
2056  ReturnCode_t ret)
2057  {
2058  if (m_portconnListeners != NULL)
2059  {
2061  portconnret_[ON_DISCONNECT_NEXT].notify(portname, profile, ret);
2062  }
2063  }
2064 
2065  inline void onDisconnected(const char* portname,
2066  RTC::ConnectorProfile& profile,
2067  ReturnCode_t ret)
2068  {
2069  if (m_portconnListeners != NULL)
2070  {
2072  portconnret_[ON_DISCONNECTED].notify(portname, profile, ret);
2073  }
2074  }
2075 
2076  protected:
2084  mutable Logger rtclog;
2092  PortProfile m_profile;
2093 
2101  RTC::PortService_var m_objref;
2112 
2120  std::string m_ownerInstanceName;
2121 
2130 
2203 
2219 
2234 
2236 
2237  //============================================================
2238  // Functor
2239  //============================================================
2248  {
2249  find_conn_id(const char* id) : m_id(id) {};
2250  bool operator()(const ConnectorProfile& cprof)
2251  {
2252  return m_id == std::string(cprof.connector_id);
2253  }
2254  std::string m_id;
2255  }; // struct find_conn_id
2256 
2265  {
2266  find_port_ref(PortService_ptr port_ref) : m_port(port_ref) {};
2267  bool operator()(PortService_ptr port_ref)
2268  {
2269  return m_port->_is_equivalent(port_ref);
2270  }
2271  PortService_ptr m_port;
2272  }; // struct find_port_ref
2273 
2282  {
2283  find_interface(const char* name, PortInterfacePolarity pol)
2284  : m_name(name), m_pol(pol)
2285  {}
2286 
2287  bool operator()(const PortInterfaceProfile& prof)
2288  {
2289  CORBA::String_var name(CORBA::string_dup(prof.instance_name));
2290  return ((m_name == (const char *)name) && (m_pol == prof.polarity));
2291  }
2292  std::string m_name;
2293  PortInterfacePolarity m_pol;
2294  }; // struct find_interface
2295  }; // class PortBase
2296 }; // namespace RTC
2297 
2298 #ifdef WIN32
2299 #pragma warning( default : 4290 )
2300 #endif
2301 
2302 #endif // RTC_PORTBASE_H
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
Definition: PortConnectListener.h:163
void setOnSubscribeInterfaces(ConnectionCallback *on_subscribe)
Setting callback called on publish interfaces.
bool isEmptyId(const ConnectorProfile &connector_profile) const
Check whether connector_id of ConnectorProfile is empty.
void onPublishInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2007
coil::Guard< coil::Mutex > Guard
Definition: PortBase.h:2111
const PortProfile & getProfile() const
Get the PortProfile of the Port.
virtual ReturnCode_t connect(ConnectorProfile &connector_profile)
[CORBA interface] Connect the Port
RT-Component.
PortInterfacePolarity m_pol
Definition: PortBase.h:2293
void setUUID(ConnectorProfile &connector_profile) const
Generate and set the UUID to the ConnectorProfile.
Definition: PortConnectListener.h:51
std::string m_ownerInstanceName
Instance name.
Definition: PortBase.h:2120
virtual ReturnCode_t _publishInterfaces(void)
Publish interface information.
bool operator()(const PortInterfaceProfile &prof)
Definition: PortBase.h:2287
DirectPortBase class.
ConnectionCallback * m_onSubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2159
virtual DirectPortBase * getDirectPort()
ConnectionCallback * m_onUnsubscribeInterfaces
Callback functor objects.
Definition: PortBase.h:2188
void onNotifyConnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1978
Mutex class.
Definition: Mutex.h:40
RT component logger class.
void onDisconnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2054
find_interface(const char *name, PortInterfacePolarity pol)
Definition: PortBase.h:2283
coil::Mutex m_connectorsMutex
Definition: PortBase.h:2110
coil::Mutex m_profile_mutex
Mutex of PortProfile.
Definition: PortBase.h:2109
Functor to find a ConnectorProfile named id.
Definition: PortBase.h:2247
bool appendStringValue(SDOPackage::NVList &nv, const char *name, const char *value)
Append the specified string to element of NVList.
Definition: PortConnectListener.h:168
void setOnPublishInterfaces(ConnectionCallback *on_publish)
Setting callback called on publish interfaces.
ConnectionCallback * m_onDisconnected
Callback functor objects.
Definition: PortBase.h:2202
void onDisconnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2065
void setOnConnected(ConnectionCallback *on_connected)
Setting callback called on connection established.
virtual void setConnectionLimit(int limit_value)
Set the maximum number of connections.
Definition: PortConnectListener.h:52
void setOwner(RTObject_ptr owner)
Set the owner RTObject of the Port.
bool deleteInterface(const char *name, PortInterfacePolarity pol)
Delete the interface registration from the PortInterfaceProfile.
virtual ReturnCode_t connectNext(ConnectorProfile &connector_profile)
Call notify_connect() of the next Port.
bool operator()(PortService_ptr port_ref)
Definition: PortBase.h:2267
const char * getName() const
Get the name of this Port.
void setOnUnsubscribeInterfaces(ConnectionCallback *on_subscribe)
Setting callback called on unsubscribe interfaces.
virtual PortProfile * get_port_profile()
[CORBA interface] Get the PortProfile of the Port
Functor to find interface from name and polarity.
Definition: PortBase.h:2281
virtual ReturnCode_t notify_disconnect(const char *connector_id)
[CORBA interface] Notify the Ports disconnection
std::string m_id
Definition: PortBase.h:2254
virtual void activateInterfaces()=0
Activate all Port interfaces.
virtual ConnectorProfileList * get_connector_profiles()
[CORBA interface] Get the ConnectorProfileList of the Port
void setPortRef(PortService_ptr port_ref)
Set the object reference of this Port.
virtual ReturnCode_t disconnect_all()
[CORBA interface] Disconnect the All Ports
bool operator()(const ConnectorProfile &cprof)
Definition: PortBase.h:2250
Definition: DirectPortBase.h:46
void onConnectNextport(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2019
coil::Properties m_properties
Properties.
Definition: PortBase.h:1933
void appendProperty(const char *key, const char *value)
Append NameValue data to PortProfile&#39;s properties.
Definition: PortBase.h:1922
ConnectionCallback * m_onConnected
Callback functor objects.
Definition: PortBase.h:2174
Port base class.
Definition: PortBase.h:135
void setPortConnectListenerHolder(PortConnectListeners *portconnListeners)
Setting PortConnectListener holder.
PortService_ptr m_port
Definition: PortBase.h:2271
PortConnectListeners class.
Definition: PortConnectListener.h:487
std::string m_name
Definition: PortBase.h:2292
Guard template class.
NameValue and NVList utility functions.
Definition: PortConnectListener.h:166
PortBase(const char *name="")
Constructor.
CORBA sequence utility template functions.
void onConnected(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2043
DirectPortBase * m_directport
Definition: PortBase.h:2235
CORBA::Long findConnProfileIndex(const char *id)
Find ConnectorProfile with id.
Logger class.
Definition: SystemLogger.h:99
void setOnConnectionLost(ConnectionCallback *on_connection_lost)
Setting callback called on connection lost.
virtual void unsubscribeInterfaces(const ConnectorProfile &connector_profile)=0
Disconnect interface connection.
void updateConnectors()
Disconnect ports that doesn&#39;t exist.
virtual ReturnCode_t disconnect(const char *connector_id)
[CORBA interface] Disconnect the Port
Definition: PortConnectListener.h:165
PortProfile m_profile
PortProfile of the Port.
Definition: PortBase.h:2092
Class represents a set of properties.
Definition: Properties.h:101
virtual ReturnCode_t publishInterfaces(ConnectorProfile &connector_profile)=0
Publish interface information.
PortService_ptr getPortRef() const
Get the object reference of this Port.
PortConnectListeners * m_portconnListeners
PortConnectListener holder.
Definition: PortBase.h:2233
bool eraseConnectorProfile(const char *id)
Delete the ConnectorProfile.
find_conn_id(const char *id)
Definition: PortBase.h:2249
Definition: PortConnectListener.h:167
virtual ReturnCode_t notify_connect(ConnectorProfile &connector_profile)
[CORBA interface] Notify the Ports connection
void onUnsubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1997
bool isExistingConnId(const char *id)
Check whether the given id exists in stored ConnectorProfiles.
virtual void deactivateInterfaces()=0
Deactivate all Port interfaces.
virtual ReturnCode_t disconnectNext(ConnectorProfile &connector_profile)
Call notify_disconnect() of the next Port.
bool appendInterface(const char *name, const char *type_name, PortInterfacePolarity pol)
Append an interface to the PortInterfaceProfile.
virtual ReturnCode_t subscribeInterfaces(const ConnectorProfile &connector_profile)=0
Publish interface information.
void onSubscribeInterfaces(const char *portname, RTC::ConnectorProfile &profile, ReturnCode_t ret)
Definition: PortBase.h:2031
void onNotifyDisconnect(const char *portname, RTC::ConnectorProfile &profile)
Definition: PortBase.h:1988
int m_connectionLimit
The maximum number of connections.
Definition: PortBase.h:2129
RTC::PortService_var m_objref
Object Reference of the Port.
Definition: PortBase.h:2101
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
Definition: CORBA_SeqUtil.h:175
Definition: PortConnectListener.h:53
RTComponent header.
ConnectionCallback * m_onConnectionLost
Callback functor objects.
Definition: PortBase.h:2218
find_port_ref(PortService_ptr port_ref)
Definition: PortBase.h:2266
const std::string getUUID() const
Generate the UUID.
void addProperty(const char *key, ValueType value)
Add NameValue data to PortProfile&#39;s properties.
Definition: PortBase.h:1891
ConnectionCallback * m_onPublishInterfaces
Callback functor objects.
Definition: PortBase.h:2145
bool checkPorts(::RTC::PortServiceList &ports)
Existence of ports.
const PortProfile & getPortProfile() const
Get the PortProfile of the Port.
Functor to find the object reference that is identical port_ref.
Definition: PortBase.h:2264
void updateConnectorProfile(const ConnectorProfile &connector_profile)
Append or update the ConnectorProfile list.
Callback functor abstract for connect/notify_connect() funcs.
Definition: PortCallback.h:55
void setName(const char *name)
Set the name of this Port.
ConnectorProfile findConnProfile(const char *id)
Find ConnectorProfile with id.
port&#39;s internal action listener classes
Definition: PortConnectListener.h:164
virtual ConnectorProfile * get_connector_profile(const char *connector_id)
[CORBA interface] Get the ConnectorProfile
void setOnDisconnected(ConnectionCallback *on_disconnected)
Setting callback called on disconnected.
Logger rtclog
Logger stream.
Definition: PortBase.h:2084
virtual ~PortBase(void)
Destructor.