diff --git a/src/Transports/CommManager/Router.hpp b/src/Transports/CommManager/Router.hpp index f58070dc80..949c109ff2 100644 --- a/src/Transports/CommManager/Router.hpp +++ b/src/Transports/CommManager/Router.hpp @@ -51,30 +51,29 @@ namespace Transports { public: - Router(Task* task) - { - m_parent = task; - m_medium = 4; - m_gsm_entity_id = -1; - m_iridium_entity_id = -1; - m_reqid = 0; - c_wifi_timeout = 15; - } + explicit Router(Task* const task): + m_parent(task), + m_medium(4), + m_gsm_entity_id(-1), + m_iridium_entity_id(-1), + m_reqid(0), + c_wifi_timeout(15) + { } void - process(IMC::VehicleMedium* msg) + process(const IMC::VehicleMedium* msg) { m_medium = msg->medium; } void - process(IMC::RSSI* msg) + process(const IMC::RSSI* msg) { m_rssi_msg_map[msg->getSourceEntity()] = msg->value; } void - process(IMC::Announce* msg) + process(const IMC::Announce* msg) { std::vector list; String::split(msg->services, ";", list); @@ -172,19 +171,18 @@ namespace Transports void clearTimeouts() { - std::map::iterator it; double time = Time::Clock::getSinceEpoch(); - it = m_transmission_requests.begin(); + auto it = m_transmission_requests.begin(); while (it != m_transmission_requests.end()) { if (it->second->deadline <= time) { - m_parent->inf("Transmission Request %d is expired by %f seconds", it->second->req_id, it->second->deadline - time); + m_parent->inf("Transmission Request %d is expired by %f seconds", it->second->req_id, it->second->deadline - time); answer(it->second, "Transmission timed out.", IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); Memory::clear(it->second); - m_transmission_requests.erase(it++); + m_transmission_requests.erase(it); } else ++it; @@ -214,7 +212,7 @@ namespace Transports case IMC::TransmissionRequest::DMODE_INLINEMSG: { tx.type = IMC::AcousticRequest::TYPE_MSG; - tx.msg.set(msg->msg_data.get()->clone()); + tx.msg.set(*msg->msg_data.get()); break; } @@ -291,10 +289,9 @@ namespace Transports case IMC::TransmissionRequest::DMODE_INLINEMSG: { IMC::ImcIridiumMessage m; - const IMC::Message * inlinemsg = msg->msg_data.get(); m.destination = 0xFFFF; m.source = m_parent->getSystemId(); - m.msg = inlinemsg->clone(); + m.msg = msg->msg_data.get()->clone(); uint8_t buffer[65535]; int len = m.serialize(buffer); tx.data.assign(buffer, buffer + len); @@ -578,9 +575,9 @@ namespace Transports { uint16_t newId = createInternalId(); m_transmission_requests[newId] = msg->clone(); - IMC::TransmissionRequest* msg2 = msg->clone(); - msg2->req_id = newId; - answerTCPStatus(msg2, "Didn't find TCP server on destination host", + IMC::TransmissionRequest msg2 = *msg; + msg2.req_id = newId; + answerTCPStatus(&msg2, "Didn't find TCP server on destination host", IMC::TCPStatus::TCPSTAT_HOST_UNKNOWN); return; } @@ -592,7 +589,7 @@ namespace Transports { case IMC::TransmissionRequest::DMODE_INLINEMSG: { - send.msg_data.set(msg->msg_data.get()->clone()); + send.msg_data.set(*msg->msg_data.get()); break; } default: @@ -747,18 +744,20 @@ namespace Transports m_iridium_entity_id = id; } - std::map* + std::map& getList() { - return &m_transmission_requests; + return m_transmission_requests; } ~Router() { + for (auto& pair : m_transmission_requests) + Memory::clear(pair.second); } private: - Task* m_parent; + Task* const m_parent; uint8_t m_medium; int m_gsm_entity_id; @@ -809,9 +808,7 @@ namespace Transports if (system.empty()) return false; - std::map::iterator it; - - it = m_wifi_map.find(system); + auto it = m_wifi_map.find(system); if (it == m_wifi_map.end()) return false; @@ -828,8 +825,7 @@ namespace Transports if (system.empty()) return false; - std::map::iterator it; - it = m_gsm_announce_map.find(system); + auto it = m_gsm_announce_map.find(system); if (it != m_gsm_announce_map.end()) { std = it->second; @@ -855,8 +851,8 @@ namespace Transports { if (m_gsm_entity_id == -1) return false; - std::map::iterator it; - it = m_rssi_msg_map.find(m_gsm_entity_id); + + auto it = m_rssi_msg_map.find(m_gsm_entity_id); if (it == m_rssi_msg_map.end()) return false; if (it->second > 0) @@ -869,8 +865,8 @@ namespace Transports { if (m_iridium_entity_id == -1) return false; - std::map::iterator it; - it = m_rssi_msg_map.find(m_iridium_entity_id); + + auto it = m_rssi_msg_map.find(m_iridium_entity_id); if (it == m_rssi_msg_map.end()) return false; if (it->second >= 20) diff --git a/src/Transports/CommManager/Task.cpp b/src/Transports/CommManager/Task.cpp index 7e314d38dd..2d44b96161 100644 --- a/src/Transports/CommManager/Task.cpp +++ b/src/Transports/CommManager/Task.cpp @@ -151,6 +151,7 @@ namespace Transports Memory::clear(m_pstate); Memory::clear(m_vstate); Memory::clear(m_estate); + Memory::clear(m_vmedium); } //! Initialize resources and configure modem @@ -161,7 +162,7 @@ namespace Transports // verify modem local address value -> GSM std::map mapName; - std::vector addrs = m_ctx.config.options(m_args.gsm_addr_section); + auto addrs = m_ctx.config.options(m_args.gsm_addr_section); for (unsigned i = 0; i < addrs.size(); ++i) { std::string addr; @@ -262,7 +263,7 @@ namespace Transports return; Memory::replace(m_vmedium, new IMC::VehicleMedium(*msg)); - m_router.process(msg->clone()); + m_router.process(msg); } void @@ -279,13 +280,13 @@ namespace Transports { if (msg->getSource() != getSystemId()) return; - m_router.process(msg->clone()); + m_router.process(msg); } void consume(const IMC::Announce* msg) { - m_router.process(msg->clone()); + m_router.process(msg); } void @@ -297,12 +298,11 @@ namespace Transports if (msg->getDestinationEntity() != getEntityId()) return; - std::map* tr_list = - m_router.getList(); + auto tr_list = m_router.getList(); - if (tr_list->find(msg->req_id) != tr_list->end()) + if (tr_list.find(msg->req_id) != tr_list.end()) { - IMC::TransmissionRequest* req = tr_list->operator [](msg->req_id); + IMC::TransmissionRequest* req = tr_list[msg->req_id]; if (req->comm_mean != IMC::TransmissionRequest::CMEAN_SATELLITE) return; @@ -322,21 +322,20 @@ namespace Transports m_router.answer(req, "Message has been sent via Iridium.", IMC::TransmissionStatus::TSTAT_SENT); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; case (IMC::IridiumTxStatus::TXSTATUS_ERROR): m_router.answer(req, "Error while trying to transmit message.", IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); - + tr_list.erase(msg->req_id); break; case (IMC::IridiumTxStatus::TXSTATUS_EXPIRED): m_router.answer(req, "Timeout while trying to transmit message.", IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; default: break; @@ -349,11 +348,12 @@ namespace Transports { if (msg->getSource() != getSystemId()) return; - std::map* tr_list = m_router.getList(); - if (tr_list->find(msg->req_id) != tr_list->end()) + auto tr_list = m_router.getList(); + + if (tr_list.find(msg->req_id) != tr_list.end()) { - IMC::TransmissionRequest* req = tr_list->operator [](msg->req_id); + IMC::TransmissionRequest* req = tr_list[msg->req_id]; switch (msg->status) { case (IMC::SmsStatus::SMSSTAT_QUEUED): @@ -366,22 +366,22 @@ namespace Transports m_router.answer(req, "Message has been sent via GSM.", IMC::TransmissionStatus::TSTAT_SENT); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; case (IMC::SmsStatus::SMSSTAT_INPUT_FAILURE): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_INPUT_FAILURE); - tr_list->erase(msg->req_id); Memory::clear(req); + tr_list.erase(msg->req_id); break; case (IMC::SmsStatus::SMSSTAT_ERROR): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); + tr_list.erase(msg->req_id); break; default: @@ -398,13 +398,13 @@ namespace Transports { return; } - std::map* tr_list = - m_router.getList(); - if (tr_list->find(msg->req_id) != tr_list->end()) + auto tr_list = m_router.getList(); + + if (tr_list.find(msg->req_id) != tr_list.end()) { - IMC::TransmissionRequest* req = tr_list->operator [](msg->req_id); + IMC::TransmissionRequest* req = tr_list[msg->req_id]; if (req->comm_mean != IMC::TransmissionRequest::CMEAN_ACOUSTIC) return; @@ -424,7 +424,7 @@ namespace Transports m_router.answer(req, "Message has been sent via Acoustic.", IMC::TransmissionStatus::TSTAT_SENT); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; case (IMC::AcousticStatus::STATUS_RANGE_RECEIVED): @@ -432,30 +432,30 @@ namespace Transports IMC::TransmissionStatus::TSTAT_RANGE_RECEIVED, msg->range); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; case (IMC::AcousticStatus::STATUS_ERROR): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); + tr_list.erase(msg->req_id); break; case (IMC::AcousticStatus::STATUS_BUSY): m_router.answer(req, "Acoustic modem is busy.", IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); + tr_list.erase(msg->req_id); break; case (IMC::AcousticStatus::STATUS_INPUT_FAILURE): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_INPUT_FAILURE); - tr_list->erase(msg->req_id); Memory::clear(req); + tr_list.erase(msg->req_id); break; default: @@ -616,11 +616,12 @@ namespace Transports { if (msg->getSource() != getSystemId()) return; - - std::map* tr_list = m_router.getList(); - if (tr_list->find(msg->req_id) != tr_list->end()) + + auto tr_list = m_router.getList(); + + if (tr_list.find(msg->req_id) != tr_list.end()) { - IMC::TransmissionRequest* req = tr_list->operator [](msg->req_id); + IMC::TransmissionRequest* req = tr_list[msg->req_id]; switch (msg->status) { case (IMC::TCPStatus::TCPSTAT_QUEUED): @@ -632,30 +633,30 @@ namespace Transports m_router.answer(req, "Message has been sent via TCP.", IMC::TransmissionStatus::TSTAT_SENT); Memory::clear(req); - tr_list->erase(msg->req_id); + tr_list.erase(msg->req_id); break; case (IMC::TCPStatus::TCPSTAT_HOST_UNKNOWN): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); + tr_list.erase(msg->req_id); break; case (IMC::TCPStatus::TCPSTAT_CANT_CONNECT): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_TEMPORARY_FAILURE); - tr_list->erase(msg->req_id); m_retransmission_list.push_back(req->clone()); Memory::clear(req); + tr_list.erase(msg->req_id); break; case (IMC::TCPStatus::TCPSTAT_INPUT_FAILURE): m_router.answer(req, msg->info, IMC::TransmissionStatus::TSTAT_INPUT_FAILURE); - tr_list->erase(msg->req_id); Memory::clear(req); + tr_list.erase(msg->req_id); break; default: @@ -879,6 +880,7 @@ namespace Transports while (!m_retransmission_list.empty()) { consume(m_retransmission_list.front()); + delete m_retransmission_list.front(); m_retransmission_list.pop_front(); } m_retransmission_timer.reset();