4
0
mirror of https://github.com/QuasarApp/Patronum.git synced 2025-05-09 21:49:35 +00:00

first work version

This commit is contained in:
Andrei Yankovich 2020-04-18 21:29:33 +03:00
parent f5efdb9a50
commit d1761f868f
14 changed files with 274 additions and 21 deletions

3
.gitmodules vendored

@ -1,3 +1,6 @@
[submodule "Patronum/qt-solutions"]
path = Patronum/qt-solutions
url = https://github.com/QuasarApp/qt-solutions.git
[submodule "Patronum/QuasarAppLib"]
path = Patronum/QuasarAppLib
url = https://github.com/QuasarApp/QuasarAppLib.git

@ -1,5 +1,5 @@
#
# Copyright (C) 2018-2019 QuasarApp.
# Copyright (C) 2018-2020 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.

@ -9,6 +9,7 @@ cmake_minimum_required(VERSION 3.1)
project(Patronum LANGUAGES CXX)
add_subdirectory(qt-solutions/qtservice)
add_subdirectory(QuasarAppLib)
include(qt-solutions/CMake/ProjectOut.cmake)
include(qt-solutions/CMake/Version.cmake)
@ -29,7 +30,7 @@ file(GLOB SOURCE_CPP
)
add_library(${PROJECT_NAME} SHARED ${SOURCE_CPP})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt-Service)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt-Service QuasarApp)
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
setVersion(0 1 0)

1
Patronum/QuasarAppLib Submodule

@ -0,0 +1 @@
Subproject commit 7070d77012d8305363eb20a46f59a0837cfa6651

@ -1,5 +1,9 @@
#include "controller.h"
#include "serviceprivate.h"
#include <QDateTime>
#include <QVariantMap>
#include <quasarapp.h>
namespace Patronum {
Controller::Controller(const QString &name):
@ -7,8 +11,79 @@ Controller::Controller(const QString &name):
d_ptr = new ServicePrivate(name, this);
}
bool Controller::send(int argc, char **argv) {
if (!QuasarAppUtils::Params::parseParams(argc, argv)) {
return false;
}
if (!QuasarAppUtils::Params::customParamasSize() ||
QuasarAppUtils::Params::isEndable("h") ||
QuasarAppUtils::Params::isEndable("help")) {
if (!d_ptr->sendFeaturesRequest()) {
return false;
}
return true;
}
QList<Feature> sendData = {};
auto userParams = QuasarAppUtils::Params::getUserParamsMap();
for (auto val = userParams.begin(); val != userParams.end(); ++val) {
sendData += {val.key(), val.value()};
}
return d_ptr->sendCmd(sendData);
}
bool Controller::waitForResponce(int msec) {
_responce = false;
qint64 waitFor = QDateTime::currentMSecsSinceEpoch() + msec;
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
QCoreApplication::processEvents();
}
return _responce;
}
void Controller::handleFeatures(const QList<Feature> &features) {
QuasarAppUtils::Help::Options options;
for (const auto& feature: features ) {
QString cmd, description;
if (feature.arg().isNull()) {
cmd = feature.cmd();
} else {
cmd = QString("-%0 value").arg(feature.cmd());
}
description = feature.description();
if (!feature.example().isEmpty()) {
description += ". Example : " + feature.example();
}
options.insert(cmd, description);
}
_features = features;
QuasarAppUtils::Help::print(options);
_responce = true;
}
void Controller::handleResponce(const QVariantMap &responce) {
QuasarAppUtils::Help::Options options;
for(auto iter = responce.begin(); iter != responce.end(); ++iter) {
options.insert(iter.key(), iter.value().toString());
}
QuasarAppUtils::Help::print(options);
}
}

