client.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 int IGTASKBYTE = 0xFF;
  27. static constexpr int GENMSGBYTE = 0xFE;
  28. static constexpr int PINGBYTE = 0xFD;
  29. } // namespace TaskCode
  30. typedef std::map<int, std::string> CommandMap;
  31. typedef std::map<int, std::vector<std::string>> CommandArgMap;
  32. typedef QVector<QString> StringVec;
  33. struct SentFile {
  34. int timestamp;
  35. QString name;
  36. FileType type;
  37. };
  38. Q_DECLARE_METATYPE(StringVec)
  39. Q_DECLARE_METATYPE(QVector<QByteArray>);
  40. class Client : public QDialog {
  41. Q_OBJECT
  42. QThread workerThread;
  43. public:
  44. class MessageHandler {
  45. public:
  46. MessageHandler(std::function<void()> cb) : m_cb(cb) {}
  47. void operator()() { m_cb(); }
  48. private:
  49. std::function<void()> m_cb;
  50. };
  51. Client(QWidget* parent = nullptr);
  52. Client(QWidget* parent, int count, char** arguments);
  53. ~Client();
  54. void start();
  55. void closeConnection();
  56. void execute();
  57. QString getAppName(int mask);
  58. int getSelectedApp();
  59. // Move this to private after moving responsibilities to Client
  60. void scheduleTask(Scheduler::Task* task);
  61. MessageHandler createMessageHandler(std::function<void()> cb);
  62. public slots:
  63. void sendMessage(const QString& s);
  64. void setSelectedApp(std::vector<QString> app_names);
  65. void sendFiles(Scheduler::Task* task);
  66. void ping();
  67. signals:
  68. void messageReceived(int t, QString s, QVector<QString> args);
  69. void eventReceived(int t, std::string event, StringVec args);
  70. private:
  71. void sendEncoded(std::string message);
  72. void sendFileEncoded(QByteArray bytes);
  73. void sendTaskEncoded(TaskType type, std::vector<std::string> args);
  74. void sendTaskEncoded(Scheduler::Task* task);
  75. void processFileQueue();
  76. void handleMessages();
  77. void sendPackets(uint8_t* data, int size);
  78. int argc;
  79. char** argv;
  80. int m_client_socket_fd;
  81. Task* m_outbound_task;
  82. bool executing;
  83. bool file_was_sent;
  84. CommandMap m_commands;
  85. CommandArgMap m_command_arg_map;
  86. std::vector<int> selected_commands;
  87. QQueue<Scheduler::KFileData> outgoing_files;
  88. std::vector<SentFile> sent_files;
  89. Scheduler::TaskQueue m_task_queue;
  90. };
  91. #endif // CLIENT_HPP