FlatBufferClient.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. // util
  3. #include <utils/Logger.h>
  4. #include <utils/Image.h>
  5. #include <utils/ColorRgb.h>
  6. #include <utils/ColorRgba.h>
  7. #include <utils/Components.h>
  8. // flatbuffer FBS
  9. #include "hyperion_reply_generated.h"
  10. #include "hyperion_request_generated.h"
  11. class QTcpSocket;
  12. class QTimer;
  13. namespace flatbuf {
  14. class HyperionRequest;
  15. }
  16. ///
  17. /// @brief Socket (client) of FlatBufferServer
  18. ///
  19. class FlatBufferClient : public QObject
  20. {
  21. Q_OBJECT
  22. public:
  23. ///
  24. /// @brief Construct the client
  25. /// @param socket The socket
  26. /// @param timeout The timeout when a client is automatically disconnected and the priority unregistered
  27. /// @param parent The parent
  28. ///
  29. explicit FlatBufferClient(QTcpSocket* socket, int timeout, QObject *parent = nullptr);
  30. signals:
  31. ///
  32. /// @brief forward register data to HyperionDaemon
  33. ///
  34. void registerGlobalInput(int priority, hyperion::Components component, const QString& origin = "FlatBuffer", const QString& owner = "", unsigned smooth_cfg = 0);
  35. ///
  36. /// @brief Forward clear command to HyperionDaemon
  37. ///
  38. void clearGlobalInput(int priority, bool forceClearAll=false);
  39. ///
  40. /// @brief forward prepared image to HyperionDaemon
  41. ///
  42. bool setGlobalInputImage(int priority, const Image<ColorRgb>& image, int timeout_ms, bool clearEffect = false);
  43. ///
  44. /// @brief Forward requested color
  45. ///
  46. void setGlobalInputColor(int priority, const std::vector<ColorRgb> &ledColor, int timeout_ms, const QString& origin = "FlatBuffer" ,bool clearEffects = true);
  47. ///
  48. /// @brief Emit the final processed image
  49. ///
  50. void setBufferImage(const QString& name, const Image<ColorRgb>& image);
  51. ///
  52. /// @brief Emits whenever the client disconnected
  53. ///
  54. void clientDisconnected();
  55. public slots:
  56. ///
  57. /// @brief Requests a registration from the client
  58. ///
  59. void registationRequired(int priority);
  60. ///
  61. /// @brief close the socket and call disconnected()
  62. ///
  63. void forceClose();
  64. private slots:
  65. ///
  66. /// @brief Is called whenever the socket got new data to read
  67. ///
  68. void readyRead();
  69. ///
  70. /// @brief Is called when the socket closed the connection, also requests thread exit
  71. ///
  72. void disconnected();
  73. private:
  74. ///
  75. /// @brief Handle the received message
  76. ///
  77. void handleMessage(const hyperionnet::Request * req);
  78. ///
  79. /// Register new priority
  80. ///
  81. void handleRegisterCommand(const hyperionnet::Register *regReq);
  82. ///
  83. /// @brief Hande Color message
  84. ///
  85. void handleColorCommand(const hyperionnet::Color *colorReq);
  86. ///
  87. /// Handle an incoming Image message
  88. ///
  89. /// @param image the incoming image
  90. ///
  91. void handleImageCommand(const hyperionnet::Image *image);
  92. ///
  93. /// @brief Handle clear command
  94. ///
  95. /// @param clear the incoming clear request
  96. ///
  97. void handleClearCommand(const hyperionnet::Clear *clear);
  98. ///
  99. /// Send handle not implemented
  100. ///
  101. void handleNotImplemented();
  102. ///
  103. /// Send a message to the connected client
  104. ///
  105. void sendMessage();
  106. ///
  107. /// Send a standard reply indicating success
  108. ///
  109. void sendSuccessReply();
  110. ///
  111. /// Send an error message back to the client
  112. ///
  113. /// @param error String describing the error
  114. ///
  115. void sendErrorReply(const std::string & error);
  116. private:
  117. Logger *_log;
  118. QTcpSocket *_socket;
  119. const QString _clientAddress;
  120. QTimer *_timeoutTimer;
  121. int _timeout;
  122. int _priority;
  123. QByteArray _receiveBuffer;
  124. // Flatbuffers builder
  125. flatbuffers::FlatBufferBuilder _builder;
  126. };