Satellite-Gateway
GatewayClientHandler.cpp
Go to the documentation of this file.
1 
24 #include "GatewayClientHandler.h"
25 #include "GatewayClient.h"
28 #include "GatewayFrameData.h"
29 #include <assert.h>
30 
31 GatewayClientHandler::GatewayClientHandler(boost::asio::io_service& a_IOService, std::shared_ptr<ConfigServerHandlerCollection> a_ConfigServerHandlerCollection,
32  std::shared_ptr<HdlcdClientHandlerCollection> a_HdlcdClientHandlerCollection, uint16_t a_ReferenceNbr,
33  std::string a_RemoteAddress, uint16_t a_RemotePortNbr):
34  m_IOService(a_IOService), m_ConfigServerHandlerCollection(a_ConfigServerHandlerCollection),
35  m_HdlcdClientHandlerCollection(a_HdlcdClientHandlerCollection), m_GatewayClientConnectGuard(a_ConfigServerHandlerCollection, a_ReferenceNbr),
36  m_RemoteAddress(a_RemoteAddress), m_RemotePortNbr(a_RemotePortNbr), m_ReferenceNbr(a_ReferenceNbr), m_Resolver(a_IOService), m_ConnectionRetryTimer(a_IOService) {
37  // Checks
38  assert(m_ConfigServerHandlerCollection);
39  assert(m_HdlcdClientHandlerCollection);
40 
41  // Trigger activity
42  ResolveDestination();
43 }
44 
46  // Drop all shared pointers
47  if (m_GatewayClient) {
48  m_GatewayClient->Close();
49  m_GatewayClient.reset();
50  } // if
51 
52  m_ConfigServerHandlerCollection.reset();
53  m_HdlcdClientHandlerCollection.reset();
54 }
55 
56 void GatewayClientHandler::SendPacket(uint16_t a_SerialPortNbr, const std::vector<unsigned char> &a_Buffer) {
57  if (m_GatewayClient) {
58  m_GatewayClient->SendPacket(a_SerialPortNbr, a_Buffer);
59  } // if
60 }
61 
62 void GatewayClientHandler::ResolveDestination() {
63  std::stringstream l_OStream;
64  l_OStream << m_RemotePortNbr;
65  m_Resolver.async_resolve({m_RemoteAddress, l_OStream.str()}, [this](const boost::system::error_code& a_ErrorCode, boost::asio::ip::tcp::resolver::iterator a_EndpointIterator) {
66  if (a_ErrorCode) {
67  std::cout << "Failed to resolve host name: " << m_RemoteAddress << std::endl;
68  m_ConnectionRetryTimer.expires_from_now(boost::posix_time::seconds(2));
69  m_ConnectionRetryTimer.async_wait([this](const boost::system::error_code& a_ErrorCode) {
70  if (!a_ErrorCode) {
71  // Reestablish the connection to the HDLC Daemon
72  ResolveDestination();
73  } // if
74  }); // async_wait
75  } else {
76  // Start the HDLCd access client
77  m_GatewayClient = std::make_shared<GatewayClient>(m_IOService, a_EndpointIterator, m_ConfigServerHandlerCollection, m_HdlcdClientHandlerCollection, m_GatewayClientConnectGuard, m_ReferenceNbr);
78 
79  // On any error, restart after a short delay
80  m_GatewayClient->SetOnClosedCallback([this]() {
81  m_ConnectionRetryTimer.expires_from_now(boost::posix_time::seconds(2));
82  m_ConnectionRetryTimer.async_wait([this](const boost::system::error_code& a_ErrorCode) {
83  if (!a_ErrorCode) {
84  // Reestablish the connection to the HDLC Daemon
85  ResolveDestination();
86  } // if
87  }); // async_wait
88  }); // SetOnClosedCallback
89  } // else
90  }); // async_resolve
91 }
GatewayClientHandler(boost::asio::io_service &a_IOService, std::shared_ptr< ConfigServerHandlerCollection > a_ConfigServerHandlerCollection, std::shared_ptr< HdlcdClientHandlerCollection > a_HdlcdClientHandlerCollection, uint16_t a_ReferenceNbr, std::string a_RemoteAddress, uint16_t a_RemotePortNbr)
void SendPacket(uint16_t a_SerialPortNbr, const std::vector< unsigned char > &a_Payload)