// system includes #include // project includes #include #include "JsonClientConnection.h" // hyperion include #include // qt includes #include #include #include JsonServer::JsonServer(uint16_t port) : QObject() , _server() , _hyperion(Hyperion::getInstance()) , _openConnections() , _log(Logger::getInstance("JSONSERVER")) { if (!_server.listen(QHostAddress::Any, port)) { throw std::runtime_error("JSONSERVER ERROR: could not bind to port"); } QList list = Hyperion::getInstance()->getForwarder()->getJsonSlaves(); for ( int i=0; ilocalAddress().toString().toStdString().c_str()); JsonClientConnection * connection = new JsonClientConnection(socket); _openConnections.insert(connection); // register slot for cleaning up after the connection closed connect(connection, &JsonClientConnection::connectionClosed, this, &JsonServer::closedConnection); } } } void JsonServer::closedConnection(void) { JsonClientConnection* connection = qobject_cast(sender()); Debug(_log, "Connection closed"); _openConnections.remove(connection); // schedule to delete the connection object connection->deleteLater(); } void JsonServer::componentStateChanged(const hyperion::Components component, bool enable) { if (component == hyperion::COMP_FORWARDER && _forwarder_enabled != enable) { _forwarder_enabled = enable; Info(_log, "forwarder change state to %s", (enable ? "enabled" : "disabled") ); if(_forwarder_enabled) { connect(_hyperion, &Hyperion::forwardJsonMessage, this, &JsonServer::forwardJsonMessage); } else { disconnect(_hyperion, &Hyperion::forwardJsonMessage, this, &JsonServer::forwardJsonMessage); } } } void JsonServer::forwardJsonMessage(const QJsonObject &message) { QTcpSocket client; QList list = _hyperion->getForwarder()->getJsonSlaves(); for ( int i=0; iwrite(serializedMessage); if (!socket->waitForBytesWritten()) { Debug(_log, "Error while writing data to host"); return; } // read reply data QByteArray serializedReply; while (!serializedReply.contains('\n')) { // receive reply if (!socket->waitForReadyRead()) { Debug(_log, "Error while writing data from host"); return; } 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; } }