hyperion.ng/libsrc/jsonserver/JsonClientConnection.h

109 lines
3.1 KiB
C
Raw Normal View History

#pragma once
// stl includes
#include <string>
// Qt includes
#include <QByteArray>
#include <QTcpSocket>
// jsoncpp includes
#include <json/json.h>
// Hyperion includes
#include <hyperion/Hyperion.h>
// util includes
#include <utils/jsonschema/JsonSchemaChecker.h>
2013-08-18 20:55:59 +02:00
class ImageProcessor;
2013-08-31 14:36:54 +02:00
/// @brief The Connection object created by \a JsonServer when a new connection is establshed
///
class JsonClientConnection : public QObject
{
Q_OBJECT
public:
2013-08-31 14:36:54 +02:00
/// @brief Constructor
/// @param socket The Socket object for this connection
/// @param hyperion The Hyperion server
JsonClientConnection(QTcpSocket * socket, Hyperion * hyperion);
2013-08-31 14:36:54 +02:00
/// @brief Destructor
~JsonClientConnection();
signals:
2013-08-31 14:36:54 +02:00
/// @brief Signal which is emitted when the connection is being closed
/// @param connection This connection object
void connectionClosed(JsonClientConnection * connection);
private slots:
2013-08-31 14:36:54 +02:00
/// @brief Slot called when new data has arrived
void readData();
2013-08-31 14:36:54 +02:00
/// @brief Slot called when this connection is being closed
void socketClosed();
private:
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON message
/// @param message the incoming message as string
void handleMessage(const std::string & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Color message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleColorCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Image message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleImageCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Server info message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleServerInfoCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Clear message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleClearCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Clearall message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleClearallCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON Transform message
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleTransformCommand(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Handle an incoming JSON message of unknown type
/// @param message the incoming message
2013-08-18 12:02:17 +02:00
void handleNotImplemented();
2013-08-31 14:36:54 +02:00
/// @brief Send a message to the connected client
/// @param message The JSON message to send
void sendMessage(const Json::Value & message);
2013-08-31 14:36:54 +02:00
/// @brief Send a standard reply indicating success
void sendSuccessReply();
2013-08-31 14:36:54 +02:00
/// @brief Send an error message back to the client
/// @param error String describing the error
void sendErrorReply(const std::string & error);
private:
2013-08-31 14:36:54 +02:00
/// @brief Check if a JSON messag is valid according to a given JSON schema
/// @param message JSON message which need to be checked
/// @param schemaResource Qt esource identifier with the JSON schema
/// @param errors Output error message
/// @return true if message conforms the given JSON schema
2013-08-18 12:02:17 +02:00
bool checkJson(const Json::Value & message, const QString &schemaResource, std::string & errors);
2013-08-18 12:02:17 +02:00
private:
QTcpSocket * _socket;
2013-08-18 20:55:59 +02:00
ImageProcessor * _imageProcessor;
Hyperion * _hyperion;
QByteArray _receiveBuffer;
};