2015-08-14 03:00:28 +00: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 12:32:15 +00:00
|
|
|
// (at your option) any later version. //
|
2015-08-14 03:00:28 +00: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-30 00:19:59 +00:00
|
|
|
#include <QDebug>
|
2015-09-01 23:51:49 +00:00
|
|
|
#include <QMutexLocker>
|
2014-05-18 15:52:39 +00:00
|
|
|
#include "util/messagequeue.h"
|
|
|
|
#include "util/message.h"
|
|
|
|
|
|
|
|
MessageQueue::MessageQueue(QObject* parent) :
|
|
|
|
QObject(parent),
|
2015-09-01 23:51:49 +00:00
|
|
|
m_lock(QMutex::Recursive),
|
2014-05-18 15:52:39 +00:00
|
|
|
m_queue()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueue::~MessageQueue()
|
|
|
|
{
|
2015-08-14 03:00:28 +00:00
|
|
|
Message* message;
|
|
|
|
|
|
|
|
while ((message = pop()) != 0)
|
|
|
|
{
|
2015-08-30 00:19:59 +00:00
|
|
|
qDebug() << "MessageQueue::~MessageQueue: message: " << message->getIdentifier() << " was still in queue";
|
2015-08-14 03:00:28 +00:00
|
|
|
delete message;
|
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 03:00:28 +00:00
|
|
|
void MessageQueue::push(Message* message, bool emitSignal)
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
2015-08-14 03:00:28 +00:00
|
|
|
if (message)
|
|
|
|
{
|
|
|
|
m_lock.lock();
|
|
|
|
m_queue.append(message);
|
|
|
|
m_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitSignal)
|
|
|
|
{
|
|
|
|
emit messageEnqueued();
|
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 03:00:28 +00:00
|
|
|
Message* MessageQueue::pop()
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
2015-09-01 23:51:49 +00:00
|
|
|
QMutexLocker locker(&m_lock);
|
2014-05-18 15:52:39 +00:00
|
|
|
|
2015-08-14 03:00:28 +00:00
|
|
|
if (m_queue.isEmpty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_queue.takeFirst();
|
|
|
|
}
|
2014-05-18 15:52:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 03:00:28 +00:00
|
|
|
int MessageQueue::size()
|
2014-05-18 15:52:39 +00:00
|
|
|
{
|
2015-09-01 23:51:49 +00:00
|
|
|
QMutexLocker locker(&m_lock);
|
2014-05-18 15:52:39 +00:00
|
|
|
|
|
|
|
return m_queue.size();
|
|
|
|
}
|
2015-08-12 07:03:02 +00:00
|
|
|
|
|
|
|
void MessageQueue::clear()
|
|
|
|
{
|
2015-09-01 23:51:49 +00:00
|
|
|
QMutexLocker locker(&m_lock);
|
2019-07-23 01:22:35 +00:00
|
|
|
|
|
|
|
while (!m_queue.isEmpty()) {
|
|
|
|
delete m_queue.takeFirst();
|
|
|
|
}
|
2015-08-12 07:03:02 +00:00
|
|
|
}
|