HDLC-Daemon
SerialPortHandlerCollection.cpp
Go to the documentation of this file.
1 
23 #include "SerialPortHandler.h"
24 #include "HdlcdServerHandler.h"
25 
26 SerialPortHandlerCollection::SerialPortHandlerCollection(boost::asio::io_service& a_IOService): m_IOService(a_IOService) {
27 }
28 
30  // No need to cleanup, this class possesses only weak pointers
31 }
32 
33 std::shared_ptr<std::shared_ptr<SerialPortHandler>> SerialPortHandlerCollection::GetSerialPortHandler(const std::string &a_SerialPortName, std::shared_ptr<HdlcdServerHandler> a_HdlcdServerHandler) {
34  std::shared_ptr<std::shared_ptr<SerialPortHandler>> l_SerialPortHandler;
35  bool l_HasToBeStarted = false;
36  {
37  // This is some magic here to implement automatic cleanup
38  auto& l_SerialPortHandlerWeak(m_SerialPortHandlerMap[a_SerialPortName]);
39  l_SerialPortHandler = l_SerialPortHandlerWeak.lock();
40  if (!l_SerialPortHandler) {
41  auto l_NewSerialPortHandler = std::make_shared<SerialPortHandler>(a_SerialPortName, shared_from_this(), m_IOService);
42  std::shared_ptr<std::shared_ptr<SerialPortHandler>> l_NewSerialPortHandlerStopper(new std::shared_ptr<SerialPortHandler>(l_NewSerialPortHandler), [=](std::shared_ptr<SerialPortHandler>* todelete){ (*todelete)->Stop(); delete(todelete); });
43  l_SerialPortHandler = l_NewSerialPortHandlerStopper;
44  l_SerialPortHandlerWeak = l_SerialPortHandler;
45  l_HasToBeStarted = true;
46  } // if
47  }
48 
49  l_SerialPortHandler.get()->get()->AddHdlcdServerHandler(a_HdlcdServerHandler);
50  if (l_HasToBeStarted) {
51  if (l_SerialPortHandler.get()->get()->Start() == false) {
52  l_SerialPortHandler.reset();
53  } // if
54  } // if
55 
56  return l_SerialPortHandler;
57 }
58 
59 void SerialPortHandlerCollection::DeregisterSerialPortHandler(std::shared_ptr<SerialPortHandler> a_SerialPortHandler) {
60  assert(a_SerialPortHandler);
61  for (auto it = m_SerialPortHandlerMap.begin(); it != m_SerialPortHandlerMap.end(); ++it) {
62  if (auto cph = it->second.lock()) {
63  if (*(cph.get()) == a_SerialPortHandler) {
64  m_SerialPortHandlerMap.erase(it);
65  break;
66  } // if
67  } // if
68  } // for
69 }
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...
void DeregisterSerialPortHandler(std::shared_ptr< SerialPortHandler > a_SerialPortHandler)
SerialPortHandlerCollection(boost::asio::io_service &a_IOService)
std::shared_ptr< std::shared_ptr< SerialPortHandler > > GetSerialPortHandler(const std::string &a_SerialPortName, std::shared_ptr< HdlcdServerHandler > a_HdlcdServerHandler)
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...