client.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. void sendIPCMessage(const QString& s);
  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. template <typename T>
  72. void sendEncoded(T message);
  73. void sendFileEncoded(QByteArray bytes);
  74. void sendTaskEncoded(Scheduler::Task* task);
  75. void processFileQueue();
  76. void handleMessages();
  77. void handleEvent(std::string data);
  78. void sendPackets(uint8_t* data, int size);
  79. int argc;
  80. char** argv;
  81. int m_client_socket_fd;
  82. bool executing;
  83. bool file_was_sent;
  84. Scheduler::Task* m_outbound_task;
  85. CommandMap m_commands;
  86. CommandArgMap m_command_arg_map;
  87. std::vector<int> selected_commands;
  88. std::vector<SentFile> sent_files;
  89. Scheduler::TaskQueue m_task_queue;
  90. QQueue<Scheduler::KFileData> outgoing_files;
  91. };
  92. #endif // CLIENT_HPP