Skip to content

Commit

Permalink
qmlui: handle more network meta types
Browse files Browse the repository at this point in the history
Started to forward Tardis actions to network
Added undo command to actions menu
  • Loading branch information
mcallegari committed Oct 14, 2017
1 parent 75f589d commit c504c33
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 141 deletions.
6 changes: 4 additions & 2 deletions qmlui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void App::startup()
// register an uncreatable type just to use the enums in QML
qmlRegisterUncreatableType<ShowManager>("org.qlcplus.classes", 1, 0, "ShowManager", "Can't create a ShowManager !");

m_networkManager = new NetworkManager(this);
m_networkManager = new NetworkManager(this, m_doc);
rootContext()->setContextProperty("networkManager", m_networkManager);

// register an uncreatable type just to use the enums in QML
Expand All @@ -150,7 +150,9 @@ void App::startup()
connect(m_networkManager, &NetworkManager::accessMaskChanged, this, &App::setAccessMask);
connect(m_networkManager, &NetworkManager::requestProjectLoad, this, &App::slotLoadDocFromMemory);

m_tardis = new Tardis(this, m_doc, m_networkManager, m_fixtureManager, m_functionManager, m_showManager, m_virtualConsole);
m_tardis = new Tardis(this, m_doc, m_networkManager, m_fixtureManager, m_functionManager,
m_contextManager, m_showManager, m_virtualConsole);
rootContext()->setContextProperty("tardis", m_tardis);

