namespace for IPRefresher instead of class
This commit is contained in:
		@@ -11,26 +11,28 @@
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
 | 
			
		||||
using namespace IPRefresher;
 | 
			
		||||
 | 
			
		||||
bool IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    FileLogger logger;
 | 
			
		||||
    testspace::testi5();
 | 
			
		||||
 | 
			
		||||
    IPAPI ipapi;
 | 
			
		||||
    std::string ip = ipapi.getGlobalIp();
 | 
			
		||||
 | 
			
		||||
    if (ip.empty()) {
 | 
			
		||||
        //no internet connection (or other error)
 | 
			
		||||
        Logger::warning("no internet connection");
 | 
			
		||||
        return IPRefresher_Status_Code::ERROR_NO_INTERNET;
 | 
			
		||||
        return Status_Code::ERROR_NO_INTERNET;
 | 
			
		||||
    } else if (!IpHelper::isIpValid(ip)) {
 | 
			
		||||
        // error when ip doesn't contain a :
 | 
			
		||||
        Logger::warning("an error occured when getting the global ip");
 | 
			
		||||
        return IPRefresher_Status_Code::ERROR;
 | 
			
		||||
        return Status_Code::ERROR;
 | 
			
		||||
    } else {
 | 
			
		||||
        std::string oldip = logger.readip();
 | 
			
		||||
 | 
			
		||||
        if (oldip == ip && !force) {
 | 
			
		||||
            Logger::message("no change -- ip: " + ip);
 | 
			
		||||
            return IPRefresher_Status_Code::NOREFRESH;
 | 
			
		||||
            return Status_Code::NOREFRESH;
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
 | 
			
		||||
 | 
			
		||||
@@ -46,27 +48,26 @@ bool IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
            } else if (!result) {
 | 
			
		||||
                //error
 | 
			
		||||
                Logger::error("failed to write ip to dynu api!");
 | 
			
		||||
                return IPRefresher_Status_Code::ERROR;
 | 
			
		||||
                return Status_Code::ERROR;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            logger.safeip(ip);
 | 
			
		||||
            return result ? IPRefresher_Status_Code::SUCCESS : IPRefresher_Status_Code::ERROR;
 | 
			
		||||
            return result ? Status_Code::SUCCESS : Status_Code::ERROR;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IPRefresher::IPRefresher(bool loop) {
 | 
			
		||||
    if (loop) {
 | 
			
		||||
        Logger::message("startup of service");
 | 
			
		||||
        Logger::message("Version: " + Version::VERSION);
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
            while (true) {
 | 
			
		||||
                Logger::message("starting check");
 | 
			
		||||
                checkIPAdress(false);
 | 
			
		||||
                std::this_thread::sleep_for(std::chrono::milliseconds(300000));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
void IPRefresher::startUpService(int interval) {
 | 
			
		||||
    Logger::message("startup of service");
 | 
			
		||||
    Logger::message("Version: " + Version::VERSION);
 | 
			
		||||
    if (Config::readConfig()) {
 | 
			
		||||
        while (true) {
 | 
			
		||||
            Logger::message("starting check");
 | 
			
		||||
            checkIPAdress(false);
 | 
			
		||||
            std::this_thread::sleep_for(std::chrono::milliseconds(interval * 1000));
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,10 @@
 | 
			
		||||
 | 
			
		||||
#include <thread>
 | 
			
		||||
 | 
			
		||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
 | 
			
		||||
MainWindow::MainWindow() : QMainWindow(), ui(new Ui::MainWindow) {
 | 
			
		||||
    ui->setupUi(this);
 | 
			
		||||
 | 
			
		||||
    // needs to be defined out of scope -- would be termintated after this constructor.
 | 
			
		||||
    // needs to be defined with new -- would be termintated after this constructor.
 | 
			
		||||
    new std::thread([this]() {
 | 
			
		||||
        IPAPI ipapi;
 | 
			
		||||
        std::string ip = ipapi.getGlobalIp();
 | 
			
		||||
@@ -20,11 +20,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
 | 
			
		||||
        this->ui->labelCurrentIP->setText(msg.c_str());
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if (Config::validateConfig())
 | 
			
		||||
        ui->labelConfig->setText("Config is: OK");
 | 
			
		||||
    else
 | 
			
		||||
        ui->labelConfig->setText("Config is: NOT OK");
 | 
			
		||||
    // set config info label and initial check if config is valid
 | 
			
		||||
    ui->labelConfig->setText(Config::validateConfig() ? "Config is: OK" : "Config is: NOT OK");
 | 
			
		||||
 | 
			
		||||
    connect(ui->buttonCheckConfig, SIGNAL(clicked()), this, SLOT(checkConfigBtn()));
 | 
			
		||||
    connect(ui->buttonRefreshIP, SIGNAL(clicked()), this, SLOT(refreshIPBtn()));
 | 
			
		||||
@@ -33,6 +30,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MainWindow::~MainWindow() {
 | 
			
		||||
    // todo check if disconnects are really necessary
 | 
			
		||||
    disconnect(ui->buttonCheckConfig);
 | 
			
		||||
    disconnect(ui->buttonRefreshIP);
 | 
			
		||||
    delete ui;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -56,20 +56,19 @@ void MainWindow::refreshIPBtn() {
 | 
			
		||||
    appendLogField("");
 | 
			
		||||
    appendLogField("start refreshing Dynu IP.");
 | 
			
		||||
    new std::thread([this]() {
 | 
			
		||||
        IPRefresher ipr;
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
            int code = ipr.checkIPAdress(false);
 | 
			
		||||
            int code = IPRefresher::checkIPAdress(false);
 | 
			
		||||
            switch (code) {
 | 
			
		||||
                case IPRefresher_Status_Code::SUCCESS:
 | 
			
		||||
                case IPRefresher::Status_Code::SUCCESS:
 | 
			
		||||
                    appendLogField("successfully refreshed IP!");
 | 
			
		||||
                    break;
 | 
			
		||||
                case IPRefresher_Status_Code::NOREFRESH:
 | 
			
		||||
                case IPRefresher::Status_Code::NOREFRESH:
 | 
			
		||||
                    appendLogField("IP is already correct.");
 | 
			
		||||
                    break;
 | 
			
		||||
                case IPRefresher_Status_Code::ERROR_NO_INTERNET:
 | 
			
		||||
                case IPRefresher::Status_Code::ERROR_NO_INTERNET:
 | 
			
		||||
                    appendLogField("Error: No Internet connection");
 | 
			
		||||
                    break;
 | 
			
		||||
                case IPRefresher_Status_Code::ERROR:
 | 
			
		||||
                case IPRefresher::Status_Code::ERROR:
 | 
			
		||||
                    appendLogField("An error occured while refreshing.");
 | 
			
		||||
                    break;
 | 
			
		||||
                default:
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,9 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 09.05.20.
 | 
			
		||||
//
 | 
			
		||||
/**
 | 
			
		||||
 * Main GUI controller - User IO handlings
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lukas Heiligenbrunner
 | 
			
		||||
 * @date 09.05.2020
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
@@ -14,19 +17,34 @@ class MainWindow : public QMainWindow {
 | 
			
		||||
Q_OBJECT
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit MainWindow(QWidget *parent = nullptr);
 | 
			
		||||
    /**
 | 
			
		||||
     * constructor with basic initializations
 | 
			
		||||
     */
 | 
			
		||||
    explicit MainWindow();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * destruct all gui elements
 | 
			
		||||
     */
 | 
			
		||||
    ~MainWindow();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Ui::MainWindow *ui;
 | 
			
		||||
private slots:
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * executed click handler for config button
 | 
			
		||||
     */
 | 
			
		||||
    void checkConfigBtn();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * executed click handler for refresh btn
 | 
			
		||||
     */
 | 
			
		||||
    void refreshIPBtn();
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * append a String line to the Log field
 | 
			
		||||
     *
 | 
			
		||||
     * @param QString string to be appended
 | 
			
		||||
     */
 | 
			
		||||
    void appendLogField(QString);
 | 
			
		||||
};
 | 
			
		||||
@@ -21,15 +21,14 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
        } else if (firstarg == "-v" || firstarg == "--version") {
 | 
			
		||||
            std::cout << "Version " << Version::VERSION << std::endl;
 | 
			
		||||
        } else if (firstarg == "-f" || firstarg == "--force") {
 | 
			
		||||
            IPRefresher ipr;
 | 
			
		||||
            if (Config::readConfig()) {
 | 
			
		||||
                ipr.checkIPAdress(true);
 | 
			
		||||
                IPRefresher::checkIPAdress(true);
 | 
			
		||||
            } else {
 | 
			
		||||
                std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else if (firstarg == "-l" || firstarg == "--loop") {
 | 
			
		||||
            IPRefresher(true);
 | 
			
		||||
            IPRefresher::startUpService(true);
 | 
			
		||||
        } else if (firstarg == "-c" || firstarg == "--checkconfig") {
 | 
			
		||||
            if (Config::validateConfig()) {
 | 
			
		||||
                Logger::message("Config file is OK");
 | 
			
		||||
@@ -44,10 +43,9 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
            Logger::message("wrong arguments!  -h for help");
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        IPRefresher ipr;
 | 
			
		||||
        Logger::message("starting check");
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
            ipr.checkIPAdress(false);
 | 
			
		||||
            IPRefresher::checkIPAdress(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,13 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 09.05.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <QApplication>
 | 
			
		||||
#include "gui/MainWindow.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * application entry point
 | 
			
		||||
 */
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
    QApplication a(argc, argv);
 | 
			
		||||
    MainWindow w;
 | 
			
		||||
    w.setWindowTitle("startUpService");
 | 
			
		||||
    w.show();
 | 
			
		||||
 | 
			
		||||
    return QApplication::exec();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user