AppChat  0.5.0
TCP client-server caht application with boost::asio library.
control.h
1 #ifndef CONTROL_H
2 #define CONTROL_H
3 
4 #include <QWidget>
5 #include "client/client/client.h"
6 #include "client/gui/mainwindow.h"
7 
12 class Control: public QObject
13 {
14  Q_OBJECT
15 public:
16  Control(int argc, char** argv);
17 
26  void connect_to_server(const std::string& login, const std::string& password, TypeCommand command);
27 
32  std::cout << "Destr Control" << std::endl;
33  if (client) {
34  client->close_connection();
35  }
36  }
37 
38 signals:
45  void send_text_to_gui(const std::string& login, const std::string& text, DateTime dt);
46 
47 public slots:
54  void autorisation(const std::string& login, const std::string& password) {
55  std::thread th([this, login, password]() {
56  try {
57  connect_to_server(login, password, TypeCommand::AuthorisationRequest);
58  } catch (std::exception &ex) {
59  std::cout << "exception from thread: " << ex.what() << std::endl;;
60  }
61  });
62  th.detach();
63  }
64 
71  void registration(const std::string& login, const std::string& password) {
72  std::thread th([this, login, password]() {
73  try {
74  connect_to_server(login, password, TypeCommand::RegistrationRequest);
75  } catch (std::exception &ex) {
76  std::cout << "exception from thread: " << ex.what() << std::endl;;
77  }
78  });
79  th.detach();
80  }
81 
88  void get_text_from_gui(const std::string& login, const std::string& text, int room_id) {
89  client->write(std::make_shared<TextRequest>(login, room_id, text));
90  }
91 
98  void text_from_client(const std::string& from, const std::string& text, DateTime dt) {
99  send_text_to_gui(from, text, dt);
100  }
101 
106  void change_room(int new_room_id) {
107  client->write(std::make_shared<JoinRoomRequest>(new_room_id));
108  }
109 
110 private:
111  std::unique_ptr<Client> client;
112  MainWindow w;
113 
117  std::string ip = "127.0.0.1";
118  int32_t port = SERVER_DEFAULT_PORT;
119 };
120 
121 #endif // CONTROL_H
void send_text_to_gui(const std::string &login, const std::string &text, DateTime dt)
Show reveived message.
void text_from_client(const std::string &from, const std::string &text, DateTime dt)
Notify UI about received message.
Definition: control.h:98
void change_room(int new_room_id)
Change chat room to another one.
Definition: control.h:106
void get_text_from_gui(const std::string &login, const std::string &text, int room_id)
Send message.
Definition: control.h:88
void autorisation(const std::string &login, const std::string &password)
User autorization.
Definition: control.h:54
Controller.
Definition: control.h:12
Definition: datetime.h:6
void registration(const std::string &login, const std::string &password)
User registration.
Definition: control.h:71
MainWindow.
Definition: mainwindow.h:21
void connect_to_server(const std::string &login, const std::string &password, TypeCommand command)
Start communication with server.
Definition: control.cpp:25
~Control()
Close client socket when destroy UI controller.
Definition: control.h:31