summaryrefslogtreecommitdiff
path: root/Network
diff options
context:
space:
mode:
authortony mancill <tmancill@debian.org>2023-09-03 19:46:43 -0700
committertony mancill <tmancill@debian.org>2023-09-03 19:46:43 -0700
commit393a486ee1d7e308a49f8e0b8c3a4b2f43223248 (patch)
tree6a6e8a56ace360795e1cf1a310d5652bb23f1386 /Network
parent6a285ab382ebff18c88e52bec75dc1773ac33b74 (diff)
New upstream version 2.7.0~rc2+repackupstream/2.7.0_rc2+repack
Diffstat (limited to 'Network')
-rwxr-xr-xNetwork/FileDownload.cpp229
-rwxr-xr-xNetwork/FileDownload.hpp54
-rwxr-xr-xNetwork/LotWUsers.cpp179
-rwxr-xr-xNetwork/LotWUsers.hpp1
-rwxr-xr-xNetwork/PSKReporter.cpp137
-rwxr-xr-xNetwork/PSKReporter.hpp4
6 files changed, 458 insertions, 146 deletions
diff --git a/Network/FileDownload.cpp b/Network/FileDownload.cpp
new file mode 100755
index 00000000..7647dab5
--- /dev/null
+++ b/Network/FileDownload.cpp
@@ -0,0 +1,229 @@
+
+#include "FileDownload.hpp"
+#include <QCoreApplication>
+#include <QUrl>
+#include <QNetworkRequest>
+#include <QtNetwork/QNetworkAccessManager>
+#include <QtNetwork/QNetworkReply>
+#include <QFileInfo>
+#include <QDir>
+#include <QIODevice>
+#include "qt_helpers.hpp"
+#include "Logger.hpp"
+
+FileDownload::FileDownload() : QObject(nullptr)
+{
+ redirect_count_ = 0;
+ url_valid_ = false;
+}
+
+FileDownload::~FileDownload()
+{
+}
+#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
+void FileDownload::errorOccurred(QNetworkReply::NetworkError code)
+{
+ LOG_INFO(QString{"FileDownload [%1]: errorOccurred %2 -> %3"}.arg(user_agent_).arg(code).arg(reply_->errorString()));
+ Q_EMIT error (reply_->errorString ());
+ destfile_.cancelWriting ();
+ destfile_.commit ();
+}
+#else
+void FileDownload::obsoleteError()
+{
+ LOG_INFO(QString{"FileDownload [%1]: error -> %3"}.arg(user_agent_).arg(reply_->errorString()));
+ Q_EMIT error (reply_->errorString ());
+ destfile_.cancelWriting ();
+ destfile_.commit ();
+}
+#endif
+
+void FileDownload::configure(QNetworkAccessManager *network_manager, const QString &source_url, const QString &destination_path, const QString &user_agent)
+{
+ manager_ = network_manager;
+ source_url_ = source_url;
+ destination_filename_ = destination_path;
+ user_agent_ = user_agent;
+}
+
+void FileDownload::store()
+{
+ if (destfile_.isOpen())
+ destfile_.write (reply_->read (reply_->bytesAvailable ()));
+ else
+ LOG_INFO(QString{ "FileDownload [%1]: file is not open."}.arg(user_agent_));
+}
+
+void FileDownload::replyComplete()
+{
+ QFileInfo destination_file(destination_filename_);
+ QDir tmpdir_(destination_file.absoluteFilePath());
+
+ LOG_DEBUG(QString{ "FileDownload [%1]: replyComplete"}.arg(user_agent_));
+ if (!reply_)
+ {
+ Q_EMIT load_finished ();
+ return; // we probably deleted it in an earlier call
+ }
+
+ QUrl redirect_url {reply_->attribute (QNetworkRequest::RedirectionTargetAttribute).toUrl ()};
+
+ if (reply_->error () == QNetworkReply::NoError && !redirect_url.isEmpty ())
+ {
+ if ("https" == redirect_url.scheme () && !QSslSocket::supportsSsl ())
+ {
+ Q_EMIT download_error (tr ("Network Error - SSL/TLS support not installed, cannot fetch:\n\'%1\'")
+ .arg (redirect_url.toDisplayString ()));
+ url_valid_ = false; // reset
+ Q_EMIT load_finished ();
+ }
+ else if (++redirect_count_ < 10) // maintain sanity
+ {
+ // follow redirect
+ download (reply_->url ().resolved (redirect_url));
+ }
+ else
+ {
+ Q_EMIT download_error (tr ("Network Error - Too many redirects:\n\'%1\'")
+ .arg (redirect_url.toDisplayString ()));
+ url_valid_ = false; // reset
+ Q_EMIT load_finished ();
+ }
+ }
+ else if (reply_->error () != QNetworkReply::NoError)
+ {
+ destfile_.cancelWriting();
+ destfile_.commit();
+ url_valid_ = false; // reset
+ // report errors that are not due to abort
+ if (QNetworkReply::OperationCanceledError != reply_->error ())
+ {
+ Q_EMIT download_error (tr ("Network Error:\n%1")
+ .arg (reply_->errorString ()));
+ }
+ Q_EMIT load_finished ();
+ }
+ else
+ {
+ if (!url_valid_)
+ {
+ // now get the body content
+ url_valid_ = true;
+ download (reply_->url ().resolved (redirect_url));
+ }
+ else // the body has completed. Save it.
+ {
+ url_valid_ = false; // reset
+ // load the database asynchronously
+ // future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_.fileName ());
+ LOG_INFO(QString{ "FileDownload [%1]: complete. File path is %2"}.arg(user_agent_).arg(destfile_.fileName()));
+ destfile_.commit();
+ emit complete(destination_filename_);
+ }
+ }
+
+ if (reply_ && reply_->isFinished ())
+ {
+ reply_->deleteLater ();
+ }
+
+}
+
+void FileDownload::downloadComplete(QNetworkReply *data)
+{
+ // make a temp file in the same place as the file we're downloading. Needs to be on the same
+ // filesystem as where we eventually want to 'mv' it.
+
+ QUrl r = request_.url();
+ LOG_INFO(QString{"FileDownload [%1]: finished %2 of %3 -> %4 (%5)"}.arg(user_agent_).arg(data->operation()).arg(source_url_).arg(destination_filename_).arg(r.url()));
+
+#ifdef DEBUG_FILEDOWNLOAD
+ LOG_INFO("Request Headers:");
+ Q_FOREACH (const QByteArray& hdr, request_.rawHeaderList()) {
+ LOG_INFO(QString{ "%1 -> %2"}.arg(QString(hdr)).arg(QString(request_.rawHeader(hdr))));
+ }
+
+ LOG_INFO("Response Headers:");
+ Q_FOREACH (const QByteArray& hdr, reply_->rawHeaderList()) {
+ LOG_INFO(QString{ "%1 -> %2"}.arg(QString(hdr)).arg(QString(reply_->rawHeader(hdr))));
+ }
+#endif
+ data->deleteLater();
+}
+
+void FileDownload::start_download()
+{
+ url_valid_ = false;
+ download(QUrl(source_url_));
+}
+
+void FileDownload::download(QUrl qurl)
+{
+ request_.setUrl(qurl);
+
+#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
+ if (QNetworkAccessManager::Accessible != manager_->networkAccessible ())
+ {
+ // try and recover network access for QNAM
+ manager_->setNetworkAccessible (QNetworkAccessManager::Accessible);
+ }
+#endif
+
+ LOG_INFO(QString{"FileDownload [%1]: Starting download of %2 to %3"}.arg(user_agent_).arg(source_url_).arg(destination_filename_));
+
+ request_.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
+ request_.setRawHeader("Accept", "*/*");
+ request_.setRawHeader ("User-Agent", user_agent_.toLocal8Bit()); // Must have a UA for some sites, like country-files
+
+ if (!url_valid_)
+ {
+ reply_ = manager_->head(request_);
+ }
+ else
+ {
+ reply_ = manager_->get (request_);
+ }
+
+ QObject::connect(manager_, &QNetworkAccessManager::finished, this, &FileDownload::downloadComplete, Qt::UniqueConnection);
+ QObject::connect(reply_, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress, Qt::UniqueConnection);
+ QObject::connect(reply_, &QNetworkReply::finished, this, &FileDownload::replyComplete, Qt::UniqueConnection);
+#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
+ QObject::connect(reply_, &QNetworkReply::errorOccurred,this, &FileDownload::errorOccurred, Qt::UniqueConnection);
+#else
+ QObject::connect(reply_, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &FileDownload::obsoleteError, Qt::UniqueConnection);
+#endif
+ QObject::connect(reply_, &QNetworkReply::readyRead, this, &FileDownload::store, Qt::UniqueConnection);
+
+ QFileInfo destination_file(destination_filename_);
+ QString const tmpfile_base = destination_file.fileName();
+ QString const &tmpfile_path = destination_file.absolutePath();
+ QDir tmpdir{};
+ if (!tmpdir.mkpath(tmpfile_path))
+ {
+ LOG_INFO(QString{"FileDownload [%1]: Directory %2 does not exist"}.arg(user_agent_).arg(tmpfile_path).arg(
+ destfile_.errorString()));
+ }
+
+ if (url_valid_) {
+ destfile_.setFileName(destination_file.absoluteFilePath());
+ if (!destfile_.open(QSaveFile::WriteOnly | QIODevice::WriteOnly)) {
+ LOG_INFO(QString{"FileDownload [%1]: Unable to open %2: %3"}.arg(user_agent_).arg(destfile_.fileName()).arg(
+ destfile_.errorString()));
+ return;
+ }
+ }
+}
+
+void FileDownload::downloadProgress(qint64 received, qint64 total)
+{
+ LOG_DEBUG(QString{"FileDownload: [%1] Progress %2 from %3, total %4, so far %5"}.arg(user_agent_).arg(destination_filename_).arg(source_url_).arg(total).arg(received));
+ Q_EMIT progress(QString{"%4 bytes downloaded"}.arg(received));
+}
+
+void FileDownload::abort ()
+{
+ if (reply_ && reply_->isRunning ())
+ {
+ reply_->abort ();
+ }
+}
diff --git a/Network/FileDownload.hpp b/Network/FileDownload.hpp
new file mode 100755
index 00000000..140283c3
--- /dev/null
+++ b/Network/FileDownload.hpp
@@ -0,0 +1,54 @@
+#ifndef WSJTX_FILEDOWNLOAD_H
+#define WSJTX_FILEDOWNLOAD_H
+
+#include <QObject>
+#include <QString>
+#include <QPointer>
+#include <QtNetwork/QNetworkAccessManager>
+#include <QtNetwork/QNetworkReply>
+#include <QTemporaryFile>
+#include <QSaveFile>
+
+class FileDownload : public QObject {
+ Q_OBJECT
+
+public:
+ explicit FileDownload();
+ ~FileDownload();
+
+ void configure(QNetworkAccessManager *network_manager, const QString& source_url, const QString& destination_filename, const QString& user_agent);
+
+private:
+ QNetworkAccessManager *manager_;
+ QString source_url_;
+ QString destination_filename_;
+ QString user_agent_;
+ QPointer<QNetworkReply> reply_;
+ QNetworkRequest request_;
+ QSaveFile destfile_;
+ bool url_valid_;
+ int redirect_count_;
+signals:
+ void complete(QString filename);
+ void progress(QString filename);
+ void load_finished() const;
+ void download_error (QString const& reason) const;
+ void error(QString const& reason) const;
+
+
+public slots:
+ void start_download();
+ void download(QUrl url);
+ void store();
+ void abort();
+ void downloadComplete(QNetworkReply* data);
+ void downloadProgress(qint64 recieved, qint64 total);
+#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
+ void errorOccurred(QNetworkReply::NetworkError code);
+#else
+ void obsoleteError();
+#endif
+ void replyComplete();
+};
+
+#endif //WSJTX_FILEDOWNLOAD_H
diff --git a/Network/LotWUsers.cpp b/Network/LotWUsers.cpp
index f33b75c1..73480f68 100755
--- a/Network/LotWUsers.cpp
+++ b/Network/LotWUsers.cpp
@@ -16,7 +16,9 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
-
+#include "qt_helpers.hpp"
+#include "Logger.hpp"
+#include "FileDownload.hpp"
#include "pimpl_impl.hpp"
#include "moc_LotWUsers.cpp"
@@ -39,6 +41,7 @@ public:
, url_valid_ {false}
, redirect_count_ {0}
, age_constraint_ {365}
+ , connected_ {false}
{
}
@@ -48,161 +51,50 @@ public:
auto csv_file_name = csv_file_.fileName ();
auto exists = QFileInfo::exists (csv_file_name);
if (fetch && (!exists || forced_fetch))
+ {
+ current_url_.setUrl(url);
+ if (current_url_.isValid() && !QSslSocket::supportsSsl())
{
- current_url_.setUrl (url);
- if (current_url_.isValid () && !QSslSocket::supportsSsl ())
- {
- current_url_.setScheme ("http");
- }
- redirect_count_ = 0;
- download (current_url_);
- }
- else
- {
- if (exists)
- {
- // load the database asynchronously
- future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_name);
- }
- }
- }
-
- void download (QUrl url)
- {
-#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
- if (QNetworkAccessManager::Accessible != network_manager_->networkAccessible ())
- {
- // try and recover network access for QNAM
- network_manager_->setNetworkAccessible (QNetworkAccessManager::Accessible);
- }
-#endif
-
- QNetworkRequest request {url};
- request.setRawHeader ("User-Agent", "WSJT LotW User Downloader");
- request.setOriginatingObject (this);
-
- // this blocks for a second or two the first time it is used on
- // Windows - annoying
- if (!url_valid_)
- {
- reply_ = network_manager_->head (request);
- }
- else
- {
- reply_ = network_manager_->get (request);
+ current_url_.setScheme("http");
}
+ redirect_count_ = 0;
- connect (reply_.data (), &QNetworkReply::finished, this, &LotWUsers::impl::reply_finished);
- connect (reply_.data (), &QNetworkReply::readyRead, this, &LotWUsers::impl::store);
- }
+ Q_EMIT self_->progress (QString("Starting download from %1").arg(url));
- void reply_finished ()
- {
- if (!reply_)
- {
- Q_EMIT self_->load_finished ();
- return; // we probably deleted it in an earlier call
- }
- QUrl redirect_url {reply_->attribute (QNetworkRequest::RedirectionTargetAttribute).toUrl ()};
- if (reply_->error () == QNetworkReply::NoError && !redirect_url.isEmpty ())
+ lotw_downloader_.configure(network_manager_,
+ url,
+ csv_file_name,
+ "WSJT-X LotW User Downloader");
+ if (!connected_)
{
- if ("https" == redirect_url.scheme () && !QSslSocket::supportsSsl ())
- {
- Q_EMIT self_->LotW_users_error (tr ("Network Error - SSL/TLS support not installed, cannot fetch:\n\'%1\'")
- .arg (redirect_url.toDisplayString ()));
- url_valid_ = false; // reset
- Q_EMIT self_->load_finished ();
- }
- else if (++redirect_count_ < 10) // maintain sanity
- {
- // follow redirect
- download (reply_->url ().resolved (redirect_url));
- }
- else
- {
- Q_EMIT self_->LotW_users_error (tr ("Network Error - Too many redirects:\n\'%1\'")
- .arg (redirect_url.toDisplayString ()));
- url_valid_ = false; // reset
- Q_EMIT self_->load_finished ();
- }
+ connect(&lotw_downloader_, &FileDownload::complete, [this, csv_file_name] {
+ LOG_INFO(QString{"LotWUsers: Loading LotW file %1"}.arg(csv_file_name));
+ future_load_ = std::async(std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_name);
+ });
+ connect(&lotw_downloader_, &FileDownload::error, [this] (QString const& msg) {
+ LOG_INFO(QString{"LotWUsers: Error downloading LotW file: %1"}.arg(msg));
+ Q_EMIT self_->LotW_users_error (msg);
+ });
+ connect( &lotw_downloader_, &FileDownload::progress, [this] (QString const& msg) {
+ Q_EMIT self_->progress (msg);
+ });
+ connected_ = true;
}
- else if (reply_->error () != QNetworkReply::NoError)
- {
- csv_file_.cancelWriting ();
- csv_file_.commit ();
- url_valid_ = false; // reset
- // report errors that are not due to abort
- if (QNetworkReply::OperationCanceledError != reply_->error ())
- {
- Q_EMIT self_->LotW_users_error (tr ("Network Error:\n%1")
- .arg (reply_->errorString ()));
- }
- Q_EMIT self_->load_finished ();
+ lotw_downloader_.start_download();
}
else
{
- if (url_valid_ && !csv_file_.commit ())
- {
- Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot commit changes to:\n\"%1\"")
- .arg (csv_file_.fileName ()));
- url_valid_ = false; // reset
- Q_EMIT self_->load_finished ();
- }
- else
- {
- if (!url_valid_)
- {
- // now get the body content
- url_valid_ = true;
- download (reply_->url ().resolved (redirect_url));
- }
- else
- {
- url_valid_ = false; // reset
- // load the database asynchronously
- future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_.fileName ());
- }
- }
- }
- if (reply_ && reply_->isFinished ())
- {
- reply_->deleteLater ();
- }
- }
-
- void store ()
- {
- if (url_valid_)
- {
- if (!csv_file_.isOpen ())
- {
- // create temporary file in the final location
- if (!csv_file_.open (QSaveFile::WriteOnly))
- {
- abort ();
- Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot open file:\n\"%1\"\nError(%2): %3")
- .arg (csv_file_.fileName ())
- .arg (csv_file_.error ())
- .arg (csv_file_.errorString ()));
- }
- }
- if (csv_file_.write (reply_->read (reply_->bytesAvailable ())) < 0)
+ if (exists)
{
- abort ();
- Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot write to file:\n\"%1\"\nError(%2): %3")
- .arg (csv_file_.fileName ())
- .arg (csv_file_.error ())
- .arg (csv_file_.errorString ()));
+ // load the database asynchronously
+ future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_name);
}
}
}
void abort ()
{
- if (reply_ && reply_->isRunning ())
- {
- reply_->abort ();
- }
+ lotw_downloader_.abort();
}
// Load the database from the given file name
@@ -222,12 +114,14 @@ public:
auto pos = l.indexOf (',');
result[l.left (pos)] = QDate::fromString (l.mid (pos + 1, l.indexOf (',', pos + 1) - pos - 1), "yyyy-MM-dd");
}
-// qDebug () << "LotW User Data Loaded";
}
else
{
throw std::runtime_error {QObject::tr ("Failed to open LotW users CSV file: '%1'").arg (f.fileName ()).toStdString ()};
}
+ LOG_INFO(QString{"LotWUsers: Loaded %1 records from %2"}.arg(result.size()).arg(lotw_csv_file));
+ Q_EMIT self_->progress (QString{"Loaded %1 records from LotW."}.arg(result.size()));
+ Q_EMIT self_->load_finished();
return result;
}
@@ -241,6 +135,8 @@ public:
std::future<dictionary> future_load_;
dictionary last_uploaded_;
qint64 age_constraint_; // days
+ FileDownload lotw_downloader_;
+ bool connected_;
};
#include "LotWUsers.moc"
@@ -249,6 +145,7 @@ LotWUsers::LotWUsers (QNetworkAccessManager * network_manager, QObject * parent)
: QObject {parent}
, m_ {this, network_manager}
{
+
}
LotWUsers::~LotWUsers ()
diff --git a/Network/LotWUsers.hpp b/Network/LotWUsers.hpp
index b3fd6601..cfb9b890 100755
--- a/Network/LotWUsers.hpp
+++ b/Network/LotWUsers.hpp
@@ -31,6 +31,7 @@ public:
bool user (QString const& call) const;
Q_SIGNAL void LotW_users_error (QString const& reason) const;
+ Q_SIGNAL void progress (QString const& reason) const;
Q_SIGNAL void load_finished () const;
private:
diff --git a/Network/PSKReporter.cpp b/Network/PSKReporter.cpp
index 4db1d756..b9e58d11 100755
--- a/Network/PSKReporter.cpp
+++ b/Network/PSKReporter.cpp
@@ -6,6 +6,8 @@
//
// Reports will be sent in batch mode every 5 minutes.
+#include <fstream>
+#include <iostream>
#include <cmath>
#include <QObject>
#include <QString>
@@ -18,6 +20,7 @@
#include <QByteArray>
#include <QDataStream>
#include <QTimer>
+#include <QDir>
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#include <QRandomGenerator>
#endif
@@ -29,19 +32,26 @@
#include "moc_PSKReporter.cpp"
+#define DEBUGECLIPSE 0
+
namespace
{
QLatin1String HOST {"report.pskreporter.info"};
// QLatin1String HOST {"127.0.0.1"};
quint16 SERVICE_PORT {4739};
// quint16 SERVICE_PORT {14739};
- int MIN_SEND_INTERVAL {15}; // in seconds
- int FLUSH_INTERVAL {4 * 5}; // in send intervals
+ int MIN_SEND_INTERVAL {120}; // in seconds
+ int FLUSH_INTERVAL {MIN_SEND_INTERVAL + 5}; // in send intervals
bool ALIGNMENT_PADDING {true};
int MIN_PAYLOAD_LENGTH {508};
- int MAX_PAYLOAD_LENGTH {1400};
+ int MAX_PAYLOAD_LENGTH {10000};
+ int CACHE_TIMEOUT {300}; // default to 5 minutes for repeating spots
+ QMap<QString, time_t> spot_cache;
}
+static int added;
+static int removed;
+
class PSKReporter::impl final
: public QObject
{
@@ -83,6 +93,7 @@ public:
send_receiver_data_ = 3; // three times
}
});
+ eclipse_load(config->data_dir ().absoluteFilePath ("eclipse.txt"));
}
void check_connection ()
@@ -167,7 +178,7 @@ public:
if (!report_timer_.isActive ())
{
- report_timer_.start (MIN_SEND_INTERVAL * 1000);
+ report_timer_.start (MIN_SEND_INTERVAL+1 * 1000); // we add 1 to give some more randomization
}
if (!descriptor_timer_.isActive ())
{
@@ -188,6 +199,8 @@ public:
void send_report (bool send_residue = false);
void build_preamble (QDataStream&);
+ void eclipse_load(QString filename);
+ bool eclipse_active(QDateTime now = QDateTime::currentDateTime());
bool flushing ()
{
@@ -196,6 +209,14 @@ public:
return flush;
}
+ QString getStringFromQDateTime(const QString& dateTimeString, const QString& format)
+ {
+ QDateTime dateTime = QDateTime::fromString(dateTimeString, format);
+ return dateTime.toString();
+ }
+
+ QList<QDateTime> eclipseDates;
+
logger_type mutable logger_;
PSKReporter * self_;
Configuration const * config_;
@@ -272,6 +293,69 @@ namespace
}
}
+bool PSKReporter::impl::eclipse_active(QDateTime timeutc)
+{
+#ifdef DEBUGECLIPSE
+ std::ofstream mylog("/temp/eclipse.log", std::ios_base::app);
+#endif
+ QDateTime dateNow = QDateTime::currentDateTimeUtc();
+ for (int i=0; i< eclipseDates.size(); ++i)
+ {
+ QDateTime check = eclipseDates.at(i); // already in UTC time
+ // +- 6 hour window
+ qint64 secondsDiff = qAbs(check.secsTo(dateNow));
+ if (secondsDiff <= 3600*6) // 6 hour check
+ {
+#ifdef DEBUGECLIPSE
+ mylog << dateNow.toString(Qt::ISODate) << " Eclipse! " << "secondsDiff=" << secondsDiff << std::endl;
+#endif
+ return true;
+ }
+ }
+#ifdef DEBUGECLIPSE
+ mylog << timeutc.toString("yyyy-MM-dd HH:mm:ss") << " no eclipse" << "\n";
+#endif
+ return false;
+}
+
+void PSKReporter::impl::eclipse_load(QString eclipse_file)
+{
+ std::ifstream fs(qPrintable(eclipse_file));
+ std::string mydate,mytime,myline;
+#ifdef DEBUGECLIPSE
+ std::ofstream mylog("c:/temp/eclipse.log");
+ mylog << "eclipse_file=" << eclipse_file << std::endl;
+#endif
+ if (fs.is_open())
+ {
+ while(!fs.eof())
+ {
+ std::getline(fs, myline);
+ if (myline[0] != '#' && myline.length() > 2) // make sure to skip blank lines
+ {
+ //QString format = "yyyy-MM-dd hh:mm:ss";
+ QDateTime qdate = QDateTime::fromString(QString::fromStdString(myline), Qt::ISODate);
+ QDateTime now = QDateTime::currentDateTimeUtc();
+ // only add the date if we can cover the whole 12 hours
+ //if (now < qdate.toUTC().addSecs(-3600*6))
+ eclipseDates.append(qdate);
+#ifdef DEBUGECLIPSE
+ //else
+ // mylog << "not adding " << myline << std::endl;
+#endif
+
+ }
+#ifdef DEBUGECLIPSE
+ mylog << myline << std::endl;
+#endif
+ }
+ }
+#ifdef DEBUGECLIPSE
+ if (eclipse_active(QDateTime::currentDateTime().toUTC())) mylog << "Eclipse is active" << std::endl;
+ else mylog << "Eclipse is not active" << std::endl;
+#endif
+}
+
void PSKReporter::impl::build_preamble (QDataStream& message)
{
// Message Header
@@ -516,6 +600,11 @@ void PSKReporter::reconnect ()
m_->reconnect ();
}
+bool PSKReporter::eclipse_active(QDateTime now)
+{
+ return m_->eclipse_active(now);
+}
+
void PSKReporter::setLocalStation (QString const& call, QString const& gridSquare, QString const& antenna)
{
LOG_LOG_LOCATION (m_->logger_, trace, "call: " << call << " grid: " << gridSquare << " ant: " << antenna);
@@ -542,7 +631,45 @@ bool PSKReporter::addRemoteStation (QString const& call, QString const& grid, Ra
{
reconnect ();
}
- m_->spots_.enqueue ({call, grid, snr, freq, mode, QDateTime::currentDateTimeUtc ()});
+ // remove any earlier spots of this call to reduce pskreporter load
+#ifdef DEBUGPSK
+ static std::fstream fs;
+ if (!fs.is_open()) fs.open("/temp/psk.log", std::fstream::in | std::fstream::out | std::fstream::app);
+#endif
+ added++;
+
+ QDateTime qdateNow = QDateTime::currentDateTime().toUTC();
+ // we allow all spots through +/- 6 hours around an eclipse for the HamSCI group
+ if (!spot_cache.contains(call) || freq > 49000000 || eclipse_active(qdateNow)) // then it's a new spot
+ {
+ m_->spots_.enqueue ({call, grid, snr, freq, mode, QDateTime::currentDateTimeUtc ()});
+ spot_cache.insert(call, time(NULL));
+#ifdef DEBUGPSK
+ if (fs.is_open()) fs << "Adding " << call << " freq=" << freq << " " << spot_cache[call] << " count=" << m_->spots_.count() << std::endl;
+#endif
+ }
+ else if (time(NULL) - spot_cache[call] > CACHE_TIMEOUT) // then the cache has expired
+ {
+ m_->spots_.enqueue ({call, grid, snr, freq, mode, QDateTime::currentDateTimeUtc ()});
+#ifdef DEBUGPSK
+ if (fs.is_open()) fs << "Adding # " << call << spot_cache[call] << " count=" << m_->spots_.count() << std::endl;
+#endif
+ spot_cache[call] = time(NULL);
+ }
+ else
+ {
+ removed++;
+#ifdef DEBUGPSK
+ if (fs.is_open()) fs << "Removing " << call << " " << time(NULL) << " reduction=" << removed/(double)added*100 << "%" << std::endl;
+#endif
+ }
+ // remove cached items over 10 minutes old to save a little memory
+ QMapIterator<QString, time_t> i(spot_cache);
+ time_t tmptime = time(NULL);
+ while(i.hasNext()) {
+ i.next();
+ if (tmptime - i.value() > 600) spot_cache.remove(i.key());
+ }
return true;
}
return false;
diff --git a/Network/PSKReporter.hpp b/Network/PSKReporter.hpp
index 7bac5421..483e6e9b 100755
--- a/Network/PSKReporter.hpp
+++ b/Network/PSKReporter.hpp
@@ -31,6 +31,10 @@ public:
//
void sendReport (bool last = false);
+ //
+ // True if current time falls withing a +/- window of a solar eclipse for HamSCI use
+ bool eclipse_active(QDateTime now);
+
Q_SIGNAL void errorOccurred (QString const& reason);
private: