HDLC-Daemon
HdlcdServerHandlerCollection.cpp
Go to the documentation of this file.
1 
23 #include "HdlcdServerHandler.h"
24 #include <assert.h>
25 using boost::asio::ip::tcp;
26 
27 HdlcdServerHandlerCollection::HdlcdServerHandlerCollection(boost::asio::io_service& a_IOService, std::shared_ptr<SerialPortHandlerCollection> a_SerialPortHandlerCollection, uint16_t a_TcpPortNbr):
28  m_IOService(a_IOService), m_SerialPortHandlerCollection(a_SerialPortHandlerCollection), m_TcpAcceptor(a_IOService, tcp::endpoint(tcp::v4(), a_TcpPortNbr)), m_TcpSocket(a_IOService) {
29  // Checks
30  assert(m_SerialPortHandlerCollection);
31 
32  // Trigger activity
33  DoAccept();
34 }
35 
37  // Stop accepting subsequent TCP connections
38  m_TcpAcceptor.close();
39 
40  // Release all client handler objects. They deregister themselves.
41  while (!m_HdlcdServerHandlerList.empty()) {
42  (*m_HdlcdServerHandlerList.begin())->Stop();
43  } // while
44 
45  // Drop all shared pointers
46  assert(m_HdlcdServerHandlerList.empty());
47  m_SerialPortHandlerCollection.reset();
48 }
49 
50 void HdlcdServerHandlerCollection::RegisterHdlcdServerHandler(std::shared_ptr<HdlcdServerHandler> a_HdlcdServerHandler) {
51  m_HdlcdServerHandlerList.emplace_back(std::move(a_HdlcdServerHandler));
52 }
53 
54 void HdlcdServerHandlerCollection::DeregisterHdlcdServerHandler(std::shared_ptr<HdlcdServerHandler> a_HdlcdServerHandler) {
55  m_HdlcdServerHandlerList.remove(a_HdlcdServerHandler);
56 }
57 
58 void HdlcdServerHandlerCollection::DoAccept() {
59  m_TcpAcceptor.async_accept(m_TcpSocket, [this](boost::system::error_code a_ErrorCode) {
60  if (!a_ErrorCode) {
61  // Create a HDLCd server handler object and start it. It registers itself to the HDLCd server handler collection
62  auto l_HdlcdServerHandler = std::make_shared<HdlcdServerHandler>(m_IOService, shared_from_this(), m_TcpSocket);
63  l_HdlcdServerHandler->Start(m_SerialPortHandlerCollection);
64  } // if
65 
66  // Wait for subsequent TCP connections
67  DoAccept();
68  }); // async_accept
69 }
HdlcdServerHandlerCollection(boost::asio::io_service &a_IOService, std::shared_ptr< SerialPortHandlerCollection > a_SerialPortHandlerCollection, uint16_t a_TcpPortNbr)
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...
void RegisterHdlcdServerHandler(std::shared_ptr< HdlcdServerHandler > a_HdlcdServerHandler)
void DeregisterHdlcdServerHandler(std::shared_ptr< HdlcdServerHandler > a_HdlcdServerHandler)
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...