DDS  ver. 1.6
ServerChannelImpl.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 
6 #ifndef DDS_ServerChannelImpl_h
7 #define DDS_ServerChannelImpl_h
8 
9 #include "BaseChannelImpl.h"
10 
11 namespace dds
12 {
13  namespace protocol_api
14  {
15  template <class T>
16  class CServerChannelImpl : public CBaseChannelImpl<T>
17  {
18  protected:
19  CServerChannelImpl<T>(boost::asio::io_service& _service, const channelTypeVector_t _requiredChannelTypes)
20  : CBaseChannelImpl<T>(_service)
21  , m_requiredChannelTypes(_requiredChannelTypes)
22  {
23  // Register handshake callback
24  std::function<bool(SCommandAttachmentImpl<cmdHANDSHAKE>::ptr_t _attachment,
25  CServerChannelImpl * _channel)>
26  funcHandshake = [this](SCommandAttachmentImpl<cmdHANDSHAKE>::ptr_t _attachment,
27  CServerChannelImpl* _channel) -> bool {
28  // send shutdown if versions are incompatible
29  bool versionCompatible = m_requiredChannelTypes.empty();
30 
31  if (!versionCompatible)
32  {
33  for (const auto& v : m_requiredChannelTypes)
34  {
35  SVersionCmd versionCmd;
36  versionCmd.m_channelType = v;
37  versionCompatible = (*_attachment == versionCmd);
38  if (versionCompatible)
39  break;
40  }
41  }
42 
43  if (!versionCompatible)
44  {
45  this->m_isHandshakeOK = false;
47  // Send reply that the version of the protocol is incompatible
48  std::string msg("Incompatible protocol version of the client");
49  LOG(MiscCommon::warning) << msg << this->remoteEndIDString();
50  this->template pushMsg<cmdREPLY_HANDSHAKE_ERR>(SSimpleMsgCmd(msg, MiscCommon::fatal));
51 
52  // notify all subscribers about the event
54  }
55  else
56  {
57  this->m_isHandshakeOK = true;
58  this->m_channelType = static_cast<EChannelType>(_attachment->m_channelType);
59 
60  // The following commands starts message processing which might have been queued before.
61  this->template pushMsg<cmdUNKNOWN>();
62 
63  // everything is OK, we can work with this agent
64  LOG(MiscCommon::info) << "[" << this->socket().remote_endpoint().address().to_string()
65  << "] has successfully connected.";
66 
67  this->template pushMsg<cmdREPLY_HANDSHAKE_OK>();
68 
69  // notify all subscribers about the event
71  }
72  return true;
73  };
74  this->template registerMessageHandler<cmdHANDSHAKE>(funcHandshake);
75  }
76 
78  {
79  }
80 
81  private:
82  channelTypeVector_t m_requiredChannelTypes;
83  };
84  }
85 }
86 
87 #endif
bool m_isHandshakeOK
Definition: BaseChannelImpl.h:898
Definition: def.h:151
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: SimpleMsgCmd.h:16
Definition: VersionCmd.h:17
Definition: ChannelEventsImpl.h:21
Definition: dds-agent/src/AgentConnectionManager.h:16
std::string remoteEndIDString()
Definition: BaseChannelImpl.h:667
Definition: def.h:153
Definition: BaseChannelImpl.h:180
void onEvent(EChannelEvents _type)
Definition: ChannelEventsImpl.h:50
Definition: BaseChannelImpl.h:42
std::vector< EChannelType > channelTypeVector_t
Definition: BaseChannelImpl.h:151
uint16_t m_channelType
Definition: VersionCmd.h:26
boost::asio::ip::tcp::socket & socket()
Definition: BaseChannelImpl.h:265
Definition: def.h:150