2018-12-28 17:55:49 +01:00
|
|
|
// stl includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QRgb>
|
|
|
|
|
2019-07-20 11:28:16 +02:00
|
|
|
// flatbuffer includes
|
2018-12-28 17:55:49 +01:00
|
|
|
#include <flatbufserver/FlatBufferConnection.h>
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
FlatBufferConnection::FlatBufferConnection(const QString& origin, const QString & address, const int& priority, const bool& skipReply)
|
2018-12-28 18:12:45 +01:00
|
|
|
: _socket()
|
2018-12-30 22:07:53 +01:00
|
|
|
, _origin(origin)
|
|
|
|
, _priority(priority)
|
2018-12-28 18:12:45 +01:00
|
|
|
, _prevSocketState(QAbstractSocket::UnconnectedState)
|
|
|
|
, _log(Logger::getInstance("FLATBUFCONNECTION"))
|
2018-12-30 22:07:53 +01:00
|
|
|
, _registered(false)
|
2018-12-28 18:12:45 +01:00
|
|
|
{
|
2018-12-28 17:55:49 +01:00
|
|
|
QStringList parts = address.split(":");
|
|
|
|
if (parts.size() != 2)
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
throw std::runtime_error(QString("FLATBUFCONNECTION ERROR: Unable to parse address (%1)").arg(address).toStdString());
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
_host = parts[0];
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
_port = parts[1].toUShort(&ok);
|
|
|
|
if (!ok)
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
throw std::runtime_error(QString("FLATBUFCONNECTION ERROR: Unable to parse the port (%1)").arg(parts[1]).toStdString());
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
if(!skipReply)
|
|
|
|
connect(&_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection);
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
// init connect
|
2018-12-28 17:55:49 +01:00
|
|
|
Info(_log, "Connecting to Hyperion: %s:%d", _host.toStdString().c_str(), _port);
|
|
|
|
connectToHost();
|
|
|
|
|
|
|
|
// start the connection timer
|
|
|
|
_timer.setInterval(5000);
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
connect(&_timer, &QTimer::timeout, this, &FlatBufferConnection::connectToHost);
|
2018-12-28 17:55:49 +01:00
|
|
|
_timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
FlatBufferConnection::~FlatBufferConnection()
|
|
|
|
{
|
|
|
|
_timer.stop();
|
|
|
|
_socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatBufferConnection::readData()
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
_receiveBuffer += _socket.readAll();
|
2018-12-28 17:55:49 +01:00
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
// check if we can read a header
|
|
|
|
while(_receiveBuffer.size() >= 4)
|
|
|
|
{
|
2018-12-28 17:55:49 +01:00
|
|
|
uint32_t messageSize =
|
2018-12-28 18:12:45 +01:00
|
|
|
((_receiveBuffer[0]<<24) & 0xFF000000) |
|
|
|
|
((_receiveBuffer[1]<<16) & 0x00FF0000) |
|
|
|
|
((_receiveBuffer[2]<< 8) & 0x0000FF00) |
|
|
|
|
((_receiveBuffer[3] ) & 0x000000FF);
|
2018-12-28 17:55:49 +01:00
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
// 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-28 18:12:45 +01:00
|
|
|
const uint8_t* msgData = reinterpret_cast<const uint8_t*>(msg.constData());
|
|
|
|
flatbuffers::Verifier verifier(msgData, messageSize);
|
2018-12-28 17:55:49 +01:00
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
if (hyperionnet::VerifyReplyBuffer(verifier))
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
parseReply(hyperionnet::GetReply(msgData));
|
2018-12-28 18:12:45 +01:00
|
|
|
continue;
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
2018-12-28 18:12:45 +01:00
|
|
|
Error(_log, "Unable to parse reply");
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
void FlatBufferConnection::setSkipReply(const bool& skip)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
if(skip)
|
|
|
|
disconnect(&_socket, &QTcpSocket::readyRead, 0, 0);
|
|
|
|
else
|
|
|
|
connect(&_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection);
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void FlatBufferConnection::setRegister(const QString& origin, int priority)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
auto registerReq = hyperionnet::CreateRegister(_builder, _builder.CreateString(QSTRING_CSTR(origin)), priority);
|
|
|
|
auto req = hyperionnet::CreateRequest(_builder, hyperionnet::Command_Register, registerReq.Union());
|
2018-12-28 17:55:49 +01:00
|
|
|
|
|
|
|
_builder.Finish(req);
|
2019-02-05 19:55:48 +01:00
|
|
|
uint32_t size = _builder.GetSize();
|
|
|
|
const uint8_t header[] = {
|
|
|
|
uint8_t((size >> 24) & 0xFF),
|
|
|
|
uint8_t((size >> 16) & 0xFF),
|
|
|
|
uint8_t((size >> 8) & 0xFF),
|
|
|
|
uint8_t((size ) & 0xFF)};
|
|
|
|
|
|
|
|
// write message
|
|
|
|
int count = 0;
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(header), 4);
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(_builder.GetBufferPointer()), size);
|
|
|
|
_socket.flush();
|
|
|
|
_builder.Clear();
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void FlatBufferConnection::setColor(const ColorRgb & color, int priority, int duration)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
auto colorReq = hyperionnet::CreateColor(_builder, (color.red << 16) | (color.green << 8) | color.blue, duration);
|
|
|
|
auto req = hyperionnet::CreateRequest(_builder, hyperionnet::Command_Color, colorReq.Union());
|
|
|
|
|
2018-12-28 17:55:49 +01:00
|
|
|
_builder.Finish(req);
|
2018-12-30 22:07:53 +01:00
|
|
|
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void FlatBufferConnection::setImage(const Image<ColorRgb> &image)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
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());
|
2018-12-28 17:55:49 +01:00
|
|
|
|
|
|
|
_builder.Finish(req);
|
|
|
|
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void FlatBufferConnection::clear(int priority)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
auto clearReq = hyperionnet::CreateClear(_builder, priority);
|
|
|
|
auto req = hyperionnet::CreateRequest(_builder,hyperionnet::Command_Clear, clearReq.Union());
|
2018-12-28 17:55:49 +01:00
|
|
|
|
|
|
|
_builder.Finish(req);
|
|
|
|
sendMessage(_builder.GetBufferPointer(), _builder.GetSize());
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void FlatBufferConnection::clearAll()
|
|
|
|
{
|
|
|
|
clear(-1);
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:55:49 +01:00
|
|
|
void FlatBufferConnection::connectToHost()
|
|
|
|
{
|
|
|
|
// try connection only when
|
|
|
|
if (_socket.state() == QAbstractSocket::UnconnectedState)
|
|
|
|
_socket.connectToHost(_host, _port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size)
|
|
|
|
{
|
|
|
|
// print out connection message only when state is changed
|
|
|
|
if (_socket.state() != _prevSocketState )
|
|
|
|
{
|
2019-02-05 19:55:48 +01:00
|
|
|
_registered = false;
|
2018-12-30 22:07:53 +01:00
|
|
|
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;
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
_prevSocketState = _socket.state();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_socket.state() != QAbstractSocket::ConnectedState)
|
2018-12-30 22:07:53 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(!_registered)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
setRegister(_origin, _priority);
|
2018-12-28 17:55:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t header[] = {
|
|
|
|
uint8_t((size >> 24) & 0xFF),
|
|
|
|
uint8_t((size >> 16) & 0xFF),
|
|
|
|
uint8_t((size >> 8) & 0xFF),
|
|
|
|
uint8_t((size ) & 0xFF)};
|
|
|
|
|
|
|
|
// write message
|
|
|
|
int count = 0;
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(header), 4);
|
|
|
|
count += _socket.write(reinterpret_cast<const char *>(buffer), size);
|
2018-12-30 22:07:53 +01:00
|
|
|
_socket.flush();
|
|
|
|
_builder.Clear();
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
bool FlatBufferConnection::parseReply(const hyperionnet::Reply *reply)
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
if (!reply->error())
|
2018-12-28 17:55:49 +01:00
|
|
|
{
|
2019-02-05 19:55:48 +01:00
|
|
|
// no error set must be a success or registered or video
|
2018-12-30 22:07:53 +01:00
|
|
|
const auto videoMode = reply->video();
|
2019-02-05 19:55:48 +01:00
|
|
|
const auto registered = reply->registered();
|
2018-12-30 22:07:53 +01:00
|
|
|
if (videoMode != -1) {
|
|
|
|
// We got a video reply.
|
|
|
|
emit setVideoMode(static_cast<VideoMode>(videoMode));
|
|
|
|
return true;
|
2019-02-05 19:55:48 +01:00
|
|
|
}
|
2018-12-28 17:55:49 +01:00
|
|
|
|
2019-02-05 19:55:48 +01:00
|
|
|
// We got a registered reply.
|
|
|
|
if (registered != -1 && registered != _priority)
|
|
|
|
_registered = false;
|
|
|
|
else
|
|
|
|
_registered = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-30 22:07:53 +01:00
|
|
|
return false;
|
2018-12-28 17:55:49 +01:00
|
|
|
}
|