@ -8,17 +8,57 @@ namespace Patronum {
class ServicePrivate;
/**
* @brief The Controller class provide control functionality for your service
* how to use :
* just inherit from the Service Controller and override the methods you need.
* So, invoke the send methon, and if you need to get a responce from your service then invoke a waitForResponce method.
*/
class PATRONUM_LIBRARYSHARED_EXPORT Controller : public QtServiceController, protected IController
{
public:
/**
* @brief Controller
* @param name - name of you service
*/
Controller(const QString& name);
/**
* @brief send - this method send request to service
* @param argc - count of arguments
* @param argv - arguments list
* @return true if all sendet seccussful
*/
bool send(int argc, char **argv);
/**
* @brief waitForResponce - waut for get a responce from servece
* @param msec timeout
* @return true if all seccussful
*/
bool waitForResponce(int msec = 10000);
// IControler interface
protected:
/**
* @brief handleFeatures - override this method if you want cerate a custom reaction of get service features
* default inplenebtation prin help of available command of your service
* @param features - list of features
*/
void handleFeatures(const QList<Feature> &features);
/**
* @brief handleResponce - override this method if you want create a custom reaction of get responce from service
* Default inplementation print responce to console.
* @param feature - responce from service
*/
void handleResponce(const QVariantMap &feature);
private:
ServicePrivate * d_ptr = nullptr;
ServicePrivate *d_ptr = nullptr;
QList<Feature> _features;
bool _responce = false;
};
}

