forked from ragaeeb/golden_retriever
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned version of NetworkInfoFetcher.
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |