Satellite-Gateway
ConfigServerHandlerCollection.cpp
Go to the documentation of this file.
1 
27 #include "ConfigServerHandler.h"
28 #include <iostream>
29 #include <assert.h>
30 using boost::asio::ip::tcp;
31 
32 ConfigServerHandlerCollection::ConfigServerHandlerCollection(boost::asio::io_service& a_IOService, uint16_t a_TcpPortNbr): m_IOService(a_IOService), m_TcpAcceptor(a_IOService, tcp::endpoint(tcp::v4(), a_TcpPortNbr)), m_TcpSocket(a_IOService) {
33 }
34 
35 void ConfigServerHandlerCollection::Initialize(std::shared_ptr<GatewayClientHandlerCollection> a_GatewayClientHandlerCollection,
36  std::shared_ptr<HdlcdClientHandlerCollection> a_HdlcdClientHandlerCollection) {
37  assert(a_GatewayClientHandlerCollection);
38  assert(a_HdlcdClientHandlerCollection);
39  m_GatewayClientHandlerCollection = a_GatewayClientHandlerCollection;
40  m_HdlcdClientHandlerCollection = a_HdlcdClientHandlerCollection;
41 
42  // Start accepting incoming TCP connections
43  DoAccept();
44 }
45 
47  // Stop accepting subsequent TCP connections
48  m_TcpAcceptor.close();
49 
50  // Drop all shared pointers
51  if (m_ConfigServerHandler) {
52  m_ConfigServerHandler->Close();
53  m_ConfigServerHandler.reset();
54  } // if
55 
56  m_GatewayClientHandlerCollection.reset();
57  m_HdlcdClientHandlerCollection.reset();
58 }
59 
60 void ConfigServerHandlerCollection::RegisterConfigServerHandler(std::shared_ptr<ConfigServerHandler> a_ConfigServerHandler) {
61  // This "collection" does only allow one single incoming control connection. If one already existed, it is stopped and discarded
62  assert(a_ConfigServerHandler);
63  if (m_ConfigServerHandler) {
64  m_ConfigServerHandler->Close();
65  } // if
66 
67  m_ConfigServerHandler = a_ConfigServerHandler;
68 }
69 
70 void ConfigServerHandlerCollection::DeregisterConfigServerHandler(std::shared_ptr<ConfigServerHandler> a_ConfigServerHandler) {
71  // One control connection terminated. If this is the current one, drop it.
72  assert(a_ConfigServerHandler);
73  if (m_ConfigServerHandler == a_ConfigServerHandler) {
74  // Closing is not required anymore (already in DTOR)
75  m_ConfigServerHandler.reset();
76  } // if
77 }
78 
79 void ConfigServerHandlerCollection::DoAccept() {
80  m_TcpAcceptor.async_accept(m_TcpSocket, [this](boost::system::error_code a_ErrorCode) {
81  if (!a_ErrorCode) {
82  // Create config server handler and start it. It registers and deregisters itself
83  auto l_ConfigServerHandler = std::make_shared<ConfigServerHandler>(m_IOService, m_TcpSocket, m_GatewayClientHandlerCollection, m_HdlcdClientHandlerCollection);
84  l_ConfigServerHandler->Start(shared_from_this());
85  } // if
86 
87  // Wait for subsequent TCP connections
88  DoAccept();
89  });
90 }
91 
93  // There is only one config server handler entity
94  std::cerr << "Gateway client entity was created, RefNbr=" << a_ReferenceNbr << std::endl;
95  if (m_ConfigServerHandler) {
96  m_ConfigServerHandler->GatewayClientCreated(a_ReferenceNbr);
97  } // if
98 }
99 
101  // There is only one config server handler entity
102  std::cerr << "Gateway client entity was destroyed, RefNbr=" << a_ReferenceNbr << std::endl;
103  if (m_ConfigServerHandler) {
104  m_ConfigServerHandler->GatewayClientDestroyed(a_ReferenceNbr);
105  } // if
106 }
107 
109  // There is only one config server handler entity
110  std::cerr << "Gateway client entity was connected, RefNbr=" << a_ReferenceNbr << std::endl;
111  if (m_ConfigServerHandler) {
112  m_ConfigServerHandler->GatewayClientConnected(a_ReferenceNbr);
113  } // if
114 }
115 
117  // There is only one config server handler entity
118  std::cerr << "Gateway client entity was disconnected, RefNbr=" << a_ReferenceNbr << std::endl;
119  if (m_ConfigServerHandler) {
120  m_ConfigServerHandler->GatewayClientDisconnected(a_ReferenceNbr);
121  } // if
122 }
123 
125  // There is only one config server handler entity
126  std::cerr << "Gateway client entity error, RefNbr=" << a_ReferenceNbr << ", ErrorCode=" << std::hex << uint8_t(a_ErrorCode) << std::dec << std::endl;
127  if (m_ConfigServerHandler) {
128  m_ConfigServerHandler->GatewayClientError(a_ReferenceNbr, a_ErrorCode);
129  } // if
130 }
131 
133  // There is only one config server handler entity
134  std::cerr << "HDLCd client entity was created, PortNbr=" << a_SerialPortNbr << std::endl;
135  if (m_ConfigServerHandler) {
136  m_ConfigServerHandler->HdlcdClientCreated(a_SerialPortNbr);
137  } // if
138 }
139 
141  // There is only one config server handler entity
142  std::cerr << "HDLCd client entity was destroyed, PortNbr=" << a_SerialPortNbr << std::endl;
143  if (m_ConfigServerHandler) {
144  m_ConfigServerHandler->HdlcdClientDestroyed(a_SerialPortNbr);
145  } // if
146 }
147 
149  // There is only one config server handler entity
150  std::cerr << "HDLCd client entity was connected, PortNbr=" << a_SerialPortNbr << std::endl;
151  if (m_ConfigServerHandler) {
152  m_ConfigServerHandler->HdlcdClientConnected(a_SerialPortNbr);
153  } // if
154 }
155 
157  // There is only one config server handler entity
158  std::cerr << "HDLCd client entity was disconnected, PortNbr=" << a_SerialPortNbr << std::endl;
159  if (m_ConfigServerHandler) {
160  m_ConfigServerHandler->HdlcdClientDisconnected(a_SerialPortNbr);
161  } // if
162 }
163 
164 void ConfigServerHandlerCollection::HdlcdClientNewStatus(uint16_t a_SerialPortNbr, bool a_bIsResumed, bool a_bIsAlive) {
165  // There is only one config server handler entity
166  std::cerr << "HDLCd client entity status update, PortNbr=" << a_SerialPortNbr << ", IsResumed=" << a_bIsResumed << ", IsAlive=" << a_bIsAlive << std::endl;
167  if (m_ConfigServerHandler) {
168  m_ConfigServerHandler->HdlcdClientNewStatus(a_SerialPortNbr, a_bIsResumed, a_bIsAlive);
169  } // if
170 }
171 
172 void ConfigServerHandlerCollection::HdlcdClientError(uint16_t a_SerialPortNbr, E_HDLCD_CLIENT_ERROR a_ErrorCode) {
173  // There is only one config server handler entity
174  std::cerr << "HDLCd client entity error, PortNbr=" << a_SerialPortNbr << ", ErrorCode=" << std::hex << uint8_t(a_ErrorCode) << std::dec << std::endl;
175  if (m_ConfigServerHandler) {
176  m_ConfigServerHandler->HdlcdClientError(a_SerialPortNbr, a_ErrorCode);
177  } // if
178 }
void GatewayClientCreated(uint16_t a_ReferenceNbr)
void HdlcdClientConnected(uint16_t a_SerialPortNbr)
void DeregisterConfigServerHandler(std::shared_ptr< ConfigServerHandler > a_ConfigServerHandler)
E_GATEWAY_CLIENT_ERROR
void HdlcdClientError(uint16_t a_SerialPortNbr, E_HDLCD_CLIENT_ERROR a_ErrorCode)
void GatewayClientConnected(uint16_t a_ReferenceNbr)
void HdlcdClientDisconnected(uint16_t a_SerialPortNbr)
E_HDLCD_CLIENT_ERROR
void HdlcdClientDestroyed(uint16_t a_SerialPortNbr)
ConfigServerHandlerCollection(boost::asio::io_service &a_IOService, uint16_t a_TcpPortNbr)
void HdlcdClientCreated(uint16_t a_SerialPortNbr)
void RegisterConfigServerHandler(std::shared_ptr< ConfigServerHandler > a_ConfigServerHandler)
void GatewayClientDestroyed(uint16_t a_ReferenceNbr)
void GatewayClientError(uint16_t a_ReferenceNbr, E_GATEWAY_CLIENT_ERROR a_ErrorCode)
void HdlcdClientNewStatus(uint16_t a_SerialPortNbr, bool a_bIsResumed, bool a_bIsAlive)
void Initialize(std::shared_ptr< GatewayClientHandlerCollection > a_GatewayClientHandlerCollection, std::shared_ptr< HdlcdClientHandlerCollection > a_HdlcdClientHandlerCollection)
void GatewayClientDisconnected(uint16_t a_ReferenceNbr)