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