2015-08-13 23:00:28 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2015 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// This program 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 as version 3 of the License, or //
|
2019-04-11 08:32:15 -04:00
|
|
|
// (at your option) any later version. //
|
2015-08-13 23:00:28 -04:00
|
|
|
// //
|
|
|
|
// This program 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-29 20:19:59 -04:00
|
|
|
#include <QDebug>
|
2015-09-01 19:51:49 -04:00
|
|
|
#include <QMutexLocker>
|
2014-05-18 11:52:39 -04:00
|
|
|
#include "util/messagequeue.h"
|
|
|
|
#include "util/message.h"
|
|
|
|
|
|
|
|
MessageQueue::MessageQueue(QObject* parent) :
|
|
|
|
QObject(parent),
|
2015-09-01 19:51:49 -04:00
|
|
|
m_lock(QMutex::Recursive),
|
2014-05-18 11:52:39 -04:00
|
|
|
m_queue()
|
|
|
|
{
|
2022-02-25 17:43:50 -05:00
|
|
|
setObjectName("MessageQueue");
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueue::~MessageQueue()
|
|
|
|
{
|
2015-08-13 23:00:28 -04:00
|
|
|
Message* message;
|
|
|
|
|
|
|
|
while ((message = pop()) != 0)
|
|
|
|
{
|
2015-08-29 20:19:59 -04:00
|
|
|
qDebug() << "MessageQueue::~MessageQueue: message: " << message->getIdentifier() << " was still in queue";
|
2015-08-13 23:00:28 -04:00
|
|
|
delete message;
|
|
|
|
}
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-13 23:00:28 -04:00
|
|
|
void MessageQueue::push(Message* message, bool emitSignal)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2015-08-13 23:00:28 -04:00
|
|
|
if (message)
|
|
|
|
{
|
|
|
|
m_lock.lock();
|
|
|
|
m_queue.append(message);
|
|
|
|
m_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitSignal)
|
|
|
|
{
|
|
|
|
emit messageEnqueued();
|
|
|
|
}
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-13 23:00:28 -04:00
|
|
|
Message* MessageQueue::pop()
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2015-09-01 19:51:49 -04:00
|
|
|
QMutexLocker locker(&m_lock);
|
2014-05-18 11:52:39 -04:00
|
|
|
|
2015-08-13 23:00:28 -04:00
|
|
|
if (m_queue.isEmpty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_queue.takeFirst();
|
|
|
|
}
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-13 23:00:28 -04:00
|
|
|
int MessageQueue::size()
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2015-09-01 19:51:49 -04:00
|
|
|
QMutexLocker locker(&m_lock);
|
2014-05-18 11:52:39 -04:00
|
|
|
|
|
|
|
return m_queue.size();
|
|
|
|
}
|
2015-08-12 03:03:02 -04:00
|
|
|
|
|
|
|
void MessageQueue::clear()
|
|
|
|
{
|
2015-09-01 19:51:49 -04:00
|
|
|
QMutexLocker locker(&m_lock);
|
2019-07-22 21:22:35 -04:00
|
|
|
|
|
|
|
while (!m_queue.isEmpty()) {
|
|
|
|
delete m_queue.takeFirst();
|
|
|
|
}
|
2015-08-12 03:03:02 -04:00
|
|
|
}
|