DDS  ver. 3.6
ClientChannelImpl.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 
6 #ifndef DDS_ClientChannelImpl_h
7 #define DDS_ClientChannelImpl_h
8 
9 // DDS
10 #include "BaseChannelImpl.h"
11 #include "Version.h"
12 // STD
13 #include <functional>
14 
15 namespace dds
16 {
17  namespace protocol_api
18  {
19  template <class T>
20  class CClientChannelImpl : public CBaseChannelImpl<T>
21  {
22 
23  typedef std::function<void(T*)> handlerEventFunction_t;
24 
25  protected:
26  CClientChannelImpl<T>(boost::asio::io_context& _service,
27  EChannelType _channelType,
28  uint64_t _protocolHeaderID)
29  : CBaseChannelImpl<T>(_service, _protocolHeaderID)
30  {
31  this->m_channelType = _channelType;
32  // Register handshake OK callback
33  this->template registerHandler<cmdREPLY_HANDSHAKE_OK>(
34  [this](const SSenderInfo& _sender,
36  {
37  LOG(dds::misc::info) << "Successfull handshake";
38 
39  this->m_isHandshakeOK = true;
40 
41  // The following commands starts message processing which might be queued before.
42  this->template pushMsg<cmdUNKNOWN>(_sender.m_ID);
43 
44  // notify all subscribers about the event
45  this->dispatchHandlers(EChannelEvents::OnHandshakeOK, _sender);
46  });
47 
48  // Register handshake ERROR callback
49  this->template registerHandler<cmdREPLY_HANDSHAKE_ERR>(
50  [this](const SSenderInfo& _sender,
52  {
53  LOG(dds::misc::info) << "Handshake failed with the following error: " << _attachment->m_sMsg;
54 
55  this->m_isHandshakeOK = false;
57 
58  // notify all subscribers about the event
59  this->dispatchHandlers(EChannelEvents::OnHandshakeFailed, _sender);
60 
61  // close connection
62  this->stop();
63  });
64  }
65 
67  {
68  }
69 
70  public:
71  void reconnect()
72  {
73  connect(m_endpoint_iterator);
74  }
75 
76  void connect(boost::asio::ip::tcp::resolver::iterator _endpoint_iterator)
77  {
78  m_endpoint_iterator = _endpoint_iterator;
79  boost::asio::async_connect(
80  this->socket(),
81  _endpoint_iterator,
82  [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator)
83  {
84  if (!ec)
85  {
86  LOG(dds::misc::debug) << "Client channel connected.";
87  // notify all subscribers about the event
88  this->dispatchHandlers(EChannelEvents::OnConnected, SSenderInfo());
89 
90  // start the communication channel
91  this->start();
92 
93  // Prepare a hand shake message
94  SVersionCmd cmd;
95  cmd.m_channelType = this->m_channelType;
96  cmd.m_sSID = this->m_sessionID;
97  cmd.m_version = DDS_PROTOCOL_VERSION;
98  this->template pushMsg<cmdHANDSHAKE>(cmd, this->m_protocolHeaderID);
99  }
100  else
101  {
102  LOG(dds::misc::error) << "Failed to connect to remote end.";
103  // notify all subscribers about the event
104  this->dispatchHandlers(EChannelEvents::OnFailedToConnect, SSenderInfo());
105  }
106  });
107  }
108 
109  private:
110  boost::asio::ip::tcp::resolver::iterator m_endpoint_iterator;
111  };
112  } // namespace protocol_api
113 } // namespace dds
114 
115 #endif
bool m_isHandshakeOK
Definition: BaseChannelImpl.h:978
std::string m_sSID
Definition: VersionCmd.h:25
Definition: BaseEventHandlersImpl.h:48
EChannelType
Definition: ProtocolDef.h:15
EChannelType m_channelType
Definition: BaseChannelImpl.h:979
std::shared_ptr< SEmptyCmd > ptr_t
Definition: CommandAttachmentImpl.h:64
std::string m_sessionID
Definition: BaseChannelImpl.h:980
#define LOG(severity)
Definition: Logger.h:34
Definition: VersionCmd.h:17
void start()
Definition: BaseChannelImpl.h:300
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
uint16_t m_version
Session ID.
Definition: VersionCmd.h:26
Definition: BaseChannelImpl.h:219
Definition: def.h:147
Definition: BaseChannelImpl.h:35
void reconnect()
Definition: ClientChannelImpl.h:71
Definition: def.h:146
uint16_t m_channelType
Definition: VersionCmd.h:27
boost::asio::ip::tcp::socket & socket()
Definition: BaseChannelImpl.h:319
uint64_t m_ID
Definition: BaseEventHandlersImpl.h:50
void connect(boost::asio::ip::tcp::resolver::iterator _endpoint_iterator)
Definition: ClientChannelImpl.h:76
Definition: def.h:149
void stop()
Definition: BaseChannelImpl.h:311
uint64_t m_protocolHeaderID
Definition: BaseChannelImpl.h:981