From 28b4c31deeb71f8953cd6326a001afddb7d9754f Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Tue, 9 Jun 2015 14:30:23 +0000 Subject: [PATCH] Various WSPR fixes Make WSPRnet.org spot uploads tolerant of network issues, spots still get discarded for any period that has problems but now uploading resumes on the next period. Ensure that decoded text starts with correct font by not using the base class append method directly. Fixed a major memory leak in the WSPRNet class which was not freeing processed request reply objects. Added some helpful debug prints in WSPRnet.org spot processing. Also tidied up a number of class implementations that were not including he MOC generated code. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@5560 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- FrequencyList.cpp | 2 ++ Modes.cpp | 2 ++ PollingTransceiver.cpp | 2 ++ TransceiverBase.cpp | 2 ++ displaytext.cpp | 8 ++--- displaytext.h | 3 +- echograph.cpp | 3 +- mainwindow.cpp | 17 +++++---- mainwindow.h | 2 +- messageaveraging.cpp | 3 +- wsprnet.cpp | 82 ++++++++++++++++++++++++++++-------------- wsprnet.h | 27 +++++++++----- 12 files changed, 101 insertions(+), 52 deletions(-) diff --git a/FrequencyList.cpp b/FrequencyList.cpp index d7043457b..d76264f9c 100644 --- a/FrequencyList.cpp +++ b/FrequencyList.cpp @@ -15,6 +15,8 @@ #include "Bands.hpp" #include "pimpl_impl.hpp" +#include "moc_FrequencyList.cpp" + namespace { FrequencyList::FrequencyItems const default_frequency_list = diff --git a/Modes.cpp b/Modes.cpp index 503330dfe..c325823fc 100644 --- a/Modes.cpp +++ b/Modes.cpp @@ -3,6 +3,8 @@ #include #include +#include "moc_Modes.cpp" + namespace { char const * const mode_names[] = diff --git a/PollingTransceiver.cpp b/PollingTransceiver.cpp index 0f16bb8ae..04da65ef6 100644 --- a/PollingTransceiver.cpp +++ b/PollingTransceiver.cpp @@ -6,6 +6,8 @@ #include #include +#include "moc_PollingTransceiver.cpp" + namespace { unsigned const polls_to_stabilize {3}; diff --git a/TransceiverBase.cpp b/TransceiverBase.cpp index af4624f78..74e5d98a5 100644 --- a/TransceiverBase.cpp +++ b/TransceiverBase.cpp @@ -7,6 +7,8 @@ #include #include +#include "moc_TransceiverBase.cpp" + namespace { auto const unexpected = TransceiverBase::tr ("Unexpected rig error"); diff --git a/displaytext.cpp b/displaytext.cpp index 094099e90..bcc95a200 100644 --- a/displaytext.cpp +++ b/displaytext.cpp @@ -47,10 +47,10 @@ void DisplayText::mouseDoubleClickEvent(QMouseEvent *e) void DisplayText::insertLineSpacer(QString const& line) { - _insertText (line, "#d3d3d3"); + appendText (line, "#d3d3d3"); } -void DisplayText::_insertText(const QString text, const QString bg) +void DisplayText::appendText(QString const& text, QString const& bg) { QString s = "
" + text.trimmed ().replace (' ', " ") + "
"; @@ -161,7 +161,7 @@ void DisplayText::displayDecodedText(DecodedText decodedText, QString myCall, _appendDXCCWorkedB4(/*mod*/decodedText,bg,logBook,color_CQ, color_DXCC,color_NewCall); - _insertText(decodedText.string(),bg); + appendText(decodedText.string(),bg); } @@ -176,5 +176,5 @@ void DisplayText::displayTransmittedText(QString text, QString modeTx, qint32 tx QString t = QDateTime::currentDateTimeUtc().toString("hhmm") + \ " Tx " + t2 + t1 + text; // The position of the 'Tx' is searched for in DecodedText and in MainWindow. Not sure if thats required anymore? VK3ACF - _insertText(t,bg); + appendText(t,bg); } diff --git a/displaytext.h b/displaytext.h index 7bca6a0eb..aa61d22d3 100644 --- a/displaytext.h +++ b/displaytext.h @@ -25,13 +25,12 @@ signals: void selectCallsign(bool shift, bool ctrl); public slots: - + void appendText(QString const& text, QString const& bg = "white"); protected: void mouseDoubleClickEvent(QMouseEvent *e); private: - void _insertText(const QString text, const QString bg); void _appendDXCCWorkedB4(/*mod*/DecodedText& t1, QString &bg, LogBook logBook, QColor color_CQ, QColor color_DXCC, QColor color_NewCall); diff --git a/echograph.cpp b/echograph.cpp index d3f33a55b..fce457d07 100644 --- a/echograph.cpp +++ b/echograph.cpp @@ -1,8 +1,9 @@ +#include "echograph.h" #include "commons.h" #include #include "echoplot.h" -#include "echograph.h" #include "ui_echograph.h" +#include "moc_echograph.cpp" #define NSMAX2 1366 diff --git a/mainwindow.cpp b/mainwindow.cpp index 8143d762d..dc785e30b 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -37,6 +37,7 @@ #include "StationList.hpp" #include "LiveFrequencyValidator.hpp" #include "MessageClient.hpp" +#include "wsprnet.h" #include "ui_mainwindow.h" #include "moc_mainwindow.cpp" @@ -112,6 +113,7 @@ MainWindow::MainWindow(bool multiple, QSettings * settings, QSharedMemory *shdme m_sentFirst73 {false}, m_currentMessageType {-1}, m_lastMessageType {-1}, + m_uploading {false}, m_nonWSPRTab {-1}, m_appDir {QApplication::applicationDirPath ()}, mem_jt9 {shdmem}, @@ -390,7 +392,6 @@ MainWindow::MainWindow(bool multiple, QSettings * settings, QSharedMemory *shdme m_nrx=1; m_tx=0; m_txNext=false; - m_uploading=false; m_grid6=false; m_nseq=0; m_ntr=0; @@ -431,7 +432,6 @@ MainWindow::MainWindow(bool multiple, QSettings * settings, QSharedMemory *shdme m_wideGraph->setTol(m_tol); m_bShMsgs=false; m_bDopplerTracking0=false; - m_uploading=false; m_bTxTime=false; m_rxDone=false; m_bHaveTransmitted=false; @@ -2277,7 +2277,7 @@ void MainWindow::startTx2() if (m_config.TX_messages ()) { t = " Transmitting " + m_mode + " ----------------------- " + m_config.bands ()->find (m_dialFreq); - ui->decodedTextBrowser->append(t.rightJustified (71, '-')); + ui->decodedTextBrowser->appendText(t.rightJustified (71, '-')); } QFile f {m_dataDir.absoluteFilePath ("ALL_WSPR.TXT")}; @@ -4222,12 +4222,12 @@ void MainWindow::p1ReadFromStdout() //p1readFromStdout QString band; Frequency f=1000000.0*rxFields.at(3).toDouble()+0.5; band = ' ' + m_config.bands ()->find (f); - ui->decodedTextBrowser->append(band.rightJustified (71, '-')); + ui->decodedTextBrowser->appendText(band.rightJustified (71, '-')); m_blankLine = false; } -// ui->decodedTextBrowser->append(t); - ui->decodedTextBrowser->append(rxLine); +// ui->decodedTextBrowser->appendText(t); + ui->decodedTextBrowser->appendText(rxLine); } } } @@ -4237,7 +4237,8 @@ void MainWindow::uploadSpots() if(m_diskData) return; if(m_uploading) { qDebug() << "Previous upload has not completed, spots were lost"; - return; + wsprNet->abortOutstandingRequests (); + m_uploading = false; } QString rfreq = QString("%1").arg(0.000001*(m_dialFreqRxWSPR + 1500), 0, 'f', 6); QString tfreq = QString("%1").arg(0.000001*(m_dialFreqRxWSPR + @@ -4255,6 +4256,8 @@ void MainWindow::uploadResponse(QString response) m_uploading=false; } else if (response == "Upload Failed") { m_uploading=false; + } else { + qDebug () << "WSPRnet.org status:" << response; } } diff --git a/mainwindow.h b/mainwindow.h index a7bd87282..f822c5a0d 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,7 +33,6 @@ #include "Detector.hpp" #include "Modulator.hpp" #include "decodedtext.h" -#include "wsprnet.h" #define NUM_JT4_SYMBOLS 206 #define NUM_JT65_SYMBOLS 126 @@ -64,6 +63,7 @@ class MessageClient; class QTime; class WSPRBandHopping; class HelpTextWindow; +class WSPRNet; class MainWindow : public QMainWindow { diff --git a/messageaveraging.cpp b/messageaveraging.cpp index 6ad21ff46..0518f6aa6 100644 --- a/messageaveraging.cpp +++ b/messageaveraging.cpp @@ -1,7 +1,8 @@ -#include #include "messageaveraging.h" +#include #include "ui_messageaveraging.h" #include "commons.h" +#include "moc_messageaveraging.cpp" MessageAveraging::MessageAveraging(QSettings * settings, QWidget *parent) : QWidget(parent), diff --git a/wsprnet.cpp b/wsprnet.cpp index b6004684f..942dc0f70 100644 --- a/wsprnet.cpp +++ b/wsprnet.cpp @@ -4,21 +4,34 @@ #include "wsprnet.h" -WSPRNet::WSPRNet(QObject *parent) : - QObject(parent) -{ - wsprNetUrl = "http://wsprnet.org/post?"; - //wsprNetUrl = "http://127.0.0.1/post.php?"; - networkManager = new QNetworkAccessManager(this); - connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkReply(QNetworkReply*))); +#include +#include +#include +#include +#include +#include - uploadTimer = new QTimer(this); +#include "moc_wsprnet.cpp" + +namespace +{ + char const * const wsprNetUrl = "http://wsprnet.org/post?"; + // char const * const wsprNetUrl = "http://127.0.0.1/post?"; +}; + +WSPRNet::WSPRNet(QObject *parent) + : QObject{parent} + , networkManager {new QNetworkAccessManager {this}} + , uploadTimer {new QTimer {this}} + , m_urlQueueSize {0} +{ + connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkReply(QNetworkReply*))); connect( uploadTimer, SIGNAL(timeout()), this, SLOT(work())); } -void WSPRNet::upload(QString call, QString grid, QString rfreq, QString tfreq, - QString mode, QString tpct, QString dbm, QString version, - QString fileName) +void WSPRNet::upload(QString const& call, QString const& grid, QString const& rfreq, QString const& tfreq, + QString const& mode, QString const& tpct, QString const& dbm, QString const& version, + QString const& fileName) { m_call = call; m_grid = grid; @@ -58,23 +71,36 @@ void WSPRNet::upload(QString call, QString grid, QString rfreq, QString tfreq, void WSPRNet::networkReply(QNetworkReply *reply) { + if (QNetworkReply::NoError != reply->error ()) { + Q_EMIT uploadStatus (QString {"Error: %1"}.arg (reply->error ())); + // not clearing queue or halting queuing as it may be a transient + // one off request error + } + else { QString serverResponse = reply->readAll(); if( m_uploadType == 2) { - if (!serverResponse.contains(QRegExp("spot\\(s\\) added"))) { - emit uploadStatus("Upload Failed"); - urlQueue.clear(); - uploadTimer->stop(); - } + if (!serverResponse.contains(QRegExp("spot\\(s\\) added"))) { + emit uploadStatus("Upload Failed"); + urlQueue.clear(); + uploadTimer->stop(); + } } if (urlQueue.isEmpty()) { - emit uploadStatus("done"); - QFile::remove(m_file); - uploadTimer->stop(); + emit uploadStatus("done"); + QFile::remove(m_file); + uploadTimer->stop(); } + } + + m_outstandingRequests.removeOne (reply); + qDebug () << QString {"WSPRnet.org %1 outstanding requests"}.arg (m_outstandingRequests.size ()); + + // delete request object instance on return to the event loop otherwise it is leaked + reply->deleteLater (); } -bool WSPRNet::decodeLine(QString line, QHash &query) +bool WSPRNet::decodeLine(QString const& line, QHash &query) { // 130223 2256 7 -21 -0.3 14.097090 DU1MGA PK04 37 0 40 0 // Date Time Sync dBm DT Freq Msg @@ -152,7 +178,7 @@ QString WSPRNet::urlEncodeNoSpot() return queryString;; } -QString WSPRNet::urlEncodeSpot(QHash query) +QString WSPRNet::urlEncodeSpot(QHash const& query) { QString queryString; queryString += "function=" + query["function"] + "&"; @@ -179,14 +205,16 @@ void WSPRNet::work() if (!urlQueue.isEmpty()) { QUrl url(urlQueue.dequeue()); QNetworkRequest request(url); - networkManager->get(request); - QString status = "Uploading Spot " + QString::number(m_urlQueueSize - urlQueue.size()) + - "/"+ QString::number(m_urlQueueSize); - emit uploadStatus(status); + m_outstandingRequests << networkManager->get(request); + emit uploadStatus(QString {"Uploading Spot %1/%2"}.arg (m_urlQueueSize - urlQueue.size()).arg (m_urlQueueSize)); } else { uploadTimer->stop(); } } - - +void WSPRNet::abortOutstandingRequests () { + urlQueue.clear (); + for (auto& request : m_outstandingRequests) { + request->abort (); + } +} diff --git a/wsprnet.h b/wsprnet.h index 1c9379d73..0494e73c6 100644 --- a/wsprnet.h +++ b/wsprnet.h @@ -2,17 +2,25 @@ #define WSPRNET_H #include -#include +#include +#include +#include +#include + +class QNetworkAccessManager; +class QTimer; +class QNetworkReply; class WSPRNet : public QObject { -Q_OBJECT + Q_OBJECT; + public: - explicit WSPRNet(QObject *parent = 0); - void upload(QString call, QString grid, QString rfreq, QString tfreq, - QString mode, QString tpct, QString dbm, QString version, - QString fileName); - static bool decodeLine(QString line, QHash &query); + explicit WSPRNet(QObject *parent = nullptr); + void upload(QString const& call, QString const& grid, QString const& rfreq, QString const& tfreq, + QString const& mode, QString const& tpct, QString const& dbm, QString const& version, + QString const& fileName); + static bool decodeLine(QString const& line, QHash &query); signals: void uploadStatus(QString); @@ -20,10 +28,11 @@ signals: public slots: void networkReply(QNetworkReply *); void work(); + void abortOutstandingRequests (); private: QNetworkAccessManager *networkManager; - QString wsprNetUrl; + QList m_outstandingRequests; QString m_call, m_grid, m_rfreq, m_tfreq, m_mode, m_tpct, m_dbm, m_vers, m_file; QQueue urlQueue; QTimer *uploadTimer; @@ -31,7 +40,7 @@ private: int m_uploadType; QString urlEncodeNoSpot(); - QString urlEncodeSpot(QHash spot); + QString urlEncodeSpot(QHash const& spot); }; #endif // WSPRNET_H