fix close connection

This commit is contained in:
Andrei Yankovich 2020-05-08 13:22:17 +03:00
parent 13b30fdb3e
commit c3473490a0
9 changed files with 50 additions and 28 deletions

View File

@ -11,6 +11,10 @@ Controller::Controller(const QString &name):
d_ptr = new ControllerPrivate(name, this);
}
Controller::~Controller() {
delete d_ptr;
}
bool Controller::send(int argc, const char **argv) {
if (!QuasarAppUtils::Params::parseParams(argc, argv)) {
return false;

View File

@ -23,6 +23,7 @@ public:
* @param name - name of you service
*/
Controller(const QString& name);
~Controller() override;
/**
* @brief send - this method send request to service
@ -47,14 +48,14 @@ protected:
* default inplenebtation prin help of available command of your service
* @param features - list of features
*/
void handleFeatures(const QList<Feature> &features);
void handleFeatures(const QList<Feature> &features) override;
/**
* @brief handleResponce - override this method if you want create a custom reaction of get responce from service
* Default inplementation print responce to console.
* @param responce - responce from service
*/
void handleResponce(const QVariantMap &responce);
void handleResponce(const QVariantMap &responce) override;
/**
* @brief defaultInstallLocation - this method must be return a path for service executable or wrapper

View File

@ -29,6 +29,9 @@ public:
d_ptr = new ServicePrivate(name, this);
}
~Service() override {
delete d_ptr;
}
// IService interface
protected:
/**

View File

@ -10,7 +10,7 @@ namespace Patronum {
ControllerPrivate::ControllerPrivate(const QString &name, IController *controller, QObject *parent):
QObject(parent) {
_socket = new LocalSocket(name);
_socket = new LocalSocket(name, this);
if (!_socket->connectToTarget()) {
QuasarAppUtils::Params::log("Connect to service fail !");
@ -48,7 +48,7 @@ bool ControllerPrivate::sendCmd(const QList<Feature> &result) {
QByteArray request;
QDataStream stream(&request, QIODevice::WriteOnly);
stream << Command::Feature << result;
stream << static_cast<char>(Command::Feature) << result;
if (_socket->send(request)){
_responce = false;

View File

@ -6,13 +6,21 @@
namespace Patronum {
LocalSocket::LocalSocket(const QString &target) {
LocalSocket::LocalSocket(const QString &target, QObject *ptr):
QObject(ptr) {
m_target = "P" + target;
}
LocalSocket::~LocalSocket() {
}
bool LocalSocket::registerSokcet(QLocalSocket *socket) {
m_socket = socket;
if (m_socket->parent() != this)
m_socket->setParent(this);
connect(m_socket, &QLocalSocket::stateChanged,
this, &LocalSocket::handleStateChanged);

View File

@ -22,7 +22,8 @@ public:
* @brief LocalSocket
* @param target - target it is localSocket name or ip:port
*/
LocalSocket(const QString& target);
LocalSocket(const QString& target, QObject* ptr = nullptr);
~LocalSocket();
// ISocketWraper interface
public:

View File

@ -10,7 +10,7 @@ namespace Patronum {
Patronum::ServicePrivate::ServicePrivate(const QString &name, IService *service, QObject *parent):
QObject(parent) {
_socket = new LocalSocket(name);
_socket = new LocalSocket(name, this);
if (!_socket->listen()) {
QuasarAppUtils::Params::log("Fail to create a terminal socket!");
@ -35,7 +35,7 @@ bool ServicePrivate::sendCmdResult(const QVariantMap &result) {
QByteArray responce;
QDataStream stream(&responce, QIODevice::WriteOnly);
stream << Command::FeatureResponce << result;
stream << static_cast<char>(Command::FeatureResponce) << result;
return _socket->send(responce);
}
@ -68,7 +68,7 @@ void ServicePrivate::handleReceve(QByteArray data) {
QByteArray sendData;
QDataStream stream(&sendData, QIODevice::WriteOnly);
stream << Command::Features << features;
stream << static_cast<char>(Command::Features) << features;
if (!_socket->send(sendData)) {
QuasarAppUtils::Params::log("scoket is closed!",
QuasarAppUtils::Error);

Binary file not shown.

View File

@ -8,7 +8,7 @@
#define TEST_LOCAL_HOST "127.0.0.1"
#define TEST_PORT 27777
class testProtockol : public QObject
class testPatronum : public QObject
{
Q_OBJECT
@ -16,10 +16,11 @@ private:
public:
testProtockol();
testPatronum();
void testPing();
~testProtockol();
~testPatronum();
private slots:
void initTestCase();
@ -29,15 +30,12 @@ private slots:
};
testProtockol::testProtockol() {
testPatronum::testPatronum() {
QuasarAppUtils::Params::setArg("verbose", 3);
}
void testProtockol::connectTest() {
DefaultService serv;
QTimer::singleShot(0, [](){
void testPatronum::testPing() {
const char* arg[] = {
"/",
"fd"
@ -47,20 +45,27 @@ void testProtockol::connectTest() {
QVERIFY(cli.send(2 , arg));
QVERIFY(cli.waitForResponce(1000));
QVERIFY(cli.getResponce().value("Result") == "pong");
});
}
void testPatronum::connectTest() {
DefaultService serv;
QTimer::singleShot(0, [this](){
testPing();
QCoreApplication::exit(0);
});
QVERIFY(serv.exec() == 0);
}
testProtockol::~testProtockol() {
testPatronum::~testPatronum() {
}
void testProtockol::initTestCase() {
void testPatronum::initTestCase() {
}
QTEST_APPLESS_MAIN(testProtockol)
QTEST_APPLESS_MAIN(testPatronum)
#include "tst_unittests.moc"