client.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef CLIENT_HPP
  2. #define CLIENT_HPP
  3. #include <QComboBox>
  4. #include <QDialog>
  5. #include <QLabel>
  6. #include <QLineEdit>
  7. #include <QMessageBox>
  8. #include <QMetaType>
  9. #include <QPushButton>
  10. #include <QQueue>
  11. #include <QString>
  12. #include <QThread>
  13. #include <QUuid>
  14. #include <QVector>
  15. #include <headers/util.hpp>
  16. #include <include/task/task.hpp>
  17. #include <string>
  18. #include <thread>
  19. #include <utility>
  20. static constexpr int MESSAGE_UPDATE_TYPE = 1;
  21. static constexpr int COMMANDS_UPDATE_TYPE = 2;
  22. static constexpr int EVENT_UPDATE_TYPE = 3;
  23. static constexpr int PROCESS_REQUEST_TYPE = 4;
  24. //using namespace Scheduler;
  25. namespace TaskCode {
  26. static constexpr uint32_t GENMSGBYTE = 0xFE;
  27. static constexpr uint32_t PINGBYTE = 0xFD;
  28. } // namespace TaskCode
  29. typedef std::map<int, std::string> CommandMap;
  30. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  31. typedef QVector<QString> StringVec;
  32. struct SentFile {
  33. int timestamp;
  34. QString name;
  35. Scheduler::FileType type;
  36. };
  37. Q_DECLARE_METATYPE(StringVec)
  38. Q_DECLARE_METATYPE(QVector<QByteArray>);
  39. class Client : public QDialog {
  40. Q_OBJECT
  41. QThread workerThread;
  42. public:
  43. class MessageHandler {
  44. public:
  45. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  46. void operator()() { m_cb(); }
  47. private:
  48. std::function<void()> m_cb;
  49. };
  50. Client(QWidget* parent = nullptr);
  51. Client(QWidget* parent, int count, char** arguments);
  52. ~Client();
  53. void start();
  54. void closeConnection();
  55. void execute();
  56. QString getAppName(int mask);
  57. int getSelectedApp();
  58. // Move this to private after moving responsibilities to Client
  59. void scheduleTask(Scheduler::Task* task);
  60. MessageHandler createMessageHandler(std::function<void()> cb);
  61. public slots:
  62. void sendMessage(const QString& s);
  63. void setSelectedApp(std::vector<QString> app_names);
  64. void sendFiles(Scheduler::Task* task);
  65. void ping();
  66. signals:
  67. void messageReceived(int t, QString s, QVector<QString> args);
  68. void eventReceived(int t, std::string event, StringVec args);
  69. private:
  70. void sendEncoded(std::string message);
  71. void sendFileEncoded(QByteArray bytes);
  72. void sendTaskEncoded(Scheduler::Task* task);
  73. void processFileQueue();
  74. void handleMessages();
  75. void handleEvent(std::string data);
  76. void sendPackets(uint8_t* data, int size);
  77. int argc;
  78. char** argv;
  79. int m_client_socket_fd;
  80. bool executing;
  81. bool file_was_sent;
  82. Scheduler::Task* m_outbound_task;
  83. CommandMap m_commands;
  84. CommandArgMap m_command_arg_map;
  85. std::vector<int> selected_commands;
  86. std::vector<SentFile> sent_files;
  87. Scheduler::TaskQueue m_task_queue;
  88. QQueue<Scheduler::KFileData> outgoing_files;
  89. };
  90. #endif // CLIENT_HPP