DDS  ver. 1.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 #include "BaseChannelImpl.h"
10 #include <functional>
11 
12 namespace dds
13 {
14  namespace protocol_api
15  {
16  template <class T>
17  class CClientChannelImpl : public CBaseChannelImpl<T>
18  {
19 
20  typedef std::function<void(T*)> handlerEventFunction_t;
21 
22  protected:
23  CClientChannelImpl<T>(boost::asio::io_service& _service, EChannelType _channelType)
24  : CBaseChannelImpl<T>(_service)
25  {
26  this->m_channelType = _channelType;
27  // Register handshake OK callback
28  std::function<bool(SCommandAttachmentImpl<cmdREPLY_HANDSHAKE_OK>::ptr_t _attachment,
29  CClientChannelImpl * _channel)>
30  funcHandshakeOK = [this](SCommandAttachmentImpl<cmdREPLY_HANDSHAKE_OK>::ptr_t _attachment,
31  CClientChannelImpl* _channel) -> bool {
32  LOG(MiscCommon::info) << "Successfull handshake";
33 
34  this->m_isHandshakeOK = true;
35 
36  // The following commands starts message processing which might be queued before.
37  this->template pushMsg<cmdUNKNOWN>();
38 
39  // notify all subscribers about the event
41 
42  return true;
43  };
44  this->template registerMessageHandler<cmdREPLY_HANDSHAKE_OK>(funcHandshakeOK);
45 
46  // Register handshake ERROR callback
47  std::function<bool(SCommandAttachmentImpl<cmdREPLY_HANDSHAKE_ERR>::ptr_t _attachment,
48  CClientChannelImpl * _channel)>
49  funcHandshakeERR = [this](SCommandAttachmentImpl<cmdREPLY_HANDSHAKE_ERR>::ptr_t _attachment,
50  CClientChannelImpl* _channel) -> bool {
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
58 
59  return true;
60  };
61  this->template registerMessageHandler<cmdREPLY_HANDSHAKE_ERR>(funcHandshakeERR);
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
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  this->template pushMsg<cmdHANDSHAKE>(cmd);
94  }
95  else
96  {
97  LOG(MiscCommon::error) << "Failed to connect to remote end.";
98  // notify all subscribers about the event
100  }
101  });
102  }
103 
104  private:
105  boost::asio::ip::tcp::resolver::iterator m_endpoint_iterator;
106  };
107  }
108 }
109 
110 #endif
bool m_isHandshakeOK
Definition: BaseChannelImpl.h:898
Definition: ChannelEventsImpl.h:19
EChannelType
Definition: BaseChannelImpl.h:144
Definition: ChannelEventsImpl.h:22
EChannelType m_channelType
Definition: BaseChannelImpl.h:899
std::shared_ptr< SEmptyCmd > ptr_t
Definition: CommandAttachmentImpl.h:65
#define LOG(severity)
Definition: Logger.h:54
Definition: VersionCmd.h:17
void start()
Definition: BaseChannelImpl.h:246
Definition: ChannelEventsImpl.h:21
Definition: dds-agent/src/AgentConnectionManager.h:16
Definition: BaseChannelImpl.h:180
void onEvent(EChannelEvents _type)
Definition: ChannelEventsImpl.h:50
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:26
boost::asio::ip::tcp::socket & socket()
Definition: BaseChannelImpl.h:265
void connect(boost::asio::ip::tcp::resolver::iterator _endpoint_iterator)
Definition: ClientChannelImpl.h:74
Definition: def.h:150
Definition: ChannelEventsImpl.h:18