2013-08-17 15:39:29 +02:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// project includes
|
|
|
|
#include <jsonserver/JsonServer.h>
|
|
|
|
#include "JsonClientConnection.h"
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
// bonjour include
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <bonjour/bonjourserviceregister.h>
|
2017-06-24 11:52:22 +02:00
|
|
|
|
|
|
|
// qt includes
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <QTcpServer>
|
2017-06-24 11:52:22 +02:00
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QByteArray>
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
JsonServer::JsonServer(const QJsonDocument& config)
|
2016-07-31 22:21:35 +02:00
|
|
|
: QObject()
|
2018-12-27 23:11:32 +01:00
|
|
|
, _server(new QTcpServer(this))
|
2016-07-31 22:21:35 +02:00
|
|
|
, _openConnections()
|
|
|
|
, _log(Logger::getInstance("JSONSERVER"))
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
Debug(_log, "Created instance");
|
2016-02-18 10:32:38 +01:00
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
// Set trigger for incoming connections
|
2018-12-27 23:11:32 +01:00
|
|
|
connect(_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
2013-08-17 19:20:19 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
// init
|
|
|
|
handleSettingsUpdate(settings::JSONSERVER, config);
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonServer::~JsonServer()
|
|
|
|
{
|
|
|
|
foreach (JsonClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void JsonServer::start()
|
|
|
|
{
|
|
|
|
if(_server->isListening())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_server->listen(QHostAddress::Any, _port))
|
|
|
|
{
|
|
|
|
Error(_log,"Could not bind to port '%d', please use an available port", _port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Info(_log, "Started on port %d", _port);
|
|
|
|
|
|
|
|
if(_serviceRegister == nullptr)
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
_serviceRegister = new BonjourServiceRegister(this);
|
|
|
|
_serviceRegister->registerService("_hyperiond-json._tcp", _port);
|
|
|
|
}
|
|
|
|
else if( _serviceRegister->getPort() != _port)
|
|
|
|
{
|
|
|
|
delete _serviceRegister;
|
|
|
|
_serviceRegister = new BonjourServiceRegister(this);
|
2018-12-27 23:11:32 +01:00
|
|
|
_serviceRegister->registerService("_hyperiond-json._tcp", _port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonServer::stop()
|
|
|
|
{
|
|
|
|
if(!_server->isListening())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_server->close();
|
|
|
|
Info(_log, "Stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonServer::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
|
|
|
|
{
|
|
|
|
if(type == settings::JSONSERVER)
|
|
|
|
{
|
|
|
|
QJsonObject obj = config.object();
|
|
|
|
if(_port != obj["port"].toInt())
|
|
|
|
{
|
|
|
|
_port = obj["port"].toInt();
|
|
|
|
stop();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:39:29 +02:00
|
|
|
uint16_t JsonServer::getPort() const
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
return _port;
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonServer::newConnection()
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
while(_server->hasPendingConnections())
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
if (QTcpSocket * socket = _server->nextPendingConnection())
|
2017-11-20 00:06:45 +01:00
|
|
|
{
|
|
|
|
Debug(_log, "New connection from: %s ",socket->localAddress().toString().toStdString().c_str());
|
|
|
|
JsonClientConnection * connection = new JsonClientConnection(socket);
|
|
|
|
_openConnections.insert(connection);
|
2013-08-17 15:39:29 +02:00
|
|
|
|
2017-11-20 00:06:45 +01:00
|
|
|
// register slot for cleaning up after the connection closed
|
|
|
|
connect(connection, &JsonClientConnection::connectionClosed, this, &JsonServer::closedConnection);
|
|
|
|
}
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:06:45 +01:00
|
|
|
void JsonServer::closedConnection(void)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender());
|
2016-07-11 17:08:22 +02:00
|
|
|
Debug(_log, "Connection closed");
|
2013-08-17 15:39:29 +02:00
|
|
|
_openConnections.remove(connection);
|
|
|
|
|
|
|
|
// schedule to delete the connection object
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|
2017-06-17 23:29:04 +02:00
|
|
|
|
2017-06-24 11:52:22 +02:00
|
|
|
void JsonServer::sendMessage(const QJsonObject & message, QTcpSocket * socket)
|
2017-06-17 23:29:04 +02:00
|
|
|
{
|
2017-06-24 11:52:22 +02:00
|
|
|
// serialize message
|
|
|
|
QJsonDocument writer(message);
|
|
|
|
QByteArray serializedMessage = writer.toJson(QJsonDocument::Compact) + "\n";
|
|
|
|
|
|
|
|
// write message
|
|
|
|
socket->write(serializedMessage);
|
|
|
|
if (!socket->waitForBytesWritten())
|
2017-06-17 23:29:04 +02:00
|
|
|
{
|
2017-06-24 11:52:22 +02:00
|
|
|
Debug(_log, "Error while writing data to host");
|
|
|
|
return;
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
|
|
|
// read reply data
|
|
|
|
QByteArray serializedReply;
|
|
|
|
while (!serializedReply.contains('\n'))
|
2017-06-17 23:29:04 +02:00
|
|
|
{
|
2017-06-24 11:52:22 +02:00
|
|
|
// receive reply
|
|
|
|
if (!socket->waitForReadyRead())
|
|
|
|
{
|
|
|
|
Debug(_log, "Error while writing data from host");
|
|
|
|
return;
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
|
|
|
serializedReply += socket->readAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse reply data
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument reply = QJsonDocument::fromJson(serializedReply ,&error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
Error(_log, "Error while parsing reply: invalid json");
|
|
|
|
return;
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2017-06-17 23:29:04 +02:00
|
|
|
}
|