Commit 55bc70ff authored by Don Gagne's avatar Don Gagne

Fix LinkManager link removal

parent ac45d022
......@@ -138,6 +138,11 @@ public:
{
return getCurrentDataRate(outDataIndex, outDataWriteTimes, outDataWriteAmounts);
}
// These are left unimplemented in order to cause linker errors which indicate incorrect usage of
// connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
bool connect(void);
bool disconnect(void);
public slots:
......
......@@ -92,7 +92,7 @@ void LinkManager::add(LinkInterface* link)
if (!_links.contains(link))
{
connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(removeObj(QObject*)));
connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(_removeLink(QObject*)));
_links.append(link);
_dataMutex.unlock();
emit newLink(link);
......@@ -174,7 +174,7 @@ bool LinkManager::disconnectAll()
foreach (LinkInterface* link, _links)
{
Q_ASSERT(link);
if (!link->disconnect()) {
if (!disconnectLink(link)) {
allDisconnected = false;
}
}
......@@ -200,39 +200,33 @@ bool LinkManager::disconnectLink(LinkInterface* link)
return link->_disconnect();
}
void LinkManager::removeObj(QObject* link)
void LinkManager::_removeLink(QObject* obj)
{
// Be careful of the fact that by the time this signal makes it through the queue
// the link object has already been destructed.
removeLink((LinkInterface*)link);
}
bool LinkManager::removeLink(LinkInterface* link)
{
if(link)
Q_ASSERT(obj);
LinkInterface* link = static_cast<LinkInterface*>(obj);
_dataMutex.lock();
for (int i=0; i < _links.size(); i++)
{
_dataMutex.lock();
for (int i=0; i < _links.size(); i++)
{
if(link==_links.at(i))
{
_links.removeAt(i); //remove from link list
}
}
// Remove link from protocol map
QList<ProtocolInterface* > protocols = _protocolLinks.keys(link);
foreach (ProtocolInterface* proto, protocols)
if(link==_links.at(i))
{
_protocolLinks.remove(proto, link);
_links.removeAt(i); //remove from link list
}
_dataMutex.unlock();
// Emit removal of link
emit linkRemoved(link);
return true;
}
return false;
// Remove link from protocol map
QList<ProtocolInterface* > protocols = _protocolLinks.keys(link);
foreach (ProtocolInterface* proto, protocols)
{
_protocolLinks.remove(proto, link);
}
_dataMutex.unlock();
// Emit removal of link
emit linkRemoved(link);
}
/**
......
......@@ -88,13 +88,11 @@ public:
void setConnectionsAllowed(void) { _connectionsSuspended = false; }
public slots:
/// @brief Adds the link to the LinkManager. No need to remove a link you added. Just delete it and LinkManager will pick up on
/// that and remove the link from the list.
void add(LinkInterface* link);
void addProtocol(LinkInterface* link, ProtocolInterface* protocol);
void removeObj(QObject* obj);
bool removeLink(LinkInterface* link);
bool connectAll();
bool connectLink(LinkInterface* link);
......@@ -111,6 +109,8 @@ private:
static LinkManager* _instance;
void _removeLink(QObject* obj);
QList<LinkInterface*> _links;
QMultiMap<ProtocolInterface*,LinkInterface*> _protocolLinks;
QMutex _dataMutex;
......
This diff is collapsed.
/*=====================================================================
QGroundControl Open Source Ground Control Station
(c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
This file is part of the QGROUNDCONTROL project
QGROUNDCONTROL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QGROUNDCONTROL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
#ifndef UASUNITTEST_H
#define UASUNITTEST_H
#include <QObject>
#include "LinkManager.h"
/// @file
/// @brief LinkManager Unit Test
///
/// @author Don Gagne <don@thegagnes.com>
class LinkManagerTest : public QObject
{
Q_OBJECT
public:
LinkManagerTest(void);
private slots:
void initTestCase(void);
void init(void);
void cleanup(void);
void _setUAS_test(void);
void _minRCChannels_test(void);
void _fullCalibration_test(void);
};
DECLARE_TEST(LinkManagerTest)
#endif
......@@ -341,10 +341,7 @@ void UASUnitTest::signalUASLink_test()
QCOMPARE(links.count(), uas->getLinks()->count());
QCOMPARE(uas->getLinks()->count(), 2);
LinkInterface* ff99 = static_cast<LinkInterface*>(links.at(1));
LinkManager::instance()->removeLink(ff99);
delete link2;
QCOMPARE(LinkManager::instance()->getLinks().count(), 1);
QCOMPARE(uas->getLinks()->count(), 1);
......@@ -359,10 +356,8 @@ void UASUnitTest::signalUASLink_test()
//all the links in LinkManager must be deleted because LinkManager::instance
//is static.
LinkManager::instance()->removeLink(link3);
delete link3;
QCOMPARE(LinkManager::instance()->getLinks().count(), 1);
LinkManager::instance()->removeLink(link);
delete link;
QCOMPARE(LinkManager::instance()->getLinks().count(), 0);
......@@ -405,13 +400,9 @@ void UASUnitTest::signalIdUASLink_test()
QCOMPARE(c->getName(), QString("COM 19"));
QCOMPARE(d->getName(), QString("COM 20"));
LinkManager::instance()->removeLink(myLink4);
delete myLink4;
LinkManager::instance()->removeLink(myLink3);
delete myLink3;
LinkManager::instance()->removeLink(myLink2);
delete myLink2;
LinkManager::instance()->removeLink(myLink);
delete myLink;
QCOMPARE(LinkManager::instance()->getLinks().count(), 0);
......
......@@ -376,10 +376,7 @@ void CommConfigurationWindow::remove()
action=NULL;
if(link) {
LinkManager::instance()->removeLink(link); //remove link from LinkManager list
link->disconnect(); //disconnect port, and also calls terminate() to stop the thread
if (link->isRunning()) link->terminate(); // terminate() the serial thread just in case it is still running
link->wait(); // wait() until thread is stoped before deleting
LinkManager::instance()->disconnectLink(link); // disconnect connection
link->deleteLater();
}
link=NULL;
......
......@@ -330,7 +330,6 @@ bool QGCMAVLinkLogPlayer::loadLogFile(const QString& file)
if (logLink)
{
LinkManager::instance()->disconnectLink(logLink);
LinkManager::instance()->removeLink(logLink);
logLink->deleteLater();
}
logLink = new MAVLinkSimulationLink("");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment