added finished method for controller

This commit is contained in:
Andrei Yankovich 2021-04-28 18:38:39 +03:00
parent d1520b6215
commit de2aed2301
10 changed files with 43 additions and 41 deletions

View File

@ -73,6 +73,11 @@ public:
*/
virtual void handleError(ControllerError errorCode) = 0;
/**
* @brief finished This method ivoked when controler received Command::CloseConnection from the server.
*/
virtual void finished() = 0;
};
}

View File

@ -100,11 +100,6 @@ int Controller::startDetached() const {
return d_ptr->start();
}
bool Controller::waitForResponce(int msec) {
return d_ptr->waitForResponce(msec);
}
QuasarAppUtils::Help::Section Controller::help() const {
QuasarAppUtils::Help::Section help {
{QObject::tr("Options that available after start"), {
@ -154,7 +149,6 @@ void Controller::handleFeatures(const QList<Feature> &features) {
}
QuasarAppUtils::Help::print(options);
QCoreApplication::exit(0);
}
void Controller::handleResponce(const QVariantMap &responce) {
@ -165,6 +159,9 @@ void Controller::handleResponce(const QVariantMap &responce) {
}
QuasarAppUtils::Help::print(options);
}
void Controller::finished() {
QCoreApplication::exit(0);
}

View File

@ -57,13 +57,6 @@ public:
*/
int startDetached() const;
/**
* @brief waitForResponce - Wait for get a responce from servece.
* @param msec Timeout in msec.
* @return true if all seccussful.
*/
bool waitForResponce(int msec = 10000);
/**
* @brief help This method return help of the Controller.
* @return Available otions list.
@ -94,6 +87,13 @@ protected:
*/
void handleResponce(const QVariantMap &responce) override;
/**
* @brief finished This method invoked when controler receive from service the Command::CloseConnection command
* This implementation invoke he exit method of the QCoreApplication and finished application.
* If do not want to stop application after receive Command::CloseConnection then override this method.
*/
void finished() override;
/**
* @brief features - This method return current features of connected service.
* @note If Respond from service not received then return empty list.
@ -106,8 +106,6 @@ private:
ControllerPrivate *d_ptr = nullptr;
void printDefaultHelp() const;
};
}
#endif // CONTROLLER_H

View File

@ -64,7 +64,6 @@ bool ControllerPrivate::sendCmd(const QSet<Feature> &result) {
}
if (_socket->send(_parser->createPackage(Command::Feature, result))) {
_responce = false;
return true;
}
@ -135,24 +134,6 @@ bool ControllerPrivate::resume() {
return sendCmd({Feature("resume")});
}
bool Patronum::ControllerPrivate::waitForResponce(int msec) {
if (!dynamic_cast<QCoreApplication*>(QCoreApplication::instance())) {
QuasarAppUtils::Params::log("Before run the waitForResponce method you need run a exec method of your QApplication class.",
QuasarAppUtils::Warning);
return false;
}
qint64 waitFor = QDateTime::currentMSecsSinceEpoch() + msec;
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
QCoreApplication::processEvents();
}
QCoreApplication::processEvents();
return _responce;
}
QList<Feature> ControllerPrivate::features() const {
return _features;
}
@ -219,7 +200,7 @@ void ControllerPrivate::handleReceve(QByteArray data) {
}
case Command::CloseConnection: {
_responce = true;
_controller->finished();
break;
}

View File

@ -33,8 +33,6 @@ public:
bool pause();
bool resume();
bool waitForResponce(int msec);
QList<Feature> features() const;
bool isConnected() const;
@ -54,7 +52,6 @@ private:
LocalSocket *_socket = nullptr;
IController *_controller = nullptr;
bool _responce = false;
QList<Feature> _features;
QString _serviceExe = "";
Installer *_installer = nullptr;

View File

@ -9,6 +9,14 @@ QVariantMap DefaultController::getResponce() {
return _receiveData;
}
bool DefaultController::isFinished() const {
return _finished;
}
void DefaultController::handleResponce(const QVariantMap &feature) {
_receiveData = feature;
}
void DefaultController::finished() {
_finished = true;
}

View File

@ -7,13 +7,15 @@ class DefaultController : public Patronum::Controller
public:
DefaultController();
QVariantMap getResponce();
bool isFinished() const;
protected:
void handleResponce(const QVariantMap &feature);
void finished();
private:
QVariantMap _receiveData;
bool _finished = false;
};

View File

@ -18,3 +18,12 @@ bool TestUtils::wait(const bool &forWait, int msec) {
return forWait;
}
bool TestUtils::wait(std::function<bool ()> forWait, int msec) {
auto curmsec = QDateTime::currentMSecsSinceEpoch() + msec;
while (curmsec > QDateTime::currentMSecsSinceEpoch() && !forWait()) {
QCoreApplication::processEvents();
}
QCoreApplication::processEvents();
return forWait();
}

View File

@ -8,6 +8,7 @@ class TestUtils
public:
TestUtils();
static bool wait(const bool &forWait, int msec);
static bool wait(std::function<bool ()> forWait, int msec);
};

View File

@ -39,6 +39,8 @@ testPatronum::testPatronum() {
}
void testPatronum::testPing() {
TestUtils utils;
const char* arg[] = {
"/",
"ping"
@ -46,11 +48,13 @@ void testPatronum::testPing() {
DefaultController cli;
QuasarAppUtils::Params::clearParsedData();
QVERIFY(cli.send(2 , const_cast<char**>(arg)));
QVERIFY(cli.waitForResponce(1000));
QVERIFY(utils.wait([&cli](){return cli.isFinished();}, 1000));
QVERIFY(cli.getResponce().value("Result") == "pong");
}
void testPatronum::testRandomCommad() {
TestUtils utils;
const char* arg[] = {
"/",
"fd"
@ -59,7 +63,7 @@ void testPatronum::testRandomCommad() {
QuasarAppUtils::Params::clearParsedData();
QVERIFY(cli.send(2 , const_cast<char**>(arg)));
QVERIFY(cli.waitForResponce(1000));
QVERIFY(utils.wait([&cli](){return cli.isFinished();}, 1000));
QVERIFY(cli.getResponce().contains("Error"));
}