mirror of
https://github.com/QuasarApp/Patronum.git
synced 2025-04-26 15:44:32 +00:00
Merge pull request #21 from QuasarApp/hash_map
Using the hash instead, a set
This commit is contained in:
commit
8190026192
@ -29,7 +29,7 @@ public:
|
||||
* For each command will invoke the IService::handleReceive method.
|
||||
* @param data This is array of the incomming requests (commands)
|
||||
*/
|
||||
virtual void handleReceiveData(const QSet<Feature>& data) = 0;
|
||||
virtual void handleReceiveData(const QHash<QString, Feature>& data) = 0;
|
||||
|
||||
/**
|
||||
* @brief handleReceive This method invoked when service receive a request from terminal.
|
||||
@ -37,6 +37,8 @@ public:
|
||||
* @param data This is input data.
|
||||
* @return This method should be return true if the @a data command is supported and processed successful.
|
||||
* IF you return false then a negative message will be sent to a terminal app.
|
||||
* @see ServiceBase::handleReceiveData
|
||||
|
||||
*/
|
||||
virtual bool handleReceive(const Feature &data) = 0;
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <QDateTime>
|
||||
#include <QFileInfo>
|
||||
#include <QVariantMap>
|
||||
#include <iostream>
|
||||
#include <quasarapp.h>
|
||||
#include <QCoreApplication>
|
||||
#include <QTimer>
|
||||
@ -65,7 +66,7 @@ bool Controller::send() {
|
||||
return d_ptr->pause();
|
||||
}
|
||||
|
||||
QSet<Feature> sendData = {};
|
||||
QHash<QString, Feature> sendData = {};
|
||||
auto userParams = QuasarAppUtils::Params::getUserParamsMap();
|
||||
for (auto val = userParams.begin(); val != userParams.end(); ++val) {
|
||||
|
||||
@ -75,7 +76,7 @@ bool Controller::send() {
|
||||
continue;
|
||||
}
|
||||
|
||||
sendData.insert(Feature{val.key(), val.value()});
|
||||
sendData.insert(val.key(), Feature{val.key(), val.value()});
|
||||
}
|
||||
|
||||
if (!d_ptr->sendCmd(sendData)) {
|
||||
@ -149,6 +150,12 @@ void Controller::handleFeatures(const QList<Feature> &features) {
|
||||
}
|
||||
|
||||
void Controller::handleResponce(const QVariantMap &responce) {
|
||||
// raw responce
|
||||
if (responce.size() == 1 && responce.firstKey().isEmpty()) {
|
||||
std::cout << responce.first().toByteArray().toStdString();
|
||||
return;
|
||||
}
|
||||
|
||||
QuasarAppUtils::Help::Options options;
|
||||
|
||||
for(auto iter = responce.begin(); iter != responce.end(); ++iter) {
|
||||
|
@ -25,7 +25,6 @@ QString Feature::cmd() const {
|
||||
|
||||
void Feature::setCmd(const QString &cmd) {
|
||||
_cmd = cmd;
|
||||
_id = qHash(_cmd);
|
||||
}
|
||||
|
||||
QString Feature::arg() const {
|
||||
@ -59,10 +58,6 @@ QString Feature::toString() const {
|
||||
return _cmd;
|
||||
}
|
||||
|
||||
unsigned int Feature::id() const {
|
||||
return _id;
|
||||
}
|
||||
|
||||
QDataStream &operator<<(QDataStream &stream, const Feature &obj) {
|
||||
stream << obj._cmd << obj._arg;
|
||||
|
||||
@ -78,12 +73,11 @@ QDataStream &operator>>(QDataStream &stream, Feature &obj) {
|
||||
}
|
||||
|
||||
bool operator==(const Feature &left, const Feature &right) {
|
||||
return left.id() == right.id();
|
||||
return left.cmd() == right.cmd();
|
||||
}
|
||||
|
||||
|
||||
uint qHash(const Feature &feature) {
|
||||
return feature.id();
|
||||
return qHash(feature.cmd());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QList>
|
||||
#include <QHash>
|
||||
#include <Patronum_global.h>
|
||||
|
||||
namespace Patronum {
|
||||
@ -43,6 +43,11 @@ public:
|
||||
* @return Argument value.
|
||||
*/
|
||||
QString arg() const;
|
||||
|
||||
/**
|
||||
* @brief setArg This method sets arg value for this object.
|
||||
* @param arg This is a new value.
|
||||
*/
|
||||
void setArg(const QString &arg);
|
||||
|
||||
PATRONUM_LIBRARYSHARED_EXPORT friend QDataStream& operator<<(QDataStream& stream, const Feature& obj);
|
||||
@ -80,15 +85,8 @@ public:
|
||||
*/
|
||||
QString toString() const;
|
||||
|
||||
/**
|
||||
* @brief id This method retun id of the feature. The id is qHash from command string.
|
||||
* @return id of the feature.
|
||||
*/
|
||||
unsigned int id() const;
|
||||
|
||||
private:
|
||||
|
||||
unsigned int _id;
|
||||
QString _cmd;
|
||||
QString _description;
|
||||
QString _example;
|
||||
|
@ -47,7 +47,7 @@ ServiceBase::~ServiceBase() {
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceBase::handleReceiveData(const QSet<Feature> &data) {
|
||||
void ServiceBase::handleReceiveData(const QHash<QString, Feature> &data) {
|
||||
auto list = supportedFeatures();
|
||||
|
||||
QString commandList;
|
||||
@ -91,6 +91,10 @@ bool ServiceBase::sendResuylt(const QString &result) {
|
||||
return d_ptr->sendCmdResult({{"Result", result}});
|
||||
}
|
||||
|
||||
bool ServiceBase::sendRawResuylt(const QByteArray &result) {
|
||||
return d_ptr->sendCmdResult({{"", result}});
|
||||
}
|
||||
|
||||
void ServiceBase::onStop() {
|
||||
sendResuylt("Success! Use default stop function");
|
||||
|
||||
|
@ -46,8 +46,10 @@ protected:
|
||||
* @brief handleReceiveData - This method invoice when service receive new command from terminal of controller of this service.
|
||||
* @param data - Is list of commands from controller.
|
||||
* Default implementation send message about error, and invoke the.
|
||||
* @note If you override this method the ServiceBase::handleReceive method wil not invoked wuthou base implementation of thism method.
|
||||
* @see ServiceBase::handleReceive
|
||||
*/
|
||||
void handleReceiveData(const QSet<Feature> &data) override final;
|
||||
void handleReceiveData(const QHash<QString, Feature> &data) override;
|
||||
|
||||
/**
|
||||
* @brief supportedFeatures
|
||||
@ -69,6 +71,13 @@ protected:
|
||||
*/
|
||||
bool sendResuylt(const QString &result);
|
||||
|
||||
/**
|
||||
* @brief sendRawResuylt This method send raw text responce to controller.
|
||||
* @param result - Message.
|
||||
* @return True if data sendet is seccusseful.
|
||||
*/
|
||||
bool sendRawResuylt(const QByteArray &result);
|
||||
|
||||
/**
|
||||
* @brief createApplication Default implementation create a Application object and parse arguments.
|
||||
*/
|
||||
|
@ -48,7 +48,7 @@ bool ControllerPrivate::sendFeaturesRequest() {
|
||||
return _socket->send(_parser->createPackage(Command::FeaturesRequest));
|
||||
}
|
||||
|
||||
bool ControllerPrivate::sendCmd(const QSet<Feature> &result) {
|
||||
bool ControllerPrivate::sendCmd(const QHash<QString, Feature> &result) {
|
||||
if (!_socket->isValid()) {
|
||||
QuasarAppUtils::Params::log("scoket is closed!",
|
||||
QuasarAppUtils::Debug);
|
||||
@ -64,16 +64,20 @@ bool ControllerPrivate::sendCmd(const QSet<Feature> &result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ControllerPrivate::sendCmd(const QString &cmd) {
|
||||
return sendCmd({{cmd, Feature(cmd)}});
|
||||
}
|
||||
|
||||
bool ControllerPrivate::stop() {
|
||||
return sendCmd({Feature("stop")});
|
||||
return sendCmd("stop");
|
||||
}
|
||||
|
||||
bool ControllerPrivate::pause() {
|
||||
return sendCmd({Feature("pause")});
|
||||
return sendCmd("pause");
|
||||
}
|
||||
|
||||
bool ControllerPrivate::resume() {
|
||||
return sendCmd({Feature("resume")});
|
||||
return sendCmd("resume");
|
||||
}
|
||||
|
||||
QList<Feature> ControllerPrivate::features() const {
|
||||
|
@ -22,7 +22,8 @@ public:
|
||||
ControllerPrivate(IController* controller = nullptr, QObject *parent = nullptr);
|
||||
~ControllerPrivate();
|
||||
bool sendFeaturesRequest();
|
||||
bool sendCmd(const QSet<Feature> &result);
|
||||
bool sendCmd(const QHash<QString, Feature> &result);
|
||||
bool sendCmd(const QString &cmd);
|
||||
|
||||
bool stop();
|
||||
bool pause();
|
||||
|
@ -156,7 +156,7 @@ bool ServicePrivate::startDeamon() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ServicePrivate::handleStandartCmd(QSet<Feature> *cmds) {
|
||||
bool ServicePrivate::handleStandartCmd(QHash<QString, Feature> *cmds) {
|
||||
|
||||
if (!cmds)
|
||||
return false;
|
||||
@ -164,17 +164,17 @@ bool ServicePrivate::handleStandartCmd(QSet<Feature> *cmds) {
|
||||
if (!_service)
|
||||
return false;
|
||||
|
||||
if (cmds->contains(Feature{"stop"})) {
|
||||
if (cmds->contains("stop")) {
|
||||
_service->onStop();
|
||||
cmds->remove(Feature{"stop"});
|
||||
cmds->remove("stop");
|
||||
|
||||
} else if (cmds->contains(Feature{"pause"})) {
|
||||
} else if (cmds->contains("pause")) {
|
||||
_service->onPause();
|
||||
cmds->remove(Feature{"pause"});
|
||||
cmds->remove("pause");
|
||||
|
||||
} else if (cmds->contains(Feature{"resume"})) {
|
||||
} else if (cmds->contains("resume")) {
|
||||
_service->onResume();
|
||||
cmds->remove(Feature{"resume"});
|
||||
cmds->remove("resume");
|
||||
|
||||
}
|
||||
|
||||
@ -239,7 +239,7 @@ void ServicePrivate::handleReceve(QByteArray data) {
|
||||
|
||||
QDataStream stream(pkg.data());
|
||||
|
||||
QSet<Feature> feature;
|
||||
QHash<QString, Feature> feature;
|
||||
stream >> feature;
|
||||
handleStandartCmd(&feature);
|
||||
|
||||
|
@ -88,7 +88,7 @@ private:
|
||||
* @param cmds
|
||||
* @return
|
||||
*/
|
||||
bool handleStandartCmd(QSet<Feature> *cmds);
|
||||
bool handleStandartCmd(QHash<QString, Feature> *cmds);
|
||||
|
||||
/**
|
||||
* @brief getServiceLauncher This method return path to launcher file.
|
||||
|
Loading…
x
Reference in New Issue
Block a user