diff --git a/lib/App/common/deviceNode2D.cpp b/lib/App/common/deviceNode2D.cpp index c855d193..99ab4b46 100644 --- a/lib/App/common/deviceNode2D.cpp +++ b/lib/App/common/deviceNode2D.cpp @@ -3310,6 +3310,20 @@ bool device2DSensor::validateCommunicationRessource() the link was down; a fast reconnect was not seen here as CS_DISCONNECTED, so the sensor was only polled in idle mode (getState2), believed to be in detector mode, and stayed "offline" until a restart. */ + if (m_pPort != NULL && m_oDataTransport != m_pPort) + { + /* + The transport object was deleted (e.g. the node factory rebuilt the Ethernet2UART gateway node, + or its settings changed): the pointer is stale. Request the resource again - the new object - + and run the connection sequence on it. Before, the sensor stayed bound to the deleted gateway + and was "unavailable" until la_mojo was restarted. + */ + LOG("Transport object destroyed - binding to the new resource"); + m_pPort = NULL; + updateDataStatus(ARGUS_DS_DISCONNECTED); + setWM(LRF_WM_CONNECTION); + return false; + }; if (m_pPort != NULL && m_pPort->getConnectEpoch() != m_nPortEpoch) { LOG("Transport reconnected - re-initialising the sensor"); diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index 38c333d0..2d48d9da 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -270,11 +270,14 @@ bool deviceETH2UART::validate(void) #endif m_bValid = connectSOL(); }; - if (m_bValid) - { - m_bValid = deviceNode::validate(); - } - return m_bValid; + /* + The node is valid while its DB record is unchanged - the link state is not part of it: the node + reconnects itself (above, with the back-off) and the users see it through isValid() / + getConnectionStatus() / getConnectEpoch(). Returning false on a lost link made the node factory + delete this object and create a new one: the sensors bound to it kept the deleted object and + were never restored. + */ + return deviceNode::validate(); } #include diff --git a/lib/components/base/basicTransport.cpp b/lib/components/base/basicTransport.cpp index 501bb724..e6a2df56 100644 --- a/lib/components/base/basicTransport.cpp +++ b/lib/components/base/basicTransport.cpp @@ -16,8 +16,13 @@ basicTransport::basicTransport() : basicTransport::~basicTransport() { - // ToDo: Cleanup parsers vector + // Tell the bound parsers: they kept the pointer to this object after it was deleted (a sensor + // behind an Ethernet2UART gateway that was rebuilt kept talking to the old gateway object) m_mQueueProtectorMutex.lock(); + for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) + { + (*index)->transportDestroyed(this); + }; m_vParsers.clear(); m_mQueueProtectorMutex.unlock(); } diff --git a/lib/data/protocol/protocol.cpp b/lib/data/protocol/protocol.cpp index 2503e64e..dbbe875d 100644 --- a/lib/data/protocol/protocol.cpp +++ b/lib/data/protocol/protocol.cpp @@ -15,6 +15,7 @@ protocolParser::protocolParser() : m_pLog = basicLogCollection::getInstance(); m_eConnectionStatus = CS_UNKNOWN; m_oDataTransport = NULL; + m_bTransportRecipient = false; m_pParserBuffer = (U8 *) malloc(PROTOCOL_PARSER_BUFFER_SIZE); if (m_pParserBuffer) { @@ -40,6 +41,14 @@ protocolParser::~protocolParser() LOG("Destructor Begin"); #endif m_mBufferProtectorMutex.unlock(); + // Leave the transport's recipient list: it would call setConnectionStatus() / deliver data to + // a deleted parser (only when added by setTransport(): other paths set the pointer only) + if (m_bTransportRecipient && m_oDataTransport != NULL) + { + m_oDataTransport->removeRecipient(this); + }; + m_oDataTransport = NULL; + m_bTransportRecipient = false; /* #ifdef PROTOCOL_PARSER_IN_THREAD if (m_bParserThreadStatus > TH_ARMED ) { @@ -137,17 +146,19 @@ bool protocolParser::setTransport(basicTransport * Transport, bool CAE) }; } else - { // Danger -> If transport Object is deleted the pointer is wrong! - if (m_oDataTransport != NULL) + { // A deleted transport calls transportDestroyed(): the pointer is NULL then + if (m_oDataTransport != NULL && m_bTransportRecipient) { m_oDataTransport->removeRecipient(this); }; }; - m_oDataTransport = Transport; + m_oDataTransport = Transport; + m_bTransportRecipient = false; if (Transport != NULL) { setConnectionStatus(CS_CONNECTED); - return m_oDataTransport->addRecipient(this); + m_bTransportRecipient = m_oDataTransport->addRecipient(this); + return m_bTransportRecipient; } else { @@ -156,6 +167,16 @@ bool protocolParser::setTransport(basicTransport * Transport, bool CAE) return true; } +void protocolParser::transportDestroyed(basicTransport * Transport) +{ + if (Transport != NULL && m_oDataTransport == Transport) + { + m_oDataTransport = NULL; + m_bTransportRecipient = false; + setConnectionStatus(CS_DISCONNECTED); + }; +} + basicTransport * protocolParser::getCurrentTransport(void) { LOG("Get Transport Req"); diff --git a/lib/data/protocol/protocol.h b/lib/data/protocol/protocol.h index b7956d4f..938887dd 100644 --- a/lib/data/protocol/protocol.h +++ b/lib/data/protocol/protocol.h @@ -53,6 +53,7 @@ class protocolParser protected: basicTransport * m_oDataTransport; + bool m_bTransportRecipient; // added to m_oDataTransport's recipients by setTransport() CONNECTION_STATUS m_eConnectionStatus; U8 m_bStatus; packagePoll m_vPackagePool; @@ -98,6 +99,9 @@ class protocolParser CONNECTION_STATUS getConnectionStatus(void); bool setTransport(basicTransport * ptrTransport, bool check_if_allready = true); basicTransport * getCurrentTransport(void); + // Called by the transport's destructor: the pointer is dropped (it would dangle) and the + // connection is reported as lost, so the owner can bind to a new transport object + void transportDestroyed(basicTransport * ptrTransport); virtual bool validate() {