| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
 | 
					
						
							|  |  |  | // written by Christian Daniel                                                   //
 | 
					
						
							|  |  |  | // Copyright (C) 2015, 2019, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com>   //
 | 
					
						
							|  |  |  | // Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai>                      //
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 14:32:15 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02: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 02:19:59 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-09-02 01:51:49 +02:00
										 |  |  | #include <QMutexLocker>
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include "util/messagequeue.h"
 | 
					
						
							|  |  |  | #include "util/message.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MessageQueue::MessageQueue(QObject* parent) : | 
					
						
							|  |  |  | 	QObject(parent), | 
					
						
							|  |  |  | 	m_queue() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-25 23:43:50 +01:00
										 |  |  | 	setObjectName("MessageQueue"); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MessageQueue::~MessageQueue() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 	Message* message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ((message = pop()) != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-30 02:19:59 +02:00
										 |  |  | 		qDebug() << "MessageQueue::~MessageQueue: message: " << message->getIdentifier() << " was still in queue"; | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 		delete message; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | void MessageQueue::push(Message* message, bool emitSignal) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 	if (message) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_lock.lock(); | 
					
						
							|  |  |  | 		m_queue.append(message); | 
					
						
							|  |  |  | 		m_lock.unlock(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (emitSignal) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		emit messageEnqueued(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | Message* MessageQueue::pop() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-02 01:51:49 +02:00
										 |  |  | 	QMutexLocker locker(&m_lock); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 	if (m_queue.isEmpty()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return m_queue.takeFirst(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | int MessageQueue::size() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-02 01:51:49 +02:00
										 |  |  | 	QMutexLocker locker(&m_lock); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return m_queue.size(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-08-12 09:03:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void MessageQueue::clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-02 01:51:49 +02:00
										 |  |  | 	QMutexLocker locker(&m_lock); | 
					
						
							| 
									
										
										
										
											2019-07-23 03:22:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while (!m_queue.isEmpty()) { | 
					
						
							|  |  |  |         delete m_queue.takeFirst(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-12 09:03:02 +02:00
										 |  |  | } |