mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
remove protobuf (part 2)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <QTcpSocket>
|
||||
#include <QHostAddress>
|
||||
#include <QTimer>
|
||||
#include <QRgb>
|
||||
|
||||
#include <hyperion/Hyperion.h>
|
||||
#include <QDebug>
|
||||
@@ -11,7 +12,7 @@ FlatBufferClient::FlatBufferClient(QTcpSocket* socket, const int &timeout, QObje
|
||||
: QObject(parent)
|
||||
, _log(Logger::getInstance("FLATBUFSERVER"))
|
||||
, _socket(socket)
|
||||
, _clientAddress(socket->peerAddress().toString())
|
||||
, _clientAddress("@"+socket->peerAddress().toString())
|
||||
, _timeoutTimer(new QTimer(this))
|
||||
, _timeout(timeout * 1000)
|
||||
, _priority()
|
||||
@@ -49,12 +50,12 @@ void FlatBufferClient::readyRead()
|
||||
const QByteArray msg = _receiveBuffer.right(messageSize);
|
||||
_receiveBuffer.remove(0, messageSize + 4);
|
||||
|
||||
const uint8_t* msgData = reinterpret_cast<const uint8_t*>(msg.constData());
|
||||
const auto* msgData = reinterpret_cast<const uint8_t*>(msg.constData());
|
||||
flatbuffers::Verifier verifier(msgData, messageSize);
|
||||
|
||||
if (flatbuf::VerifyHyperionRequestBuffer(verifier))
|
||||
if (hyperionnet::VerifyRequestBuffer(verifier))
|
||||
{
|
||||
auto message = flatbuf::GetHyperionRequest(msgData);
|
||||
auto message = hyperionnet::GetRequest(msgData);
|
||||
handleMessage(message);
|
||||
continue;
|
||||
}
|
||||
@@ -83,79 +84,92 @@ void FlatBufferClient::disconnected()
|
||||
emit clientDisconnected();
|
||||
}
|
||||
|
||||
void FlatBufferClient::handleMessage(const flatbuf::HyperionRequest * message)
|
||||
void FlatBufferClient::handleMessage(const hyperionnet::Request * req)
|
||||
{
|
||||
switch (message->command())
|
||||
{
|
||||
case flatbuf::Command_COLOR:
|
||||
qDebug()<<"handle colorReuest";
|
||||
if (!flatbuffers::IsFieldPresent(message, flatbuf::HyperionRequest::VT_COLORREQUEST))
|
||||
{
|
||||
sendErrorReply("Received COLOR command without ColorRequest");
|
||||
break;
|
||||
}
|
||||
//handleColorCommand(message->colorRequest());
|
||||
break;
|
||||
case flatbuf::Command_IMAGE:
|
||||
qDebug()<<"handle imageReuest";
|
||||
if (!flatbuffers::IsFieldPresent(message, flatbuf::HyperionRequest::VT_IMAGEREQUEST))
|
||||
{
|
||||
sendErrorReply("Received IMAGE command without ImageRequest");
|
||||
break;
|
||||
}
|
||||
handleImageCommand(message->imageRequest());
|
||||
break;
|
||||
case flatbuf::Command_CLEAR:
|
||||
if (!flatbuffers::IsFieldPresent(message, flatbuf::HyperionRequest::VT_CLEARREQUEST))
|
||||
{
|
||||
sendErrorReply("Received CLEAR command without ClearRequest");
|
||||
break;
|
||||
}
|
||||
//handleClearCommand(message->clearRequest());
|
||||
break;
|
||||
case flatbuf::Command_CLEARALL:
|
||||
//handleClearallCommand();
|
||||
break;
|
||||
default:
|
||||
qDebug()<<"handleNotImplemented";
|
||||
handleNotImplemented();
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
void FlatBufferClient::handleImageCommand(const flatbuf::ImageRequest *message)
|
||||
void FlatBufferClient::handleColorCommand(const hyperionnet::Color *colorReq)
|
||||
{
|
||||
// extract parameters
|
||||
int priority = message->priority();
|
||||
int duration = message->duration();
|
||||
int width = message->imagewidth();
|
||||
int height = message->imageheight();
|
||||
const auto & imageData = message->imagedata();
|
||||
const int32_t rgbData = colorReq->data();
|
||||
ColorRgb color;
|
||||
color.red = qRed(rgbData);
|
||||
color.green = qGreen(rgbData);
|
||||
color.blue = qBlue(rgbData);
|
||||
|
||||
// make sure the prio is registered before setInput()
|
||||
if(priority != _priority)
|
||||
{
|
||||
_hyperion->clear(_priority);
|
||||
_hyperion->registerInput(priority, hyperion::COMP_FLATBUFSERVER, "proto@"+_clientAddress);
|
||||
_priority = priority;
|
||||
}
|
||||
|
||||
// check consistency of the size of the received data
|
||||
if ((int) imageData->size() != width*height*3)
|
||||
{
|
||||
sendErrorReply("Size of image data does not match with the width and height");
|
||||
return;
|
||||
}
|
||||
|
||||
// create ImageRgb
|
||||
Image<ColorRgb> image(width, height);
|
||||
memcpy(image.memptr(), imageData->data(), imageData->size());
|
||||
|
||||
_hyperion->setInputImage(_priority, image, duration);
|
||||
// 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)
|
||||
{
|
||||
const auto *img = static_cast<const hyperionnet::RawImage*>(reqPtr);
|
||||
const auto & imageData = img->data();
|
||||
const int width = img->width();
|
||||
const int height = img->height();
|
||||
|
||||
if ((int) imageData->size() != width*height*3)
|
||||
{
|
||||
sendErrorReply("Size of image data does not match with the width and height");
|
||||
return;
|
||||
}
|
||||
|
||||
Image<ColorRgb> image(width, height);
|
||||
memmove(image.memptr(), imageData->data(), imageData->size());
|
||||
_hyperion->setInputImage(_priority, image, duration);
|
||||
}
|
||||
|
||||
// send reply
|
||||
sendSuccessReply();
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void FlatBufferClient::handleNotImplemented()
|
||||
{
|
||||
@@ -175,7 +189,7 @@ void FlatBufferClient::sendMessage()
|
||||
|
||||
void FlatBufferClient::sendSuccessReply()
|
||||
{
|
||||
auto reply = flatbuf::CreateHyperionReplyDirect(_builder, flatbuf::Type_REPLY, true);
|
||||
auto reply = hyperionnet::CreateReplyDirect(_builder);
|
||||
_builder.Finish(reply);
|
||||
|
||||
// send reply
|
||||
@@ -185,7 +199,7 @@ void FlatBufferClient::sendSuccessReply()
|
||||
void FlatBufferClient::sendErrorReply(const std::string &error)
|
||||
{
|
||||
// create reply
|
||||
auto reply = flatbuf::CreateHyperionReplyDirect(_builder, flatbuf::Type_REPLY, false, error.c_str());
|
||||
auto reply = hyperionnet::CreateReplyDirect(_builder, error.c_str());
|
||||
_builder.Finish(reply);
|
||||
|
||||
// send reply
|
||||
|
@@ -75,14 +75,31 @@ private:
|
||||
///
|
||||
/// @brief Handle the received message
|
||||
///
|
||||
void handleMessage(const flatbuf::HyperionRequest *message);
|
||||
void handleMessage(const hyperionnet::Request * req);
|
||||
|
||||
///
|
||||
/// Register new priority
|
||||
///
|
||||
void handleRegisterCommand(const hyperionnet::Register *regReq);
|
||||
|
||||
///
|
||||
/// @brief Hande Color message
|
||||
///
|
||||
void handleColorCommand(const hyperionnet::Color *colorReq);
|
||||
|
||||
///
|
||||
/// Handle an incoming Image message
|
||||
///
|
||||
/// @param message the incoming message
|
||||
/// @param image the incoming image
|
||||
///
|
||||
void handleImageCommand(const flatbuf::ImageRequest * message);
|
||||
void handleImageCommand(const hyperionnet::Image *image);
|
||||
|
||||
///
|
||||
/// @brief Handle clear command
|
||||
///
|
||||
/// @param clear the incoming clear request
|
||||
///
|
||||
void handleClearCommand(const hyperionnet::Clear *clear);
|
||||
|
||||
///
|
||||
/// Send handle not implemented
|
||||
@@ -108,12 +125,12 @@ private:
|
||||
|
||||
private:
|
||||
Logger *_log;
|
||||
QTcpSocket *_socket;
|
||||
QTcpSocket *_socket;
|
||||
const QString _clientAddress;
|
||||
QTimer *_timeoutTimer;
|
||||
int _timeout;
|
||||
int _priority;
|
||||
Hyperion* _hyperion;
|
||||
Hyperion* _hyperion;
|
||||
|
||||
QByteArray _receiveBuffer;
|
||||
|
||||
|
@@ -7,10 +7,13 @@
|
||||
// protoserver includes
|
||||
#include <flatbufserver/FlatBufferConnection.h>
|
||||
|
||||
FlatBufferConnection::FlatBufferConnection(const QString & address)
|
||||
FlatBufferConnection::FlatBufferConnection(const QString& origin, const QString & address, const int& priority, const bool& skipReply)
|
||||
: _socket()
|
||||
, _origin(origin)
|
||||
, _priority(priority)
|
||||
, _prevSocketState(QAbstractSocket::UnconnectedState)
|
||||
, _log(Logger::getInstance("FLATBUFCONNECTION"))
|
||||
, _registered(false)
|
||||
{
|
||||
QStringList parts = address.split(":");
|
||||
if (parts.size() != 2)
|
||||
@@ -26,6 +29,9 @@ FlatBufferConnection::FlatBufferConnection(const QString & address)
|
||||
throw std::runtime_error(QString("FLATBUFCONNECTION ERROR: Unable to parse the port (%1)").arg(parts[1]).toStdString());
|
||||
}
|
||||
|
||||
if(!skipReply)
|
||||
connect(&_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection);
|
||||
|
||||
// init connect
|
||||
Info(_log, "Connecting to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
connectToHost();
|
||||
@@ -66,10 +72,9 @@ void FlatBufferConnection::readData()
|
||||
const uint8_t* msgData = reinterpret_cast<const uint8_t*>(msg.constData());
|
||||
flatbuffers::Verifier verifier(msgData, messageSize);
|
||||
|
||||
if (flatbuf::VerifyHyperionReplyBuffer(verifier))
|
||||
if (hyperionnet::VerifyReplyBuffer(verifier))
|
||||
{
|
||||
auto message = flatbuf::GetHyperionReply(msgData);
|
||||
parseReply(message);
|
||||
parseReply(hyperionnet::GetReply(msgData));
|
||||
continue;
|
||||
}
|
||||
Error(_log, "Unable to parse reply");
|
||||
@@ -84,28 +89,39 @@ void FlatBufferConnection::setSkipReply(const bool& skip)
|
||||
connect(&_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
void FlatBufferConnection::setColor(const ColorRgb & color, int priority, int duration)
|
||||
void FlatBufferConnection::setRegister(const QString& origin, int priority)
|
||||
{
|
||||
auto colorReq = flatbuf::CreateColorRequest(_builder, priority, (color.red << 16) | (color.green << 8) | color.blue, duration);
|
||||
auto req = flatbuf::CreateHyperionRequest(_builder,flatbuf::Command_COLOR, colorReq);
|
||||
auto registerReq = hyperionnet::CreateRegister(_builder, _builder.CreateString(QSTRING_CSTR(origin)), priority);
|
||||
auto req = hyperionnet::CreateRequest(_builder, hyperionnet::Command_Register, registerReq.Union());
|
||||
|
||||
_builder.Finish(req);
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
||||
}
|
||||
|
||||
void FlatBufferConnection::setImage(const Image<ColorRgb> &image, int priority, int duration)
|
||||
void FlatBufferConnection::setColor(const ColorRgb & color, int priority, int duration)
|
||||
{
|
||||
/* #TODO #BROKEN auto imgData = _builder.CreateVector<flatbuffers::Offset<uint8_t>>(image.memptr(), image.width() * image.height() * 3);
|
||||
auto imgReq = flatbuf::CreateImageRequest(_builder, priority, image.width(), image.height(), imgData, duration);
|
||||
auto req = flatbuf::CreateHyperionRequest(_builder,flatbuf::Command_IMAGE,0,imgReq);
|
||||
auto colorReq = hyperionnet::CreateColor(_builder, (color.red << 16) | (color.green << 8) | color.blue, duration);
|
||||
auto req = hyperionnet::CreateRequest(_builder, hyperionnet::Command_Color, colorReq.Union());
|
||||
|
||||
_builder.Finish(req);
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());*/
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
||||
}
|
||||
|
||||
void FlatBufferConnection::setImage(const Image<ColorRgb> &image)
|
||||
{
|
||||
auto imgData = _builder.CreateVector(reinterpret_cast<const uint8_t*>(image.memptr()), image.size());
|
||||
auto rawImg = hyperionnet::CreateRawImage(_builder, imgData, image.width(), image.height());
|
||||
auto imageReq = hyperionnet::CreateImage(_builder, hyperionnet::ImageType_RawImage, rawImg.Union(), -1);
|
||||
auto req = hyperionnet::CreateRequest(_builder,hyperionnet::Command_Image,imageReq.Union());
|
||||
|
||||
_builder.Finish(req);
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
||||
}
|
||||
|
||||
void FlatBufferConnection::clear(int priority)
|
||||
{
|
||||
auto clearReq = flatbuf::CreateClearRequest(_builder, priority);
|
||||
auto req = flatbuf::CreateHyperionRequest(_builder,flatbuf::Command_CLEAR,0,0,clearReq);
|
||||
auto clearReq = hyperionnet::CreateClear(_builder, priority);
|
||||
auto req = hyperionnet::CreateRequest(_builder,hyperionnet::Command_Clear, clearReq.Union());
|
||||
|
||||
_builder.Finish(req);
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
||||
@@ -113,11 +129,7 @@ void FlatBufferConnection::clear(int priority)
|
||||
|
||||
void FlatBufferConnection::clearAll()
|
||||
{
|
||||
auto req = flatbuf::CreateHyperionRequest(_builder,flatbuf::Command_CLEARALL);
|
||||
|
||||
// send command message
|
||||
_builder.Finish(req);
|
||||
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
||||
clear(-1);
|
||||
}
|
||||
|
||||
void FlatBufferConnection::connectToHost()
|
||||
@@ -135,26 +147,30 @@ void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size)
|
||||
// print out connection message only when state is changed
|
||||
if (_socket.state() != _prevSocketState )
|
||||
{
|
||||
switch (_socket.state() )
|
||||
{
|
||||
case QAbstractSocket::UnconnectedState:
|
||||
Info(_log, "No connection to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
break;
|
||||
|
||||
case QAbstractSocket::ConnectedState:
|
||||
Info(_log, "Connected to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug(_log, "Connecting to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
break;
|
||||
switch (_socket.state() )
|
||||
{
|
||||
case QAbstractSocket::UnconnectedState:
|
||||
Info(_log, "No connection to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
break;
|
||||
case QAbstractSocket::ConnectedState:
|
||||
Info(_log, "Connected to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
_registered = false;
|
||||
break;
|
||||
default:
|
||||
Debug(_log, "Connecting to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
||||
break;
|
||||
}
|
||||
_prevSocketState = _socket.state();
|
||||
}
|
||||
|
||||
|
||||
if (_socket.state() != QAbstractSocket::ConnectedState)
|
||||
return;
|
||||
|
||||
if(!_registered)
|
||||
{
|
||||
_registered = true;
|
||||
setRegister(_origin, _priority);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -168,46 +184,22 @@ void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size)
|
||||
int count = 0;
|
||||
count += _socket.write(reinterpret_cast<const char *>(header), 4);
|
||||
count += _socket.write(reinterpret_cast<const char *>(buffer), size);
|
||||
if (!_socket.waitForBytesWritten())
|
||||
{
|
||||
Error(_log, "Error while writing data to host");
|
||||
return;
|
||||
}
|
||||
_socket.flush();
|
||||
_builder.Clear();
|
||||
}
|
||||
|
||||
bool FlatBufferConnection::parseReply(const flatbuf::HyperionReply *reply)
|
||||
bool FlatBufferConnection::parseReply(const hyperionnet::Reply *reply)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
switch (reply->type())
|
||||
if (!reply->error())
|
||||
{
|
||||
case flatbuf::Type_REPLY:
|
||||
{
|
||||
if (!reply->success())
|
||||
{
|
||||
if (flatbuffers::IsFieldPresent(reply, flatbuf::HyperionReply::VT_ERROR))
|
||||
{
|
||||
throw std::runtime_error("PROTOCONNECTION ERROR: " + reply->error()->str());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("PROTOCONNECTION ERROR: No error info");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case flatbuf::Type_VIDEO:
|
||||
{
|
||||
VideoMode vMode = (VideoMode)reply->video();
|
||||
emit setVideoMode(vMode);
|
||||
break;
|
||||
// no error set must be a success or video
|
||||
const auto videoMode = reply->video();
|
||||
if (videoMode != -1) {
|
||||
// We got a video reply.
|
||||
emit setVideoMode(static_cast<VideoMode>(videoMode));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return success;
|
||||
return false;
|
||||
}
|
||||
|
@@ -58,6 +58,7 @@ void FlatBufferServer::newConnection()
|
||||
{
|
||||
if(QTcpSocket* socket = _server->nextPendingConnection())
|
||||
{
|
||||
|
||||
Debug(_log, "New connection from %s", QSTRING_CSTR(socket->peerAddress().toString()));
|
||||
FlatBufferClient *client = new FlatBufferClient(socket, _timeout, this);
|
||||
// internal
|
||||
|
@@ -1,15 +1,8 @@
|
||||
namespace flatbuf;
|
||||
namespace hyperionnet;
|
||||
|
||||
enum Type : int {
|
||||
REPLY = 0,
|
||||
VIDEO = 1,
|
||||
}
|
||||
|
||||
table HyperionReply {
|
||||
type:Type;
|
||||
success:bool;
|
||||
table Reply {
|
||||
error:string;
|
||||
video:int;
|
||||
video:int = -1;
|
||||
}
|
||||
|
||||
root_type HyperionReply;
|
||||
root_type Reply;
|
||||
|
@@ -1,35 +1,37 @@
|
||||
namespace flatbuf;
|
||||
namespace hyperionnet;
|
||||
|
||||
enum Command : int {
|
||||
COLOR = 0,
|
||||
IMAGE = 1,
|
||||
CLEAR = 2,
|
||||
CLEARALL = 3,
|
||||
}
|
||||
|
||||
table HyperionRequest {
|
||||
command:Command;
|
||||
colorRequest:flatbuf.ColorRequest;
|
||||
imageRequest:flatbuf.ImageRequest;
|
||||
clearRequest:flatbuf.ClearRequest;
|
||||
}
|
||||
|
||||
table ColorRequest {
|
||||
priority:int;
|
||||
RgbColor:int;
|
||||
duration:int;
|
||||
}
|
||||
|
||||
table ImageRequest {
|
||||
priority:int;
|
||||
imagewidth:int;
|
||||
imageheight:int;
|
||||
imagedata:[ubyte];
|
||||
duration:int;
|
||||
}
|
||||
|
||||
table ClearRequest {
|
||||
// A priority value of -1 clears all priorities
|
||||
table Register {
|
||||
origin:string (required);
|
||||
priority:int;
|
||||
}
|
||||
|
||||
root_type HyperionRequest;
|
||||
table RawImage {
|
||||
data:[ubyte];
|
||||
width:int = -1;
|
||||
height:int = -1;
|
||||
}
|
||||
|
||||
union ImageType {RawImage}
|
||||
|
||||
table Image {
|
||||
data:ImageType (required);
|
||||
duration:int = -1;
|
||||
}
|
||||
|
||||
table Clear {
|
||||
priority:int;
|
||||
}
|
||||
|
||||
table Color {
|
||||
data:int = -1;
|
||||
duration:int = -1;
|
||||
}
|
||||
|
||||
union Command {Color, Image, Clear, Register}
|
||||
|
||||
table Request {
|
||||
command:Command (required);
|
||||
}
|
||||
|
||||
root_type Request;
|
||||
|
Reference in New Issue
Block a user