hyperion.ng/libsrc/flatbufserver/FlatBufferClient.cpp

208 lines
5.7 KiB
C++
Raw Normal View History

2018-12-28 17:55:49 +01:00
#include "FlatBufferClient.h"
// qt
#include <QTcpSocket>
#include <QHostAddress>
#include <QTimer>
2018-12-30 22:07:53 +01:00
#include <QRgb>
2018-12-28 17:55:49 +01:00
#include <hyperion/Hyperion.h>
#include <QDebug>
FlatBufferClient::FlatBufferClient(QTcpSocket* socket, const int &timeout, QObject *parent)
: QObject(parent)
, _log(Logger::getInstance("FLATBUFSERVER"))
, _socket(socket)
2018-12-30 22:07:53 +01:00
, _clientAddress("@"+socket->peerAddress().toString())
2018-12-28 17:55:49 +01:00
, _timeoutTimer(new QTimer(this))
, _timeout(timeout * 1000)
, _priority()
, _hyperion(Hyperion::getInstance())
{
// timer setup
_timeoutTimer->setSingleShot(true);
_timeoutTimer->setInterval(_timeout);
connect(_timeoutTimer, &QTimer::timeout, this, &FlatBufferClient::forceClose);
// connect socket signals
connect(_socket, &QTcpSocket::readyRead, this, &FlatBufferClient::readyRead);
connect(_socket, &QTcpSocket::disconnected, this, &FlatBufferClient::disconnected);
}
void FlatBufferClient::readyRead()
{
_timeoutTimer->start();
_receiveBuffer += _socket->readAll();
// check if we can read a header
while(_receiveBuffer.size() >= 4)
{
uint32_t messageSize =
((_receiveBuffer[0]<<24) & 0xFF000000) |
((_receiveBuffer[1]<<16) & 0x00FF0000) |
((_receiveBuffer[2]<< 8) & 0x0000FF00) |
((_receiveBuffer[3] ) & 0x000000FF);
// check if we can read a complete message
if((uint32_t) _receiveBuffer.size() < messageSize + 4) return;
// extract message only and remove header + msg from buffer :: QByteArray::remove() does not return the removed data
const QByteArray msg = _receiveBuffer.right(messageSize);
_receiveBuffer.remove(0, messageSize + 4);
2018-12-28 17:55:49 +01:00
2018-12-30 22:07:53 +01:00
const auto* msgData = reinterpret_cast<const uint8_t*>(msg.constData());
2018-12-28 17:55:49 +01:00
flatbuffers::Verifier verifier(msgData, messageSize);
2018-12-30 22:07:53 +01:00
if (hyperionnet::VerifyRequestBuffer(verifier))
2018-12-28 17:55:49 +01:00
{
2018-12-30 22:07:53 +01:00
auto message = hyperionnet::GetRequest(msgData);
2018-12-28 17:55:49 +01:00
handleMessage(message);
continue;
}
sendErrorReply("Unable to parse message");
}
//emit newMessage(msgData,messageSize);
// Emit this to send a new priority register event to all Hyperion instances,
// emit registerGlobalInput(_priority, hyperion::COMP_FLATBUFSERVER, QString("%1@%2").arg("PLACE_ORIGIN_STRING_FROM_SENDER_HERE",_socket->peerAddress()));
// Emit this to send the image data event to all Hyperion instances
// emit setGlobalInput(_priority, _image, _timeout);
}
void FlatBufferClient::forceClose()
{
_socket->close();
}
void FlatBufferClient::disconnected()
{
qDebug()<<"Socket Closed";
//emit clearGlobalPriority(_priority, hyperion::COMP_FLATBUFSERVER);
_socket->deleteLater();
emit clientDisconnected();
}
2018-12-30 22:07:53 +01:00
void FlatBufferClient::handleMessage(const hyperionnet::Request * req)
2018-12-28 17:55:49 +01:00
{
2018-12-30 22:07:53 +01:00
const void* reqPtr;
if ((reqPtr = req->command_as_Color()) != nullptr) {
handleColorCommand(static_cast<const hyperionnet::Color*>(reqPtr));
} else if ((reqPtr = req->command_as_Image()) != nullptr) {
handleImageCommand(static_cast<const hyperionnet::Image*>(reqPtr));
} else if ((reqPtr = req->command_as_Clear()) != nullptr) {
handleClearCommand(static_cast<const hyperionnet::Clear*>(reqPtr));
} else if ((reqPtr = req->command_as_Register()) != nullptr) {
handleRegisterCommand(static_cast<const hyperionnet::Register*>(reqPtr));
} else {
sendErrorReply("Received invalid packet.");
2018-12-28 17:55:49 +01:00
}
}
2018-12-30 22:07:53 +01:00
void FlatBufferClient::handleColorCommand(const hyperionnet::Color *colorReq)
2018-12-28 17:55:49 +01:00
{
// extract parameters
2018-12-30 22:07:53 +01:00
const int32_t rgbData = colorReq->data();
ColorRgb color;
color.red = qRed(rgbData);
color.green = qGreen(rgbData);
color.blue = qBlue(rgbData);
2018-12-28 17:55:49 +01:00
2018-12-30 22:07:53 +01:00
// set output
_hyperion->setColor(_priority, color, colorReq->duration());
// send reply
sendSuccessReply();
}
void FlatBufferClient::handleRegisterCommand(const hyperionnet::Register *regReq)
{
_priority = regReq->priority();
_hyperion->registerInput(_priority, hyperion::COMP_FLATBUFSERVER, regReq->origin()->c_str()+_clientAddress);
}
void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
{
// extract parameters
int duration = image->duration();
const void* reqPtr;
if ((reqPtr = image->data_as_RawImage()) != nullptr)
2018-12-28 17:55:49 +01:00
{
2018-12-30 22:07:53 +01:00
const auto *img = static_cast<const hyperionnet::RawImage*>(reqPtr);
const auto & imageData = img->data();
const int width = img->width();
const int height = img->height();
2018-12-28 17:55:49 +01:00
2018-12-30 22:07:53 +01:00
if ((int) imageData->size() != width*height*3)
{
sendErrorReply("Size of image data does not match with the width and height");
return;
}
2018-12-28 17:55:49 +01:00
2018-12-30 22:07:53 +01:00
Image<ColorRgb> image(width, height);
memmove(image.memptr(), imageData->data(), imageData->size());
_hyperion->setInputImage(_priority, image, duration);
}
2018-12-28 17:55:49 +01:00
// send reply
sendSuccessReply();
}
2018-12-30 22:07:53 +01:00
void FlatBufferClient::handleClearCommand(const hyperionnet::Clear *clear)
{
// extract parameters
const int priority = clear->priority();
if (priority == -1) {
_hyperion->clearall();
}
else {
// Check if we are clearing ourselves.
if (priority == _priority) {
_priority = -1;
}
_hyperion->clear(priority);
}
sendSuccessReply();
}
2018-12-28 17:55:49 +01:00
void FlatBufferClient::handleNotImplemented()
{
sendErrorReply("Command not implemented");
}
void FlatBufferClient::sendMessage()
{
auto size = _builder.GetSize();
const uint8_t* buffer = _builder.GetBufferPointer();
uint8_t sizeData[] = {uint8_t(size >> 24), uint8_t(size >> 16), uint8_t(size >> 8), uint8_t(size)};
_socket->write((const char *) sizeData, sizeof(sizeData));
_socket->write((const char *)buffer, size);
_socket->flush();
_builder.Clear();
}
void FlatBufferClient::sendSuccessReply()
{
2018-12-30 22:07:53 +01:00
auto reply = hyperionnet::CreateReplyDirect(_builder);
2018-12-28 17:55:49 +01:00
_builder.Finish(reply);
// send reply
sendMessage();
}
void FlatBufferClient::sendErrorReply(const std::string &error)
{
// create reply
2018-12-30 22:07:53 +01:00
auto reply = hyperionnet::CreateReplyDirect(_builder, error.c_str());
2018-12-28 17:55:49 +01:00
_builder.Finish(reply);
// send reply
sendMessage();
}