Satellite-Gateway
HdlcdClientHandlerCollection.cpp
Go to the documentation of this file.
1 
27 #include "HdlcdClientHandler.h"
28 #include "HdlcdClientErrorCodes.h"
29 #include <assert.h>
30 
31 HdlcdClientHandlerCollection::HdlcdClientHandlerCollection(boost::asio::io_service& a_IOService): m_IOService(a_IOService) {
32 }
33 
34 void HdlcdClientHandlerCollection::Initialize(std::shared_ptr<ConfigServerHandlerCollection> a_ConfigServerHandlerCollection,
35  std::shared_ptr<GatewayClientHandlerCollection> a_GatewayClientHandlerCollection) {
36  // Checks
37  assert(a_ConfigServerHandlerCollection);
38  assert(a_GatewayClientHandlerCollection);
39  m_ConfigServerHandlerCollection = a_ConfigServerHandlerCollection;
40  m_GatewayClientHandlerCollection = a_GatewayClientHandlerCollection;
41 }
42 
44  // Drop all shared pointers
45  m_ConfigServerHandlerCollection.reset();
46  m_GatewayClientHandlerCollection.reset();
47  for (auto l_HandlerIterator = m_HdlcdClientHandlerMap.begin(); l_HandlerIterator != m_HdlcdClientHandlerMap.end(); ++l_HandlerIterator) {
48  auto& l_Handler = l_HandlerIterator->second;
49  l_Handler->Close();
50  l_Handler.reset();
51  } // for
52 }
53 
55  // Silently remove all previous hdlcd client entites
56  for (auto l_HandlerIterator = m_HdlcdClientHandlerMap.begin(); l_HandlerIterator != m_HdlcdClientHandlerMap.end(); ++l_HandlerIterator) {
57  auto& l_Handler = l_HandlerIterator->second;
58  l_Handler->Close();
59  l_Handler.reset();
60  } // for
61 
62  // Finally clean the map to drop all entries
63  m_HdlcdClientHandlerMap.clear();
64 }
65 
66 void HdlcdClientHandlerCollection::CreateHdlcdClient(const std::string &a_RemoteAddress, uint16_t a_TcpPortNbr, uint16_t a_SerialPortNbr) {
67  // First check whether there is already a client handler for the specified serial port
68  if (m_HdlcdClientHandlerMap.find(a_SerialPortNbr) == m_HdlcdClientHandlerMap.end()) {
69  // The element did not exist yet, create it
70  auto l_NewHdlcdClientHandler = std::make_shared<HdlcdClientHandler>(m_IOService, m_ConfigServerHandlerCollection, m_GatewayClientHandlerCollection,
71  a_RemoteAddress, a_TcpPortNbr, a_SerialPortNbr);
72  m_HdlcdClientHandlerMap[a_SerialPortNbr] = l_NewHdlcdClientHandler;
73  } else {
74  // The client handler already existed
75  m_ConfigServerHandlerCollection->HdlcdClientError(a_SerialPortNbr, HDLCD_CLIENT_ERROR_ALREADY_EXISTED);
76  } // else
77 
78  // Deliver the "created" message in each case
79  m_ConfigServerHandlerCollection->HdlcdClientCreated(a_SerialPortNbr);
80 }
81 
82 void HdlcdClientHandlerCollection::DestroyHdlcdClient(uint16_t a_SerialPortNbr) {
83  // First check whether there is a client handler for the specified serial port
84  auto l_HandlerIterator = m_HdlcdClientHandlerMap.find(a_SerialPortNbr);
85  if (l_HandlerIterator == m_HdlcdClientHandlerMap.end()) {
86  // The element did not exist
87  m_ConfigServerHandlerCollection->HdlcdClientError(a_SerialPortNbr, HDLCD_CLIENT_ERROR_NOT_EXISTED);
88  } else {
89  // The client handler exists
90  l_HandlerIterator->second->Close();
91  l_HandlerIterator->second.reset();
92  m_HdlcdClientHandlerMap.erase(l_HandlerIterator);
93  } // else
94 
95  // Deliver the "destroyed" message in each case
96  m_ConfigServerHandlerCollection->HdlcdClientDestroyed(a_SerialPortNbr);
97 }
98 
99 void HdlcdClientHandlerCollection::SuspendHdlcdClient(uint16_t a_SerialPortNbr) {
100  // First check whether there is a client handler for the specified serial port
101  auto l_HandlerIterator = m_HdlcdClientHandlerMap.find(a_SerialPortNbr);
102  if (l_HandlerIterator == m_HdlcdClientHandlerMap.end()) {
103  // The element did not exist
104  m_ConfigServerHandlerCollection->HdlcdClientError(a_SerialPortNbr, HDLCD_CLIENT_ERROR_NOT_EXISTED);
105  } else {
106  // The client handler exists
107  l_HandlerIterator->second->Suspend();
108  } // else
109 }
110 
111 void HdlcdClientHandlerCollection::ResumeHdlcdClient(uint16_t a_SerialPortNbr) {
112  // First check whether there is a client handler for the specified serial port
113  auto l_HandlerIterator = m_HdlcdClientHandlerMap.find(a_SerialPortNbr);
114  if (l_HandlerIterator == m_HdlcdClientHandlerMap.end()) {
115  // The element did not exist
116  m_ConfigServerHandlerCollection->HdlcdClientError(a_SerialPortNbr, HDLCD_CLIENT_ERROR_NOT_EXISTED);
117  } else {
118  // The client handler exists
119  l_HandlerIterator->second->Resume();
120  } // else
121 }
122 
123 void HdlcdClientHandlerCollection::SendPacket(uint16_t a_SerialPortNbr, const std::vector<unsigned char> &a_Payload) {
124  // First check whether there is a client handler for the specified serial port
125  auto l_HandlerIterator = m_HdlcdClientHandlerMap.find(a_SerialPortNbr);
126  if (l_HandlerIterator != m_HdlcdClientHandlerMap.end()) {
127  // The client handler exists
128  l_HandlerIterator->second->SendPacket(a_Payload);
129  } else {
130  // Do not emit an error message... may be useful, but may also clutter the logs
131  } // else
132 }
void Initialize(std::shared_ptr< ConfigServerHandlerCollection > a_ConfigServerHandlerCollection, std::shared_ptr< GatewayClientHandlerCollection > a_GatewayClientHandlerCollection)
void ResumeHdlcdClient(uint16_t a_SerialPortNbr)
void DestroyHdlcdClient(uint16_t a_SerialPortNbr)
void CreateHdlcdClient(const std::string &a_RemoteAddress, uint16_t a_TcpPortNbr, uint16_t a_SerialPortNbr)
void SuspendHdlcdClient(uint16_t a_SerialPortNbr)
HdlcdClientHandlerCollection(boost::asio::io_service &a_IOService)
void SendPacket(uint16_t a_SerialPortNbr, const std::vector< unsigned char > &a_Payload)