From: Gustavo Martin Morcuende Date: Fri, 6 Sep 2013 16:42:10 +0000 (+0200) Subject: Messing around with Qt. Just the beginning. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=b3d67b53d469034ec2e61edc42868e45058b8d67;p=agent-device-independence%2F.git Messing around with Qt. Just the beginning. --- diff --git a/Qt/DeviceAgent/DeviceAgent.pro b/Qt/DeviceAgent/DeviceAgent.pro new file mode 100644 index 0000000..27e8845 --- /dev/null +++ b/Qt/DeviceAgent/DeviceAgent.pro @@ -0,0 +1,21 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-09-06T18:25:52 +# +#------------------------------------------------- + +QT += core gui +QT += network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = DeviceAgent +TEMPLATE = app + + +SOURCES += main.cpp\ + deviceagent.cpp + +HEADERS += deviceagent.h + +FORMS += deviceagent.ui diff --git a/Qt/DeviceAgent/DeviceAgent.pro.user b/Qt/DeviceAgent/DeviceAgent.pro.user new file mode 100644 index 0000000..374ab33 --- /dev/null +++ b/Qt/DeviceAgent/DeviceAgent.pro.user @@ -0,0 +1,242 @@ + + + + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + System + false + 4 + false + true + 1 + true + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Qt4ProjectManager.Target.DesktopTarget + 0 + 0 + 0 + + ProjectExplorer.ToolChain.Gcc:{30faa792-e0fc-4eb8-8712-7fd4df2f4a33} + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Qt 4.8.2 in PATH (System) Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + /home/gustavo/github/agent-device-independence/Qt/DeviceAgent-build-desktop-Qt_4_8_2_in_PATH__System__Release + 2 + true + + + ProjectExplorer.ToolChain.Gcc:{30faa792-e0fc-4eb8-8712-7fd4df2f4a33} + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Qt 4.8.2 in PATH (System) Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + /home/gustavo/github/agent-device-independence/Qt/DeviceAgent-build-desktop-Qt_4_8_2_in_PATH__System__Debug + 2 + true + + 2 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + No deployment + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + + false + false + false + false + true + 0.01 + 10 + true + 25 + + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + DeviceAgent + + Qt4ProjectManager.Qt4RunConfiguration + 2 + + DeviceAgent.pro + false + false + + + 3768 + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.EnvironmentId + {2d84f71d-1f97-415d-981e-0aeaafeabca9} + + + ProjectExplorer.Project.Updater.FileVersion + 11 + + diff --git a/Qt/DeviceAgent/deviceagent.cpp b/Qt/DeviceAgent/deviceagent.cpp new file mode 100644 index 0000000..7d516ca --- /dev/null +++ b/Qt/DeviceAgent/deviceagent.cpp @@ -0,0 +1,105 @@ +#include "deviceagent.h" +#include "ui_deviceagent.h" +#include +#include +#include +#include + +DeviceAgent::DeviceAgent(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::DeviceAgent) +{ + tcpServer = new QTcpServer(this); + QHostAddress address = QHostAddress::LocalHost; + tcpServer->listen(address, 3333); + file = new QTemporaryFile(); + file->open(); + out = new QTextStream (file); + + connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection())); + + ui->setupUi(this); +} + +DeviceAgent::~DeviceAgent() +{ + delete ui; +} + +void DeviceAgent::acceptConnection() +{ + clientConnection = tcpServer->nextPendingConnection(); + connect(clientConnection, SIGNAL(disconnected()), + clientConnection, SLOT(deleteLater())); + + connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), +//! [3] + this, SLOT(displayError(QAbstractSocket::SocketError))); +// connect(clientConnection, SIGNAL(readyRead()), this, SLOT(dataFromClient())); + connect(clientConnection, SIGNAL(disconnected()), this, SLOT(endFromClient())); + + char data[10]; + char currentData[11]; + memset(data, 0, sizeof(data)); + memset(currentData, 0, sizeof(currentData)); + for (;;) { + if (clientConnection->waitForReadyRead()) { + int numData; + numData = clientConnection->read(data, sizeof(data)); + while (numData != 0) { + + if (numData == -1) { + clientConnection->close(); + endFromClient(); + return; + } + + memcpy(currentData, data, numData); + + *out << currentData; + + memset(data, 0, sizeof(data)); + memset(currentData, 0, sizeof(currentData)); + + numData = clientConnection->read(data, sizeof(data)); + } + + + } + else { + clientConnection->close(); + endFromClient(); + return; + } + } +} + +void DeviceAgent::endFromClient() +{ + clientConnection->deleteLater(); + file->close(); +} + +void DeviceAgent::displayError(QAbstractSocket::SocketError socketError) +{ + switch (socketError) { + case QAbstractSocket::RemoteHostClosedError: + break; + case QAbstractSocket::HostNotFoundError: + QMessageBox::information(this, tr("Fortune Client"), + tr("The host was not found. Please check the " + "host name and port settings.")); + break; + case QAbstractSocket::ConnectionRefusedError: + QMessageBox::information(this, tr("Fortune Client"), + tr("The connection was refused by the peer. " + "Make sure the fortune server is running, " + "and check that the host name and port " + "settings are correct.")); + break; + default: + QMessageBox::information(this, tr("Fortune Client"), + tr("The following error occurred: %1.") + .arg(clientConnection->errorString())); + } +} diff --git a/Qt/DeviceAgent/deviceagent.h b/Qt/DeviceAgent/deviceagent.h new file mode 100644 index 0000000..f55c23e --- /dev/null +++ b/Qt/DeviceAgent/deviceagent.h @@ -0,0 +1,39 @@ +#ifndef DEVICEAGENT_H +#define DEVICEAGENT_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QTcpServer; +class QTcpSocket; +QT_END_NAMESPACE + +namespace Ui { +class DeviceAgent; +} + +class DeviceAgent : public QMainWindow +{ + Q_OBJECT + +public: + explicit DeviceAgent(QWidget *parent = 0); + ~DeviceAgent(); + +private slots: + void acceptConnection(); + void displayError(QAbstractSocket::SocketError socketError); + void endFromClient(); + +private: + Ui::DeviceAgent *ui; + QTcpServer *tcpServer; + QTcpSocket *clientConnection; + QTextStream *out; + QTemporaryFile *file; +}; + +#endif // DEVICEAGENT_H diff --git a/Qt/DeviceAgent/deviceagent.ui b/Qt/DeviceAgent/deviceagent.ui new file mode 100644 index 0000000..c27d5ca --- /dev/null +++ b/Qt/DeviceAgent/deviceagent.ui @@ -0,0 +1,57 @@ + + + DeviceAgent + + + + 0 + 0 + 400 + 300 + + + + DeviceAgent + + + + + + 70 + 60 + 241 + 51 + + + + + + + + 0 + 0 + 400 + 22 + + + + + DeviceAgent + + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/Qt/DeviceAgent/main.cpp b/Qt/DeviceAgent/main.cpp new file mode 100644 index 0000000..b4b4e07 --- /dev/null +++ b/Qt/DeviceAgent/main.cpp @@ -0,0 +1,11 @@ +#include +#include "deviceagent.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + DeviceAgent w; + w.show(); + + return a.exec(); +}