Patronum
Loading...
Searching...
No Matches
PController.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2025 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "PController.h"
9#include "controllerprivate.h"
10#include <QDateTime>
11#include <QFileInfo>
12#include <QVariantMap>
13#include <quasarapp.h>
14#include <QCoreApplication>
15#include <QTimer>
16
17namespace Patronum {
18
20 d_ptr = new ControllerPrivate(this);
21}
22
24 delete d_ptr;
25}
26
27bool Controller::send(int argc, char **argv) {
28 if (!QuasarAppUtils::Params::size() && !QuasarAppUtils::Params::parseParams(argc, argv)) {
29 return false;
30 }
31
32 return send();
33}
34
36
37 bool printHelp = !QuasarAppUtils::Params::size() ||
38 QuasarAppUtils::Params::isEndable("h") ||
39 QuasarAppUtils::Params::isEndable("help");
40
41 if (printHelp) {
42 printDefaultHelp();
43 }
44
45 if (!d_ptr->connectToHost()) {
46 return false;
47 }
48
49 if (printHelp) {
50 if (!d_ptr->sendFeaturesRequest()) {
51 return false;
52 }
53 return true;
54 }
55
56 if (QuasarAppUtils::Params::isEndable("stop")) {
57 return d_ptr->stop();
58 }
59
60 if (QuasarAppUtils::Params::isEndable("resume")) {
61 return d_ptr->resume();
62 }
63
64 if (QuasarAppUtils::Params::isEndable("pause")) {
65 return d_ptr->pause();
66 }
67
68 QSet<Feature> sendData = {};
69 auto userParams = QuasarAppUtils::Params::getUserParamsMap();
70 for (auto val = userParams.begin(); val != userParams.end(); ++val) {
71
72 bool fIgnore = val.key() == "verbose" || val.key() == "fileLog";
73
74 if (fIgnore) {
75 continue;
76 }
77
78 sendData.insert(Feature{val.key(), val.value()});
79 }
80
81 if (!d_ptr->sendCmd(sendData)) {
82 return false;
83 }
84
85 QTimer::singleShot(1000, nullptr, []() {
87 QCoreApplication::exit(static_cast<int>(PatronumError::TimeOutError));
88 });
89
90 return true;
91}
92
94 if (!d_ptr)
95 return false;
96
97 d_ptr->setEcho(false);
98
99 if (!d_ptr->connectToHost(false)) {
100 return false;
101 }
102
103 _disableFinished = true;
104
105 return d_ptr->stop();
106}
107
108QuasarAppUtils::Help::Section Controller::help() const {
109 QuasarAppUtils::Help::Section help {
110 {QObject::tr("Options that available after start"), {
111 {"stop", QObject::tr("Stop a service")},
112 {"pause", QObject::tr("Pause a service")},
113 {"resume", QObject::tr("Resume a service")}
114
115 }
116 }
117 };
118
119 return help;
120}
121
123 qCritical() << errorToString(error);
124}
125
126void Controller::handleFeatures(const QList<Feature> &features) {
127
128 QuasarAppUtils::Help::Options options;
129
130 for (const auto& feature: features ) {
131 QString cmd, description;
132 if (feature.arg().isNull()) {
133 cmd = feature.cmd();
134 } else {
135 cmd = QString("-%0 value").arg(feature.cmd());
136 }
137
138 description = feature.description();
139
140 if (!feature.example().isEmpty()) {
141 description += ". Example : " + feature.example();
142 }
143
144 options.insert(cmd, description);
145 }
146
147 QuasarAppUtils::Help::print(options);
148}
149
150void Controller::handleResponce(const QVariantMap &responce) {
151 QuasarAppUtils::Help::Options options;
152
153 for(auto iter = responce.begin(); iter != responce.end(); ++iter) {
154 options.insert(iter.key(), iter.value().toString());
155 }
156
157 QuasarAppUtils::Help::print(options);
158}
159
161 if (!_disableFinished)
162 QCoreApplication::exit(0);
163}
164
165QList<Feature> Controller::features() {
166 return d_ptr->features();
167}
168
169void Controller::printDefaultHelp() const {
170
171 auto quasarappHelp = QuasarAppUtils::Params::getHelp();
172 QuasarAppUtils::Help::print(quasarappHelp.unite(help()));
173}
174
175}
void finished() override
finished This method invoked when controler receive from service the Command::CloseConnection command...
void handleError(PatronumError error) override
handleError - override this method if you want track errors the default implementation print error me...
void handleFeatures(const QList< Feature > &features) override
handleFeatures - Override this method if you want create a custom reaction of get service features....
QuasarAppUtils::Help::Section help() const
help This method return help of the Controller.
bool send()
send - This method send request to service.
bool sendStop()
sendStop This method send stop command to service;
Controller()
Controller This is base constructor of the controller.
QList< Feature > features()
features - This method return current features of connected service.
void handleResponce(const QVariantMap &responce) override
handleResponce - Override this method if you want create a custom reaction of get responce from servi...
The Feature class it is atomic type for describe service command.
Definition PFeature.h:22
The Patronum namespace - It is main name space of Patronum Library. The Patronum library support the ...
QString errorToString(PatronumError error)
errorToString This method convert the PatronumError to QString.
PatronumError
The PatronumError enum - controller work error codes.
@ TimeOutError
Timeout error. service unavailable or not started.