m_contextManager->registerContext(m_virtualConsole);
m_contextManager->registerContext(m_showManager);
Expand Down
2 changes: 1 addition & 1 deletion qmlui/contextmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ void ContextManager::setFixturePosition(quint32 fxID, qreal x, qreal y, qreal z)
{
MonitorProperties *mProps = m_doc->monitorProperties();
QVector3D position(x, y, z);
Tardis::instance()->enqueueAction(FixturePosition, m_doc->fixture(fxID),
Tardis::instance()->enqueueAction(FixtureSetPosition, m_doc->fixture(fxID),
QVariant(mProps->fixturePosition(fxID)),
QVariant(position));

Expand Down
4 changes: 4 additions & 0 deletions qmlui/mainview2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QQmlComponent>

#include "doc.h"
#include "tardis.h"
#include "mainview2d.h"
#include "qlccapability.h"
#include "qlcfixturemode.h"
Expand Down Expand Up @@ -212,6 +213,9 @@ void MainView2D::createFixtureItem(quint32 fxID, qreal x, qreal y, bool mmCoords
y = availablePos.y();
// add the new fixture to the Doc monitor properties
m_monProps->setFixturePosition(fxID, QVector3D(x, y, 0));
Tardis::instance()->enqueueAction(FixtureSetPosition, fixture,
QVariant(QVector3D(0, 0, 0)),
QVariant(QVector3D(x, y, 0)));
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions qmlui/qml/ActionsMenu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,19 @@ Popup
saveDialog.open()
}
}
ContextMenuEntry
{
id: undo
imgSource: "qrc:/undo.svg"
entryText: qsTr("Undo")
onEntered: submenuItem = null

onClicked:
{
menuRoot.close()
tardis.undoAction()
}
}
ContextMenuEntry
{
imgSource: "qrc:/network.svg"
Expand Down
2 changes: 1 addition & 1 deletion qmlui/qml/PopupNetworkServer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CustomPopupDialog
{
height: UISettings.listItemHeight
Layout.columnSpan: 2
label: ""
label: networkManager.connectionsCount
}

// Row 6
Expand Down
128 changes: 114 additions & 14 deletions qmlui/tardis/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
*/

#include <QNetworkInterface>
#include <QXmlStreamWriter>
#include <QtCore/qbuffer.h>
#include <QFile>

#include "networkmanager.h"
#include "networkpacketizer.h"
#include "simplecrypt.h"
#include "tardisactions.h"

#include "function.h"
#include "vcwidget.h"
#include "fixture.h"
#include "doc.h"

#define DEFAULT_UDP_PORT 9997
#define DEFAULT_TCP_PORT 9998
Expand All @@ -32,8 +38,10 @@

static const quint64 defaultKey = 0x5131632B4E33744B; // this is "Q1c+N3tK"

NetworkManager::NetworkManager(QObject *parent)
NetworkManager::NetworkManager(QObject *parent, Doc *doc)
: QObject(parent)
, m_doc(doc)
, m_encryptPackets(true)
, m_udpSocket(NULL)
, m_tcpServer(NULL)
, m_serverStarted(false)
Expand Down Expand Up @@ -77,6 +85,61 @@ void NetworkManager::setHostName(QString hostName)
emit hostNameChanged(m_hostName);
}

int NetworkManager::connectionsCount()
{
if (m_hostType == ServerHostType)
return m_hostsMap.count();
else if (m_hostType == ClientHostType)
return m_clientStatus == Connected ? 1 : 0;

return 0;
}

void NetworkManager::sendAction(quint32 objID, TardisAction action)
{
QByteArray packet;
m_packetizer->initializePacket(packet, action.m_action);

switch (action.m_action)
{
case FixtureCreate:
{
QBuffer buffer;
buffer.open(QIODevice::WriteOnly | QIODevice::Text);
QXmlStreamWriter xmlWriter(&buffer);

Fixture *fixture = qobject_cast<Fixture *>(action.m_object);
if (fixture && fixture->saveXML(&xmlWriter))
m_packetizer->addSection(packet, buffer.buffer());
}
break;
default:
{
m_packetizer->addSection(packet, objID);
m_packetizer->addSection(packet, action.m_newValue);
}
break;
}

if (m_hostType == ServerHostType)
{
/* Send packet to all connected clients */
auto i = m_hostsMap.constBegin();
while (i != m_hostsMap.constEnd())
{
NetworkHost *host = i.value();
sendTCPPacket(host->tcpSocket, packet, m_encryptPackets);

++i;
}
}
else
{
/* Send packet to the connected server */
sendTCPPacket(m_tcpSocket, packet, m_encryptPackets);
}
}

QString NetworkManager::defaultName()
{
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
Expand Down Expand Up @@ -226,28 +289,33 @@ bool NetworkManager::setClientAccess(QString hostName, bool allow, int accessMas
m_packetizer->addSection(reply, QVariant("Failed"));
}

sendTCPPacket(host->tcpSocket, reply, true);
sendTCPPacket(host->tcpSocket, reply, m_encryptPackets);

return true;
}

bool NetworkManager::sendWorkspaceToClient(QString hostName, QString filename)
{
QByteArray packet;
int pktCounter = 0;
QFile workspace(filename);
if (workspace.exists() == false)
return false;

QHostAddress clientAddress = getHostFromName(hostName);
NetworkHost *host = m_hostsMap.value(clientAddress, NULL);

if (host == NULL || clientAddress.isNull())
return false;

if (!workspace.open(QIODevice::ReadOnly))
if (workspace.exists() == false)
{
m_packetizer->initializePacket(packet, NetProjectTransfer);
m_packetizer->addSection(packet, QVariant(0));
m_packetizer->addSection(packet, QVariant(0));
sendTCPPacket(host->tcpSocket, packet, m_encryptPackets);
return false;
}

QByteArray packet;
int pktCounter = 0;
if (!workspace.open(QIODevice::ReadOnly))
return false;

while (!workspace.atEnd())
{
Expand All @@ -267,11 +335,13 @@ bool NetworkManager::sendWorkspaceToClient(QString hostName, QString filename)
m_packetizer->addSection(packet, QVariant(2));
}
else
{
m_packetizer->addSection(packet, QVariant(1));
}

m_packetizer->addSection(packet, QVariant(data));

sendTCPPacket(host->tcpSocket, packet, true);
sendTCPPacket(host->tcpSocket, packet, m_encryptPackets);

pktCounter++;
}
Expand Down Expand Up @@ -388,7 +458,7 @@ bool NetworkManager::connectClient(QString ipAddress)

setClientStatus(WaitAuthentication);

return sendTCPPacket(m_tcpSocket, packet, true);
return sendTCPPacket(m_tcpSocket, packet, m_encryptPackets);
}

bool NetworkManager::disconnectClient()
Expand Down Expand Up @@ -508,7 +578,7 @@ void NetworkManager::slotProcessTCPPackets()
QByteArray datagram = wholeData.mid(bytesProcessed);
int read = m_packetizer->decodePacket(datagram, opCode, paramsList, m_crypt);

qDebug() << "Bytes processed" << read << QString::number(opCode, 16); // << paramsList;
qDebug() << "Bytes processed" << read << "opCode" << QString::number(opCode, 16) << "params" << paramsList.count();

if (read < 0)
{
Expand Down Expand Up @@ -551,7 +621,7 @@ void NetworkManager::slotProcessTCPPackets()
QByteArray reply;
m_packetizer->initializePacket(reply, NetAuthenticationReply);
m_packetizer->addSection(reply, QVariant("Failed"));
sendTCPPacket(host->tcpSocket, reply, true);
sendTCPPacket(host->tcpSocket, reply, m_encryptPackets);
}
}
break;
Expand Down Expand Up @@ -579,6 +649,12 @@ void NetworkManager::slotProcessTCPPackets()
if (seqType == 0)
{
m_projectSize = paramsList.at(1).toInt();
if (m_projectSize == 0)
{
setClientStatus(Connected);
emit connectionsCountChanged();
break;
}
m_projectData.clear();
m_projectData.append(paramsList.at(2).toByteArray());
}
Expand All @@ -592,11 +668,26 @@ void NetworkManager::slotProcessTCPPackets()
emit requestProjectLoad(m_projectData);
m_projectData.clear();
setClientStatus(Connected);
emit connectionsCountChanged();
}
}
break;
case FixtureCreate:
{
QBuffer buffer;
buffer.setData(paramsList.at(0).toByteArray());
buffer.open(QIODevice::ReadOnly | QIODevice::Text);
QXmlStreamReader xmlReader(&buffer);
xmlReader.readNextStartElement();
Fixture::loader(xmlReader, m_doc);
}
break;
default:
{
emit actionReady(opCode, paramsList.at(0).toUInt(), paramsList.at(1));

//qDebug() << "Unsupported opCode" << opCode;
}
break;
}

Expand Down Expand Up @@ -626,6 +717,7 @@ void NetworkManager::slotProcessNewTCPConnection()
newHost->isAuthenticated = false;
newHost->tcpSocket = clientConnection;
m_hostsMap[senderAddress] = newHost;
emit connectionsCountChanged();
}
connect(clientConnection, SIGNAL(readyRead()),
this, SLOT(slotProcessTCPPackets()));
Expand All @@ -634,5 +726,13 @@ void NetworkManager::slotProcessNewTCPConnection()
void NetworkManager::slotHostDisconnected()
{
QTcpSocket *socket = (QTcpSocket *)sender();
qDebug() << "Host with address" << socket->peerAddress().toString() << "disconnected !";
QHostAddress senderAddress = socket->peerAddress();
qDebug() << "Host with address" << senderAddress.toString() << "disconnected !";

if (m_hostsMap.contains(senderAddress) == true)
{
NetworkHost *host = m_hostsMap.take(senderAddress);
delete host;
emit connectionsCountChanged();
}
}
22 changes: 19 additions & 3 deletions qmlui/tardis/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <QThread>
#include <QHash>

#include "tardisactions.h"

class Doc;
class SimpleCrypt;
class NetworkPacketizer;

Expand All @@ -38,7 +41,6 @@ typedef struct
QString hostName;
/** The TCP socket for unicast client/server communication */
QTcpSocket *tcpSocket;

} NetworkHost;

class NetworkManager : public QObject
Expand All @@ -49,9 +51,10 @@ class NetworkManager : public QObject
Q_PROPERTY(bool serverStarted READ serverStarted WRITE setServerStarted NOTIFY serverStartedChanged)
Q_PROPERTY(QVariant serverList READ serverList NOTIFY serverListChanged)
Q_PROPERTY(int clientStatus READ clientStatus WRITE setClientStatus NOTIFY clientStatusChanged)
Q_PROPERTY(int connectionsCount READ connectionsCount NOTIFY connectionsCountChanged)

public:
explicit NetworkManager(QObject *parent = 0);
explicit NetworkManager(QObject *parent = 0, Doc *doc = NULL);
~NetworkManager();

enum HostType
Expand All @@ -65,6 +68,11 @@ class NetworkManager : public QObject
QString hostName() const;
void setHostName(QString hostName);

int connectionsCount();

public slots:
void sendAction(quint32 objID, TardisAction action);

protected:
QString defaultName();

Expand All @@ -73,6 +81,8 @@ class NetworkManager : public QObject

signals:
void hostNameChanged(QString hostName);
void connectionsCountChanged();
void actionReady(int code, quint32 id, QVariant value);

protected slots:
/** Async event raised when UDP packets are received */
Expand All @@ -82,6 +92,12 @@ protected slots:
void slotProcessTCPPackets();

private:
/** Reference to the QLC+ Doc */
Doc *m_doc;

/** Global flag to enable/disable packets encryption */
bool m_encryptPackets;

/** The host name in the QLC+ network */
QString m_hostName;

Expand Down Expand Up @@ -133,7 +149,7 @@ protected slots:
bool m_serverStarted;

/** Map of the QLC+ hosts detected on the network */
QHash<QHostAddress, NetworkHost *>m_hostsMap;
QHash<QHostAddress, NetworkHost *> m_hostsMap;

/*********************************************************************
* Client
Expand Down

0 comments on commit c504c33

Please sign in to comment.