logic: IDE Communication

Network code and controller added to communicate with IDE plugins. Does not include GUI integration though.
This commit is contained in:
Manuel Dobusch
2015-10-14 15:40:10 +02:00
parent 4e4cb25540
commit 0664f9a81b
31 changed files with 993 additions and 57 deletions
+7
View File
@@ -39,6 +39,13 @@ add_files(
qt/graphics/QtRoundedRectItem.h
qt/graphics/QtStraightLineItem.cpp
qt/graphics/QtStraightLineItem.h
qt/network/QtIDECommunicationController.cpp
qt/network/QtIDECommunicationController.h
qt/network/QtNetworkFactory.cpp
qt/network/QtNetworkFactory.h
qt/network/QtTcpWrapper.cpp
qt/network/QtTcpWrapper.h
qt/utility/QtDeviceScaledPixmap.cpp
qt/utility/QtDeviceScaledPixmap.h
+6 -1
View File
@@ -9,10 +9,13 @@
#include "Application.h"
#include "includes.h" // defines 'void setup(int argc, char *argv[])'
#include "qt/window/QtSplashScreen.h"
#include "qt/network/QtNetworkFactory.h"
#include "qt/utility/utilityQt.h"
#include "qt/view/QtViewFactory.h"
#include "version.h"
#include "component/controller/helper/NetworkProtocolHelper.h" // remove when done
void init()
{
std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>();
@@ -44,7 +47,9 @@ int main(int argc, char *argv[])
init();
QtViewFactory viewFactory;
std::shared_ptr<Application> app = Application::create(&viewFactory);
QtNetworkFactory networkFactory;
std::shared_ptr<Application> app = Application::create(&viewFactory, &networkFactory);
if (splash)
{
@@ -0,0 +1,17 @@
#include "QtIDECommunicationController.h"
#include <functional>
QtIDECommunicationController::QtIDECommunicationController(QObject* parent)
: m_tcpWrapper(parent)
{
m_tcpWrapper.setReadCallback(std::bind(&QtIDECommunicationController::handleIncomingMessage, this, std::placeholders::_1));
}
QtIDECommunicationController::~QtIDECommunicationController()
{}
void QtIDECommunicationController::sendMessage(const std::string& message) const
{
m_tcpWrapper.sendMessage(message);
}
@@ -0,0 +1,23 @@
#ifndef QT_IDE_COMMUNICATION_CONTROLLER
#define QT_IDE_COMMUNICATION_CONTROLLER
#include <qobject.h>
#include "QtTcpWrapper.h"
#include "component/controller/IDECommunicationController.h"
class QtIDECommunicationController
: public IDECommunicationController
{
public:
QtIDECommunicationController(QObject* parent);
~QtIDECommunicationController();
private:
virtual void sendMessage(const std::string& message) const;
QtTcpWrapper m_tcpWrapper;
};
#endif // QT_IDE_COMMUNICATION_CONTROLLER
+16
View File
@@ -0,0 +1,16 @@
#include "QtNetworkFactory.h"
#include "QtIDECommunicationController.h"
QtNetworkFactory::QtNetworkFactory()
{
}
QtNetworkFactory::~QtNetworkFactory()
{
}
std::shared_ptr<IDECommunicationController> QtNetworkFactory::createIDECommunicationController() const
{
return std::make_shared<QtIDECommunicationController>(nullptr);
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef QT_NETWORK_FACTORY_H
#define QT_NETWORK_FACTORY_H
#include "component/controller/NetworkFactory.h"
class QtNetworkFactory : public NetworkFactory
{
public:
QtNetworkFactory();
virtual ~QtNetworkFactory();
virtual std::shared_ptr<IDECommunicationController> createIDECommunicationController() const;
};
#endif // QT_NETWORK_FACTORY_H
+91
View File
@@ -0,0 +1,91 @@
#include "QtTcpWrapper.h"
#include "utility/logging/logging.h"
QtTcpWrapper::QtTcpWrapper(QObject* parent)
: QObject(parent)
, m_port(6667)
, m_ip("127.0.0.1")
{
QHostAddress address(m_ip.c_str());
m_tcpServer = new QTcpServer(this);
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if (!m_tcpServer->listen(QHostAddress::LocalHost, m_port))
{
LOG_ERROR_STREAM(<< "TCP server failed to start. Unable to listen for IDE plugin messages.");
}
}
QtTcpWrapper::QtTcpWrapper(QObject* parent, const std::string& ip, const quint16 port)
: QObject(parent)
, m_port(port)
, m_ip(ip)
{
QHostAddress address(m_ip.c_str());
m_tcpServer = new QTcpServer(this);
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if (!m_tcpServer->listen(QHostAddress::LocalHost, m_port))
{
LOG_ERROR_STREAM(<< "TCP server failed to start. Unable to listen for IDE plugin messages.");
}
}
QtTcpWrapper::~QtTcpWrapper()
{
if (m_tcpServer != NULL)
{
if (m_tcpServer->isListening())
{
m_tcpServer->close();
}
delete m_tcpServer;
}
}
void QtTcpWrapper::sendMessage(const std::string& message) const
{
QByteArray data;
data.append(message.c_str());
QTcpSocket socket;
socket.connectToHost(QHostAddress::LocalHost, 6666);
if (socket.waitForConnected())
{
socket.write(data);
socket.flush();
socket.waitForBytesWritten();
socket.close();
}
}
void QtTcpWrapper::setReadCallback(const std::function<void(const std::string&)>& callback)
{
m_readCallback = callback;
}
void QtTcpWrapper::acceptConnection()
{
m_tcpClient = m_tcpServer->nextPendingConnection();
connect(m_tcpClient, SIGNAL(readyRead()), this, SLOT(startRead()));
}
void QtTcpWrapper::startRead()
{
char buffer[1024] = { 0 };
m_tcpClient->read(buffer, m_tcpClient->bytesAvailable());
m_tcpClient->close();
if (m_readCallback != NULL)
{
m_readCallback(buffer);
}
}
+40
View File
@@ -0,0 +1,40 @@
#ifndef QT_SOCKET_WRAPPER_H
#define QT_SOCKET_WRAPPER_H
#include <functional>
#include <qobject.h>
#include <qudpsocket.h>
#include <qtcpserver.h>
#include <qtcpsocket.h>
class QtTcpWrapper : public QObject
{
Q_OBJECT
public:
QtTcpWrapper(QObject* parent);
QtTcpWrapper(QObject* parent, const std::string& ip, const quint16 port);
~QtTcpWrapper();
void sendMessage(const std::string& message) const;
void setReadCallback(const std::function<void(const std::string&)>& callback);
signals:
public slots:
void acceptConnection();
void startRead();
private:
quint16 m_port;
std::string m_ip;
std::function<void(const std::string&)> m_readCallback;
QTcpServer* m_tcpServer;
QTcpSocket* m_tcpClient;
};
#endif // QT_SOCKET_WRAPPER_H
@@ -0,0 +1,49 @@
#include "SocketTest.h"
#include "utility/logging/logging.h"
SocketTest::SocketTest(QObject* parent)
: QObject(parent)
, m_socket(NULL)
{
m_socket = new QUdpSocket(this);
QHostAddress address("127.0.0.1");
m_socket->bind(address, 6666);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
SocketTest::~SocketTest()
{
if(m_socket != NULL)
{
delete m_socket;
}
}
void SocketTest::sendTestMessage()
{
LOG_WARNING("SocketTest::sendTestMessage");
QByteArray Data;
Data.append("qt foo");
m_socket->writeDatagram(Data, QHostAddress::Any, 6666);
}
void SocketTest::readyRead()
{
LOG_WARNING("SocketTest::readyRead");
QByteArray buffer;
buffer.resize(m_socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
std::string message = buffer.data();
LOG_WARNING(message);
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef SOCKET_TEST_H
#define SOCKET_TEST_H
#include <qobject.h>
#include <qudpsocket.h>
class SocketTest : public QObject
{
Q_OBJECT
public:
SocketTest(QObject* parent);
~SocketTest();
void sendTestMessage();
signals:
public slots:
void readyRead();
private:
QUdpSocket* m_socket;
};
#endif // SOCKET_TEST_H