Skip to content

Commit

Permalink
Cleaned version of NetworkInfoFetcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragaeeb committed Apr 12, 2019
1 parent c120bad commit 935e7ee
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions GoldenRetrieverService/src/NetworkInfoFetcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "precompiled.h"

#include "NetworkInfoFetcher.h"
#include "Command.h"

namespace {

void fetchInfoForInterface(QString const& name, QString& ipv6, QStringList& result)
{
QNetworkInterface const& i = QNetworkInterface::interfaceFromName(name);

if ( i.isValid() )
{
QString ipv4;

foreach ( QNetworkAddressEntry const& entry, i.addressEntries() )
{
QAbstractSocket::NetworkLayerProtocol p = entry.ip().protocol();

if ( p == QAbstractSocket::IPv4Protocol || ( p == QAbstractSocket::IPv6Protocol && ipv4.isNull() && entry.ip().toString() != ipv6 ) ) {
ipv4 = entry.ip().toString();
} else if ( p == QAbstractSocket::IPv6Protocol && ipv6.isNull() ) {
ipv6 = entry.ip().toString();
}
}

if ( !ipv4.isNull() ) {
result << QString("%1=%2").arg(name).arg(ipv4);
}
}
}

}

namespace golden {

NetworkInfoFetcher::NetworkInfoFetcher(QStringList const& tokens, QObject* parent) : QObject(parent)
{
Q_UNUSED(tokens);

connect( &m_network, SIGNAL( requestComplete(QVariant const&, QByteArray const&) ), this, SLOT( onRequestComplete(QVariant const&, QByteArray const&) ) );
}


void NetworkInfoFetcher::onRequestComplete(QVariant const& cookie, QByteArray const& data)
{
Q_UNUSED(cookie);

QString result = QString(data);
QStringList tokens = result.split(";");
QString ip;
QString host;

if ( !tokens.isEmpty() ) {
ip = tokens.first();
}

if ( tokens.size() > 1 ) {
host = tokens.last();
}

QStringList deviceInfo;

QString ipv6;
QStringList networkAddresses;
fetchInfoForInterface("bcm0", ipv6, networkAddresses);
fetchInfoForInterface("msm0", ipv6, networkAddresses);
fetchInfoForInterface("bptp0", ipv6, networkAddresses);
deviceInfo << networkAddresses;

if ( !ip.isEmpty() ) {
deviceInfo << QString("ip=%1").arg(ip);
}

if ( !host.isEmpty() ) {
deviceInfo << QString("host=%1").arg(host);
}

QString replyBody = deviceInfo.join("\n");

emit commandProcessed(Command::NetworkInfo, replyBody);
}


void NetworkInfoFetcher::process() {
m_network.doGet("http://host.com/path/showIpEndpoint.php");
}


NetworkInfoFetcher::~NetworkInfoFetcher()
{
}

} /* namespace golden */

0 comments on commit 935e7ee

Please sign in to comment.