2018-08-31 22:37:23 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2019-01-22 18:44:13 -05:00
|
|
|
// Copyright (C) 2018-2019 Edouard Griffiths, F4EXB //
|
2018-08-31 22:37:23 -04: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 00:39:30 -04:00
|
|
|
// (at your option) any later version. //
|
2018-08-31 22:37:23 -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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
#include "remotesourcethread.h"
|
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
#include <channel/remotedatablock.h>
|
|
|
|
#include <channel/remotedataqueue.h>
|
2018-08-31 22:37:23 -04:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <QUdpSocket>
|
2019-04-15 04:42:39 -04:00
|
|
|
#include "cm256cc/cm256.h"
|
2018-08-31 22:37:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(RemoteSourceThread::MsgStartStop, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(RemoteSourceThread::MsgDataBind, Message)
|
2018-08-31 22:37:23 -04:00
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
RemoteSourceThread::RemoteSourceThread(RemoteDataQueue *dataQueue, QObject* parent) :
|
2018-08-31 22:37:23 -04:00
|
|
|
QThread(parent),
|
|
|
|
m_running(false),
|
|
|
|
m_dataQueue(dataQueue),
|
|
|
|
m_address(QHostAddress::LocalHost),
|
|
|
|
m_socket(0)
|
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
std::fill(m_dataBlocks, m_dataBlocks+4, (RemoteDataBlock *) 0);
|
2018-08-31 22:37:23 -04:00
|
|
|
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
RemoteSourceThread::~RemoteSourceThread()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::~RemoteSourceThread");
|
2018-08-31 22:37:23 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::startStop(bool start)
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
|
|
|
MsgStartStop *msg = MsgStartStop::create(start);
|
|
|
|
m_inputMessageQueue.push(msg);
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::dataBind(const QString& address, uint16_t port)
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
|
|
|
MsgDataBind *msg = MsgDataBind::create(address, port);
|
|
|
|
m_inputMessageQueue.push(msg);
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::startWork()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::startWork");
|
2018-08-31 22:37:23 -04:00
|
|
|
m_startWaitMutex.lock();
|
|
|
|
m_socket = new QUdpSocket(this);
|
|
|
|
start();
|
|
|
|
while(!m_running)
|
|
|
|
m_startWaiter.wait(&m_startWaitMutex, 100);
|
|
|
|
m_startWaitMutex.unlock();
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::stopWork()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::stopWork");
|
2018-08-31 22:37:23 -04:00
|
|
|
delete m_socket;
|
|
|
|
m_socket = 0;
|
|
|
|
m_running = false;
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::run()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::run: begin");
|
2018-08-31 22:37:23 -04:00
|
|
|
m_running = true;
|
|
|
|
m_startWaiter.wakeAll();
|
|
|
|
|
|
|
|
while (m_running)
|
|
|
|
{
|
|
|
|
sleep(1); // Do nothing as everything is in the data handler (dequeuer)
|
|
|
|
}
|
|
|
|
|
|
|
|
m_running = false;
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::run: end");
|
2018-08-31 22:37:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::handleInputMessages()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
|
|
|
Message* message;
|
|
|
|
|
|
|
|
while ((message = m_inputMessageQueue.pop()) != 0)
|
|
|
|
{
|
|
|
|
if (MsgStartStop::match(*message))
|
|
|
|
{
|
|
|
|
MsgStartStop* notif = (MsgStartStop*) message;
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::handleInputMessages: MsgStartStop: %s", notif->getStartStop() ? "start" : "stop");
|
2018-08-31 22:37:23 -04:00
|
|
|
|
|
|
|
if (notif->getStartStop()) {
|
|
|
|
startWork();
|
|
|
|
} else {
|
|
|
|
stopWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete message;
|
|
|
|
}
|
|
|
|
else if (MsgDataBind::match(*message))
|
|
|
|
{
|
|
|
|
MsgDataBind* notif = (MsgDataBind*) message;
|
2019-01-22 18:44:13 -05:00
|
|
|
qDebug("RemoteSourceThread::handleInputMessages: MsgDataBind: %s:%d", qPrintable(notif->getAddress().toString()), notif->getPort());
|
2018-08-31 22:37:23 -04:00
|
|
|
|
|
|
|
if (m_socket)
|
|
|
|
{
|
|
|
|
disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
|
|
|
m_socket->bind(notif->getAddress(), notif->getPort());
|
|
|
|
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:44:13 -05:00
|
|
|
void RemoteSourceThread::readPendingDatagrams()
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
RemoteSuperBlock superBlock;
|
2018-08-31 22:37:23 -04:00
|
|
|
qint64 size;
|
|
|
|
|
|
|
|
while (m_socket->hasPendingDatagrams())
|
|
|
|
{
|
|
|
|
QHostAddress sender;
|
|
|
|
quint16 senderPort = 0;
|
|
|
|
//qint64 pendingDataSize = m_socket->pendingDatagramSize();
|
2019-02-02 16:58:42 -05:00
|
|
|
size = m_socket->readDatagram((char *) &superBlock, (long long int) sizeof(RemoteSuperBlock), &sender, &senderPort);
|
2018-08-31 22:37:23 -04:00
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
if (size == sizeof(RemoteSuperBlock))
|
2018-08-31 22:37:23 -04:00
|
|
|
{
|
|
|
|
unsigned int dataBlockIndex = superBlock.m_header.m_frameIndex % m_nbDataBlocks;
|
|
|
|
|
|
|
|
// create the first block for this index
|
|
|
|
if (m_dataBlocks[dataBlockIndex] == 0) {
|
2019-02-02 16:58:42 -05:00
|
|
|
m_dataBlocks[dataBlockIndex] = new RemoteDataBlock();
|
2018-08-31 22:37:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_frameIndex < 0)
|
|
|
|
{
|
|
|
|
// initialize virgin block with the frame index
|
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_frameIndex = superBlock.m_header.m_frameIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if the frame index is not the same for the same slot it means we are starting a new frame
|
|
|
|
uint32_t frameIndex = m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_frameIndex;
|
|
|
|
|
|
|
|
if (superBlock.m_header.m_frameIndex != frameIndex)
|
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
//qDebug("RemoteSourceThread::readPendingDatagrams: push frame %u", frameIndex);
|
2018-08-31 22:37:23 -04:00
|
|
|
m_dataQueue->push(m_dataBlocks[dataBlockIndex]);
|
2019-02-02 16:58:42 -05:00
|
|
|
m_dataBlocks[dataBlockIndex] = new RemoteDataBlock();
|
2018-08-31 22:37:23 -04:00
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_frameIndex = superBlock.m_header.m_frameIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dataBlocks[dataBlockIndex]->m_superBlocks[superBlock.m_header.m_blockIndex] = superBlock;
|
|
|
|
|
|
|
|
if (superBlock.m_header.m_blockIndex == 0) {
|
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_metaRetrieved = true;
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
if (superBlock.m_header.m_blockIndex < RemoteNbOrginalBlocks) {
|
2018-08-31 22:37:23 -04:00
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_originalCount++;
|
|
|
|
} else {
|
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_recoveryCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dataBlocks[dataBlockIndex]->m_rxControlBlock.m_blockCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-22 18:44:13 -05:00
|
|
|
qWarning("RemoteSourceThread::readPendingDatagrams: wrong super block size not processing");
|
2018-08-31 22:37:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|