DDS  ver. 3.4
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  LOG(MiscCommon::info) << "Successfull handshake";
37 
38  this->m_isHandshakeOK = true;
39 
40  // The following commands starts message processing which might be queued before.
41  this->template pushMsg<cmdUNKNOWN>(_sender.m_ID);
42 
43  // notify all subscribers about the event
44  this->dispatchHandlers(EChannelEvents::OnHandshakeOK, _sender);
45  });
46 
47  // Register handshake ERROR callback
48  this->template registerHandler<cmdREPLY_HANDSHAKE_ERR>(
49  [this](const SSenderInfo& _sender,
51  LOG(MiscCommon::info) << "Handshake failed with the following error: " << _attachment->m_sMsg;
52 
53  this->m_isHandshakeOK = false;
55 
56  // notify all subscribers about the event
57  this->dispatchHandlers(EChannelEvents::OnHandshakeFailed, _sender);
58 
59  // close connection
60  this->stop();
61  });
62  }
63 
65  {
66  }
67 
68  public:
69  void reconnect()
70  {
71  connect(m_endpoint_iterator);
72  }
73 
74  void connect(boost::asio::ip::tcp::resolver::iterator _endpoint_iterator)
75  {
76  m_endpoint_iterator = _endpoint_iterator;
77  boost::asio::async_connect(
78  this->socket(),
79  _endpoint_iterator,
80  [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) {
81  if (!ec)
82  {
83  LOG(MiscCommon::debug) << "Client channel connected.";
84  // notify all subscribers about the event
85  this->dispatchHandlers(EChannelEvents::OnConnected, SSenderInfo());
86 
87  // start the communication channel
88  this->start();
89 
90  // Prepare a hand shake message
91  SVersionCmd cmd;
92  cmd.m_channelType = this->m_channelType;
93  cmd.m_sSID = this->m_sessionID;
94  cmd.m_version = DDS_PROTOCOL_VERSION;
95  this->template pushMsg<cmdHANDSHAKE>(cmd, this->m_protocolHeaderID);
96  }
97  else
98  {
99  LOG(MiscCommon::error) << "Failed to connect to remote end.";
100  // notify all subscribers about the event
101  this->dispatchHandlers(EChannelEvents::OnFailedToConnect, SSenderInfo());
102  }
103  });
104  }
105 
106  private:
107  boost::asio::ip::tcp::resolver::iterator m_endpoint_iterator;
108  };
109  } // namespace protocol_api
110 } // namespace dds
111 
112 #endif
bool m_isHandshakeOK
Definition: BaseChannelImpl.h:988
std::string m_sSID
Definition: VersionCmd.h:25
Definition: BaseEventHandlersImpl.h:48
EChannelType
Definition: ProtocolDef.h:15
EChannelType m_channelType
Definition: BaseChannelImpl.h:989
std::shared_ptr< SEmptyCmd > ptr_t
Definition: CommandAttachmentImpl.h:64
std::string m_sessionID
Definition: BaseChannelImpl.h:990
#define LOG(severity)
Definition: Logger.h:56
Definition: VersionCmd.h:17
void start()
Definition: BaseChannelImpl.h:307
Definition: AgentConnectionManager.h:13
uint16_t m_version
Session ID.
Definition: VersionCmd.h:26
Definition: BaseChannelImpl.h:224
Definition: BaseChannelImpl.h:40
Definition: def.h:152
Definition: def.h:149
void reconnect()
Definition: ClientChannelImpl.h:69
uint16_t m_channelType
Definition: VersionCmd.h:27
boost::asio::ip::tcp::socket & socket()
Definition: BaseChannelImpl.h:326
uint64_t m_ID
Definition: BaseEventHandlersImpl.h:50
void connect(boost::asio::ip::tcp::resolver::iterator _endpoint_iterator)
Definition: ClientChannelImpl.h:74
Definition: def.h:150
void stop()
Definition: BaseChannelImpl.h:318
uint64_t m_protocolHeaderID
Definition: BaseChannelImpl.h:991