1
0
Fork 0
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:
Syping 2018-05-15 08:46:11 +02:00
parent 16d5d83eec
commit d5663e583b
3 changed files with 53 additions and 11 deletions

View file

@ -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;

View file

@ -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,11 +236,13 @@ const QList<mayuResult> mayu::resolve(const QString &host)
#ifdef E_DEBUG
qDebug() << "Hostname" << host << "not found";
#endif
if (!emptyWhenError) {
mayuResult m_result;
m_result.host = host;
m_result.result = "-1";
resultList += m_result;
}
}
return resultList;
}
@ -326,8 +346,10 @@ void mayu::p_workPing()
const QStringList hostsList = getHosts();
for (const QString &host : hostsList) {
double result = ping(host, p_tries, p_timeout);
if (!(result == -1 && p_clean)) {
jsonObject[host] = result;
}
}
p_saveWork(jsonObject);
}
#endif
@ -339,11 +361,12 @@ 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;
}
if (!arrayList.isEmpty())
jsonObject[host] = arrayList;
}
p_saveWork(jsonObject);

5
mayu.h
View file

@ -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