mirror of
https://gitlab.com/Syping/mayu
synced 2024-11-25 05:20:23 +01:00
add clean for remove -1 results
This commit is contained in:
parent
16d5d83eec
commit
d5663e583b
3 changed files with 53 additions and 11 deletions
18
main.cpp
18
main.cpp
|
@ -31,6 +31,7 @@ int main(int argc, char *argv[])
|
|||
QStringList arguments = a.arguments();
|
||||
arguments.removeAt(0);
|
||||
|
||||
bool a_clean = false;
|
||||
mayuMode a_mode = mayuMode::Ping;
|
||||
for (int i = arguments.length(); i > 0; i--) {
|
||||
const QString &argument = arguments.at(i-1);
|
||||
|
@ -42,16 +43,31 @@ int main(int argc, char *argv[])
|
|||
a_mode = mayuMode::Resolve;
|
||||
arguments.removeAt(i-1);
|
||||
}
|
||||
else if (argument == "-c" || argument == "--clean") {
|
||||
a_clean = true;
|
||||
arguments.removeAt(i-1);
|
||||
}
|
||||
else if (argument == "-pc" || argument == "-cp") {
|
||||
a_mode = mayuMode::Ping;
|
||||
a_clean = true;
|
||||
arguments.removeAt(i-1);
|
||||
}
|
||||
else if (argument == "-rc" || argument == "-cr") {
|
||||
a_mode = mayuMode::Resolve;
|
||||
a_clean = true;
|
||||
arguments.removeAt(i-1);
|
||||
}
|
||||
}
|
||||
|
||||
if (arguments.length() >= 2) {
|
||||
mayu a_mayu(arguments.at(0), arguments.at(1));
|
||||
a_mayu.setMayuMode(a_mode);
|
||||
a_mayu.setCleanUp(a_clean);
|
||||
a_mayu.work();
|
||||
return a_mayu.getResult();
|
||||
}
|
||||
else {
|
||||
QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " input.txt" << " output.json" << endl;
|
||||
QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " [-c clean]" << " input.txt" << " output.json" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
41
mayu.cpp
41
mayu.cpp
|
@ -37,6 +37,7 @@ mayu::mayu(const QString &hostsFile, const QString &jsonFile, QObject *parent) :
|
|||
p_return = -1;
|
||||
p_timeout = 2.5;
|
||||
p_tries = 4;
|
||||
p_clean = false;
|
||||
p_mayuMode = mayuMode::Ping;
|
||||
if (!hostsFile.isEmpty())
|
||||
setHostsFile(hostsFile);
|
||||
|
@ -76,6 +77,11 @@ void mayu::setPingTries(int tries)
|
|||
p_tries = tries;
|
||||
}
|
||||
|
||||
void mayu::setCleanUp(bool clean)
|
||||
{
|
||||
p_clean = clean;
|
||||
}
|
||||
|
||||
mayuMode mayu::getMayuMode()
|
||||
{
|
||||
return p_mayuMode;
|
||||
|
@ -106,6 +112,11 @@ int mayu::getPingTries()
|
|||
return p_tries;
|
||||
}
|
||||
|
||||
bool mayu::getCleanUp()
|
||||
{
|
||||
return p_clean;
|
||||
}
|
||||
|
||||
int mayu::getResult()
|
||||
{
|
||||
return p_return;
|
||||
|
@ -184,7 +195,14 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
|||
char hostname[100];
|
||||
len = 100;
|
||||
ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len);
|
||||
QTextStream(stdout) << "Host: " << hostname << " Ping: " << latency << "ms" << " Status: " << (pingSuccess ? "true" : "false") << endl;
|
||||
QString latencyString;
|
||||
if (latency != -1) {
|
||||
latencyString = QString::number(latency) + "ms";
|
||||
}
|
||||
else {
|
||||
latencyString = QString::number(latency);
|
||||
}
|
||||
QTextStream(stdout) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl;
|
||||
#endif
|
||||
}
|
||||
if (pingSuccess) {
|
||||
|
@ -199,7 +217,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
|||
}
|
||||
#endif
|
||||
|
||||
const QList<mayuResult> mayu::resolve(const QString &host)
|
||||
const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
|
||||
{
|
||||
QList<mayuResult> resultList;
|
||||
QList<QHostAddress> hostAddresses = QHostInfo::fromName(host).addresses();
|
||||
|
@ -218,10 +236,12 @@ const QList<mayuResult> mayu::resolve(const QString &host)
|
|||
#ifdef E_DEBUG
|
||||
qDebug() << "Hostname" << host << "not found";
|
||||
#endif
|
||||
mayuResult m_result;
|
||||
m_result.host = host;
|
||||
m_result.result = "-1";
|
||||
resultList += m_result;
|
||||
if (!emptyWhenError) {
|
||||
mayuResult m_result;
|
||||
m_result.host = host;
|
||||
m_result.result = "-1";
|
||||
resultList += m_result;
|
||||
}
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
@ -326,7 +346,9 @@ void mayu::p_workPing()
|
|||
const QStringList hostsList = getHosts();
|
||||
for (const QString &host : hostsList) {
|
||||
double result = ping(host, p_tries, p_timeout);
|
||||
jsonObject[host] = result;
|
||||
if (!(result == -1 && p_clean)) {
|
||||
jsonObject[host] = result;
|
||||
}
|
||||
}
|
||||
p_saveWork(jsonObject);
|
||||
}
|
||||
|
@ -339,12 +361,13 @@ void mayu::p_workResolve()
|
|||
QJsonObject jsonObject;
|
||||
const QStringList hostsList = getHosts();
|
||||
for (const QString &host : hostsList) {
|
||||
const QList<mayuResult> resultList = resolve(host);
|
||||
const QList<mayuResult> resultList = resolve(host, p_clean);
|
||||
QJsonArray arrayList;
|
||||
for (const mayuResult &result : resultList) {
|
||||
arrayList += result.result;
|
||||
}
|
||||
jsonObject[host] = arrayList;
|
||||
if (!arrayList.isEmpty())
|
||||
jsonObject[host] = arrayList;
|
||||
}
|
||||
p_saveWork(jsonObject);
|
||||
}
|
||||
|
|
5
mayu.h
5
mayu.h
|
@ -42,17 +42,19 @@ public:
|
|||
void setJsonFile(const QString &fileName);
|
||||
void setPingTimeout(double timeout);
|
||||
void setPingTries(int tries);
|
||||
void setCleanUp(bool clean);
|
||||
mayuMode getMayuMode();
|
||||
const QString getHostsFile();
|
||||
const QStringList getHosts();
|
||||
const QString getJsonFile();
|
||||
double getPingTimeout();
|
||||
int getPingTries();
|
||||
bool getCleanUp();
|
||||
int getResult();
|
||||
#ifdef MAYU_UNIX
|
||||
static double ping(const QString &host, int tries, double timeout = 2.5);
|
||||
#endif
|
||||
static const QList<mayuResult> resolve(const QString &host);
|
||||
static const QList<mayuResult> resolve(const QString &host, bool emptyWhenError = false);
|
||||
|
||||
public slots:
|
||||
void parse_hosts();
|
||||
|
@ -76,6 +78,7 @@ private:
|
|||
double p_timeout;
|
||||
int p_return;
|
||||
int p_tries;
|
||||
bool p_clean;
|
||||
#ifdef PRIVILEGE_DROP_REQUIRED
|
||||
uid_t p_uid;
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue