| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "aspectcommanddebugger_p.h" | 
| 41 |  | 
| 42 | #include <QtNetwork/QTcpSocket> | 
| 43 | #include <QtCore/QJsonDocument> | 
| 44 | #include <QtCore/QJsonObject> | 
| 45 |  | 
| 46 | #include <Qt3DCore/private/qabstractaspect_p.h> | 
| 47 | #include <Qt3DCore/private/qsysteminformationservice_p.h> | 
| 48 |  | 
| 49 | QT_BEGIN_NAMESPACE | 
| 50 |  | 
| 51 | namespace Qt3DCore { | 
| 52 |  | 
| 53 | namespace Debug { | 
| 54 |  | 
| 55 | namespace { | 
| 56 |  | 
| 57 | const qint32 MagicNumber = 0x454; | 
| 58 |  | 
| 59 | struct CommandHeader | 
| 60 | { | 
| 61 |     qint32 magic; | 
| 62 |     qint32 size; | 
| 63 | }; | 
| 64 |  | 
| 65 | } // anonymous | 
| 66 |  | 
| 67 | void AspectCommandDebugger::ReadBuffer::insert(const QByteArray &array) | 
| 68 | { | 
| 69 |     buffer.insert(i: endIdx, a: array); | 
| 70 |     endIdx += array.size(); | 
| 71 | } | 
| 72 |  | 
| 73 | void AspectCommandDebugger::ReadBuffer::trim() | 
| 74 | { | 
| 75 |     if (startIdx != endIdx && startIdx != 0) { | 
| 76 |         memcpy(dest: buffer.data(), | 
| 77 |                src: buffer.constData() + startIdx, | 
| 78 |                n: size()); | 
| 79 |         endIdx -= startIdx; | 
| 80 |         startIdx = 0; | 
| 81 |     } | 
| 82 | } | 
| 83 |  | 
| 84 | AspectCommandDebugger::AspectCommandDebugger(QSystemInformationService *parent) | 
| 85 |     : QTcpServer(parent) | 
| 86 |     , m_service(parent) | 
| 87 | { | 
| 88 | } | 
| 89 |  | 
| 90 | void AspectCommandDebugger::initialize() | 
| 91 | { | 
| 92 |     QObject::connect(sender: this, signal: &QTcpServer::newConnection, slot: [this] { | 
| 93 |         QTcpSocket *socket = nextPendingConnection(); | 
| 94 |         m_connections.push_back(t: socket); | 
| 95 |  | 
| 96 |         QObject::connect(sender: socket, signal: &QTcpSocket::disconnected, slot: [this, socket] { | 
| 97 |             m_connections.removeOne(t: socket); | 
| 98 |             // Destroy object to make sure all QObject connection are removed | 
| 99 |             socket->deleteLater(); | 
| 100 |         }); | 
| 101 |  | 
| 102 |         QObject::connect(sender: socket, signal: &QTcpSocket::readyRead, slot: [this, socket] { | 
| 103 |             onCommandReceived(socket); | 
| 104 |         }); | 
| 105 |     }); | 
| 106 |     const bool listening = listen(address: QHostAddress::Any, port: 8883); | 
| 107 |     if (!listening) | 
| 108 |         qWarning() << Q_FUNC_INFO << "failed to listen on port 8883" ; | 
| 109 | } | 
| 110 |  | 
| 111 | void AspectCommandDebugger::asynchronousReplyFinished(AsynchronousCommandReply *reply) | 
| 112 | { | 
| 113 |     Q_ASSERT(reply->isFinished()); | 
| 114 |     QTcpSocket *socket = m_asyncCommandToSocketEntries.take(akey: reply); | 
| 115 |     if (m_connections.contains(t: socket)) { | 
| 116 |         QJsonObject replyObj; | 
| 117 |         replyObj.insert(key: QLatin1String("command" ), value: QJsonValue(reply->commandName())); | 
| 118 |         replyObj.insert(key: QLatin1String("data" ), value: QJsonDocument::fromJson(json: reply->data()).object()); | 
| 119 |         sendReply(socket, data: QJsonDocument(replyObj).toJson()); | 
| 120 |     } | 
| 121 |     reply->deleteLater(); | 
| 122 | } | 
| 123 |  | 
| 124 |  | 
| 125 | // Expects to receive commands in the form | 
| 126 | // CommandHeader { MagicNumber; size } | 
| 127 | // JSON { | 
| 128 | //         command: "commandName" | 
| 129 | //         data: JSON Obj | 
| 130 | // } | 
| 131 | void AspectCommandDebugger::onCommandReceived(QTcpSocket *socket) | 
| 132 | { | 
| 133 |     const QByteArray newData = socket->readAll(); | 
| 134 |     m_readBuffer.insert(array: newData); | 
| 135 |  | 
| 136 |     const int commandPacketSize = sizeof(CommandHeader); | 
| 137 |     while (m_readBuffer.size() >= commandPacketSize) { | 
| 138 |         CommandHeader * = reinterpret_cast<CommandHeader *>(m_readBuffer.buffer.data() + m_readBuffer.startIdx); | 
| 139 |         if (header->magic == MagicNumber) { | 
| 140 |             // Early return, header is valid but we haven't yet received all the data | 
| 141 |             if ((m_readBuffer.size() - commandPacketSize) < header->size) | 
| 142 |                 return; | 
| 143 |             // We have a valid command | 
| 144 |             // We expect command to be a CommandHeader + some json text | 
| 145 |             const QJsonDocument doc = QJsonDocument::fromJson( | 
| 146 |                         json: QByteArray(m_readBuffer.buffer.data() + m_readBuffer.startIdx + commandPacketSize, | 
| 147 |                                    header->size)); | 
| 148 |  | 
| 149 |             if (!doc.isNull()) { | 
| 150 |                 // Send command to the aspectEngine | 
| 151 |                 QJsonObject commandObj = doc.object(); | 
| 152 |                 const QJsonValue commandNameValue = commandObj.value(key: QLatin1String("command" )); | 
| 153 |                 executeCommand(command: commandNameValue.toString(), socket); | 
| 154 |             } | 
| 155 |  | 
| 156 |             m_readBuffer.startIdx += commandPacketSize + header->size; | 
| 157 |         } | 
| 158 |     } | 
| 159 |     // Copy remaining length of buffer at begininning if we have read some commands | 
| 160 |     // and some partial one remain | 
| 161 |     m_readBuffer.trim(); | 
| 162 | } | 
| 163 |  | 
| 164 | void AspectCommandDebugger::sendReply(QTcpSocket *socket, const QByteArray &payload) | 
| 165 | { | 
| 166 |     CommandHeader ; | 
| 167 |  | 
| 168 |     replyHeader.magic = MagicNumber; | 
| 169 |     replyHeader.size = payload.size(); | 
| 170 |     // Write header | 
| 171 |     socket->write(data: reinterpret_cast<const char *>(&replyHeader), len: sizeof(CommandHeader)); | 
| 172 |     // Write payload | 
| 173 |     socket->write(data: payload.constData(), len: payload.size()); | 
| 174 | } | 
| 175 |  | 
| 176 | void AspectCommandDebugger::executeCommand(const QString &command, | 
| 177 |                                            QTcpSocket *socket) | 
| 178 | { | 
| 179 |     // Only a single aspect is going to reply | 
| 180 |     const QVariant response = m_service->executeCommand(command); | 
| 181 |     if (response.userType() == qMetaTypeId<AsynchronousCommandReply *>()) { // AsynchronousCommand | 
| 182 |         // Store the command | socket in a table | 
| 183 |         AsynchronousCommandReply *reply = response.value<AsynchronousCommandReply *>(); | 
| 184 |         // Command has already been completed | 
| 185 |         if (reply->isFinished()) { | 
| 186 |            asynchronousReplyFinished(reply); | 
| 187 |         } else { // Command is not completed yet | 
| 188 |             QObject::connect(sender: reply, signal: &AsynchronousCommandReply::finished, | 
| 189 |                              receiver: this, slot: &AspectCommandDebugger::asynchronousReplyFinished); | 
| 190 |             m_asyncCommandToSocketEntries.insert(akey: reply, avalue: socket); | 
| 191 |         } | 
| 192 |     } else { // Synchronous command | 
| 193 |         // and send response to client | 
| 194 |         QJsonObject reply; | 
| 195 |         reply.insert(key: QLatin1String("command" ), value: QJsonValue(command)); | 
| 196 |         // TO DO: convert QVariant to QJsonDocument/QByteArray | 
| 197 |         sendReply(socket, payload: QJsonDocument(reply).toJson()); | 
| 198 |  | 
| 199 |     } | 
| 200 | } | 
| 201 |  | 
| 202 | } // Debug | 
| 203 |  | 
| 204 | } // Qt3DCore | 
| 205 |  | 
| 206 | QT_END_NAMESPACE | 
| 207 |  |