summaryrefslogtreecommitdiff
path: root/UDPExamples/UDPDaemon.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2020-11-16 21:54:50 +0100
committerChristoph Berg <myon@debian.org>2020-11-16 21:54:50 +0100
commit77922fa33547ce24535f2e514c9a112dcb496f18 (patch)
tree9f82b0ee01281b315f43a4c60d678927b4c6ca59 /UDPExamples/UDPDaemon.cpp
parent7aa3c56b01768ba7d38fccedc17b30a4046b3fbb (diff)
New upstream version 2.3.0~rc2+repackupstream/2.3.0_rc2+repack
Diffstat (limited to 'UDPExamples/UDPDaemon.cpp')
-rw-r--r--UDPExamples/UDPDaemon.cpp152
1 files changed, 116 insertions, 36 deletions
diff --git a/UDPExamples/UDPDaemon.cpp b/UDPExamples/UDPDaemon.cpp
index be1b5ede..2d028f07 100644
--- a/UDPExamples/UDPDaemon.cpp
+++ b/UDPExamples/UDPDaemon.cpp
@@ -17,9 +17,14 @@
#include <iostream>
#include <exception>
+#include <cstdlib>
#include <QCoreApplication>
#include <QCommandLineParser>
+#include <QCommandLineOption>
+#include <QString>
+#include <QStringList>
+#include <QNetworkInterface>
#include <QDateTime>
#include <QTime>
#include <QHash>
@@ -38,15 +43,17 @@ class Client
{
Q_OBJECT
+ using ClientKey = MessageServer::ClientKey;
+
public:
- explicit Client (QString const& id, QObject * parent = nullptr)
+ explicit Client (ClientKey const& key, QObject * parent = nullptr)
: QObject {parent}
- , id_ {id}
+ , key_ {key}
, dial_frequency_ {0u}
{
}
- Q_SLOT void update_status (QString const& id, Frequency f, QString const& mode, QString const& /*dx_call*/
+ Q_SLOT void update_status (ClientKey const& key, Frequency f, QString const& mode, QString const& /*dx_call*/
, QString const& /*report*/, QString const& /*tx_mode*/, bool /*tx_enabled*/
, bool /*transmitting*/, bool /*decoding*/, qint32 /*rx_df*/, qint32 /*tx_df*/
, QString const& /*de_call*/, QString const& /*de_grid*/, QString const& /*dx_grid*/
@@ -54,77 +61,85 @@ public:
, quint8 /*special_op_mode*/, quint32 /*frequency_tolerance*/, quint32 /*tr_period*/
, QString const& /*configuration_name*/)
{
- if (id == id_)
+ if (key == key_)
{
if (f != dial_frequency_)
{
- std::cout << tr ("%1: Dial frequency changed to %2").arg (id_).arg (f).toStdString () << std::endl;
+ std::cout << QString {"%1(%2): "}.arg (key_.second).arg (key_.first.toString ()).toStdString ()
+ << QString {"Dial frequency changed to %1"}.arg (f).toStdString () << std::endl;
dial_frequency_ = f;
}
if (mode + sub_mode != mode_)
{
- std::cout << tr ("%1: Mode changed to %2").arg (id_).arg (mode + sub_mode).toStdString () << std::endl;
+ std::cout << QString {"%1(%2): "}.arg (key_.second).arg (key_.first.toString ()).toStdString ()
+ << QString {"Mode changed to %1"}.arg (mode + sub_mode).toStdString () << std::endl;
mode_ = mode + sub_mode;
}
}
}
- Q_SLOT void decode_added (bool is_new, QString const& client_id, QTime time, qint32 snr
+ Q_SLOT void decode_added (bool is_new, ClientKey const& key, QTime time, qint32 snr
, float delta_time, quint32 delta_frequency, QString const& mode
, QString const& message, bool low_confidence, bool off_air)
{
- if (client_id == id_)
+ if (key == key_)
{
qDebug () << "new:" << is_new << "t:" << time << "snr:" << snr
<< "Dt:" << delta_time << "Df:" << delta_frequency
<< "mode:" << mode << "Confidence:" << (low_confidence ? "low" : "high")
<< "On air:" << !off_air;
- std::cout << tr ("%1: Decoded %2").arg (id_).arg (message).toStdString () << std::endl;
+ std::cout << QString {"%1(%2): "}.arg (key_.second).arg (key_.first.toString ()).toStdString ()
+ << QString {"Decoded %1"}.arg (message).toStdString () << std::endl;
}
}
- Q_SLOT void beacon_spot_added (bool is_new, QString const& client_id, QTime time, qint32 snr
+ Q_SLOT void beacon_spot_added (bool is_new, ClientKey const& key, QTime time, qint32 snr
, float delta_time, Frequency delta_frequency, qint32 drift, QString const& callsign
, QString const& grid, qint32 power, bool off_air)
{
- if (client_id == id_)
+ if (key == key_)
{
qDebug () << "new:" << is_new << "t:" << time << "snr:" << snr
<< "Dt:" << delta_time << "Df:" << delta_frequency
<< "drift:" << drift;
- std::cout << tr ("%1: WSPR decode %2 grid %3 power: %4").arg (id_).arg (callsign).arg (grid).arg (power).toStdString ()
+ std::cout << QString {"%1(%2): "}.arg (key_.second).arg (key_.first.toString ()).toStdString ()
+ << QString {"WSPR decode %1 grid %2 power: %3"}
+ .arg (callsign).arg (grid).arg (power).toStdString ()
<< "On air:" << !off_air << std::endl;
}
}
- Q_SLOT void qso_logged (QString const&client_id, QDateTime time_off, QString const& dx_call, QString const& dx_grid
+ Q_SLOT void qso_logged (ClientKey const& key, QDateTime time_off, QString const& dx_call, QString const& dx_grid
, Frequency dial_frequency, QString const& mode, QString const& report_sent
, QString const& report_received, QString const& tx_power
, QString const& comments, QString const& name, QDateTime time_on
, QString const& operator_call, QString const& my_call, QString const& my_grid
, QString const& exchange_sent, QString const& exchange_rcvd, QString const& prop_mode)
{
- if (client_id == id_)
+ if (key == key_)
{
- qDebug () << "time_on:" << time_on << "time_off:" << time_off << "dx_call:" << dx_call << "grid:" << dx_grid
+ qDebug () << "time_on:" << time_on << "time_off:" << time_off << "dx_call:"
+ << dx_call << "grid:" << dx_grid
<< "freq:" << dial_frequency << "mode:" << mode << "rpt_sent:" << report_sent
<< "rpt_rcvd:" << report_received << "Tx_pwr:" << tx_power << "comments:" << comments
<< "name:" << name << "operator_call:" << operator_call << "my_call:" << my_call
<< "my_grid:" << my_grid << "exchange_sent:" << exchange_sent
<< "exchange_rcvd:" << exchange_rcvd << "prop_mode:" << prop_mode;
std::cout << QByteArray {80, '-'}.data () << '\n';
- std::cout << tr ("%1: Logged %2 grid: %3 power: %4 sent: %5 recd: %6 freq: %7 time_off: %8 op: %9 my_call: %10 my_grid: %11 exchange_sent: %12 exchange_rcvd: %13 comments: %14 prop_mode: %15")
- .arg (id_).arg (dx_call).arg (dx_grid).arg (tx_power).arg (report_sent).arg (report_received)
- .arg (dial_frequency).arg (time_off.toString("yyyy-MM-dd hh:mm:ss.z")).arg (operator_call)
- .arg (my_call).arg (my_grid).arg (exchange_sent).arg (exchange_rcvd)
- .arg (comments).arg (prop_mode).toStdString ()
+ std::cout << QString {"%1(%2): "}.arg (key_.second).arg (key_.first.toString ()).toStdString ()
+ << QString {"Logged %1 grid: %2 power: %3 sent: %4 recd: %5 freq: %6 time_off: %7 op: %8 my_call: %9 my_grid: %10 exchange_sent: %11 exchange_rcvd: %12 comments: %13 prop_mode: %14"}
+ .arg (dx_call).arg (dx_grid).arg (tx_power)
+ .arg (report_sent).arg (report_received)
+ .arg (dial_frequency).arg (time_off.toString("yyyy-MM-dd hh:mm:ss.z")).arg (operator_call)
+ .arg (my_call).arg (my_grid).arg (exchange_sent).arg (exchange_rcvd)
+ .arg (comments).arg (prop_mode).toStdString ()
<< std::endl;
}
}
- Q_SLOT void logged_ADIF (QString const&client_id, QByteArray const& ADIF)
+ Q_SLOT void logged_ADIF (ClientKey const& key, QByteArray const& ADIF)
{
- if (client_id == id_)
+ if (key == key_)
{
qDebug () << "ADIF:" << ADIF;
std::cout << QByteArray {80, '-'}.data () << '\n';
@@ -133,7 +148,7 @@ public:
}
private:
- QString id_;
+ ClientKey key_;
Frequency dial_frequency_;
QString mode_;
};
@@ -143,8 +158,10 @@ class Server
{
Q_OBJECT
+ using ClientKey = MessageServer::ClientKey;
+
public:
- Server (port_type port, QHostAddress const& multicast_group)
+ Server (port_type port, QHostAddress const& multicast_group, QStringList const& network_interface_names)
: server_ {new MessageServer {this}}
{
// connect up server
@@ -154,21 +171,26 @@ public:
connect (server_, &MessageServer::client_opened, this, &Server::add_client);
connect (server_, &MessageServer::client_closed, this, &Server::remove_client);
- server_->start (port, multicast_group);
+#if QT_VERSION >= QT_VERSION_CHECK (5, 14, 0)
+ server_->start (port, multicast_group, QSet<QString> {network_interface_names.begin (), network_interface_names.end ()});
+#else
+ server_->start (port, multicast_group, network_interface_names.toSet ());
+#endif
}
private:
- void add_client (QString const& id, QString const& version, QString const& revision)
+ void add_client (ClientKey const& key, QString const& version, QString const& revision)
{
- auto client = new Client {id};
+ auto client = new Client {key};
connect (server_, &MessageServer::status_update, client, &Client::update_status);
connect (server_, &MessageServer::decode, client, &Client::decode_added);
connect (server_, &MessageServer::WSPR_decode, client, &Client::beacon_spot_added);
connect (server_, &MessageServer::qso_logged, client, &Client::qso_logged);
connect (server_, &MessageServer::logged_ADIF, client, &Client::logged_ADIF);
- clients_[id] = client;
- server_->replay (id);
- std::cout << "Discovered WSJT-X instance: " << id.toStdString ();
+ clients_[key] = client;
+ server_->replay (key);
+ std::cout << "Discovered WSJT-X instance: " << key.second.toStdString ()
+ << '(' << key.first.toString ().toStdString () << ')';
if (version.size ())
{
std::cout << " v" << version.toStdString ();
@@ -180,23 +202,60 @@ private:
std::cout << std::endl;
}
- void remove_client (QString const& id)
+ void remove_client (ClientKey const& key)
{
- auto iter = clients_.find (id);
+ auto iter = clients_.find (key);
if (iter != std::end (clients_))
{
clients_.erase (iter);
(*iter)->deleteLater ();
}
- std::cout << "Removed WSJT-X instance: " << id.toStdString () << std::endl;
+ std::cout << "Removed WSJT-X instance: " << key.second.toStdString ()
+ << '(' << key.first.toString ().toStdString () << ')' << std::endl;
}
MessageServer * server_;
- // maps client id to clients
- QHash<QString, Client *> clients_;
+ // maps client key to clients
+ QHash<ClientKey, Client *> clients_;
};
+void list_interfaces ()
+{
+ for (auto const& net_if : QNetworkInterface::allInterfaces ())
+ {
+ if (net_if.flags () & QNetworkInterface::IsUp)
+ {
+ std::cout << net_if.humanReadableName ().toStdString () << ":\n"
+ " id: " << net_if.name ().toStdString () << " (" << net_if.index () << ")\n"
+ " addr: " << net_if.hardwareAddress ().toStdString () << "\n"
+ " flags: ";
+ if (net_if.flags () & QNetworkInterface::IsRunning)
+ {
+ std::cout << "Running ";
+ }
+ if (net_if.flags () & QNetworkInterface::CanBroadcast)
+ {
+ std::cout << "Broadcast ";
+ }
+ if (net_if.flags () & QNetworkInterface::CanMulticast)
+ {
+ std::cout << "Multicast ";
+ }
+ if (net_if.flags () & QNetworkInterface::IsLoopBack)
+ {
+ std::cout << "Loop-back ";
+ }
+ std::cout << "\n addresses:\n";
+ for (auto const& ae : net_if.addressEntries ())
+ {
+ std::cout << " " << ae.ip ().toString ().toStdString () << '\n';
+ }
+ std::cout << '\n';
+ }
+ }
+}
+
#include "UDPDaemon.moc"
int main (int argc, char * argv[])
@@ -217,6 +276,11 @@ int main (int argc, char * argv[])
auto help_option = parser.addHelpOption ();
auto version_option = parser.addVersionOption ();
+ QCommandLineOption list_option (QStringList {"l", "list-interfaces"},
+ app.translate ("UDPDaemon",
+ "Print the available network interfaces."));
+ parser.addOption (list_option);
+
QCommandLineOption port_option (QStringList {"p", "port"},
app.translate ("UDPDaemon",
"Where <PORT> is the UDP service port number to listen on.\n"
@@ -232,9 +296,25 @@ int main (int argc, char * argv[])
app.translate ("UDPDaemon", "GROUP"));
parser.addOption (multicast_addr_option);
+ QCommandLineOption network_interface_option (QStringList {"i", "network-interface"},
+ app.translate ("UDPDaemon",
+ "Where <INTERFACE> is the network interface name to join on.\n"
+ "This option can be passed more than once to specify multiple network interfaces\n"
+ "The default is use just the loop back interface."),
+ app.translate ("UDPDaemon", "INTERFACE"));
+ parser.addOption (network_interface_option);
+
parser.process (app);
- Server server {static_cast<port_type> (parser.value (port_option).toUInt ()), QHostAddress {parser.value (multicast_addr_option)}};
+ if (parser.isSet (list_option))
+ {
+ list_interfaces ();
+ return EXIT_SUCCESS;
+ }
+
+ Server server {static_cast<port_type> (parser.value (port_option).toUInt ())
+ , QHostAddress {parser.value (multicast_addr_option).trimmed ()}
+ , parser.values (network_interface_option)};
return app.exec ();
}