2 ******************************************************************************
4 * @file streamserviceplugin.h
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup StreamServicePlugin Plugin
10 * @brief Data UAV Data objects TCP/IP stream service
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "streamserviceplugin.h"
29 #include <QJsonObject>
30 #include <QJsonDocument>
31 #include <QMessageBox>
36 #include "extensionsystem/pluginmanager.h"
37 #include "../uavobjects/uavobjectmanager.h"
38 #include "../uavobjects/uavdataobject.h"
40 StreamServicePlugin::StreamServicePlugin() :
42 isSubscribed(false) {}
44 StreamServicePlugin::~StreamServicePlugin()
46 if (pServer
== Q_NULLPTR
) {
49 if (pServer
->isListening()) {
50 foreach(QTcpSocket
* client
, activeClients
) {
51 /* Disconnect the client discarding pending
53 if (client
->isOpen()) {
61 bool StreamServicePlugin::initialize(const QStringList
&arguments
, QString
*errorString
)
64 Q_UNUSED(errorString
);
66 pServer
= new QTcpServer();
68 if (!pServer
->listen(QHostAddress::Any
, port
)) {
69 *errorString
= tr("Couldn't start StreamService: ") + pServer
->errorString();
73 connect(pServer
, &QTcpServer::newConnection
, this, &StreamServicePlugin::clientConnected
);
78 void StreamServicePlugin::extensionsInitialized()
80 if (pServer
== Q_NULLPTR
) {
84 pServer
->resumeAccepting();
87 void StreamServicePlugin::shutdown()
89 if (pServer
!= Q_NULLPTR
) {
90 pServer
->pauseAccepting();
93 foreach(QTcpSocket
* pClient
, activeClients
) {
94 pClient
->disconnectFromHost();
98 void StreamServicePlugin::objectUpdated(UAVObject
*pObj
)
102 pObj
->toJson(qtjson
);
104 // Adds timestamp: Milliseconds from epoch
105 qtjson
.insert("gcs_timestamp_ms", QJsonValue(QDateTime::currentMSecsSinceEpoch()));
107 QJsonDocument
jsonDoc(qtjson
);
108 QString strJson
= QString(jsonDoc
.toJson(QJsonDocument::Compact
)) + "\n";
110 foreach(QTcpSocket
* pClient
, activeClients
) {
111 if (pClient
->isOpen()) {
112 if (pClient
->write(strJson
.toUtf8().constData(), strJson
.length())) {
119 void StreamServicePlugin::clientConnected()
121 QTcpSocket
*pending
= pServer
->nextPendingConnection();
123 if (pending
== Q_NULLPTR
) {
126 makeSureIsSubscribed();
128 connect(pending
, &QTcpSocket::disconnected
, this, &StreamServicePlugin::clientDisconnected
);
129 activeClients
.append(pending
);
132 void StreamServicePlugin::clientDisconnected()
134 disconnect(sender());
135 activeClients
.removeAll((QTcpSocket
*)sender());
138 inline void StreamServicePlugin::makeSureIsSubscribed()
144 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
145 UAVObjectManager
*objManager
= pm
->getObject
<UAVObjectManager
>();
146 Q_ASSERT(objManager
);
148 QList
< QList
<UAVDataObject
*> > objList
= objManager
->getDataObjects();
149 foreach(QList
<UAVDataObject
*> list
, objList
) {
150 foreach(UAVDataObject
* obj
, list
) {
151 connect(obj
, &UAVDataObject::objectUpdated
, this, &StreamServicePlugin::objectUpdated
);