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 <iostream>
14#include <quasarapp.h>
15#include <QCoreApplication>
16#include <QTimer>
17
18namespace Patronum {
19
21 d_ptr = new ControllerPrivate(this);
22}
23
25 delete d_ptr;
26}
27
28bool Controller::send(int argc, char **argv) {
29 if (!QuasarAppUtils::Params::size() && !QuasarAppUtils::Params::parseParams(argc, argv)) {
30 return false;
31 }
32
33 return send();
34}
35
37
38 bool printHelp = !QuasarAppUtils::Params::size() ||
39 QuasarAppUtils::Params::isEndable("h") ||
40 QuasarAppUtils::Params::isEndable("help");
41
42 if (printHelp) {
43 printDefaultHelp();
44 }
45
46 if (!d_ptr->connectToHost()) {
47 return false;
48 }
49
50 if (printHelp) {
51 if (!d_ptr->sendFeaturesRequest()) {
52 return false;
53 }
54 return true;
55 }
56
57 if (QuasarAppUtils::Params::isEndable("stop")) {
58 return d_ptr->stop();
59 }
60
61 if (QuasarAppUtils::Params::isEndable("resume")) {
62 return d_ptr->resume();
63 }
64
65 if (QuasarAppUtils::Params::isEndable("pause")) {
66 return d_ptr->pause();
67 }
68
69 QHash<QString, Feature> sendData = {};
70 auto userParams = QuasarAppUtils::Params::getUserParamsMap();
71 for (auto val = userParams.begin(); val != userParams.end(); ++val) {
72
73 bool fIgnore = val.key() == "verbose" || val.key() == "fileLog";
74
75 if (fIgnore) {
76 continue;
77 }
78
79 sendData.insert(val.key(), Feature{val.key(), val.value()});
80 }
81
82 if (!d_ptr->sendCmd(sendData)) {
83 return false;
84 }
85
86 QTimer::singleShot(1000, nullptr, []() {
88 QCoreApplication::exit(static_cast<int>(PatronumError::TimeOutError));
89 });
90
91 return true;
92}
93
95 if (!d_ptr)
96 return false;
97
98 d_ptr->setEcho(false);
99
100 if (!d_ptr->connectToHost(false)) {
101 return false;
102 }
103
104 _disableFinished = true;
105
106 return d_ptr->stop();
107}
108
109QuasarAppUtils::Help::Section Controller::help() const {
110 QuasarAppUtils::Help::Section help {
111 {QObject::tr("Options that available after start"), {
112 {"stop", QObject::tr("Stop a service")},
113 {"pause", QObject::tr("Pause a service")},
114 {"resume", QObject::tr("Resume a service")}
115
116 }
117 }
118 };
119
120 return help;
121}
122
124 qCritical() << errorToString(error);
125}
126
127void Controller::handleFeatures(const QList<Feature> &features) {
128
129 QuasarAppUtils::Help::Options options;
130
131 for (const auto& feature: features ) {
132 QString cmd, description;
133 if (feature.arg().isNull()) {
134 cmd = feature.cmd();
135 } else {
136 cmd = QString("-%0 value").arg(feature.cmd());
137 }
138
139 description = feature.description();
140
141 if (!feature.example().isEmpty()) {
142 description += ". Example : " + feature.example();
143 }
144
145 options.insert(cmd, description);
146 }
147
148 QuasarAppUtils::Help::print(options);
149}
150
151void Controller::handleResponce(const QVariantMap &responce) {
152 // raw responce
153 if (responce.size() == 1 && responce.firstKey().isEmpty()) {
154 std::cout << responce.first().toByteArray().toStdString();
155 return;
156 }
157
158 QuasarAppUtils::Help::Options options;
159
160 for(auto iter = responce.begin(); iter != responce.end(); ++iter) {
161 options.insert(iter.key(), iter.value().toString());
162 }
163
164 QuasarAppUtils::Help::print(options);
165}
166
168 if (!_disableFinished)
169 QCoreApplication::exit(0);
170}
171
172QList<Feature> Controller::features() {
173 return d_ptr->features();
174}
175
176void Controller::printDefaultHelp() const {
177
178 auto quasarappHelp = QuasarAppUtils::Params::getHelp();
179 QuasarAppUtils::Help::print(quasarappHelp.unite(help()));
180}
181
182}
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.