in the backend constructor
#include "backend.h"
{
client = new ClientServices("192.168.1.72",6547);
connect(client, &ClientServices::hasReadSome, this, &Backend::receivedSomething);
connect(client, &ClientServices::statusChanged, this, &Backend::setStatus);
// FIXME change this connection to the new syntax
//connect data to source database
connect(client, &ClientServices::sendRiparazione, this ,&Backend::recivedRiparazione);
}
bool Backend::getStatus()
{
return client->getStatus();
}
void Backend::setStatus(bool newStatus)
{
//qDebug() << "new status is:" << newStatus;
if (newStatus)
{ emit statusChanged("CONNECTED"); }
else
{ emit statusChanged("DISCONNECTED"); }
}
void Backend::recivedRiparazione(int code, double p_pCosto, double p_Pubblico)
{
emit sendCode(code);
emit sendPCosto(p_pCosto);
emit sendPPubblico(p_Pubblico);
}
void Backend
::receivedSomething(QString msg
) {
emit someMessage(msg);
}
{
//qDebug() << "got error";
switch (err)
{
case 0:
strError = "Connection was refused";
break;
case 1:
strError = "Remote host closed the connection";
break;
case 2:
strError = "Host address was not found";
break;
case 5:
strError = "Connection timed out";
break;
default:
strError = "Unknown error";
}
emit someError(strError);
}
void Backend::connectClicked()
{
client->connect2host();
}
void Backend
::sendClicked(QString msg
) {
//out.setVersion(QDataStream::Qt_5_10);
out << quint16(0) << msg;
out.device()->seek(0);
out << quint16(arrBlock.size() - sizeof(quint16));
client->tcpSocket->write(arrBlock);
}
void Backend::disconnectClicked()
{
client->closeConnection();
}
#include "backend.h"
Backend::Backend(QObject *parent) : QObject(parent)
{
client = new ClientServices("192.168.1.72",6547);
connect(client, &ClientServices::hasReadSome, this, &Backend::receivedSomething);
connect(client, &ClientServices::statusChanged, this, &Backend::setStatus);
// FIXME change this connection to the new syntax
connect(client->tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(gotError(QAbstractSocket::SocketError)));
//connect data to source database
connect(client, &ClientServices::sendRiparazione, this ,&Backend::recivedRiparazione);
}
bool Backend::getStatus()
{
return client->getStatus();
}
void Backend::setStatus(bool newStatus)
{
//qDebug() << "new status is:" << newStatus;
if (newStatus)
{ emit statusChanged("CONNECTED"); }
else
{ emit statusChanged("DISCONNECTED"); }
}
void Backend::recivedRiparazione(int code, double p_pCosto, double p_Pubblico)
{
emit sendCode(code);
emit sendPCosto(p_pCosto);
emit sendPPubblico(p_Pubblico);
}
void Backend::receivedSomething(QString msg)
{
emit someMessage(msg);
}
void Backend::gotError(QAbstractSocket::SocketError err)
{
//qDebug() << "got error";
QString strError = "unknown";
switch (err)
{
case 0:
strError = "Connection was refused";
break;
case 1:
strError = "Remote host closed the connection";
break;
case 2:
strError = "Host address was not found";
break;
case 5:
strError = "Connection timed out";
break;
default:
strError = "Unknown error";
}
emit someError(strError);
}
void Backend::connectClicked()
{
client->connect2host();
}
void Backend::sendClicked(QString msg)
{
QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
//out.setVersion(QDataStream::Qt_5_10);
out << quint16(0) << msg;
out.device()->seek(0);
out << quint16(arrBlock.size() - sizeof(quint16));
client->tcpSocket->write(arrBlock);
}
void Backend::disconnectClicked()
{
client->closeConnection();
}
To copy to clipboard, switch view to plain text mode
this is clientService.cpp:
#include "clientservices.h"
#include <QBuffer>
ClientServices
::ClientServices(const QString hostAddress,
int portNumber
) : QObject (),m_nNextBlockSize
(0){
status = false;
host = hostAddress;
port = portNumber;
timeoutTimer->setSingleShot(true);
connect(timeoutTimer, &QTimer::timeout, this, &ClientServices::connectionTimeout);
connect(tcpSocket, &QTcpSocket::disconnected, this, &ClientServices::closeConnection);
}
bool ClientServices::getStatus(){return status;
}
void ClientServices::closeConnection()
{
timeoutTimer->stop();
//qDebug() << tcpSocket->state();
disconnect(tcpSocket, &QTcpSocket::connected, 0, 0);
disconnect(tcpSocket, &QTcpSocket::readyRead, 0, 0);
bool shouldEmit = false;
switch (tcpSocket->state())
{
case 0:
tcpSocket->disconnectFromHost();
shouldEmit = true;
break;
case 2:
tcpSocket->abort();
shouldEmit = true;
break;
default:
tcpSocket->abort();
}
if (shouldEmit)
{
status = false;
emit statusChanged(status);
}
}
void ClientServices::connect2host()
{
timeoutTimer->start(3000);
tcpSocket->connectToHost(host, port);
connect(tcpSocket, &QTcpSocket::connected, this, &ClientServices::connected);
connect(tcpSocket, &QTcpSocket::readyRead, this, &ClientServices::readyRead);
}
void ClientServices::readyRead()
{
in >> code >> pCost >> pPublic;
qDebug() << code << pCost << pPublic;
emit sendRiparazione(code,pCost,pPublic);
}
void ClientServices::connected()
{
status = true;
emit statusChanged(status);
}
void ClientServices::connectionTimeout()
{
//qDebug() << tcpSocket->state();
{
tcpSocket->abort();
}
}
#include "clientservices.h"
#include <QBuffer>
ClientServices::ClientServices(const QString hostAddress, int portNumber) : QObject (),m_nNextBlockSize(0)
{
status = false;
tcpSocket = new QTcpSocket();
host = hostAddress;
port = portNumber;
timeoutTimer = new QTimer();
timeoutTimer->setSingleShot(true);
connect(timeoutTimer, &QTimer::timeout, this, &ClientServices::connectionTimeout);
connect(tcpSocket, &QTcpSocket::disconnected, this, &ClientServices::closeConnection);
}
bool ClientServices::getStatus(){return status;
}
void ClientServices::closeConnection()
{
timeoutTimer->stop();
//qDebug() << tcpSocket->state();
disconnect(tcpSocket, &QTcpSocket::connected, 0, 0);
disconnect(tcpSocket, &QTcpSocket::readyRead, 0, 0);
bool shouldEmit = false;
switch (tcpSocket->state())
{
case 0:
tcpSocket->disconnectFromHost();
shouldEmit = true;
break;
case 2:
tcpSocket->abort();
shouldEmit = true;
break;
default:
tcpSocket->abort();
}
if (shouldEmit)
{
status = false;
emit statusChanged(status);
}
}
void ClientServices::connect2host()
{
timeoutTimer->start(3000);
tcpSocket->connectToHost(host, port);
connect(tcpSocket, &QTcpSocket::connected, this, &ClientServices::connected);
connect(tcpSocket, &QTcpSocket::readyRead, this, &ClientServices::readyRead);
}
void ClientServices::readyRead()
{
QDataStream in(tcpSocket);//QtcpSocket
in >> code >> pCost >> pPublic;
qDebug() << code << pCost << pPublic;
emit sendRiparazione(code,pCost,pPublic);
}
void ClientServices::connected()
{
status = true;
emit statusChanged(status);
}
void ClientServices::connectionTimeout()
{
//qDebug() << tcpSocket->state();
if(tcpSocket->state() == QAbstractSocket::ConnectingState)
{
tcpSocket->abort();
emit tcpSocket->error(QAbstractSocket::SocketTimeoutError);
}
}
To copy to clipboard, switch view to plain text mode
my Idea Controller Backend.cpp Model clientservice.cpp(and Other) View clsses Qml
Bookmarks