@ -4,9 +4,12 @@
namespace Patronum {
Feature::Feature(const QString &cmd, const QVariantList &arg) {
Feature::Feature(const QString &cmd, const QVariant &arg,
const QString &description, const QString &example) {
_cmd = cmd;
_arg = arg;
_description = description;
_example = example;
}
QString Feature::cmd() const {
@ -17,7 +20,7 @@ void Feature::setCmd(const QString &cmd) {
_cmd = cmd;
}
QVariantList Feature::arg() const {
QVariant Feature::arg() const {
return _arg;
}
@ -25,6 +28,22 @@ void Feature::setArg(const QVariantList &arg) {
_arg = arg;
}
QString Feature::description() const {
return _description;
}
void Feature::setDescription(const QString &description) {
_description = description;
}
QString Feature::example() const {
return _example;
}
void Feature::setExample(const QString &example) {
_example = example;
}
QDataStream &operator<<(QDataStream &stream, const Feature &obj) {
stream << obj._cmd << obj._arg;

@ -2,7 +2,7 @@
#define FEaTURE_H
#include <QString>
#include <QVariantList>
#include <QVariant>
#include <QList>
#include <Patronum_global.h>
@ -16,19 +16,28 @@ class PATRONUM_LIBRARYSHARED_EXPORT Feature
{
public:
Feature() = default;
Feature(const QString& cmd, const QVariantList& arg);
Feature(const QString& cmd, const QVariant& arg = {},
const QString& description = "", const QString& example = "");
QString cmd() const;
void setCmd(const QString &cmd);
QVariantList arg() const;
QVariant arg() const;
void setArg(const QVariantList &arg);
friend QDataStream& operator<<(QDataStream& stream, const Feature& obj);
friend QDataStream& operator>>(QDataStream& stream, Feature& obj);
QString description() const;
void setDescription(const QString &description);
QString example() const;
void setExample(const QString &example);
private:
QString _cmd;
QVariantList _arg;
QString _description;
QString _example;
QVariant _arg;
};

@ -2,7 +2,7 @@
#define ICONTROLER_H
#include <QList>
#include <QVariantMap>
namespace Patronum {
@ -13,6 +13,8 @@ class IController
public:
IController();
virtual void handleFeatures(const QList<Feature>& features) = 0;
virtual void handleResponce(const QVariantMap& feature) = 0;
};
}

@ -12,7 +12,7 @@ class IService
public:
IService();
virtual void handleReceve(const Feature& data) = 0;
virtual void handleReceve(const QList<Feature>& data) = 0;
virtual QList<Feature> supportedFeatures() = 0;
};

@ -8,7 +8,8 @@ namespace Patronum {
enum class Command: char {
FeaturesRequest,
Features,
Feature
Feature,
FeatureResponce
};
struct Package

@ -10,10 +10,21 @@
namespace Patronum {
/**
* Service -
* How to use
* just inherit from the Service class and override the methods you need.
*/
template<class Application>
class PATRONUM_LIBRARYSHARED_EXPORT Service : public QtService<Application>, protected IService
{
public:
/**
* @brief Service
* @param argc - count params
* @param argv - test of arguments
* @param name - name of your service
*/
Service(int argc, char **argv, const QString &name)
: QtService<Application>(argc, argv, name) {
d_ptr = new ServicePrivate(name, nullptr, this);
@ -21,14 +32,31 @@ public:
}
// IService interface
protected:
void handleReceve(const Feature &data) {
/**
* @brief handleReceve - this method invoce when service receive new command from terminal of controller of this service
* @param data - is list of commands from controller
*/
void handleReceve(const QList<Feature> &data) {
Q_UNUSED(data)
};
/**
* @brief supportedFeatures
* @return list of supported features of this service. override this method for correctly work of your pair (service and controller)
*/
QList<Feature> supportedFeatures() {
return QList<Feature>();
}
/**
* @brief sendResuylt - call this method for send responce from service to tour controller
* @param result
* @return true if data sendet is seccusseful
*/
bool sendResuylt(const QVariantMap &result) {
return d_ptr->sendCmdResult(result);
}
private:
ServicePrivate *d_ptr = nullptr;
};

@ -4,6 +4,7 @@
#include "icontroller.h"
#include "localsocket.h"
#include "package.h"
#include <quasarapp.h>
namespace Patronum {
@ -18,6 +19,52 @@ Patronum::ServicePrivate::ServicePrivate(const QString &name, IController *contr
}
bool ServicePrivate::sendCmdResult(const QVariantMap &result) {
if (!_socket->isValid()) {
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);
return false;
}
QByteArray responce;
QDataStream stream(&responce, QIODevice::WriteOnly);
stream << Command::FeatureResponce << result;
return _socket->send(responce);
}
bool ServicePrivate::sendFeaturesRequest() {
if (!_socket->isValid()) {
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);
return false;
}
QByteArray responce;
QDataStream stream(&responce, QIODevice::WriteOnly);
stream << Command::FeaturesRequest;
return _socket->send(responce);
}
bool ServicePrivate::sendCmd(const QList<Feature> &result) {
if (!_socket->isValid()) {
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);
return false;
}
QByteArray responce;
QDataStream stream(&responce, QIODevice::WriteOnly);
stream << Command::Feature << result;
return _socket->send(responce);
}
void ServicePrivate::handleReceve(QByteArray data) {
if (data.size() < 2) {
@ -31,12 +78,14 @@ void ServicePrivate::handleReceve(QByteArray data) {
case Command::FeaturesRequest: {
if (!_service) {
// to-do logs
QuasarAppUtils::Params::log("System error, service is not inited!",
QuasarAppUtils::Error);
break;
}
if (!_socket->isValid()) {
// to-do logs
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);
break;
}
@ -46,7 +95,8 @@ void ServicePrivate::handleReceve(QByteArray data) {
stream << Command::Features << features;
if (!_socket->send(sendData)) {
// to-do logs
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);
}
break;
@ -55,7 +105,8 @@ void ServicePrivate::handleReceve(QByteArray data) {
case Command::Features: {
if (!_controller) {
// to-do logs
QuasarAppUtils::Params::log("System error, controller is not inited!",
QuasarAppUtils::Error);
break;
}
@ -71,21 +122,40 @@ void ServicePrivate::handleReceve(QByteArray data) {
}
case Command::Feature: {
if (!_service) {
// to-do logs
QuasarAppUtils::Params::log("System error, service is not inited!",
QuasarAppUtils::Error);
break;
}
QDataStream stream(package->data);
Feature feature;
QList<Feature> feature;
stream >> feature;
_service->handleReceve(feature);
break;
}
case Command::FeatureResponce: {
if (!_controller) {
QuasarAppUtils::Params::log("System error, controller is not inited!",
QuasarAppUtils::Error);
break;
}
QDataStream stream(package->data);
QVariantMap feature;
stream >> feature;
_controller->handleResponce(feature);
break;
}
default: {
// to-do add logs
QuasarAppUtils::Params::log("Wrong command!",
QuasarAppUtils::Error);
break;
}

@ -9,13 +9,17 @@ class LocalSocket;
class IService;
class IController;
class ServicePrivate : public QObject
class ServicePrivate: public QObject
{
Q_OBJECT
public:
ServicePrivate(const QString& name, IController* controller = nullptr,
IService* service = nullptr, QObject *parent = nullptr);
bool sendCmdResult(const QVariantMap& result);
bool sendFeaturesRequest();
bool sendCmd(const QList<Feature>& result);
signals:
void sigListFeatures(QList<Feature>);